Ejemplo n.º 1
0
int extract_file(char* dumpFile, char* file) {
	struct archive_entry *entry;
	int r;

	struct archive *a = archive_read_new();
	archive_read_support_compression_gzip(a);
	archive_read_support_format_cpio(a);
	FILE *dump = fopen(dumpFile, "r");
	r = archive_read_open_FILE(a, dump);
	if (r != ARCHIVE_OK) {
		fprintf(stderr, "Fail to read cpio file");
		fclose(dump);
		return -1;
	}
	fclose(dump);

	FILE *outputFile = fopen(basename(file), "w");
	int notfound = 1;
	while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
		if(strcmp(basename(archive_entry_pathname(entry)), file) == 0) {
			notfound = 0;
			printf("extract %s\n",archive_entry_pathname(entry));
			archive_read_data_into_fd(a, fileno(outputFile));
		}
	}
	fclose(outputFile);
	archive_read_free(a);
	if(notfound) {
		sprintf(stderr, "The crash log %s was not found\n", file);
		return -1;
	}
	return 1;
}
int
archive_read_support_compression_all(struct archive *a)
{
	/* Bzip falls back to "bunzip2" command-line */
	archive_read_support_compression_bzip2(a);
	/* The decompress code doesn't use an outside library. */
	archive_read_support_compression_compress(a);
	/* Gzip decompress falls back to "gunzip" command-line. */
	archive_read_support_compression_gzip(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_compression_lzma(a);
	/* Xz falls back to "unxz" command-line program. */
	archive_read_support_compression_xz(a);
	/* The decode code doesn't use an outside library. */
	archive_read_support_compression_uu(a);
	/* The decode code doesn't use an outside library. */
#ifndef __minix
	archive_read_support_compression_rpm(a);
#endif
	/* 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);
}
Ejemplo n.º 3
0
int extract_archive(char *filename)
{
    struct archive *a;
    struct archive_entry *entry;
    int r;
    size_t size;
    char buff[BUFFER_SIZE];
    FILE *fd;

    a = archive_read_new();
    archive_read_support_compression_gzip(a);
    archive_read_support_format_tar(a);

    r = archive_read_open_filename(a, filename, 10240);

    if (r != ARCHIVE_OK) {
        return 1;
    }

    for (;;) {
        if ((r = archive_read_next_header(a, &entry))) {
            if (r != ARCHIVE_OK) {
                if (r == ARCHIVE_EOF) {
                    return 0;
                }else{
                    return 1;
                }
            }
        }

        fd = fopen(archive_entry_pathname(entry),"wb");
        if (fd == NULL) {
            fprintf(stderr, "problem extracting archive: %s: %s\n", filename,
                    strerror(errno));
            return 1;
        }
        for (;;) {
            size = archive_read_data(a, buff, BUFFER_SIZE);
            if (size < 0) {
                return 1;
            }

            if (size == 0) {
                break;
            }

            fwrite(buff, 1, size, fd);
        }
        fclose(fd);
    }

    r = archive_read_finish(a);
    if (r != ARCHIVE_OK) {
        return 4;
    }
    return 0;
}
Ejemplo n.º 4
0
/**
 * @brief Creates a new FreeBSD package from a FILE pointer
 * @param fd A pointer to a FILE object containing a FreeBSD Package
 *
 * This creates a pkg object from a given file pointer.
 * It is able to then manipulate the package and install the it to the pkg_db.
 * @todo Write
 * @return A new package object or NULL
 */
struct pkg *
pkg_new_freebsd_from_file(FILE *fd)
{
	struct pkg *pkg;
	struct freebsd_package *fpkg;
	const char *pkg_name;

	if (fd == NULL)
		return NULL;

	/* Create the new package data object */
	fpkg = freebsd_package_new();
	if (fpkg == NULL)
		return NULL;

	fpkg->fd = fd;
	fpkg->pkg_type = fpkg_from_file;
	fpkg->archive = archive_read_new();
	archive_read_support_compression_bzip2(fpkg->archive);
	archive_read_support_compression_gzip(fpkg->archive);
	archive_read_support_format_tar(fpkg->archive);
	archive_read_open_stream(fpkg->archive, fd, 10240);
	
	/*
	 * Get the +CONTENTS file.
	 * We can't use the callbacks as we need the
	 * package name to use with pkg_new
	 */
	freebsd_open_control_files(fpkg);
	assert(fpkg->control != NULL);

	freebsd_parse_contents(fpkg);
	assert(fpkg->contents != NULL);
	if (fpkg->contents->lines[1].line_type != PKG_LINE_NAME) {
		/** @todo cleanup */
		return NULL;
	}

	pkg_name = fpkg->contents->lines[1].data;
	pkg = pkg_new(pkg_name, freebsd_get_control_files,
	    freebsd_get_control_file, freebsd_get_deps, freebsd_free);
	if (pkg == NULL) {
		/** @todo cleanup */
		return NULL;
	}
	pkg_add_callbacks_data(pkg, freebsd_get_version, freebsd_get_origin);
	pkg_add_callbacks_install(pkg, freebsd_get_next_file,
	    freebsd_run_script);
	pkg->data = fpkg;

	return pkg;
}
int
archive_read_support_compression_all(struct archive *a)
{
#if HAVE_BZLIB_H
	archive_read_support_compression_bzip2(a);
#endif
	/* The decompress code doesn't use an outside library. */
	archive_read_support_compression_compress(a);
#if HAVE_ZLIB_H
	archive_read_support_compression_gzip(a);
#endif
	return (ARCHIVE_OK);
}
Ejemplo n.º 6
0
static bool
repo_open_local(struct xbps_repo *repo, const char *repofile)
{
	struct stat st;
	int rv = 0;

	if (fstat(repo->fd, &st) == -1) {
		rv = errno;
		xbps_dbg_printf(repo->xhp, "[repo] `%s' fstat repodata %s\n",
		    repofile, strerror(rv));
		return false;
	}

	repo->ar = archive_read_new();
	archive_read_support_compression_gzip(repo->ar);
	archive_read_support_format_tar(repo->ar);

	if (archive_read_open_fd(repo->ar, repo->fd, st.st_blksize) == ARCHIVE_FATAL) {
		rv = archive_errno(repo->ar);
		xbps_dbg_printf(repo->xhp,
		    "[repo] `%s' failed to open repodata archive %s\n",
		    repofile, strerror(rv));
		return false;
	}
	if ((repo->idx = repo_get_dict(repo)) == NULL) {
		rv = archive_errno(repo->ar);
		xbps_dbg_printf(repo->xhp, "[repo] `%s' failed to internalize "
		    " index on archive, removing file.\n", repofile);
		/* broken archive, remove it */
		(void)unlink(repofile);
		return false;
	}
	xbps_dictionary_make_immutable(repo->idx);
	repo->idxmeta = repo_get_dict(repo);
	if (repo->idxmeta != NULL) {
		repo->is_signed = true;
		xbps_dictionary_make_immutable(repo->idxmeta);
	}

	return true;
}
Ejemplo n.º 7
0
/*
 * Returns a pointer to be placed into the data of the Package object
 */
static struct freebsd_package *
freebsd_get_package(FILE *fd)
{
	struct freebsd_package *f_pkg;
	struct pkg_file *file;
	size_t control_size;
	unsigned int control_pos;

	f_pkg = malloc(sizeof(struct freebsd_package));
	if (!f_pkg) {
		return NULL;
	}

	/* Init the struct */
	f_pkg->next = NULL;
	f_pkg->control = NULL;
	f_pkg->contents = NULL;
	f_pkg->fd = fd;

	/* We only need to read from gzip and bzip2 as they
	 * are the only posible file types for FreeBSD packages
	 */
	f_pkg->archive = archive_read_new();
	archive_read_support_compression_bzip2(f_pkg->archive);
	archive_read_support_compression_gzip(f_pkg->archive);
	archive_read_support_format_tar(f_pkg->archive);

	if (archive_read_open_stream(f_pkg->archive, fd, 10240)
	    != ARCHIVE_OK) {
		freebsd_free_package(f_pkg);
		return NULL;
	}

	/* Read the first file and check it has the correct name */
	file = freebsd_get_next_entry(f_pkg->archive);
	if (!file) {
		freebsd_free_package(f_pkg);
		return NULL;
	} else if (strcmp(file->filename, "+CONTENTS")) {
		/* Package error */
		pkg_file_free(file);
		freebsd_free_package(f_pkg);
		return NULL;
	}
	/* Set the control files array to be big enough for
	 * the +CONTENTS file and a null terminator
	 */
	f_pkg->contents = pkg_freebsd_contents_new(file->contents);
	control_size = sizeof(struct pkg_file *) * 2;
	f_pkg->control = malloc(control_size);
	f_pkg->control[0] = file;
	f_pkg->control[1] = NULL;
	control_pos = 1;

	/* Add all the control files to the control array */
	while (1) {
		file = freebsd_get_next_entry(f_pkg->archive);
		if (file == NULL) {
			break;
		} else if (file->filename[0] != '+') {
			f_pkg->next = file;
			break;
		} else {
			control_size += sizeof(struct pkg_file *);
			f_pkg->control = realloc(f_pkg->control, control_size);
			f_pkg->control[control_pos] = file;
			control_pos++;
			f_pkg->control[control_pos] = NULL;
		}
	}

	return f_pkg;
}
Ejemplo n.º 8
0
int HIDDEN
xbps_unpack_binary_pkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
{
	struct archive *ar = NULL;
	struct stat st;
	const char *pkgver;
	char *bpkg = NULL;
	int pkg_fd = -1, rv = 0;

	assert(xbps_object_type(pkg_repod) == XBPS_TYPE_DICTIONARY);

	xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgver", &pkgver);
	xbps_set_cb_state(xhp, XBPS_STATE_UNPACK, 0, pkgver, NULL);

	bpkg = xbps_repository_pkg_path(xhp, pkg_repod);
	if (bpkg == NULL) {
		xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
		    errno, pkgver,
		    "%s: [unpack] cannot determine binary package "
		    "file for `%s': %s", pkgver, bpkg, strerror(errno));
		return errno;
	}

	if ((ar = archive_read_new()) == NULL) {
		free(bpkg);
		return ENOMEM;
	}
	/*
	 * Enable support for tar format and gzip/bzip2/lzma compression methods.
	 */
	archive_read_support_compression_gzip(ar);
	archive_read_support_compression_bzip2(ar);
	archive_read_support_compression_xz(ar);
	archive_read_support_format_tar(ar);

	pkg_fd = open(bpkg, O_RDONLY|O_CLOEXEC);
	if (pkg_fd == -1) {
		rv = errno;
		xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
		    rv, pkgver,
		    "%s: [unpack] failed to open binary package `%s': %s",
		    pkgver, bpkg, strerror(rv));
		goto out;
	}
	if (fstat(pkg_fd, &st) == -1) {
		rv = errno;
		xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
		    rv, pkgver,
		    "%s: [unpack] failed to fstat binary package `%s': %s",
		    pkgver, bpkg, strerror(rv));
		goto out;
	}
	if (archive_read_open_fd(ar, pkg_fd, st.st_blksize) == ARCHIVE_FATAL) {
		rv = archive_errno(ar);
		xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
		    rv, pkgver,
		    "%s: [unpack] failed to read binary package `%s': %s",
		    pkgver, bpkg, strerror(rv));
		goto out;
	}
	/*
	 * Extract archive files.
	 */
	if ((rv = unpack_archive(xhp, pkg_repod, pkgver, bpkg, ar)) != 0) {
		xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
		    rv, pkgver,
		    "%s: [unpack] failed to unpack files from archive: %s",
		    pkgver, strerror(rv));
		goto out;
	}
	/*
	 * Set package state to unpacked.
	 */
	if ((rv = xbps_set_pkg_state_installed(xhp, pkgver,
	    XBPS_PKG_STATE_UNPACKED)) != 0) {
		xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
		    rv, pkgver,
		    "%s: [unpack] failed to set state to unpacked: %s",
		    pkgver, strerror(rv));
	}
