static void test_extract_length_at_end(void) { const char *refname = "test_read_format_zip_length_at_end.zip"; char *p; size_t s; struct archive *a; extract_reference_file(refname); /* Verify extraction with seeking reader. */ assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240)); verify_extract_length_at_end(a, 1); /* Verify extraction with streaming reader. */ p = slurpfile(&s, refname); assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 108)); verify_extract_length_at_end(a, 0); free(p); }
static void test_compat_zip_6(void) { const char *refname = "test_compat_zip_6.zip"; struct archive *a; void *p; size_t s; extract_reference_file(refname); p = slurpfile(&s, refname); assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 7)); compat_zip_6_verify(a); assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a)); assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 7)); compat_zip_6_verify(a); assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a)); free(p); }
/** * A file with leading garbage (similar to an SFX file). */ static void test_compat_zip_4(void) { const char *refname = "test_compat_zip_4.zip"; struct archive_entry *ae; struct archive *a; void *p; size_t s; extract_reference_file(refname); p = slurpfile(&s, refname); /* SFX files require seek support. */ assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 18)); /* First entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("foo", archive_entry_pathname(ae)); assertEqualInt(4, archive_entry_size(ae)); assert(archive_entry_size_is_set(ae)); assertEqualInt(AE_IFREG, archive_entry_filetype(ae)); assertEqualInt(0412, archive_entry_perm(ae)); /* Second entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("bar", archive_entry_pathname(ae)); assertEqualInt(AE_IFREG, archive_entry_filetype(ae)); assertEqualInt(4, archive_entry_size(ae)); assert(archive_entry_size_is_set(ae)); assertEqualInt(0567, archive_entry_perm(ae)); /* Third entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("baz", archive_entry_pathname(ae)); assertEqualInt(AE_IFREG, archive_entry_filetype(ae)); assertEqualInt(4, archive_entry_size(ae)); assert(archive_entry_size_is_set(ae)); assertEqualInt(0644, archive_entry_perm(ae)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a)); /* Try reading without seek support and watch it fail. */ assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_FATAL, read_open_memory(a, p, s, 3)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a)); free(p); }
static void test_read_format_mtree2(void) { static char archive[] = "#mtree\n" "d type=dir content=.\n"; struct archive_entry *ae; struct archive *a; assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, archive, sizeof(archive))); assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualInt(archive_format(a), ARCHIVE_FORMAT_MTREE); assertEqualString(archive_entry_pathname(ae), "d"); assertEqualInt(archive_entry_filetype(ae), AE_IFDIR); assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); assertEqualInt(1, archive_file_count(a)); assertEqualInt(ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
static void test(int pristine) { struct archive* a = archive_read_new(); int known_option_rv = pristine ? ARCHIVE_FAILED : ARCHIVE_OK; if (!pristine) { archive_read_support_filter_all(a); archive_read_support_format_all(a); } /* NULL and "" denote `no option', so they're ok no matter * what, if any, formats are registered */ should(a, ARCHIVE_OK, NULL, NULL, NULL); should(a, ARCHIVE_OK, "", "", ""); /* unknown modules and options */ should(a, ARCHIVE_FAILED, "fubar", "snafu", NULL); should(a, ARCHIVE_FAILED, "fubar", "snafu", "betcha"); /* unknown modules and options */ should(a, ARCHIVE_FAILED, NULL, "snafu", NULL); should(a, ARCHIVE_FAILED, NULL, "snafu", "betcha"); /* ARCHIVE_OK with iso9660 loaded, ARCHIVE_WARN otherwise */ should(a, known_option_rv, "iso9660", "joliet", NULL); should(a, known_option_rv, "iso9660", "joliet", NULL); should(a, known_option_rv, NULL, "joliet", NULL); should(a, known_option_rv, NULL, "joliet", NULL); archive_read_free(a); }
int main(int argc, char* argv[]) { // setup the archive object struct archive* a = archive_read_new(); archive_read_support_filter_all(a); archive_read_support_format_all(a); // actually open the archive file int r = archive_read_open_filename(a, argv[1], 10240); // Note 1 if (r != ARCHIVE_OK) return 1; // read through the entries struct archive_entry *entry; while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { // output the path to the archived file puts(archive_entry_pathname(entry)); // move over the data part of the archive entry archive_read_data_skip(a); } // close it up r = archive_read_close(a); if (r != ARCHIVE_OK) return 1; return 0; }
/* * For each entry in the archive, it checks "entry + len" pointer against searched string. * Len is always the offset of the current dir inside archive, eg: foo.tgz/bar/x, * while checking x, len will be strlen("bar/") */ static int search_inside_archive(const char *path) { char *ptr; int len = 0, ret = FTW_CONTINUE, r = 0, string_length; struct archive_entry *entry; struct archive *a = archive_read_new(); archive_read_support_filter_all(a); archive_read_support_format_all(a); string_length = strlen(sv.searched_string); if ((a) && (archive_read_open_filename(a, path, BUFF_SIZE) == ARCHIVE_OK)) { while ((!quit) && (ret == FTW_CONTINUE) && (archive_read_next_header(a, &entry) == ARCHIVE_OK)) { if (!sv.search_lazy) { r = !strncmp(archive_entry_pathname(entry) + len, sv.searched_string, string_length); } else if (strcasestr(archive_entry_pathname(entry) + len, sv.searched_string)) { r = 1; } if (r) { snprintf(sv.found_searched[sv.found_cont], PATH_MAX, "%s/%s", path, archive_entry_pathname(entry)); sv.found_cont++; if (sv.found_cont == MAX_NUMBER_OF_FOUND) { ret = FTW_STOP; } } ptr = strrchr(archive_entry_pathname(entry), '/'); if ((ptr) && (strlen(ptr) == 1)) { len = strlen(archive_entry_pathname(entry)); } } } archive_read_free(a); return quit ? FTW_STOP : ret; }
static void pack_extract(const char *pack, const char *dbname, const char *dbpath) { struct archive *a = NULL; struct archive_entry *ae = NULL; if (access(pack, F_OK) != 0) return; a = archive_read_new(); archive_read_support_filter_all(a); archive_read_support_format_tar(a); if (archive_read_open_filename(a, pack, 4096) != ARCHIVE_OK) { /* if we can't unpack it it won't be useful for us */ unlink(pack); archive_read_free(a); return; } while (archive_read_next_header(a, &ae) == ARCHIVE_OK) { if (strcmp(archive_entry_pathname(ae), dbname) == 0) { archive_entry_set_pathname(ae, dbpath); archive_read_extract(a, ae, EXTRACT_ARCHIVE_FLAGS); break; } } archive_read_free(a); }
Reader *Reader::read_open_filename(const char *filename, const char *cmd, bool raw) { struct archive *ar = archive_read_new(); try { if(cmd) { if(archive_read_support_filter_program(ar, cmd) != ARCHIVE_OK) throw 0; } else { if(archive_read_support_filter_all(ar) != ARCHIVE_OK) throw 0; } if(raw) { if(archive_read_support_format_raw(ar) != ARCHIVE_OK) throw 0; } else { if(archive_read_support_format_all(ar) != ARCHIVE_OK) throw 0; } if(archive_read_open_filename(ar, filename, 1024) != ARCHIVE_OK) throw 0; } catch(...) { std::string error_msg = archive_error_string(ar); archive_read_free(ar); throw Error(error_msg); } return new Reader(ar); }
int load_package_files(struct pkg *pkg, int fd) { struct archive *archive; struct file_t file; if (file_from_fd(&file, fd) < 0) return -1; archive = archive_read_new(); archive_read_support_filter_all(archive); archive_read_support_format_all(archive); if (archive_read_open_memory(archive, file.mmap, file.st.st_size) != ARCHIVE_OK) { archive_read_free(archive); return -1; } struct archive_entry *entry; while (archive_read_next_header(archive, &entry) == ARCHIVE_OK) { const char *entry_name = archive_entry_pathname(entry); if (!is_package_metadata(entry_name)) pkg->files = alpm_list_add(pkg->files, strdup(entry_name)); } archive_read_close(archive); archive_read_free(archive); return 0; }
static int get_format(const char *archive) { struct archive *a; struct archive_entry *ae; int found_format; assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_mtree(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_raw(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_empty(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, archive, strlen(archive))); // Read one header to settle the format. // This might return EOF or OK. archive_read_next_header(a, &ae); found_format = archive_format(a); assertEqualInt(ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); return found_format; }
/* * We should get a warning if the contents file doesn't exist. */ static void test_read_format_mtree5(void) { static char archive[] = "#mtree\n" "a type=file contents=nonexistent_file\n"; struct archive_entry *ae; struct archive *a; assertMakeDir("mtree5", 0777); assertChdir("mtree5"); assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, archive, sizeof(archive))); assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae)); assert(strlen(archive_error_string(a)) > 0); assertEqualString(archive_entry_pathname(ae), "a"); assertEqualInt(archive_entry_filetype(ae), AE_IFREG); assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); assertEqualInt(1, archive_file_count(a)); assertEqualInt(ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); assertChdir(".."); }
static void verify(unsigned char *d, size_t s, void (*f)(struct archive_entry *), int compression, int format) { struct archive_entry *ae; struct archive *a; unsigned char *buff = malloc(100000); memcpy(buff, d, s); memset(buff + s, 0, 2048); assert((a = archive_read_new()) != NULL); assertA(0 == archive_read_support_filter_all(a)); assertA(0 == archive_read_support_format_all(a)); assertA(0 == archive_read_open_memory(a, buff, s + 1024)); assertA(0 == archive_read_next_header(a, &ae)); assertEqualInt(archive_filter_code(a, 0), compression); assertEqualInt(archive_format(a), format); /* Verify the only entry. */ f(ae); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); free(buff); }
static void test_noeof(void) { char buff[64]; const char reffile[] = "test_read_format_rar_noeof.rar"; const char test_txt[] = "test text document\r\n"; int size = sizeof(test_txt)-1; struct archive_entry *ae; struct archive *a; extract_reference_file(reffile); assert((a = archive_read_new()) != NULL); assertA(0 == archive_read_support_filter_all(a)); assertA(0 == archive_read_support_format_all(a)); assertA(0 == archive_read_open_file(a, reffile, 10240)); /* First header. */ assertA(0 == archive_read_next_header(a, &ae)); assertEqualString("test.txt", archive_entry_pathname(ae)); assertA((int)archive_entry_mtime(ae)); assertA((int)archive_entry_ctime(ae)); assertA((int)archive_entry_atime(ae)); assertEqualInt(20, archive_entry_size(ae)); assertEqualInt(33188, archive_entry_mode(ae)); assertA(size == archive_read_data(a, buff, size)); assertEqualMem(buff, test_txt, size); /* Test EOF */ assertA(1 == archive_read_next_header(a, &ae)); assertEqualInt(1, archive_file_count(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
void TarUtils::read( const ibrcommon::File &extract_folder, std::istream &input ) { struct archive *a; struct archive_entry *entry; int ret,fd; a = archive_read_new(); archive_read_support_filter_all(a); archive_read_support_compression_all(a); archive_read_support_format_tar(a); archive_read_open(a, (void*) &input, &__tar_utils_open_callback, &__tar_utils_read_callback, &__tar_utils_close_callback); while ((ret = archive_read_next_header(a, &entry)) == ARCHIVE_OK ) { ibrcommon::File filename = extract_folder.get(archive_entry_pathname(entry)); ibrcommon::File path = filename.getParent(); ibrcommon::File::createDirectory(path); fd = open(filename.getPath().c_str(),O_CREAT|O_WRONLY,0600); if(fd < 0) throw ibrcommon::IOException("cannot open file " + path.getPath()); archive_read_data_into_fd(a,fd); close(fd); } archive_read_free(a); }
/* * Copy from specified archive to current archive. Returns non-zero * for write errors (which force us to terminate the entire archiving * operation). If there are errors reading the input archive, we set * bsdtar->return_value but return zero, so the overall archiving * operation will complete and return non-zero. */ static int append_archive_filename(struct bsdtar *bsdtar, struct archive *a, const char *raw_filename) { struct archive *ina; const char *filename = raw_filename; int rc; if (strcmp(filename, "-") == 0) filename = NULL; /* Library uses NULL for stdio. */ ina = archive_read_new(); archive_read_support_format_all(ina); archive_read_support_filter_all(ina); set_reader_options(bsdtar, a); if (archive_read_open_filename(ina, filename, bsdtar->bytes_per_block)) { lafe_warnc(0, "%s", archive_error_string(ina)); bsdtar->return_value = 1; return (0); } rc = append_archive(bsdtar, a, ina); if (rc != ARCHIVE_OK) { lafe_warnc(0, "Error reading archive %s: %s", raw_filename, archive_error_string(ina)); bsdtar->return_value = 1; } archive_read_free(ina); return (rc); }
/* * An archive file has no entry. */ static void test_empty_archive() { const char *refname = "test_read_format_7zip_empty_archive.7z"; struct archive_entry *ae; struct archive *a; extract_reference_file(refname); assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240)); /* End of archive. */ assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); assertEqualInt(0, archive_file_count(a)); /* Verify archive format. */ assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0)); assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a)); /* Close the archive. */ assertEqualInt(ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
int Tarball::install () { archive_entry *entry; int r; archive* a = archive_read_new(); archive_read_support_format_all(a); archive_read_support_filter_all(a); archive* ext = archive_write_disk_new(); const int flags = ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS; archive_write_disk_set_options(ext, flags); archive_write_disk_set_standard_lookup(ext); const std::string subdir = "deps"; const std::string filename = subdir + "/" + basename(this->location); printf("Unpacking archive %s\n", filename.c_str()); if ((r = archive_read_open_filename(a,filename.c_str(), 10240))) { fprintf(stderr, "Error opening archive:\n%s\n", archive_error_string(a)); return -1; } for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_EOF) { break; } if (r < ARCHIVE_OK) { fprintf(stderr, "%s\n", archive_error_string(a)); } if (r < ARCHIVE_WARN) { return -1; } rewrite_subdir(entry, subdir); r = archive_write_header(ext, entry); if (r < ARCHIVE_OK) { fprintf(stderr, "%s\n", archive_error_string(ext)); } else if (archive_entry_size(entry) > 0) { r = copy_data(a, ext); if (r < ARCHIVE_OK) { fprintf(stderr, "%s\n", archive_error_string(ext)); } if (r < ARCHIVE_WARN) { return -1; } } r = archive_write_finish_entry(ext); if (r < ARCHIVE_OK) { fprintf(stderr, "%s\n", archive_error_string(ext)); } if (r < ARCHIVE_WARN) { return -1; } } archive_read_close(a); archive_read_free(a); archive_write_close(ext); archive_write_free(ext); return 0; };
/* * Verify that we skip junk between entries. The compat_zip_2.zip file * has several bytes of junk between 'file1' and 'file2'. Such * junk is routinely introduced by some Zip writers when they manipulate * existing zip archives. */ static void test_compat_zip_2(void) { char name[] = "test_compat_zip_2.zip"; struct archive_entry *ae; struct archive *a; assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a)); extract_reference_file(name); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240)); /* Read first entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("file1", archive_entry_pathname(ae)); /* Read first entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("file2", archive_entry_pathname(ae)); assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); assertEqualInt(ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
/* * An archive file has one empty file. It means there is no content * in the archive file except for a header. */ static void test_empty_file() { const char *refname = "test_read_format_7zip_empty_file.7z"; struct archive_entry *ae; struct archive *a; extract_reference_file(refname); assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240)); /* Verify regular empty. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae)); assertEqualString("empty", archive_entry_pathname(ae)); assertEqualInt(86401, archive_entry_mtime(ae)); assertEqualInt(0, archive_entry_size(ae)); assertEqualInt(1, archive_file_count(a)); /* End of archive. */ assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); /* Verify archive format. */ assertEqualIntA(a, ARCHIVE_COMPRESSION_NONE, archive_compression(a)); assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a)); /* Close the archive. */ assertEqualInt(ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
static void verify(unsigned char *buff, size_t used, enum vtype type, struct fns *fns) { struct archive *a; struct archive_entry *ae; size_t i; /* * Read ISO image. */ assert((a = archive_read_new()) != NULL); assertEqualIntA(a, 0, archive_read_support_format_all(a)); assertEqualIntA(a, 0, archive_read_support_filter_all(a)); if (type >= 1) assertA(0 == archive_read_set_option(a, NULL, "rockridge", NULL)); if (type >= 2) assertA(0 == archive_read_set_option(a, NULL, "joliet", NULL)); assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used)); /* * Read Root Directory * Root Directory entry must be in ISO image. */ assertEqualIntA(a, 0, archive_read_next_header(a, &ae)); assertEqualInt(archive_entry_atime(ae), archive_entry_ctime(ae)); assertEqualInt(archive_entry_atime(ae), archive_entry_mtime(ae)); assertEqualString(".", archive_entry_pathname(ae)); switch (type) { case ROCKRIDGE: assert((S_IFDIR | 0555) == archive_entry_mode(ae)); break; case JOLIET: assert((S_IFDIR | 0700) == archive_entry_mode(ae)); break; case ISO9660: assert((S_IFDIR | 0700) == archive_entry_mode(ae)); break; } /* * Verify file status. */ memset(fns->names, 0, sizeof(char *) * fns->alloc); fns->cnt = 0; for (i = 0; i < fns->alloc; i++) verify_file(a, type, fns); for (i = 0; i < fns->alloc; i++) free(fns->names[i]); assertEqualInt((int)fns->longest_len, (int)fns->maxlen); /* * Verify the end of the archive. */ assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a)); }
bool Decompress(const std::string & file, const std::string & output_path, std::ostream & info_output, std::ostream & error_output) { // Ensure the output path exists. PathManager::MakeDir(output_path); struct archive * a = archive_read_new(); archive_read_support_filter_all(a); archive_read_support_format_all(a); if (archive_read_open_filename(a, file.c_str(), BUFFSIZE) != ARCHIVE_OK) { error_output << "Unable to open " << file << std::endl; return false; } char buff[BUFFSIZE]; struct archive_entry *entry; while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { std::string filename = archive_entry_pathname(entry); std::string fullpath = output_path + "/" + filename; if (archive_entry_filetype(entry) == AE_IFDIR) PathManager::MakeDir(fullpath); else { std::fstream f(fullpath.c_str(), std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (!f) { error_output << "Unable to open file for write (permissions issue?): " << fullpath << std::endl; return false; } int size = -1; while (size != 0) { size = archive_read_data(a, buff, BUFFSIZE); if (size < 0) { error_output << "Encountered corrupt file: " << filename << std::endl; return false; } else if (size != 0) f.write(buff, size); } } archive_read_data_skip(a); } if (archive_read_free(a) != ARCHIVE_OK) { error_output << "Unable to finish read of " << file << std::endl; return false; } info_output << "Successfully decompressed " << file << std::endl; return true; }
FILE * uncompress(FILE *f, FILE* out, enum Options options) { int fdtmp = -1, i, size; struct archive *a = NULL; struct archive_entry *entry; FILE *rv = f, *ftmp; char buf[BUFSIZ], tmpfile[] = "/tmp/nile.XXXXXX", nfilter = 0; if(!(options & NILE_DECOMPRESS)) goto uncompress_cleanup; a = archive_read_new(); archive_read_support_filter_all(a); archive_read_support_format_raw(a); if(archive_read_open_FILE(a, f) != ARCHIVE_OK || archive_read_next_header(a, &entry) != ARCHIVE_OK) { fprintf(stderr, "Warning: decompression failed while open\n"); goto uncompress_cleanup; } if((nfilter = archive_filter_count(a)) == 0) { goto uncompress_cleanup; } if((fdtmp = mkstemp(tmpfile)) == -1) { perror("mkstemp"); goto uncompress_cleanup; } while((size = archive_read_data(a, buf, sizeof(buf))) > 0) { write(fdtmp, buf, size); } if (size < 0) { fprintf(stderr, "Warning: decompression failed read\n"); goto uncompress_cleanup; } if((ftmp = fdopen(fdtmp, "r")) == NULL) { perror("fdopen"); goto uncompress_cleanup; } rv = ftmp; uncompress_cleanup: rewind(f); if(out != NULL) { for(i = 0; i < nfilter; i++) { fputc(archive_filter_code(a, i), out); } for(i = i % HEADER_PADDING; i < HEADER_PADDING; i++) { fputc(0, out); } } if(fdtmp > 0) close(fdtmp); if(a != NULL) { archive_read_free(a); } return rv; }
static void test_binary(void) { const char reffile[] = "test_read_format_rar_binary_data.rar"; char file1_buff[1048576]; int file1_size = sizeof(file1_buff); const char file1_test_txt[] = "\x37\xef\xb2\xbe\x33\xf6\xcc\xcb\xee\x2a\x10" "\x9d\x2e\x01\xe9\xf6\xf9\xe5\xe6\x67\x0c\x2b" "\xd8\x6b\xa0\x26\x9a\xf7\x93\x87\x42\xf1\x08" "\x42\xdc\x9b\x76\x91\x20\xa4\x01\xbe\x67\xbd" "\x08\x74\xde\xec"; char file2_buff[32618]; int file2_size = sizeof(file2_buff); const char file2_test_txt[] = "\x00\xee\x78\x00\x00\x4d\x45\x54\x41\x2d\x49" "\x4e\x46\x2f\x6d\x61\x6e\x69\x66\x65\x73\x74" "\x2e\x78\x6d\x6c\x50\x4b\x05\x06\x00\x00\x00" "\x00\x12\x00\x12\x00\xaa\x04\x00\x00\xaa\x7a" "\x00\x00\x00\x00"; struct archive_entry *ae; struct archive *a; extract_reference_file(reffile); assert((a = archive_read_new()) != NULL); assertA(0 == archive_read_support_filter_all(a)); assertA(0 == archive_read_support_format_all(a)); assertA(0 == archive_read_open_file(a, reffile, 10240)); /* First header. */ assertA(0 == archive_read_next_header(a, &ae)); assertEqualString("random_data.bin", archive_entry_pathname(ae)); assertA((int)archive_entry_mtime(ae)); assertA((int)archive_entry_ctime(ae)); assertA((int)archive_entry_atime(ae)); assertEqualInt(file1_size, archive_entry_size(ae)); assertEqualInt(33188, archive_entry_mode(ae)); assertA(file1_size == archive_read_data(a, file1_buff, file1_size)); assertEqualMem(&file1_buff[file1_size - sizeof(file1_test_txt) + 1], file1_test_txt, sizeof(file1_test_txt) - 1); /* Second header. */ assertA(0 == archive_read_next_header(a, &ae)); assertEqualString("LibarchiveAddingTest.odt", archive_entry_pathname(ae)); assertA((int)archive_entry_mtime(ae)); assertA((int)archive_entry_ctime(ae)); assertA((int)archive_entry_atime(ae)); assertEqualInt(file2_size, archive_entry_size(ae)); assertEqualInt(33188, archive_entry_mode(ae)); assertA(file2_size == archive_read_data(a, file2_buff, file2_size)); assertEqualMem(&file2_buff[file2_size + 1 - sizeof(file2_test_txt)], file2_test_txt, sizeof(file2_test_txt) - 1); /* Test EOF */ assertA(1 == archive_read_next_header(a, &ae)); assertEqualInt(2, archive_file_count(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
/* * test_compat_cpio_1.cpio checks heuristics for avoiding false * hardlinks. foo1 and foo2 are files that have nlinks=1 and so * should not be marked as hardlinks even though they have identical * ino values. bar1 and bar2 have nlinks=2 so should be marked * as hardlinks. */ static void test_compat_cpio_1(void) { char name[] = "test_compat_cpio_1.cpio"; struct archive_entry *ae; struct archive *a; assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); extract_reference_file(name); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 17)); /* Read first entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("foo1", archive_entry_pathname(ae)); assertEqualString(NULL, archive_entry_hardlink(ae)); assertEqualInt(1260250228, archive_entry_mtime(ae)); assertEqualInt(1000, archive_entry_uid(ae)); assertEqualInt(1000, archive_entry_gid(ae)); assertEqualInt(0100644, archive_entry_mode(ae)); /* Read second entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("foo2", archive_entry_pathname(ae)); assertEqualString(NULL, archive_entry_hardlink(ae)); assertEqualInt(1260250228, archive_entry_mtime(ae)); assertEqualInt(1000, archive_entry_uid(ae)); assertEqualInt(1000, archive_entry_gid(ae)); assertEqualInt(0100644, archive_entry_mode(ae)); /* Read third entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("bar1", archive_entry_pathname(ae)); assertEqualString(NULL, archive_entry_hardlink(ae)); assertEqualInt(1260250228, archive_entry_mtime(ae)); assertEqualInt(1000, archive_entry_uid(ae)); assertEqualInt(1000, archive_entry_gid(ae)); assertEqualInt(0100644, archive_entry_mode(ae)); /* Read fourth entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("bar2", archive_entry_pathname(ae)); assertEqualString("bar1", archive_entry_hardlink(ae)); assertEqualInt(1260250228, archive_entry_mtime(ae)); assertEqualInt(1000, archive_entry_uid(ae)); assertEqualInt(1000, archive_entry_gid(ae)); assertEqualInt(0100644, archive_entry_mode(ae)); /* Verify that the format detection worked. */ assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_NONE); assertEqualInt(archive_format(a), ARCHIVE_FORMAT_CPIO_SVR4_NOCRC); assertEqualInt(ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
static void test_read_format_ustar_filename_KOI8R_CP1251(const char *refname) { struct archive *a; struct archive_entry *ae; /* * Read KOI8-R filename in CP1251 with "hdrcharset=KOI8-R" option. */ if (NULL == setlocale(LC_ALL, "Russian_Russia") && NULL == setlocale(LC_ALL, "ru_RU.CP1251")) { skipping("CP1251 locale not available on this system."); return; } assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=KOI8-R")) { skipping("This system cannot convert character-set" " from KOI8-R to CP1251."); goto cleanup; } assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240)); /* Verify regular file. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("\xef\xf0\xe8\xe2\xe5\xf2", archive_entry_pathname(ae)); assertEqualInt(6, archive_entry_size(ae)); assertEqualInt(archive_entry_is_encrypted(ae), 0); assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED); /* Verify regular file. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("\xcf\xd0\xc8\xc2\xc5\xd2", archive_entry_pathname(ae)); assertEqualInt(6, archive_entry_size(ae)); assertEqualInt(archive_entry_is_encrypted(ae), 0); assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED); /* End of archive. */ assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); /* Verify archive format. */ assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0)); assertEqualIntA(a, ARCHIVE_FORMAT_TAR_USTAR, archive_format(a)); /* Close the archive. */ assertEqualInt(ARCHIVE_OK, archive_read_close(a)); cleanup: assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
static void test_read_format_ustar_filename_CP866_UTF8(const char *refname) { struct archive *a; struct archive_entry *ae; /* * Read CP866 filename in en_US.UTF-8 with "hdrcharset=CP866" option. */ if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) { skipping("en_US.UTF-8 locale not available on this system."); return; } assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=CP866")) { skipping("This system cannot convert character-set" " from CP866 to UTF-8."); goto cleanup; } assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240)); /* Verify regular file. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("\xd0\x9f\xd0\xa0\xd0\x98\xd0\x92\xd0\x95\xd0\xa2", archive_entry_pathname(ae)); assertEqualInt(6, archive_entry_size(ae)); assertEqualInt(archive_entry_is_encrypted(ae), 0); assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED); /* Verify regular file. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82", archive_entry_pathname(ae)); assertEqualInt(6, archive_entry_size(ae)); assertEqualInt(archive_entry_is_encrypted(ae), 0); assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED); /* End of archive. */ assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); /* Verify archive format. */ assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0)); assertEqualIntA(a, ARCHIVE_FORMAT_TAR_USTAR, archive_format(a)); /* Close the archive. */ assertEqualInt(ARCHIVE_OK, archive_read_close(a)); cleanup: assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
void restraint_fetch_http (SoupURI *url, const gchar *base_path, ArchiveEntryCallback archive_entry_callback, FetchFinishCallback finish_callback, gpointer user_data) { g_return_if_fail(url != NULL); g_return_if_fail(base_path != NULL); FetchData *fetch_data = g_slice_new0(FetchData); fetch_data->archive_entry_callback = archive_entry_callback; fetch_data->finish_callback = finish_callback; fetch_data->user_data = user_data; fetch_data->url = url; fetch_data->base_path = base_path; GError *tmp_error = NULL; gint r; session = soup_session_new(); fetch_data->a = archive_read_new(); if (fetch_data->a == NULL) { g_set_error(&fetch_data->error, RESTRAINT_FETCH_LIBARCHIVE_ERROR, 0, "archive_read_new failed"); g_idle_add (archive_finish_callback, fetch_data); return; } fetch_data->ext = archive_write_disk_new(); if (fetch_data->ext == NULL) { g_set_error(&fetch_data->error, RESTRAINT_FETCH_LIBARCHIVE_ERROR, 0, "archive_write_disk_new failed"); g_idle_add (archive_finish_callback, fetch_data); return; } archive_read_support_filter_all(fetch_data->a); archive_read_support_format_all(fetch_data->a); gboolean open_succeeded = myopen(fetch_data, &tmp_error); if (!open_succeeded) { g_propagate_error(&fetch_data->error, tmp_error); g_idle_add (archive_finish_callback, fetch_data); return; } r = archive_read_open(fetch_data->a, fetch_data, NULL, myread, myclose); if (r != ARCHIVE_OK) { g_set_error(&fetch_data->error, RESTRAINT_FETCH_LIBARCHIVE_ERROR, r, "archive_read_open failed: %s", archive_error_string(fetch_data->a)); g_idle_add (archive_finish_callback, fetch_data); return; } g_idle_add (http_archive_read_callback, fetch_data); }
/* * Extract a file compressed with PPMd. */ static void test_ppmd() { const char *refname = "test_read_format_7zip_ppmd.7z"; struct archive_entry *ae; struct archive *a; size_t remaining; ssize_t bytes; char buff[1024]; extract_reference_file(refname); assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240)); /* Verify regular file1. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae)); assertEqualString("ppmd_test.txt", archive_entry_pathname(ae)); assertEqualInt(1322464589, archive_entry_mtime(ae)); assertEqualInt(102400, archive_entry_size(ae)); assertEqualInt(archive_entry_is_encrypted(ae), 0); assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0); remaining = (size_t)archive_entry_size(ae); while (remaining) { if (remaining < sizeof(buff)) assertEqualInt(remaining, bytes = archive_read_data(a, buff, sizeof(buff))); else assertEqualInt(sizeof(buff), bytes = archive_read_data(a, buff, sizeof(buff))); if (bytes > 0) remaining -= bytes; else break; } assertEqualInt(0, remaining); assertEqualInt(1, archive_file_count(a)); /* End of archive. */ assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); /* Verify archive format. */ assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0)); assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a)); /* Close the archive. */ assertEqualInt(ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
static void test_read_format_ustar_filename_eucJP_CP932(const char *refname) { struct archive *a; struct archive_entry *ae; /* * Read eucJP filename in CP932/SJIS with "hdrcharset=eucJP" option. */ if (NULL == setlocale(LC_ALL, "Japanese_Japan") && NULL == setlocale(LC_ALL, "ja_JP.SJIS")) { skipping("CP932 locale not available on this system."); return; } assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=eucJP")) { skipping("This system cannot convert character-set" " from eucJP."); goto cleanup; } assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240)); /* Verify regular file. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("\x8a\xbf\x8e\x9a.txt", archive_entry_pathname(ae)); assertEqualInt(8, archive_entry_size(ae)); assertEqualInt(archive_entry_is_encrypted(ae), 0); assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED); /* Verify regular file. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("\x95\x5c.txt", archive_entry_pathname(ae)); assertEqualInt(4, archive_entry_size(ae)); assertEqualInt(archive_entry_is_encrypted(ae), 0); assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED); /* End of archive. */ assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); /* Verify archive format. */ assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0)); assertEqualIntA(a, ARCHIVE_FORMAT_TAR_USTAR, archive_format(a)); /* Close the archive. */ assertEqualInt(ARCHIVE_OK, archive_read_close(a)); cleanup: assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }