int
archive_read_support_filter_all(struct archive *a)
{
	archive_check_magic(a, ARCHIVE_READ_MAGIC,
	    ARCHIVE_STATE_NEW, "archive_read_support_filter_all");

	/* Bzip falls back to "bunzip2" command-line */
	archive_read_support_filter_bzip2(a);
	/* The decompress code doesn't use an outside library. */
	archive_read_support_filter_compress(a);
	/* Gzip decompress falls back to "gunzip" command-line. */
	archive_read_support_filter_gzip(a);
	/* Lzip falls back to "unlzip" command-line program. */
	archive_read_support_filter_lzip(a);
	/* The LZMA file format has a very weak signature, so it
	 * may not be feasible to keep this here, but we'll try.
	 * This will come back out if there are problems. */
	/* Lzma falls back to "unlzma" command-line program. */
	archive_read_support_filter_lzma(a);
	/* Xz falls back to "unxz" command-line program. */
	archive_read_support_filter_xz(a);
	/* The decode code doesn't use an outside library. */
	archive_read_support_filter_uu(a);
	/* The decode code doesn't use an outside library. */
	archive_read_support_filter_rpm(a);

	/* Note: We always return ARCHIVE_OK here, even if some of the
	 * above return ARCHIVE_WARN.  The intent here is to enable
	 * "as much as possible."  Clients who need specific
	 * compression should enable those individually so they can
	 * verify the level of support. */
	/* Clear any warning messages set by the above functions. */
	archive_clear_error(a);
	return (ARCHIVE_OK);
}
bool OdinPatcher::Impl::openInputArchive()
{
    assert(aInput == nullptr);

    aInput = archive_read_new();

    // TODO: Eventually support tar within a zip since many people distribute
    //       stock firmware packages in this format
    //archive_read_support_format_zip(aInput);
    archive_read_support_format_tar(aInput);
    archive_read_support_filter_gzip(aInput);
    archive_read_support_filter_xz(aInput);

    // Our callbacks use io::File, which supports LFS on Android. Also allows
    // progress info by counting number of bytes read.
    int ret = archive_read_open2(aInput, this, &Impl::laOpenCb, &Impl::laReadCb,
                                 &Impl::laSkipCb, &Impl::laCloseCb);
    if (ret != ARCHIVE_OK) {
        LOGW("libarchive: Failed to open for reading: %s",
             archive_error_string(aInput));
        archive_read_free(aInput);
        aInput = nullptr;
        error = ErrorCode::ArchiveReadOpenError;
        return false;
    }

    return true;
}
Example #3
0
struct mp_archive *mp_archive_new(struct mp_log *log, struct stream *src,
                                  int flags)
{
    struct mp_archive *mpa = talloc_zero(NULL, struct mp_archive);
    mpa->log = log;
    mpa->locale = newlocale(LC_ALL_MASK, "C.UTF-8", (locale_t)0);
    if (!mpa->locale)
        goto err;
    mpa->arch = archive_read_new();
    mpa->primary_src = src;
    if (!mpa->arch)
        goto err;

    // first volume is the primary streame
    if (!add_volume(log ,mpa, src, src->url))
        goto err;

    // try to open other volumes
    char** volumes = find_volumes(src);
    for (int i = 0; volumes[i]; i++) {
        if (!add_volume(log, mpa, NULL, volumes[i])) {
            talloc_free(volumes);
            goto err;
        }
    }
    talloc_free(volumes);

    locale_t oldlocale = uselocale(mpa->locale);

    archive_read_support_format_7zip(mpa->arch);
    archive_read_support_format_iso9660(mpa->arch);
    archive_read_support_format_rar(mpa->arch);
    archive_read_support_format_zip(mpa->arch);
    archive_read_support_filter_bzip2(mpa->arch);
    archive_read_support_filter_gzip(mpa->arch);
    archive_read_support_filter_xz(mpa->arch);
    if (flags & MP_ARCHIVE_FLAG_UNSAFE) {
        archive_read_support_format_gnutar(mpa->arch);
        archive_read_support_format_tar(mpa->arch);
    }

    archive_read_set_read_callback(mpa->arch, read_cb);
    archive_read_set_skip_callback(mpa->arch, skip_cb);
    archive_read_set_switch_callback(mpa->arch, switch_cb);
    archive_read_set_open_callback(mpa->arch, open_cb);
    archive_read_set_close_callback(mpa->arch, close_cb);
    if (mpa->primary_src->seekable)
        archive_read_set_seek_callback(mpa->arch, seek_cb);
    bool fail = archive_read_open1(mpa->arch) < ARCHIVE_OK;

    uselocale(oldlocale);

    if (fail)
        goto err;
    return mpa;

err:
    mp_archive_free(mpa);
    return NULL;
}
Example #4
0
// Based on libarchive's public example code.
// https://github.com/libarchive/libarchive/wiki/Examples#wiki-Constructing_Objects_On_Disk
void GuiZipper::unpackFile(const char *zipFile, const char *outputDir)
    throw (ZipperException*) {
  // TODO: use archive_write_disk_open instead (if/when it exists)
  char cwd[4096];
  getcwd(cwd, 4096);
  char *absZipFile = fileManager_->getAbsFilePath(zipFile);
  platformstl::filesystem_traits<char> traits;
  traits.set_current_directory(outputDir);
  
  struct archive *a;
  struct archive *ext;
  struct archive_entry *entry;
  int flags;
  int r;
  
  flags = ARCHIVE_EXTRACT_TIME;
  flags |= ARCHIVE_EXTRACT_PERM;
  flags |= ARCHIVE_EXTRACT_ACL;
  flags |= ARCHIVE_EXTRACT_FFLAGS;
  
  a = archive_read_new();
  archive_read_support_format_tar(a);
  archive_read_support_filter_gzip(a);
  ext = archive_write_disk_new();
  archive_write_disk_set_options(ext, flags);
  archive_write_disk_set_standard_lookup(ext);
  r = archive_read_open_filename(a, absZipFile, 10240);
  checkForErrors("Error opening archive for reading", a, r);
  for (;;) {
    r = archive_read_next_header(a, &entry);
    if (r == ARCHIVE_EOF) {
      break;
    }
    checkForErrors("Error reading next archive header", a, r);
    r = archive_write_header(ext, entry);
    checkForErrors("Error writing next archive header", a, r);
    copyData(a, ext, outputDir);
    r = archive_write_finish_entry(ext);
    checkForErrors("Error writing archive finish entry", a, r);
  }
  r = archive_read_close(a);
  checkForErrors("Error closing read archive", a, r);
  r = archive_read_free(a);
  checkForErrors("Error freeing read archive", a, r);
  r = archive_write_close(ext);
  checkForErrors("Error closing write archive", a, r);
  r = archive_write_free(ext);
  checkForErrors("Error freeing write archive", a, r);

  traits.set_current_directory(cwd);
  delete absZipFile;
}
Example #5
0
/* Open an inner archive at the current position within the given outer archive. */
static struct archive *open_inner(struct archive *outer)
{
    struct archive *inner;
    struct inner_data *data;
    int r;

    inner = archive_read_new();
    if (!inner) {
        opkg_msg(ERROR, "Failed to create inner archive object.\n");
        return NULL;
    }

    data = (struct inner_data *)xmalloc(sizeof(struct inner_data));
    data->buffer = xmalloc(EXTRACT_BUFFER_LEN);
    data->outer = outer;

    /* Inner package is in 'tar' format, gzip compressed. */
    r = archive_read_support_filter_gzip(inner);
    if (r == ARCHIVE_WARN) {
        /* libarchive returns ARCHIVE_WARN if the filter is provided by
         * an external program.
         */
        opkg_msg(INFO, "Gzip support provided by external program.\n");
    } else if (r != ARCHIVE_OK) {
        opkg_msg(ERROR, "Gzip format not supported.\n");
        goto err_cleanup;
    }

    r = archive_read_support_format_tar(inner);
    if (r != ARCHIVE_OK) {
        opkg_msg(ERROR, "Tar format not supported: %s\n",
                 archive_error_string(outer));
        goto err_cleanup;
    }

    r = archive_read_open(inner, data, NULL, inner_read, inner_close);
    if (r != ARCHIVE_OK) {
        opkg_msg(ERROR, "Failed to open inner archive: %s\n",
                 archive_error_string(inner));
        goto err_cleanup;
    }

    return inner;

 err_cleanup:
    archive_read_free(inner);
    free(data->buffer);
    free(data);
    return NULL;
}
static void
extract (int fd)
{
  struct archive *a;
  struct archive *ext;
  struct archive_entry *entry;
  int r;

  a = archive_read_new ();
  ext = archive_write_disk_new ();
  archive_read_support_format_tar (a);
  archive_read_support_filter_xz (a);
  archive_read_support_filter_gzip (a);

  if ((r = archive_read_open_fd (a, fd, 16*1024)))
    die_with_libarchive (a, "archive_read_open_fd: %s");

  while (1)
    {
      r = archive_read_next_header (a, &entry);
      if (r == ARCHIVE_EOF)
        break;

      if (r != ARCHIVE_OK)
        die_with_libarchive (a, "archive_read_next_header: %s");

      if (!should_extract (entry))
        continue;

      r = archive_write_header (ext, entry);
      if (r != ARCHIVE_OK)
        die_with_libarchive (ext, "archive_write_header: %s");
      else
        {
          copy_archive (a, ext);
          r = archive_write_finish_entry (ext);
          if (r != ARCHIVE_OK)
            die_with_libarchive (ext, "archive_write_finish_entry: %s");
        }
    }

  archive_read_close (a);
  archive_read_free (a);

  archive_write_close (ext);
  archive_write_free (ext);
}
Example #7
0
static struct archive *open_compressed_file(const char *filename)
{
    struct archive *ar;
    int r;

    ar = archive_read_new();
    if (!ar) {
        opkg_msg(ERROR,
                 "Failed to create archive object for compressed file.\n");
        return NULL;
    }

    /* Support raw data in gzip compression format. */
    r = archive_read_support_filter_gzip(ar);
    if (r == ARCHIVE_WARN) {
        /* libarchive returns ARCHIVE_WARN if the filter is provided by
         * an external program.
         */
        opkg_msg(INFO, "Gzip support provided by external program.\n");
    } else if (r != ARCHIVE_OK) {
        opkg_msg(ERROR, "Gzip format not supported: %s\n",
                 archive_error_string(ar));
        goto err_cleanup;
    }

    r = archive_read_support_format_raw(ar);
    if (r != ARCHIVE_OK) {
        opkg_msg(ERROR, "Raw format not supported: %s\n",
                 archive_error_string(ar));
        goto err_cleanup;
    }

    /* Open input file and prepare for reading. */
    r = archive_read_open_filename(ar, filename, EXTRACT_BUFFER_LEN);
    if (r != ARCHIVE_OK) {
        opkg_msg(ERROR, "Failed to open compressed file '%s': %s\n", filename,
                 archive_error_string(ar));
        goto err_cleanup;
    }

    return ar;

 err_cleanup:
    archive_read_free(ar);
    return NULL;
}
Example #8
0
/*
 * All of the sample files have the same contents; they're just
 * compressed in different ways.
 */
static void
verify(const char *name)
{
	const char *n[7] = { "f1", "f2", "f3", "d1/f1", "d1/f2", "d1/f3", NULL };
	struct archive_entry *ae;
	struct archive *a;
	int i,r;

	assert((a = archive_read_new()) != NULL);
	r = archive_read_support_filter_gzip(a);
	if (r == ARCHIVE_WARN) {
		skipping("gzip reading not fully supported on this platform");
		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
		return;
	}
	assertEqualIntA(a, ARCHIVE_OK, r);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	extract_reference_file(name);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 200));

	/* Read entries, match up names with list above. */
	for (i = 0; i < 6; ++i) {
		failure("Could not read file %d (%s) from %s", i, n[i], name);
		assertEqualIntA(a, ARCHIVE_OK,
		    archive_read_next_header(a, &ae));
		if (r != ARCHIVE_OK) {
			archive_read_free(a);
			return;
		}
		assertEqualString(n[i], archive_entry_pathname(ae));
	}

	/* Verify the end-of-archive. */
	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));

	/* Verify that the format detection worked. */
	assertEqualInt(archive_compression(a), ARCHIVE_COMPRESSION_GZIP);
	assertEqualString(archive_compression_name(a), "gzip");
	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR);

	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
}
Example #9
0
/**
 * Open a package mtree file for reading.
 * @param pkg the local package to read the changelog of
 * @return a archive structure for the package mtree file
 */
static struct archive *_cache_mtree_open(alpm_pkg_t *pkg)
{
	int r;
	struct archive *mtree;

	alpm_db_t *db = alpm_pkg_get_db(pkg);
	char *mtfile = _alpm_local_db_pkgpath(db, pkg, "mtree");

	if(access(mtfile, F_OK) != 0) {
		/* there is no mtree file for this package */
		goto error;
	}

	if((mtree = archive_read_new()) == NULL) {
		pkg->handle->pm_errno = ALPM_ERR_LIBARCHIVE;
		goto error;
	}

	archive_read_support_filter_gzip(mtree);
	archive_read_support_format_mtree(mtree);

	if((r = archive_read_open_file(mtree, mtfile, ALPM_BUFFER_SIZE))) {
		_alpm_log(pkg->handle, ALPM_LOG_ERROR, _("error while reading file %s: %s\n"),
					mtfile, archive_error_string(mtree));
		pkg->handle->pm_errno = ALPM_ERR_LIBARCHIVE;
		archive_read_finish(mtree);
		goto error;
	}

	free(mtfile);
	return mtree;

error:
	free(mtfile);
	return NULL;
}
Example #10
0
Installer::ProceedState RomInstaller::on_checked_device()
{
    // /sbin is not going to be populated with anything useful in a normal boot
    // image. We can almost guarantee that a recovery image is going to be
    // installed though, so we'll open the recovery partition with libmbp and
    // extract its /sbin with libarchive into the chroot's /sbin.

    mbp::BootImage bi;
    if (!bi.loadFile(_recovery_block_dev)) {
        display_msg("Failed to load recovery partition image");
        return ProceedState::Fail;
    }

    const unsigned char *ramdisk_data;
    std::size_t ramdisk_size;
    bi.ramdiskImageC(&ramdisk_data, &ramdisk_size);

    autoclose::archive in(archive_read_new(), archive_read_free);
    autoclose::archive out(archive_write_disk_new(), archive_write_free);

    if (!in | !out) {
        LOGE("Out of memory");
        return ProceedState::Fail;
    }

    archive_entry *entry;

    // Set up input
    archive_read_support_filter_gzip(in.get());
    archive_read_support_filter_lzop(in.get());
    archive_read_support_filter_lz4(in.get());
    archive_read_support_filter_lzma(in.get());
    archive_read_support_format_cpio(in.get());

    int ret = archive_read_open_memory(in.get(),
            const_cast<unsigned char *>(ramdisk_data), ramdisk_size);
    if (ret != ARCHIVE_OK) {
        LOGW("Failed to open recovery ramdisk: %s",
             archive_error_string(in.get()));
        return ProceedState::Fail;
    }

    // Set up output
    archive_write_disk_set_options(out.get(),
                                   ARCHIVE_EXTRACT_ACL |
                                   ARCHIVE_EXTRACT_FFLAGS |
                                   ARCHIVE_EXTRACT_PERM |
                                   ARCHIVE_EXTRACT_SECURE_NODOTDOT |
                                   ARCHIVE_EXTRACT_SECURE_SYMLINKS |
                                   ARCHIVE_EXTRACT_TIME |
                                   ARCHIVE_EXTRACT_UNLINK |
                                   ARCHIVE_EXTRACT_XATTR);

    while ((ret = archive_read_next_header(in.get(), &entry)) == ARCHIVE_OK) {
        std::string path = archive_entry_pathname(entry);

        if (!util::starts_with(path, "sbin/")) {
            continue;
        }

        LOGE("Copying from recovery: %s", path.c_str());

        archive_entry_set_pathname(entry, (_chroot + "/" + path).c_str());

        if (util::archive_copy_header_and_data(in.get(), out.get(), entry) != ARCHIVE_OK) {
            return ProceedState::Fail;
        }

        archive_entry_set_pathname(entry, path.c_str());
    }

    if (ret != ARCHIVE_EOF) {
        LOGE("Archive extraction ended without reaching EOF: %s",
             archive_error_string(in.get()));
        return ProceedState::Fail;
    }

    return ProceedState::Continue;
}
Example #11
0
/*
 * The main archive extract function, loops over the archive entries and unpack
 * them at the right place.
 */
void
extract(const char *extname, const char *filename)
{
    struct archive *a;
    struct archive *ext;
    struct archive_entry *entry;
    int flags = ARCHIVE_EXTRACT_TIME;
    int r;

    char *control_filename = psprintf("%s.control", extname);
    int cflen = strlen(control_filename);

    a = archive_read_new();
    ext = archive_write_disk_new();
    archive_write_disk_set_options(ext, flags);

    /*
     * Do we care enough about the .so size to limit ourselves here? We might
     * want to reconsider and use archive_read_support_format_all() and
     * archive_read_support_filter_all() rather than just tar.gz.
     */
    archive_read_support_format_tar(a);
    archive_read_support_filter_gzip(a);

    if ((archive_read_open_filename(a, filename, 10240)))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("Failed to open archive \"%s\"", filename),
                 errdetail("%s", archive_error_string(a))));

    elog(DEBUG1, "Unpacking archive \"%s\"", filename);

    for (;;)
    {
        char *path;
        struct archive_entry *target;

        r = archive_read_next_header(a, &entry);
        if (r == ARCHIVE_EOF)
            break;

        if (r != ARCHIVE_OK)
            ereport(ERROR,
                    (errcode(ERRCODE_UNDEFINED_FILE),
                     errmsg("%s", archive_error_string(a))));

        target = archive_entry_clone(entry);
        path   = (char *)archive_entry_pathname(target);
        path   = (char *)compute_target_path(path, control_filename, cflen);
        archive_entry_set_pathname(target, path);

        elog(DEBUG1, "Extracting \"%s\" to \"%s\"",
             archive_entry_pathname(entry), path);

        r = archive_write_header(ext, target);

        if (r != ARCHIVE_OK)
            ereport(WARNING,
                    (errcode(ERRCODE_IO_ERROR),
                     errmsg("%s", archive_error_string(ext))));
        else
        {
            copy_data(a, ext);
            r = archive_write_finish_entry(ext);
            if (r != ARCHIVE_OK)
                ereport(ERROR,
                        (errcode(ERRCODE_IO_ERROR),
                         errmsg("%s", archive_error_string(ext))));
        }
        archive_entry_free(target);
    }

    archive_read_close(a);
    archive_read_free(a);
}
/*! \brief Extracts the selected .tar.gz archive to the selected folder, will try to create the target folder if it does not exist.
* @param filePath Full path to selected archive.
* @param targetFolderPath Full path to target folder.
* @return Returns full path to the extracted archive, or empty QString if failed.
*/
QString ArchiveExtractor::extractFileToTargetFolder(QString filePath, QString targetFolderPath){
  //check if the selected archive file exist
  QFile *selectedFile = new QFile(filePath);
  if(!selectedFile->exists()){
    qDebug()<<"ERROR: File marked for decompression does not exist!";
    delete selectedFile;
    return QString("");
  }
  delete selectedFile;
  struct archive *a;
  struct archive *ext;
  struct archive_entry *entry;
  int r;
  //  /* The "flags" argument selects optional behavior, 'OR' the flags you want. */
  //  /* Default: Do not try to set owner/group. */
  //#define	ARCHIVE_EXTRACT_OWNER			(0x0001)
  //  /* Default: Do obey umask, do not restore SUID/SGID/SVTX bits. */
  //#define	ARCHIVE_EXTRACT_PERM			(0x0002)
  //  /* Default: Do not restore mtime/atime. */
  //#define	ARCHIVE_EXTRACT_TIME			(0x0004)
  //  /* Default: Replace existing files. */
  //#define	ARCHIVE_EXTRACT_NO_OVERWRITE 		(0x0008)
  //  /* Default: Try create first, unlink only if create fails with EEXIST. */
  //#define	ARCHIVE_EXTRACT_UNLINK			(0x0010)
  //  /* Default: Do not restore ACLs. */
  //#define	ARCHIVE_EXTRACT_ACL			(0x0020)
  //  /* Default: Do not restore fflags. */
  //#define	ARCHIVE_EXTRACT_FFLAGS			(0x0040)
  //  /* Default: Do not restore xattrs. */
  //#define	ARCHIVE_EXTRACT_XATTR 			(0x0080)
  //  /* Default: Do not try to guard against extracts redirected by symlinks. */
  //  /* Note: With ARCHIVE_EXTRACT_UNLINK, will remove any intermediate symlink. */
  //#define	ARCHIVE_EXTRACT_SECURE_SYMLINKS		(0x0100)
  //  /* Default: Do not reject entries with '..' as path elements. */
  //#define	ARCHIVE_EXTRACT_SECURE_NODOTDOT		(0x0200)
  //  /* Default: Create parent directories as needed. */
  //#define	ARCHIVE_EXTRACT_NO_AUTODIR		(0x0400)
  //  /* Default: Overwrite files, even if one on disk is newer. */
  //#define	ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER	(0x0800)
  //  /* Detect blocks of 0 and write holes instead. */
  //#define	ARCHIVE_EXTRACT_SPARSE			(0x1000)
  //  /* Default: Do not restore Mac extended metadata. */
  //  /* This has no effect except on Mac OS. */
  //#define	ARCHIVE_EXTRACT_MAC_METADATA		(0x2000)
  int flags = 0;
  //      flags |= ARCHIVE_EXTRACT_TIME;
  //      flags |= ARCHIVE_EXTRACT_NO_AUTODIR;
  //      flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;

  QFileInfo *fileInfo = new QFileInfo(filePath);
//  qDebug()<<"resolved filename: "<<fileInfo->fileName();
  delete fileInfo;

  //MEMORY LEAK!!! (Pointers be dangerous, man. :)
  //  const char *filename = fileInfo->fileName().toUtf8().constData();
  //AVOID IT BY CONVERTING TO A QBYTEARRAY FIRST!
  QByteArray byteArray = filePath.toUtf8();
  const char *filename = byteArray.constData();
  //That's better :D

  //toggle extraction
  bool do_extract = true;

  a = archive_read_new();
  ext = archive_write_disk_new();
  archive_write_disk_set_options(ext, flags);

  //tuned for .tar.gz
  archive_read_support_filter_gzip(a);
  archive_read_support_format_gnutar(a);

  if((r = archive_read_open_filename(a, filename, 10240)) ){
    errmsg(archive_error_string(a));
  }
  for(;;){
    r = archive_read_next_header(a, &entry);
    if(r == ARCHIVE_EOF) break;
    if(r != ARCHIVE_OK){
      errmsg(archive_error_string(a));
    }
    if (verbose || !do_extract){
      qDebug()<<"Detected files in archive: ";
      msg(archive_entry_pathname(entry));
    }
    QString currentPath(archive_entry_pathname( entry ));
    qDebug()<<currentPath;

    QDir targetFolder(targetFolderPath);
    if(!targetFolder.exists()){//target folder does not exist
      //attempt to create it
      if(!targetFolder.mkpath(targetFolderPath)){//failed to create target folder
        //break procedure
        qDebug()<<"ERROR: Target folder does not exist and cannot be created";
        return QString("");
      }
    }

    QString newPath = targetFolderPath + currentPath;
    qDebug()<<"newPath: " << newPath;

    archive_entry_set_pathname( entry, newPath.toUtf8().constData() );
    if (verbose && do_extract){
//      msg("About to start extracting\n");
    }
    if (do_extract){
      qDebug()<<"Extracting...";
      r = archive_write_header(ext, entry);
      if (r != ARCHIVE_OK) errmsg(archive_error_string(a));
      else copy_data(a, ext);
      std::string returnPath;
      returnPath = archive_entry_pathname(entry);
      qDebug()<<"File extracted: " << QString::fromStdString(returnPath);
      archive_read_close(a);
      archive_read_free(a);
      archive_write_close(ext);
      archive_write_free(ext);
      return QString::fromStdString( returnPath );
    }
  }
  archive_read_close(a);
  archive_read_free(a);
  archive_write_close(ext);
  archive_write_free(ext);
  return QString("");
}
Example #13
0
int
archive_read_append_filter(struct archive *_a, int code)
{
    int r1, r2, number_bidders, i;
    char str[20];
    struct archive_read_filter_bidder *bidder;
    struct archive_read_filter *filter;
    struct archive_read *a = (struct archive_read *)_a;

    r1 = r2 = (ARCHIVE_OK);
    switch (code)
    {
    case ARCHIVE_FILTER_NONE:
        /* No filter to add, so do nothing.
         * NOTE: An initial "NONE" type filter is always set at the end of the
         * filter chain.
         */
        r1 = (ARCHIVE_OK);
        break;
    case ARCHIVE_FILTER_GZIP:
        strcpy(str, "gzip");
        r1 = archive_read_support_filter_gzip(_a);
        break;
    case ARCHIVE_FILTER_BZIP2:
        strcpy(str, "bzip2");
        r1 = archive_read_support_filter_bzip2(_a);
        break;
    case ARCHIVE_FILTER_COMPRESS:
        strcpy(str, "compress (.Z)");
        r1 = archive_read_support_filter_compress(_a);
        break;
    case ARCHIVE_FILTER_PROGRAM:
        archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
                          "Cannot append program filter using archive_read_append_filter");
        return (ARCHIVE_FATAL);
    case ARCHIVE_FILTER_LZMA:
        strcpy(str, "lzma");
        r1 = archive_read_support_filter_lzma(_a);
        break;
    case ARCHIVE_FILTER_XZ:
        strcpy(str, "xz");
        r1 = archive_read_support_filter_xz(_a);
        break;
    case ARCHIVE_FILTER_UU:
        strcpy(str, "uu");
        r1 = archive_read_support_filter_uu(_a);
        break;
    case ARCHIVE_FILTER_RPM:
        strcpy(str, "rpm");
        r1 = archive_read_support_filter_rpm(_a);
        break;
    case ARCHIVE_FILTER_LZIP:
        strcpy(str, "lzip");
        r1 = archive_read_support_filter_lzip(_a);
        break;
    case ARCHIVE_FILTER_LRZIP:
        strcpy(str, "lrzip");
        r1 = archive_read_support_filter_lrzip(_a);
        break;
    default:
        archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
                          "Invalid filter code specified");
        return (ARCHIVE_FATAL);
    }

    if (code != ARCHIVE_FILTER_NONE)
    {
        number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);

        bidder = a->bidders;
        for (i = 0; i < number_bidders; i++, bidder++)
        {
            if (!bidder->name || !strcmp(bidder->name, str))
                break;
        }
        if (!bidder->name || strcmp(bidder->name, str))
        {
            archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
                              "Internal error: Unable to append filter");
            return (ARCHIVE_FATAL);
        }

        filter
            = (struct archive_read_filter *)calloc(1, sizeof(*filter));
        if (filter == NULL)
        {
            archive_set_error(&a->archive, ENOMEM, "Out of memory");
            return (ARCHIVE_FATAL);
        }
        filter->bidder = bidder;
        filter->archive = a;
        filter->upstream = a->filter;
        a->filter = filter;
        r2 = (bidder->init)(a->filter);
        if (r2 != ARCHIVE_OK) {
            __archive_read_close_filters(a);
            __archive_read_free_filters(a);
            return (ARCHIVE_FATAL);
        }
    }

    a->bypass_filter_bidding = 1;
    return (r1 < r2) ? r1 : r2;
}
Example #14
0
static void
extract(const char *filename, int do_extract, int flags)
{
	struct archive *a;
	struct archive *ext;
	struct archive_entry *entry;
	int r;

	a = archive_read_new();
	ext = archive_write_disk_new();
	archive_write_disk_set_options(ext, flags);
#ifndef NO_BZIP2_EXTRACT
	archive_read_support_filter_bzip2(a);
#endif
#ifndef NO_GZIP_EXTRACT
	archive_read_support_filter_gzip(a);
#endif
#ifndef NO_COMPRESS_EXTRACT
	archive_read_support_filter_compress(a);
#endif
#ifndef NO_TAR_EXTRACT
	archive_read_support_format_tar(a);
#endif
#ifndef NO_CPIO_EXTRACT
	archive_read_support_format_cpio(a);
#endif
#ifndef NO_LOOKUP
	archive_write_disk_set_standard_lookup(ext);
#endif
	if (filename != NULL && strcmp(filename, "-") == 0)
		filename = NULL;
	if ((r = archive_read_open_filename(a, filename, 10240))) {
		errmsg(archive_error_string(a));
		errmsg("\n");
		exit(r);
	}
	for (;;) {
		r = archive_read_next_header(a, &entry);
		if (r == ARCHIVE_EOF)
			break;
		if (r != ARCHIVE_OK) {
			errmsg(archive_error_string(a));
			errmsg("\n");
			exit(1);
		}
		if (verbose && do_extract)
			msg("x ");
		if (verbose || !do_extract)
			msg(archive_entry_pathname(entry));
		if (do_extract) {
			r = archive_write_header(ext, entry);
			if (r != ARCHIVE_OK)
				errmsg(archive_error_string(a));
			else
				copy_data(a, ext);
		}
		if (verbose || !do_extract)
			msg("\n");
	}
	archive_read_close(a);
	archive_read_free(a);
	exit(0);
}
Example #15
0
/* Deprecated; remove in libarchive 4.0 */
int
archive_read_support_compression_gzip(struct archive *a)
{
	return archive_read_support_filter_gzip(a);
}
Example #16
0
void mfu_flist_archive_extract(const char* filename, bool verbose, int flags)
{
    int r;

    /* TODO: this needs to be parallelized */

    /* initiate archive object for reading */
    struct archive* a = archive_read_new();

    /* initiate archive object for writing */
    struct archive* ext = archive_write_disk_new();
    archive_write_disk_set_options(ext, flags);

    /* we want all the format supports */
    archive_read_support_filter_bzip2(a);
    archive_read_support_filter_gzip(a);
    archive_read_support_filter_compress(a);
    archive_read_support_format_tar(a);

    archive_write_disk_set_standard_lookup(ext);

    if (filename != NULL && strcmp(filename, "-") == 0) {
        filename = NULL;
    }

    /* blocksize set to 1024K */
    if ((r = archive_read_open_filename(a, filename, 10240))) {
        errmsg(archive_error_string(a));
        exit(r);
    }

    struct archive_entry* entry;
    for (;;) {
        r = archive_read_next_header(a, &entry);
        if (r == ARCHIVE_EOF) {
            break;
        }
        if (r != ARCHIVE_OK) {
            errmsg(archive_error_string(a));
            exit(r);
        }

        if (verbose) {
            msg("x ");
        }

        if (verbose) {
            msg(archive_entry_pathname(entry));
        }

        r = archive_write_header(ext, entry);
        if (r != ARCHIVE_OK) {
            errmsg(archive_error_string(a));
        } else {
            copy_data(a, ext);
        }

        if (verbose) {
            msg("\n");
        }
    }

    archive_read_close(a);
    archive_read_free(a);
}
Example #17
0
emb_ruby_error_t emb_ruby_uncompress(){

  struct archive *input_archive;
  struct archive *output_archive;
  struct archive_entry* entry;

  int result;
  int output_flags;

  unsigned char buff[2048];
  int buffsize = 2048;
  int size;

  output_flags = ARCHIVE_EXTRACT_TIME;
  output_flags |= ARCHIVE_EXTRACT_PERM;
  output_flags |= ARCHIVE_EXTRACT_ACL;
  output_flags |= ARCHIVE_EXTRACT_FFLAGS;

  input_archive = archive_read_new();
  output_archive = archive_write_disk_new();

  archive_read_support_filter_gzip(input_archive);
  archive_read_support_format_tar(input_archive);
  //archive_read_support_format_raw(a);

  archive_write_disk_set_options(output_archive, output_flags);
  archive_write_disk_set_standard_lookup(output_archive);

  result = archive_read_open_memory(input_archive, (void*)binary_data, binary_size);

  if (result != ARCHIVE_OK) {
    fprintf(stderr, "%s\n", archive_error_string(input_archive));
    return E_UNCOMPRESS_ERROR;
  }


  for (;;) {

    /**
     * Read new header from memory
     */
    result = archive_read_next_header(input_archive, &entry);
    // EOF
    if (result == ARCHIVE_EOF) {
      break;
    }
    // error handling
    if (result != ARCHIVE_OK) {
      fprintf(stderr, "%s\n", archive_error_string(input_archive));
      emd_ruby_uncompress_cleanup(input_archive, output_archive);
      return E_UNCOMPRESS_ERROR;
    }

    /**
     * Write new header into disk
     */
    result = archive_write_header(output_archive, entry);
    // error handling
    if (result != ARCHIVE_OK) {
      fprintf(stderr, "%s\n", archive_error_string(output_archive));
      emd_ruby_uncompress_cleanup(input_archive, output_archive);
      return E_UNCOMPRESS_ERROR;
    }

    /**
     * copy the data
     */
     result = emb_ruby_uncompress_copy_data(input_archive, output_archive);
     if (result != ARCHIVE_OK) {
          fprintf(stderr, "%s\n", archive_error_string(output_archive));
          emd_ruby_uncompress_cleanup(input_archive, output_archive);
          return E_UNCOMPRESS_ERROR;
      }

      /**
       * Write the trailing
       */
       result = archive_write_finish_entry(output_archive);
       if (result != ARCHIVE_OK) {
            fprintf(stderr, "%s\n", archive_error_string(output_archive));
            emd_ruby_uncompress_cleanup(input_archive, output_archive);
            return E_UNCOMPRESS_ERROR;
        }

  }

  emd_ruby_uncompress_cleanup(input_archive, output_archive);

  return E_SUCCESS;

}
/* ArchiveReader::__construct {{{
 *
*/
ZEND_METHOD(ArchiveReader, __construct) 
{
	archive_file_t *arch = NULL;
	int resource_id;
	zval *this = getThis();
	const char *error_string = NULL;
	char *filename;
	long error_num, filename_len, result, format = 0, compression = 0, block_size = 0;
    zend_error_handling error_handling;
	
    zend_replace_error_handling(EH_THROW, ce_ArchiveException, &error_handling TSRMLS_CC);
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lll", &filename, &filename_len, &format, &compression, &block_size) == FAILURE) {
        zend_restore_error_handling(&error_handling TSRMLS_CC);
		return;
	}
#if PHP_API_VERSION < 20100412
	if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
        zend_restore_error_handling(&error_handling TSRMLS_CC);
		return;
	}
#endif
	
	if (php_check_open_basedir(filename TSRMLS_CC)) {
        zend_restore_error_handling(&error_handling TSRMLS_CC);
		return;
	}
    
    if(block_size <= 0){
        block_size = PHP_ARCHIVE_BUF_LEN; 
    }

	arch = (archive_file_t *) emalloc(sizeof(archive_file_t));
	
	arch->stream = NULL;
	arch->current_entry = NULL;
	arch->entries = NULL;
	arch->struct_state = ARCHIVE_OK;
	arch->block_size = block_size;
	arch->mode = PHP_ARCHIVE_READ_MODE;
	arch->buf = emalloc(arch->block_size + 1);
	arch->filename = estrndup(filename, filename_len);	
	arch->arch = archive_read_new();


    archive_read_support_filter_all(arch->arch);
    switch(format){
        case PHP_ARCHIVE_FORMAT_TAR:
            archive_read_support_format_tar(arch->arch);
            break;
        case PHP_ARCHIVE_FORMAT_CPIO:
            archive_read_support_format_cpio(arch->arch);
            break;
        default:
            archive_read_support_format_all(arch->arch);
            break;
    }

    switch(compression){
        case PHP_ARCHIVE_COMPRESSION_NONE:
            break;
        case PHP_ARCHIVE_COMPRESSION_GZIP:
            if(archive_read_support_filter_gzip(arch->arch) != ARCHIVE_OK){
				efree(arch->filename);
				efree(arch->buf);
				efree(arch);
				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Gzip compression support is not available in this build ");
                zend_restore_error_handling(&error_handling TSRMLS_CC);
                return;
            }
            break;
        case PHP_ARCHIVE_COMPRESSION_BZIP2:
            if(archive_read_support_filter_gzip(arch->arch) != ARCHIVE_OK){
				efree(arch->filename);
				efree(arch->buf);
				efree(arch);
				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bzip2 compression support is not available in this build ");
                zend_restore_error_handling(&error_handling TSRMLS_CC);
				return;
            }
        default:
            archive_read_support_filter_all(arch->arch);
            break;
    }
	
	result = archive_read_open(arch->arch, arch, _archive_open_clbk, _archive_read_clbk, _archive_close_clbk);
	
	if (result) {
		error_num = archive_errno(arch->arch);
		error_string = archive_error_string(arch->arch);
		if (arch->stream) {
			php_stream_close(arch->stream);
		}
		if (error_num && error_string) {
			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open file %s for reading: error #%d, %s", filename, error_num, error_string);
		}
		else {
			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open file %s for reading: unknown error %d", filename, result);
		}	
        zend_restore_error_handling(&error_handling TSRMLS_CC);
		archive_read_close(arch->arch);
		archive_read_free(arch->arch);
		efree(arch->filename);
		efree(arch->buf);
		efree(arch);
		return;
	}

	resource_id = zend_list_insert(arch,le_archive);
	add_property_resource(this, "fd", resource_id);
	
    zend_restore_error_handling(&error_handling TSRMLS_CC);
	return;
}
Installer::ProceedState RomInstaller::on_checked_device()
{
    // /sbin is not going to be populated with anything useful in a normal boot
    // image. We can almost guarantee that a recovery image is going to be
    // installed though, so we'll open the recovery partition with libmbp and
    // extract its /sbin with libarchive into the chroot's /sbin.

    std::string block_dev(_recovery_block_dev);
    mbp::BootImage bi;
    mbp::CpioFile innerCpio;
    const unsigned char *ramdisk_data;
    std::size_t ramdisk_size;
    bool using_boot = false;

    // Check if the device has a combined boot/recovery partition. If the
    // FOTAKernel partition is listed, it will be used instead of the combined
    // ramdisk from the boot image
    bool combined = mb_device_flags(_device)
            & FLAG_HAS_COMBINED_BOOT_AND_RECOVERY;
    if (combined && block_dev.empty()) {
        block_dev = _boot_block_dev;
        using_boot = true;
    }

    if (block_dev.empty()) {
        display_msg("Could not determine the recovery block device");
        return ProceedState::Fail;
    }

    if (!bi.loadFile(block_dev)) {
        display_msg("Failed to load recovery partition image");
        return ProceedState::Fail;
    }

    // Load ramdisk
    bi.ramdiskImageC(&ramdisk_data, &ramdisk_size);

    if (using_boot) {
        if (!innerCpio.load(ramdisk_data, ramdisk_size)) {
            display_msg("Failed to load ramdisk from combined boot image");
            return ProceedState::Fail;
        }

        if (!innerCpio.contentsC("sbin/ramdisk-recovery.cpio",
                                 &ramdisk_data, &ramdisk_size)) {
            display_msg("Could not find recovery ramdisk in combined boot image");
            return ProceedState::Fail;
        }
    }

    autoclose::archive in(archive_read_new(), archive_read_free);
    autoclose::archive out(archive_write_disk_new(), archive_write_free);

    if (!in || !out) {
        LOGE("Out of memory");
        return ProceedState::Fail;
    }

    archive_entry *entry;

    // Set up input
    archive_read_support_filter_gzip(in.get());
    archive_read_support_filter_lzop(in.get());
    archive_read_support_filter_lz4(in.get());
    archive_read_support_filter_lzma(in.get());
    archive_read_support_filter_xz(in.get());
    archive_read_support_format_cpio(in.get());

    int ret = archive_read_open_memory(in.get(),
            const_cast<unsigned char *>(ramdisk_data), ramdisk_size);
    if (ret != ARCHIVE_OK) {
        LOGW("Failed to open recovery ramdisk: %s",
             archive_error_string(in.get()));
        return ProceedState::Fail;
    }

    // Set up output
    archive_write_disk_set_options(out.get(),
                                   ARCHIVE_EXTRACT_ACL |
                                   ARCHIVE_EXTRACT_FFLAGS |
                                   ARCHIVE_EXTRACT_PERM |
                                   ARCHIVE_EXTRACT_SECURE_NODOTDOT |
                                   ARCHIVE_EXTRACT_SECURE_SYMLINKS |
                                   ARCHIVE_EXTRACT_TIME |
                                   ARCHIVE_EXTRACT_UNLINK |
                                   ARCHIVE_EXTRACT_XATTR);

    while ((ret = archive_read_next_header(in.get(), &entry)) == ARCHIVE_OK) {
        std::string path = archive_entry_pathname(entry);

        if (path == "default.prop") {
            path = "default.recovery.prop";
        } else if (!util::starts_with(path, "sbin/")) {
            continue;
        }

        LOGE("Copying from recovery: %s", path.c_str());

        archive_entry_set_pathname(entry, in_chroot(path).c_str());

        if (util::libarchive_copy_header_and_data(
                in.get(), out.get(), entry) != ARCHIVE_OK) {
            return ProceedState::Fail;
        }

        archive_entry_set_pathname(entry, path.c_str());
    }

    if (ret != ARCHIVE_EOF) {
        LOGE("Archive extraction ended without reaching EOF: %s",
             archive_error_string(in.get()));
        return ProceedState::Fail;
    }

    // Create fake /etc/fstab file to please installers that read the file
    std::string etc_fstab(in_chroot("/etc/fstab"));
    if (access(etc_fstab.c_str(), R_OK) < 0 && errno == ENOENT) {
        autoclose::file fp(autoclose::fopen(etc_fstab.c_str(), "w"));
        if (fp) {
            auto system_devs = mb_device_system_block_devs(_device);
            auto cache_devs = mb_device_cache_block_devs(_device);
            auto data_devs = mb_device_data_block_devs(_device);

            // Set block device if it's provided and non-empty
            const char *system_dev =
                    system_devs && system_devs[0] && system_devs[0][0]
                    ? system_devs[0] : "dummy";
            const char *cache_dev =
                    cache_devs && cache_devs[0] && cache_devs[0][0]
                    ? cache_devs[0] : "dummy";
            const char *data_dev =
                    data_devs && data_devs[0] && data_devs[0][0]
                    ? data_devs[0] : "dummy";

            fprintf(fp.get(), "%s /system ext4 rw 0 0\n", system_dev);
            fprintf(fp.get(), "%s /cache ext4 rw 0 0\n", cache_dev);
            fprintf(fp.get(), "%s /data ext4 rw 0 0\n", data_dev);
        }
    }

    // Load recovery properties
    util::file_get_all_properties(
            in_chroot("/default.recovery.prop"), &_recovery_props);

    return ProceedState::Continue;
}
Example #20
0
/**
 * Extract the archive stored at the given @path.  This function
 * returns -1 if an error occurred, otherwise 0.
 */