out:
	if (pkg_fd != -1)
		close(pkg_fd);
	if (ar)
		archive_read_finish(ar);
	if (bpkg)
		free(bpkg);

	return rv;
}
Ejemplo n.º 9
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_compression_bzip2(a);
#endif
#ifndef NO_GZIP_EXTRACT
    archive_read_support_compression_gzip(a);
#endif
#ifndef NO_COMPRESS_EXTRACT
    archive_read_support_compression_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_file(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_finish(a);
    exit(0);
}
Ejemplo n.º 10
0
int extractArchive(const char* filename, const char* directory) {
	struct archive *a;
	struct archive *ext;
	struct archive_entry *entry;
	int r;
	int flags = ARCHIVE_EXTRACT_TIME;
	int warnCount = 0;
	int dirlen = strlen(directory);
	a = archive_read_new();
	ext = archive_write_disk_new();
	archive_write_disk_set_options(ext, flags);
	/*
	 * Note: archive_write_disk_set_standard_lookup() is useful
	 * here, but it requires library routines that can add 500k or
	 * more to a static executable.
	 */
	archive_read_support_compression_gzip(a);
	archive_read_support_format_tar(a);
	/*
	 * On my system, enabling other archive formats adds 20k-30k
	 * each.  Enabling gzip decompression adds about 20k.
	 * Enabling bzip2 is more expensive because the libbz2 library
	 * isn't very well factored.
	 */
	if (filename != NULL && strcmp(filename, "-") == 0)
		filename = NULL;
	if ((r = archive_read_open_file(a, filename, 10240))) {
		printf("%s\n", archive_error_string(a));
		return -1;
	}
	while (true) {
		r = archive_read_next_header(a, &entry);
		if (r == ARCHIVE_EOF)
			break;
		if (r != ARCHIVE_OK) {
			printf("%s\n", archive_error_string(a));
			return -1;
		}
		//Set entry to be extracted under the given directory
		const char* path = archive_entry_pathname(entry);
		int pathlength = dirlen + strlen(path) + 2; //One for / and the other for '\0'
		char newPath[pathlength];
		snprintf(newPath, pathlength, "%s/%s", directory, path);
		archive_entry_set_pathname(entry, newPath);

		r = archive_write_header(ext, entry);
		if (r != ARCHIVE_OK) {
			printf("%s\n", archive_error_string(ext));
			warnCount++;
		} else {
			copy_data(a, ext);
			r = archive_write_finish_entry(ext);
			if (r != ARCHIVE_OK) {
				printf("%s\n", archive_error_string(ext));
				warnCount++;
			}
		}
	}
	archive_read_close(a);
	//archive_read_free(a);
	return warnCount;
}
Ejemplo n.º 11
0
const gchar* archive_extract(const char* archive_name, int flags) {
	struct archive* in;
	struct archive* out;
	struct archive_entry* entry;
	int res = ARCHIVE_OK;
	gchar* buf = NULL;
	const char* result == NULL;

	g_return_val_if_fail(archive_name != NULL, ARCHIVE_FATAL);

	fprintf(stdout, "%s: extracting\n", archive_name);
	in = archive_read_new();
	if ((res = archive_read_support_format_tar(in)) == ARCHIVE_OK) {
		if ((res = archive_read_support_compression_gzip(in)) == ARCHIVE_OK) {
			if ((res = archive_read_open_file(
				in, archive_name, READ_BLOCK_SIZE)) != ARCHIVE_OK) {
				buf = g_strdup_printf(
						"%s: %s\n", archive_name, archive_error_string(in));
				g_warning("%s\n", buf);
				g_free(buf);
				result = archive_error_string(in);
			}
			else {
				out = archive_write_disk_new();
				if ((res = archive_write_disk_set_options(
								out, flags)) == ARCHIVE_OK) {
					res = archive_read_next_header(in, &entry);
					while (res == ARCHIVE_OK) {
						fprintf(stdout, "%s\n", archive_entry_pathname(entry));
						res = archive_write_header(out, entry);
						if (res != ARCHIVE_OK) {
							buf = g_strdup_printf("%s\n", 
											archive_error_string(out));
							g_warning("%s\n", buf);
							g_free(buf);
							/* skip this file an continue */
							res = ARCHIVE_OK;
						}
						else {
							res = archive_copy_data(in, out);
							if (res != ARCHIVE_OK) {
								buf = g_strdup_printf("%s\n", 
												archive_error_string(in));
								g_warning("%s\n", buf);
								g_free(buf);
								/* skip this file an continue */
								res = ARCHIVE_OK;
							}
							else
								res = archive_read_next_header(in, &entry);
						}
					}
					if (res == ARCHIVE_EOF)
						res = ARCHIVE_OK;
					if (res != ARCHIVE_OK) {
						buf = g_strdup_printf("%s\n", archive_error_string(in));
						if (*buf == '\n') {
							g_free(buf);
							buf = g_strdup_printf("%s: Unknown error\n", archive_name);
						}
						g_warning("%s\n", buf);
						g_free(buf);
						result = archive_error_string(in);
					}
				}
				else
					result = archive_error_string(out);
				archive_read_close(in);
			}
			archive_read_finish(in);
		}
		else
			result = archive_error_string(in);
	}
	else
		result = archive_error_string(in);
	return result;
}