static void mode_list(struct cpio *cpio) { struct archive *a; struct archive_entry *entry; int r; a = archive_read_new(); if (a == NULL) lafe_errc(1, 0, "Couldn't allocate archive object"); archive_read_support_filter_all(a); archive_read_support_format_all(a); if (cpio->passphrase != NULL) r = archive_read_add_passphrase(a, cpio->passphrase); else r = archive_read_set_passphrase_callback(a, cpio, &passphrase_callback); if (r != ARCHIVE_OK) lafe_errc(1, 0, "%s", archive_error_string(a)); if (archive_read_open_filename(a, cpio->filename, cpio->bytes_per_block)) lafe_errc(1, archive_errno(a), "%s", archive_error_string(a)); for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_EOF) break; if (r != ARCHIVE_OK) { lafe_errc(1, archive_errno(a), "%s", archive_error_string(a)); } if (archive_match_path_excluded(cpio->matching, entry)) continue; if (cpio->verbose) list_item_verbose(cpio, entry); else fprintf(stdout, "%s\n", archive_entry_pathname(entry)); } r = archive_read_close(a); if (r != ARCHIVE_OK) lafe_errc(1, 0, "%s", archive_error_string(a)); if (!cpio->quiet) { int64_t blocks = (archive_filter_bytes(a, 0) + 511) / 512; fprintf(stderr, "%lu %s\n", (unsigned long)blocks, blocks == 1 ? "block" : "blocks"); } archive_read_free(a); exit(0); }
/* * 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)); 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)); }
void packing_finish(struct packing *pack) { if (pack == NULL) return; archive_read_close(pack->aread); archive_read_free(pack->aread); archive_write_close(pack->awrite); archive_write_free(pack->awrite); free(pack); }
archive::archive(const std::string& fileName) : archiveLibarchive(fileName) { applyFormats(); int ret = archive_read_open_filename(m_archive, fileName.c_str(), 4096); if (ret != ARCHIVE_OK) { archive_read_free(m_archive); m_archive = nullptr; throw std::runtime_error("libstriezel::ar::archive: Failed to open file " + fileName + "!"); } //fill entries fillEntries(); }
/* * Extract a file compressed with BCJ + LZMA2. */ static void test_bcj(const char *refname) { 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 x86exe. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualInt((AE_IFREG | 0555), archive_entry_mode(ae)); assertEqualString("x86exe", archive_entry_pathname(ae)); assertEqualInt(172802, archive_entry_mtime(ae)); assertEqualInt(27328, archive_entry_size(ae)); 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_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)); }
void print_size(char* filename) { archive *a = archive_read_new(); archive_read_support_filter_bzip2(a); archive_read_support_format_7zip(a); archive_read_open_filename(a, filename, 10240); archive_entry *entry; archive_read_next_header(a, &entry); std::cout << archive_entry_size(entry); archive_read_free(a); }
static void test(int formatted, archive_open_callback *o, archive_read_callback *r, archive_skip_callback *s, archive_close_callback *c, int rv, const char *msg) { struct archive* a = archive_read_new(); if (formatted) assertEqualInt(ARCHIVE_OK, archive_read_support_format_empty(a)); assertEqualInt(rv, archive_read_open2(a, NULL, o, r, s, c)); assertEqualString(msg, archive_error_string(a)); archive_read_free(a); }
/* 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; }
void list_archive_get(const char* const infile, char** filenames, int* nfiles, const int buffsize) { //printf("In list_archive_get\n"); //fflush(stdout); struct archive* a = archive_read_new(); archive_read_support_format_zip(a); int err = archive_read_open_filename(a, infile,10240);//Note: Blocksize isn't neccessarilly adhered to if (err != ARCHIVE_OK) { fprintf(stderr, "CRITICAL ERROR in list_archive_get(): When opening archive '%s', err=%i\n",infile,err); fprintf(stderr, "CRITICAL ERROR in list_archive_get(): %s\n",archive_error_string(a)); exit(EXIT_FAILURE); } const int nfiles_max = *nfiles; *nfiles = 0; struct archive_entry* entry; while (archive_read_next_header(a,&entry)==ARCHIVE_OK){ //printf("Found file: '%s'\n",archive_entry_pathname(entry)); //fflush(stdout); int buff_used = 0; buff_used = snprintf(filenames[*nfiles],buffsize,"%s",archive_entry_pathname(entry)); if (buff_used >= buffsize) { fprintf(stderr, "CRITICAL ERROR in list_archive_get(): When reading file '%s' from archive '%s':\n",filenames[*nfiles],infile); fprintf(stderr, "CRITICAL ERROR in list_archive_get(): Buffer too small by %i characters\n",buff_used-buffsize+1); exit(EXIT_FAILURE); } else if (buff_used < 0) { fprintf(stderr, "CRITICAL ERROR in list_archive_get(): When reading file '%s' from archive '%s':\n",filenames[*nfiles],infile); fprintf(stderr, "CRITICAL ERROR in list_archive_get(): Error in snprintf.\n"); exit(EXIT_FAILURE); } archive_read_data_skip(a); if(++(*nfiles) >= nfiles_max) { fprintf(stderr, "CRITICAL ERROR in list_archive_get(): Number of files greater than nfiles_max=%i",nfiles_max); exit(EXIT_FAILURE); } } archive_read_close(a); err = archive_read_free(a); if (err != ARCHIVE_OK){ fprintf(stderr, "CRITICAL ERROR in list_archive_get(): Error when calling archive_read_free(), '%s', err=%i\n",infile,err); fprintf(stderr, "CRITICAL ERROR in list_archive_get(): %s\n",archive_error_string(a)); exit(EXIT_FAILURE); } }
static void extract(const char *filename) { struct archive *a; struct archive *ext; struct archive_entry *entry; int flags; int r; flags = ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM; a = archive_read_new(); archive_read_support_format_all(a); #if ARCHIVE_VERSION_NUMBER < 3000000 archive_read_support_compression_all(a); #else archive_read_support_filter_all(a); #endif ext = archive_write_disk_new(); archive_write_disk_set_options(ext, flags); archive_write_disk_set_standard_lookup(ext); #if ARCHIVE_VERSION_NUMBER < 3000000 if ((r = archive_read_open_file(a, filename, 10240))) die("archive_read_open_file"); #else if ((r = archive_read_open_filename(a, filename, 10240))) die("archive_read_open_filename"); #endif for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_EOF) break; if (r < ARCHIVE_WARN) die(archive_error_string(a)); r = archive_write_header(ext, entry); if (r == ARCHIVE_OK && archive_entry_size(entry) > 0) { r = copy_data(a, ext); if (r < ARCHIVE_WARN) die(archive_error_string(a)); } r = archive_write_finish_entry(ext); if (r < ARCHIVE_WARN) die(archive_error_string(ext)); } archive_read_close(a); archive_read_free(a); archive_write_close(ext); archive_write_free(ext); }
static int archive_clean( private_sys_t* p_sys ) { libarchive_t* p_arc = p_sys->p_archive; if( p_sys->p_entry ) archive_entry_free( p_sys->p_entry ); if( p_arc ) archive_read_free( p_arc ); p_sys->p_entry = NULL; p_sys->p_archive = NULL; return VLC_SUCCESS; }
bool verify(const std::string& pathname) { if (!core::path::exists(pathname)) { throw std::runtime_error(pathname + " does not exist"); } archive* zipArchive = archive_read_new(); archive_read_support_filter_all(zipArchive); archive_read_support_format_all(zipArchive); const int results = archive_read_open_filename( zipArchive, pathname.c_str(), 10240); archive_read_free(zipArchive); return results == ARCHIVE_OK; }
int packing_finish(struct packing *pack) { assert(pack != NULL); archive_read_close(pack->aread); archive_read_free(pack->aread); archive_write_close(pack->awrite); archive_write_free(pack->awrite); free(pack); return (EPKG_OK); }
/* * Extract a non-encoded file. * The header of the 7z archive files is not encoded. */ static void test_copy(int use_open_fd) { const char *refname = "test_read_format_7zip_copy.7z"; struct archive_entry *ae; struct archive *a; char buff[128]; int fd = -1; 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)); if (use_open_fd) { fd = open(refname, O_RDONLY | O_BINARY); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_fd(a, fd, 10240)); } else { 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("file1", archive_entry_pathname(ae)); assertEqualInt(86401, archive_entry_mtime(ae)); assertEqualInt(60, archive_entry_size(ae)); assertEqualInt(archive_entry_is_encrypted(ae), 0); assert(archive_read_has_encrypted_entries(a) > ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED); assertEqualInt(60, archive_read_data(a, buff, sizeof(buff))); assertEqualMem(buff, " ", 4); 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)); if (fd != -1) close(fd); }
static int archive_conv_open(struct archive_conv *conv, const struct repo_t *repo) { int r; /* generally, repo files are gzip compressed, but there's no guarantee of * this. in order to be compression-agnostic, use libarchive's reader/writer * methods. this also gives us an opportunity to rewrite the archive as CPIO, * which is marginally faster given our staunch sequential access. */ conv->reponame = repo->name; stpcpy(stpcpy(conv->tmpfile, repo->diskfile), "~"); conv->in = archive_read_new(); conv->out = archive_write_new(); if (conv->in == NULL || conv->out == NULL) { fputs("error: failed to allocate memory for archive objects\n", stderr); return -ENOMEM; } archive_read_support_format_tar(conv->in); archive_read_support_filter_all(conv->in); r = archive_read_open_fd(conv->in, repo->tmpfile.fd, BUFSIZ); if (r != ARCHIVE_OK) { fprintf(stderr, "error: failed to create archive reader for %s: %s\n", repo->name, strerror(archive_errno(conv->in))); r = archive_errno(conv->in); goto open_error; } archive_write_set_format_cpio_newc(conv->out); archive_write_add_filter(conv->out, repo->config->compress); r = archive_write_open_filename(conv->out, conv->tmpfile); if (r != ARCHIVE_OK) { fprintf(stderr, "error: failed to open file for writing: %s: %s\n", conv->tmpfile, strerror(archive_errno(conv->out))); r = archive_errno(conv->out); goto open_error; } return 0; open_error: archive_write_free(conv->out); archive_read_free(conv->in); return -r; }
static void test_symname() { const char *refname = "test_read_format_7zip_symbolic_name.7z"; struct archive_entry *ae; struct archive *a; char buff[128]; 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 | 0644), archive_entry_mode(ae)); assertEqualString("file1", archive_entry_pathname(ae)); assertEqualInt(86401, archive_entry_mtime(ae)); assertEqualInt(32, archive_entry_size(ae)); assertEqualInt(archive_entry_is_encrypted(ae), 0); assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0); assertEqualInt(32, archive_read_data(a, buff, sizeof(buff))); assertEqualMem(buff, "hellohellohello\nhellohellohello\n", 32); /* Verify symbolic-linke symlinkfile. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae)); assertEqualString("symlinkfile", archive_entry_pathname(ae)); assertEqualString("file1", archive_entry_symlink(ae)); assertEqualInt(86401, archive_entry_mtime(ae)); assertEqualInt(archive_entry_is_encrypted(ae), 0); assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0); assertEqualInt(2, 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)); }
/* * This test only for Windows platform because other archiver * applications on Windows translate CP1251 filenames into CP866 * filenames and store it in the ustar file and so we should read * it by default on Windows. */ static void test_read_format_ustar_filename_CP866_CP1251_win(const char *refname) { struct archive *a; struct archive_entry *ae; /* * Read CP866 filename in CP1251 without "hdrcharset=CP866" option. */ if (NULL == setlocale(LC_ALL, "Russian_Russia")) { skipping("Russian_Russia 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)); 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("\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); /* 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); /* 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)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
static void test_empty_file1(void) { struct archive* a = archive_read_new(); /* Try opening an empty file with the raw handler. */ assertEqualInt(ARCHIVE_OK, archive_read_support_format_raw(a)); assertEqualInt(0, archive_errno(a)); assertEqualString(NULL, archive_error_string(a)); /* Raw handler doesn't support empty files. */ assertEqualInt(ARCHIVE_FATAL, archive_read_open_filename(a, "emptyfile", 0)); assert(NULL != archive_error_string(a)); archive_read_free(a); }
void archive_read() { struct archive *a; a = archive_read_new(); archive_read_support_format_all(a); archive_read_support_filter_all(a); archive_read_open_memory(a, buffer_, read_size_); struct archive_entry *ae; if (archive_read_next_header(a, &ae) == ARCHIVE_OK) { int size = archive_entry_size(ae); archive_buffer_ = new char[size]; archive_buffer_size_ = size; archive_read_data(a, archive_buffer_, size); } archive_read_free(a); }
int epic_fclose(struct epic_loadfile *elf) { if ((elf->fp)!=NULL) { return fclose(elf->fp); } #ifdef HAVE_LIBARCHIVE else if ((elf->a)!=NULL) { archive_read_close(elf->a); archive_read_free(elf->a); return 0; } #endif else { return EOF; } }
static void test_large_splitted_file(void) { static const char *reffiles[] = { "test_read_large_splitted_rar_aa", "test_read_large_splitted_rar_ab", "test_read_large_splitted_rar_ac", "test_read_large_splitted_rar_ad", "test_read_large_splitted_rar_ae", NULL }; const char test_txt[] = "gin-bottom: 0in\"><BR>\n</P>\n</BODY>\n</HTML>"; int size = 241647978, offset = 0; char buff[64]; struct archive_entry *ae; struct archive *a; extract_reference_files(reffiles); 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_filenames(a, reffiles, 10240)); /* First header. */ assertA(0 == archive_read_next_header(a, &ae)); assertEqualString("ppmd_lzss_conversion_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(size, archive_entry_size(ae)); assertEqualInt(33188, archive_entry_mode(ae)); while (offset + (int)sizeof(buff) < size) { assertA(sizeof(buff) == archive_read_data(a, buff, sizeof(buff))); offset += sizeof(buff); } assertA(size - offset == archive_read_data(a, buff, size - offset)); assertEqualMem(buff, test_txt, size - offset); /* 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)); }
int do_extract_mtree(char *mtree, const char *prefix) { struct archive *a = NULL; struct archive_entry *ae; char path[MAXPATHLEN]; const char *fpath; int retcode = EPKG_OK; int ret; if (mtree == NULL || *mtree == '\0') return EPKG_OK; a = archive_read_new(); archive_read_support_filter_none(a); archive_read_support_format_mtree(a); if (archive_read_open_memory(a, mtree, strlen(mtree)) != ARCHIVE_OK) { pkg_emit_error("Fail to extract the mtree: %s", archive_error_string(a)); retcode = EPKG_FATAL; goto cleanup; } while ((ret = archive_read_next_header(a, &ae)) != ARCHIVE_EOF) { if (ret != ARCHIVE_OK) { pkg_emit_error("Skipping unsupported mtree line: %s", archive_error_string(a)); continue; } fpath = archive_entry_pathname(ae); if (*fpath != '/') { snprintf(path, sizeof(path), "%s/%s", prefix, fpath); archive_entry_set_pathname(ae, path); } /* Ignored failed extraction on purpose */ archive_read_extract(a, ae, EXTRACT_ARCHIVE_FLAGS); } cleanup: if (a != NULL) archive_read_free(a); return (retcode); }
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); }
static int release_archive(atom_t symbol) { archive_wrapper *ar = PL_blob_data(symbol, NULL, NULL); struct archive *a; assert(ar->status != AR_OPENED_ENTRY); if ( (a=ar->archive) ) { ar->archive = NULL; archive_read_free(a); } free_archive(ar); PL_free(ar); return TRUE; }
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; }
/* * Issue 185: Test a regression that got in between 2.6 and 2.7 that * broke extraction of Zip entries with length-at-end. */ static void test_compat_zip_3(void) { const char *refname = "test_compat_zip_3.zip"; 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)); /* First entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("soapui-4.0.0/", archive_entry_pathname(ae)); assertEqualInt(0, archive_entry_size(ae)); assert(archive_entry_size_is_set(ae)); assertEqualInt(AE_IFDIR, archive_entry_filetype(ae)); /* Second entry. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("soapui-4.0.0/soapui-settings.xml", archive_entry_pathname(ae)); assertEqualInt(AE_IFREG, archive_entry_filetype(ae)); assertEqualInt(1030, archive_entry_size(ae)); assert(archive_entry_size_is_set(ae)); /* Extract under a different name. */ archive_entry_set_pathname(ae, "test_3.txt"); if(libz_enabled) { char *p; size_t s; assertEqualIntA(a, ARCHIVE_OK, archive_read_extract(a, ae, 0)); /* Verify the first 12 bytes actually got written to disk correctly. */ p = slurpfile(&s, "test_3.txt"); assertEqualInt(s, 1030); assertEqualMem(p, "<?xml versio", 12); free(p); } else { skipping("Skipping ZIP compression check, no libz support"); } 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)); }
static void verifyEmpty(void) { struct archive_entry *ae; struct archive *a; 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, archiveEmpty, 512)); assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_NONE); assertEqualString(archive_filter_name(a, 0), "none"); failure("512 zero bytes should be recognized as a tar archive."); assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
bool OdinPatcher::Impl::closeInputArchive() { assert(aInput != nullptr); bool ret = true; if (archive_read_close(aInput) != ARCHIVE_OK) { LOGW("libarchive: Failed to close archive: %s", archive_error_string(aInput)); // Don't clobber previous error //error = ErrorCode::ArchiveCloseError; ret = false; } archive_read_free(aInput); aInput = nullptr; return ret; }
static void test(int pristine) { struct archive* a = archive_read_new(); if (!pristine) { archive_read_support_filter_all(a); archive_read_support_format_all(a); } assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1")); /* An empty passphrase cannot be accepted. */ assertEqualInt(ARCHIVE_FAILED, archive_read_add_passphrase(a, "")); /* NULL passphrases cannot be accepted. */ assertEqualInt(ARCHIVE_FAILED, archive_read_add_passphrase(a, NULL)); archive_read_free(a); }
static void test(int pristine) { struct archive* a = archive_read_new(); if (!pristine) archive_read_support_filter_all(a); should(a, ARCHIVE_OK, NULL, NULL, NULL); should(a, ARCHIVE_OK, "", "", ""); should(a, ARCHIVE_FAILED, NULL, "fubar", NULL); should(a, ARCHIVE_FAILED, NULL, "fubar", "snafu"); should(a, ARCHIVE_FAILED, "fubar", "snafu", NULL); should(a, ARCHIVE_FAILED, "fubar", "snafu", "betcha"); archive_read_free(a); }