/* * 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 *filename) { struct archive *ina; 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_compression_all(ina); if (archive_read_open_file(ina, filename, 10240)) { bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina)); bsdtar->return_value = 1; return (0); } rc = append_archive(bsdtar, a, ina, NULL); if (archive_errno(ina)) { bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s", filename, archive_error_string(ina)); bsdtar->return_value = 1; } archive_read_finish(ina); return (rc); }
struct pkg_vulnerabilities * read_pkg_vulnerabilities_file(const char *path, int ignore_missing, int check_sum) { #ifdef BOOTSTRAP errx(EXIT_FAILURE, "Audit functions are unsupported during bootstrap"); #else struct archive *a; struct pkg_vulnerabilities *pv; int fd; if ((fd = open(path, O_RDONLY)) == -1) { if (errno == ENOENT && ignore_missing) return NULL; err(EXIT_FAILURE, "Cannot open %s", path); } if ((a = archive_read_new()) == NULL) errx(EXIT_FAILURE, "memory allocation failed"); if (archive_read_support_compression_all(a) != ARCHIVE_OK || archive_read_support_format_raw(a) != ARCHIVE_OK || archive_read_open_fd(a, fd, 65536) != ARCHIVE_OK) errx(EXIT_FAILURE, "Cannot open ``%s'': %s", path, archive_error_string(a)); pv = read_pkg_vulnerabilities_archive(a, check_sum); close(fd); return pv; #endif }
int main(int argc, char *argv[]) { struct archive *archive; int fd; fprintf(stderr, "new\n"); archive = archive_read_new(); archive_read_support_compression_all(archive); archive_read_support_format_all(archive); if (argc > 1 && strcmp(argv[1], "null") == 0) { archive = NULL; archive_read_finish(archive); return 0; } if (argc > 1 && strcmp(argv[1], "open") == 0) { printf("%d\n", argc); fd = open("/etc/passwd", O_RDONLY); archive_read_open_fd(archive, fd, 4096); } fprintf(stderr, "finish 1\n"); archive_read_finish(archive); fprintf(stderr, "finish 2\n"); archive_read_finish(archive); fprintf(stderr, "done\n"); return 0; }
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_compression_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_compression(a), compression); assertEqualInt(archive_format(a), format); /* Verify the only entry. */ f(ae); assert(0 == archive_read_close(a)); #if ARCHIVE_VERSION_NUMBER < 2000000 archive_read_finish(a); #else assert(0 == archive_read_finish(a)); #endif free(buff); }
static arc_handle_t arxive_new() { arc_handle_t ar = archive_read_new(); archive_read_support_compression_all(ar); archive_read_support_format_all(ar); return ar; }
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); }
int main(int argc, const char **argv) { if (argc > 2) { fprintf(stderr, "Usage: %s [file]\n", argv[0]); exit(1); } struct archive *a = archive_read_new(); archive_read_support_compression_all(a); archive_read_support_format_raw(a); int err; if (argc == 2) err = archive_read_open_filename(a, argv[1], BS); else err = archive_read_open_fd(a, 0, BS); if (err != ARCHIVE_OK) { fprintf(stderr, "Broken archive (1)\n"); exit(1); } struct archive_entry *ae; err = archive_read_next_header(a, &ae); if (err != ARCHIVE_OK) { fprintf(stderr, "Broken archive (2)\n"); exit(1); } (void) archive_read_data_into_fd(a, 1); archive_read_finish(a); exit(0); }
/* * memory to file */ int archive_extract_file3( void *arch_buff, size_t arch_size, const char *src, char *dest ) { int flags; const char *filename; struct archive *arch_r = NULL, *arch_w = NULL; struct archive_entry *entry; if( !src || !dest ) return -1; arch_r = archive_read_new(); archive_read_support_format_all( arch_r ); archive_read_support_compression_all( arch_r ); if( archive_read_open_memory( arch_r, arch_buff, arch_size ) != ARCHIVE_OK ) goto errout; while( archive_read_next_header( arch_r, &entry ) == ARCHIVE_OK ) { filename = archive_entry_pathname( entry ); if( fnmatch( src, filename, FNM_PATHNAME | FNM_PERIOD ) ) { if( archive_read_data_skip( arch_r ) != ARCHIVE_OK ) { goto errout; } } else { #ifdef DEBUG printf("extract:%s\n", filename ); #endif flags = ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS; arch_w = archive_write_disk_new(); archive_write_disk_set_options( arch_w, flags ); archive_write_disk_set_standard_lookup( arch_w ); archive_entry_set_pathname( entry, dest ); if( archive_read_extract2( arch_r, entry, arch_w ) != ARCHIVE_OK ) goto errout; archive_write_finish( arch_w ); } } archive_read_finish( arch_r ); return 0; errout: #ifdef DEBUG fprintf( stderr, "%s\n", archive_error_string( arch_r ) ); #endif if( arch_r ) archive_read_finish( arch_r ); if( arch_w ) archive_write_finish( arch_w ); return -1; }
int extract_archive(const char* filename, const char* to_path) { struct archive* a; struct archive* ext; struct archive_entry* entry; int r; int flags = ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS; a = archive_read_new(); ext = archive_write_disk_new(); archive_write_disk_set_options(ext, flags); archive_write_disk_set_standard_lookup(ext); archive_read_support_format_all(a); archive_read_support_compression_all(a); if(filename != NULL && strcmp(filename, "-") == 0) { filename = NULL; } if((r = archive_read_open_file(a, filename, 10240))) { printf("archive_read_open_file(): %s\n", archive_error_string(a)); return r; } for(;;) { r = archive_read_next_header(a, &entry); if(r == ARCHIVE_EOF) { break; } if(r != ARCHIVE_OK) { printf("archive_read_next_header(): %s\n", archive_error_string(a)); return 0; } // rewrite pathname const char* path = archive_entry_pathname(entry); char new_path[PATH_MAX + 1]; sprintf(new_path, "%s/%s", to_path, path + (strncmp(path, "rootfs/", 7) == 0 ? 7 : 0)); archive_entry_set_pathname(entry, new_path); r = archive_write_header(ext, entry); if(r != ARCHIVE_OK) { printf("archive_write_header(): %s\n", archive_error_string(ext)); } else { copy_data(a, ext); if(r != ARCHIVE_OK) { printf("archive_write_finish_entry(): %s\n", archive_error_string(ext)); return 0; } } r = archive_write_finish_entry(ext); } archive_read_close(a); archive_read_finish(a); archive_write_close(ext); archive_write_finish(ext); return 1; }
extern void lparchive_init(lparchive_t *handle) { handle->archive = archive_read_new(); /* FIXME: add error handling */ (void)archive_read_support_compression_all(handle->archive); (void)archive_read_support_format_all(handle->archive); handle->fd = 0; }
void PictureBank::loadArchive(const std::string &filename) { std::cout << "reading image archive: " << filename << std::endl; struct archive *a; struct archive_entry *entry; int rc; a = archive_read_new(); archive_read_support_compression_all(a); archive_read_support_format_all(a); rc = archive_read_open_filename(a, filename.c_str(), 16384); // block size if (rc != ARCHIVE_OK) { THROW("Cannot open archive " << filename); } SDL_Surface *surface; SDL_RWops *rw; const Uint32 bufferSize = 10000000; // allocated buffer std::auto_ptr< Uint8 > buffer( new Uint8[ bufferSize ] ); if( buffer.get() == 0 ) THROW("Memory error, cannot allocate buffer size " << bufferSize); std::string entryname; while( archive_read_next_header(a, &entry) == ARCHIVE_OK ) { // for all entries in archive entryname = archive_entry_pathname(entry); if ((archive_entry_stat(entry)->st_mode & S_IFREG) == 0) { // not a regular file (maybe a directory). skip it. continue; } if (archive_entry_stat(entry)->st_size >= bufferSize) { THROW("Cannot load archive: file is too big " << entryname << " in archive " << filename); } int size = archive_read_data(a, buffer.get(), bufferSize); // read data into buffer rw = SDL_RWFromMem(buffer.get(), size); surface = IMG_Load_RW(rw, 0); SDL_SetAlpha( surface, 0, 0 ); setPicture(entryname, *surface); SDL_FreeRW(rw); } rc = archive_read_finish(a); if (rc != ARCHIVE_OK) { THROW("Error while reading archive " << filename); } }
/* * memory to memory */ int archive_extract_file4( void *arch_buff, size_t arch_size, const char *src, void **dest_buff, size_t *dest_len ) { const char *filename; struct archive *arch_r = NULL; struct archive_entry *entry; if( !src ) return -1; arch_r = archive_read_new(); archive_read_support_format_all( arch_r ); archive_read_support_compression_all( arch_r ); if( archive_read_open_memory( arch_r, arch_buff, arch_size ) != ARCHIVE_OK ) goto errout; while( archive_read_next_header( arch_r, &entry ) == ARCHIVE_OK ) { filename = archive_entry_pathname( entry ); if( fnmatch( src, filename, FNM_PATHNAME | FNM_PERIOD ) ) { if( archive_read_data_skip( arch_r ) != ARCHIVE_OK ) { goto errout; } } else { #ifdef DEBUG printf("extract:%s\n", filename ); #endif *dest_len = archive_entry_size( entry ); if( *dest_len > 0 ) { *dest_buff = malloc( *dest_len + 1 ); memset( *dest_buff, 0, *dest_len + 1 ); } if( archive_read_data( arch_r, *dest_buff, *dest_len) < 0 ) goto errout; } } archive_read_finish( arch_r ); return 0; errout: #ifdef DEBUG fprintf( stderr, "%s\n", archive_error_string( arch_r ) ); #endif if( arch_r ) archive_read_finish( arch_r ); return -1; }
static void test_compat_tar_hardlink_1(void) { char name[] = "test_compat_tar_hardlink_1.tar"; struct archive_entry *ae; struct archive *a; assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_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, 10240)); /* Read first entry, which is a regular file. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("xmcd-3.3.2/docs_d/READMf", archive_entry_pathname(ae)); assertEqualString(NULL, archive_entry_hardlink(ae)); assertEqualInt(321, archive_entry_size(ae)); assertEqualInt(1082575645, archive_entry_mtime(ae)); assertEqualInt(1851, archive_entry_uid(ae)); assertEqualInt(3, archive_entry_gid(ae)); assertEqualInt(0100444, archive_entry_mode(ae)); /* Read second entry, which is a hard link at the end of archive. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("xmcd-3.3.2/README", archive_entry_pathname(ae)); assertEqualString( "xmcd-3.3.2/docs_d/READMf", archive_entry_hardlink(ae)); assertEqualInt(0, archive_entry_size(ae)); assertEqualInt(1082575645, archive_entry_mtime(ae)); assertEqualInt(1851, archive_entry_uid(ae)); assertEqualInt(3, archive_entry_gid(ae)); assertEqualInt(0100444, archive_entry_mode(ae)); /* Verify the end-of-archive. */ /* * This failed in libarchive 2.4.12 because the tar reader * tried to obey the size field for the hard link and ended * up running past the end of the file. */ assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); /* Verify that the format detection worked. */ assertEqualInt(archive_compression(a), ARCHIVE_COMPRESSION_NONE); assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR); assertEqualInt(ARCHIVE_OK, archive_read_close(a)); #if ARCHIVE_VERSION_NUMBER < 2000000 archive_read_finish(a); #else assertEqualInt(ARCHIVE_OK, archive_read_finish(a)); #endif }
static inline int lparchive_reopen(lparchive_t *handle) { /* FIXME: add error handling */ (void)archive_read_finish(handle->archive); handle->archive = archive_read_new(); (void)archive_read_support_compression_all(handle->archive); (void)archive_read_support_format_all(handle->archive); return lparchive_open_fd(handle, handle->fd); }
static int extract_pkg_static(int fd, char *p, int sz) { struct archive *a; struct archive_entry *ae; char *end; int ret, r; ret = -1; a = archive_read_new(); if (a == NULL) { warn("archive_read_new"); return (ret); } archive_read_support_compression_all(a); archive_read_support_format_tar(a); if (lseek(fd, 0, 0) == -1) { warn("lseek"); goto cleanup; } if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) { warnx("archive_read_open_fd: %s", archive_error_string(a)); goto cleanup; } ae = NULL; while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) { end = strrchr(archive_entry_pathname(ae), '/'); if (end == NULL) continue; if (strcmp(end, "/pkg-static") == 0) { r = archive_read_extract(a, ae, ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR); strlcpy(p, archive_entry_pathname(ae), sz); break; } } if (r == ARCHIVE_OK) ret = 0; else warnx("fail to extract pkg-static"); cleanup: archive_read_finish(a); return (ret); }
/* * Initialize libarchive object for unpacking an archive file. * Args: * struct archive **a The archive object to use. * Returns: ARCHIVE_OK on success, ARCHIVE_* on failure */ int unpack_init(struct archive **a) { int r = ARCHIVE_OK; if ((*a = archive_read_new()) == NULL) return ARCHIVE_FATAL; if ((r = archive_read_support_compression_all(*a)) != ARCHIVE_OK) return r; if ((r = archive_read_support_format_all(*a)) != ARCHIVE_OK) return r; return r; }
bool LibArchiveInterface::list() { kDebug(); ArchiveRead arch_reader(archive_read_new()); if (!(arch_reader.data())) { return false; } if (archive_read_support_compression_all(arch_reader.data()) != ARCHIVE_OK) { return false; } if (archive_read_support_format_all(arch_reader.data()) != ARCHIVE_OK) { return false; } if (archive_read_open_filename(arch_reader.data(), QFile::encodeName(filename()), 10240) != ARCHIVE_OK) { emit error(i18nc("@info", "Could not open the archive <filename>%1</filename>, libarchive cannot handle it.", filename())); return false; } m_cachedArchiveEntryCount = 0; m_extractedFilesSize = 0; struct archive_entry *aentry; int result; while (!m_abortOperation && (result = archive_read_next_header(arch_reader.data(), &aentry)) == ARCHIVE_OK) { if (!m_emitNoEntries) { emitEntryFromArchiveEntry(aentry); } m_extractedFilesSize += (qlonglong)archive_entry_size(aentry); m_cachedArchiveEntryCount++; archive_read_data_skip(arch_reader.data()); } m_abortOperation = false; if (result != ARCHIVE_EOF) { emit error(i18nc("@info", "The archive reading failed with the following error: <message>%1</message>", QLatin1String( archive_error_string(arch_reader.data())))); return false; } return archive_read_close(arch_reader.data()) == ARCHIVE_OK; }
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); }
/** Open an archive for reading and perform the necessary boilerplate. * This takes care of creating the libarchive 'archive' struct, setting up * compression and format options, opening a file descriptor, setting up the * buffer size, and performing a stat on the path once opened. * On error, no file descriptor is opened, and the archive pointer returned * will be set to NULL. * @param handle the context handle * @param path the path of the archive to open * @param buf space for a stat buffer for the given path * @param archive pointer to place the created archive object * @param error error code to set on failure to open archive * @return -1 on failure, >=0 file descriptor on success */ int _alpm_open_archive(alpm_handle_t *handle, const char *path, struct stat *buf, struct archive **archive, alpm_errno_t error) { int fd; size_t bufsize = ALPM_BUFFER_SIZE; errno = 0; if((*archive = archive_read_new()) == NULL) { RET_ERR(handle, ALPM_ERR_LIBARCHIVE, -1); } archive_read_support_compression_all(*archive); archive_read_support_format_all(*archive); _alpm_log(handle, ALPM_LOG_DEBUG, "opening archive %s\n", path); OPEN(fd, path, O_RDONLY); if(fd < 0) { _alpm_log(handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno)); goto error; } if(fstat(fd, buf) != 0) { _alpm_log(handle, ALPM_LOG_ERROR, _("could not stat file %s: %s\n"), path, strerror(errno)); goto error; } #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE if(buf->st_blksize > ALPM_BUFFER_SIZE) { bufsize = buf->st_blksize; } #endif if(archive_read_open_fd(*archive, fd, bufsize) != ARCHIVE_OK) { _alpm_log(handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, archive_error_string(*archive)); goto error; } return fd; error: archive_read_finish(*archive); *archive = NULL; if(fd >= 0) { CLOSE(fd); } RET_ERR(handle, error, -1); }
/* Extracts the downloaded archive and removes it upon success. * Assumed to be in destination directory before calling this. * Returns -1 on fatal errors, > 0 on extraction errors, 0 on success. */ int extract_file(const char *filename) { /* Extract the archive */ struct archive *archive; struct archive_entry *entry; int ret; int errors = 0; int extract_flags = ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_TIME; archive = archive_read_new(); if (!archive) { return error(PW_ERR_ARCHIVE_CREATE); } archive_read_support_compression_all(archive); archive_read_support_format_all(archive); ret = archive_read_open_filename(archive, filename, 16384); if (ret != ARCHIVE_OK) { return error(PW_ERR_ARCHIVE_OPEN); } while (archive_read_next_header(archive, &entry) == ARCHIVE_OK) { ret = archive_read_extract(archive, entry, extract_flags); if (ret == ARCHIVE_WARN && archive_errno(archive) != ENOSPC) { pw_fprintf(PW_LOG_WARNING, stderr, "warning given when extracting %s: %s\n", archive_entry_pathname(entry), archive_error_string(archive)); } else if (ret != ARCHIVE_OK) { pw_fprintf(PW_LOG_ERROR, stderr, "Could not extract %s\n", archive_entry_pathname(entry)); ++errors; } if (config->verbose) { printf("X %s\n", archive_entry_pathname(entry)); } } archive_read_finish(archive); /* Everything successful. Remove the file */ unlink(filename); return errors; }
ArchiveWalker(const char* archiveName) : mSize(0) , mData(NULL) { mArchive = archive_read_new(); #if ARCHIVE_VERSION_NUMBER < 3000000 archive_read_support_compression_all(mArchive); #else archive_read_support_filter_all(mArchive); #endif archive_read_support_format_all(mArchive); int r = archive_read_open_filename(mArchive, archiveName, 10240); if (r != ARCHIVE_OK) { printf("FATAL: %s", archive_error_string(mArchive)); exit(1); } }
static struct archive * prepare_read_new_archive() { struct archive *new_archive = archive_read_new(); if(!new_archive) { return NULL; } if(archive_read_support_format_all(new_archive) != ARCHIVE_OK) { return NULL; } if(archive_read_support_compression_all(new_archive) != ARCHIVE_OK) { return NULL; } return new_archive; }
static void mode_list(struct cpio *cpio) { struct archive *a; struct archive_entry *entry; unsigned long blocks; int r; a = archive_read_new(); if (a == NULL) cpio_errc(1, 0, "Couldn't allocate archive object"); archive_read_support_compression_all(a); archive_read_support_format_all(a); if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block)) cpio_errc(1, archive_errno(a), archive_error_string(a)); for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_EOF) break; if (r != ARCHIVE_OK) { cpio_errc(1, archive_errno(a), archive_error_string(a)); } if (excluded(cpio, archive_entry_pathname(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) cpio_errc(1, 0, archive_error_string(a)); if (!cpio->quiet) { blocks = (archive_position_uncompressed(a) + 511) / 512; fprintf(stderr, "%lu %s\n", blocks, blocks == 1 ? "block" : "blocks"); } archive_read_finish(a); exit(0); }
/* Copy this function for each test file and adjust it accordingly. */ static void test_compat_zip_1(void) { char name[] = "test_compat_zip_1.zip"; struct archive_entry *ae; struct archive *a; int r; assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_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("META-INF/MANIFEST.MF", archive_entry_pathname(ae)); /* Read second entry. */ r = archive_read_next_header(a, &ae); if (r != ARCHIVE_OK) { if (strcmp(archive_error_string(a), "libarchive compiled without deflate support (no libz)") == 0) { skipping("Skipping ZIP compression check: %s", archive_error_string(a)); goto finish; } } assertEqualIntA(a, ARCHIVE_OK, r); assertEqualString("tmp.class", archive_entry_pathname(ae)); assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); assertEqualInt(archive_compression(a), ARCHIVE_COMPRESSION_NONE); assertEqualInt(archive_format(a), ARCHIVE_FORMAT_ZIP); assertEqualInt(ARCHIVE_OK, archive_read_close(a)); finish: #if ARCHIVE_VERSION_NUMBER < 2000000 archive_read_finish(a); #else assertEqualInt(ARCHIVE_OK, archive_read_finish(a)); #endif }
size_t LIBARCHIVEgetEntry(char *name, char *contentFile, char **ptr) { struct mydata *mydata; struct archive *a; struct archive_entry *entry; char *buf; size_t size = 0; mydata = (struct mydata*)malloc(sizeof(struct mydata)); a = archive_read_new(); mydata->name = name; archive_read_support_format_all(a); archive_read_support_compression_all(a); if (archive_read_open(a, mydata, myopen, myread, myclose) == ARCHIVE_FATAL) { fprintf(stderr, "failed to open %s\n", mydata->name); free(mydata->name); free(mydata); return 0; } while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { if( 0 == strcmp(archive_entry_pathname(entry), contentFile)) { o_log(DEBUGM, "%s", (char *)archive_compression_name(a)); o_log(DEBUGM, "%s", (char *)archive_format_name(a)); o_log(DEBUGM, "%s", (char *)archive_entry_pathname(entry)); size = archive_entry_size(entry); if(size <= 0) o_log(DEBUGM, "zero size"); if ((buf = (char *)malloc(size+1)) == NULL) o_log(ERROR, "cannot allocate memory"); if ((size_t)archive_read_data(a, buf, size) != size) o_log(DEBUGM, "cannot read data"); buf[size] = '\0'; *ptr = buf; } else archive_read_data_skip(a); } archive_read_close(a); archive_read_finish(a); free(mydata); return size; }
struct archive * pkg_archive_open(const char *path){ struct archive *a; if(path == NULL) RETURN_P_ERR(P_ERR_INVALID_DESCRIPTOR, NULL); a = archive_read_new(); if(a == NULL) return NULL; archive_read_support_compression_all(a); archive_read_support_format_tar(a); if(archive_read_open_file(a, path, 10240) != 0){ pkg_error(0, (char *)archive_error_string(a)); return NULL; } return a; }
static void verifyEmpty(void) { struct archive_entry *ae; struct archive *a; assert((a = archive_read_new()) != NULL); assertA(0 == archive_read_support_compression_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_compression(a), ARCHIVE_COMPRESSION_NONE); failure("512 zero bytes should be recognized as a tar archive."); assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR); assert(0 == archive_read_close(a)); #if ARCHIVE_API_VERSION > 1 assert(0 == archive_read_finish(a)); #else archive_read_finish(a); #endif }
/* * All of the sample files have the same contents; they're just * compressed in different ways. */ static void compat_xz(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); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a)); r = archive_read_support_compression_xz(a); if (r == ARCHIVE_WARN) { skipping("xz reading not fully supported on this platform"); assertEqualInt(ARCHIVE_OK, archive_read_finish(a)); return; } assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); extract_reference_file(name); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 2)); /* 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)); 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_XZ); assertEqualString(archive_compression_name(a), "xz"); assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR); assertEqualInt(ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_finish(a)); }
static void test2(void) { struct archive_entry *ae; struct archive *a; const char *name = "test_read_format_iso_2.iso.Z"; extract_reference_file(name); assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 512)); assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString(".", archive_entry_pathname(ae)); assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("A", archive_entry_pathname(ae)); assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("A/B", archive_entry_pathname(ae)); assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("C", archive_entry_pathname(ae)); assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("C/D", archive_entry_pathname(ae)); assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); assertEqualInt(archive_compression(a), ARCHIVE_COMPRESSION_COMPRESS); assertEqualInt(archive_format(a), ARCHIVE_FORMAT_ISO9660); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_finish(a)); }
struct pkg_vulnerabilities * read_pkg_vulnerabilities_memory(void *buf, size_t len, int check_sum) { #ifdef BOOTSTRAP errx(EXIT_FAILURE, "Audit functions are unsupported during bootstrap"); #else struct archive *a; struct pkg_vulnerabilities *pv; if ((a = archive_read_new()) == NULL) errx(EXIT_FAILURE, "memory allocation failed"); if (archive_read_support_compression_all(a) != ARCHIVE_OK || archive_read_support_format_raw(a) != ARCHIVE_OK || archive_read_open_memory(a, buf, len) != ARCHIVE_OK) errx(EXIT_FAILURE, "Cannot open pkg_vulnerabilies buffer: %s", archive_error_string(a)); pv = read_pkg_vulnerabilities_archive(a, check_sum); return pv; #endif }