int extract_archive_from_file(const char *path)
{
    struct archive *archive = NULL;
    CallbackData *data = NULL;
    int status2;
    int status;

    archive = archive_read_new();
    if (archive == NULL) {
        note(NULL, ERROR, INTERNAL, "can't initialize archive structure");
        status = -1;
        goto end;
    }

    status = archive_read_support_format_cpio(archive);
    if (status != ARCHIVE_OK) {
        note(NULL, ERROR, INTERNAL, "can't set archive format: %s",
             archive_error_string(archive));
        status = -1;
        goto end;
    }

    status = archive_read_support_format_gnutar(archive);
    if (status != ARCHIVE_OK) {
        note(NULL, ERROR, INTERNAL, "can't set archive format: %s",
             archive_error_string(archive));
        status = -1;
        goto end;
    }

    status = archive_read_support_filter_gzip(archive);
    if (status != ARCHIVE_OK) {
        note(NULL, ERROR, INTERNAL, "can't add archive filter: %s",
             archive_error_string(archive));
        status = -1;
        goto end;
    }

    status = archive_read_support_filter_lzop(archive);
    if (status != ARCHIVE_OK) {
        note(NULL, ERROR, INTERNAL, "can't add archive filter: %s",
             archive_error_string(archive));
        status = -1;
        goto end;
    }

    data = talloc_zero(NULL, CallbackData);
    if (data == NULL) {
        note(NULL, ERROR, INTERNAL, "can't allocate callback data");
        status = -1;
        goto end;

    }

    data->path = talloc_strdup(data, path);
    if (data->path == NULL) {
        note(NULL, ERROR, INTERNAL, "can't allocate callback data path");
        status = -1;
        goto end;

    }

    status = archive_read_open(archive, data, open_callback, read_callback, close_callback);
    if (status != ARCHIVE_OK) {
        /* Don't complain if no error message were registered,
         * ie. when testing for a self-extracting archive.  */
        if (archive_error_string(archive) != NULL)
            note(NULL, ERROR, INTERNAL, "can't read archive: %s",
                 archive_error_string(archive));
        status = -1;
        goto end;
    }

    status = extract_archive(archive);
end:
    if (archive != NULL) {
        status2 = archive_read_close(archive);
        if (status2 != ARCHIVE_OK) {
            note(NULL, WARNING, INTERNAL, "can't close archive: %s",
                 archive_error_string(archive));
        }

        status2 = archive_read_free(archive);
        if (status2 != ARCHIVE_OK) {
            note(NULL, WARNING, INTERNAL, "can't free archive: %s",
                 archive_error_string(archive));
        }
    }

    TALLOC_FREE(data);

    return status;
}