static VALUE rb_libarchive_writer_write_header(VALUE self, VALUE v_entry) { struct rb_libarchive_archive_container *pa; struct rb_libarchive_entry_container *pae; Check_Class(v_entry, rb_cArchiveEntry); Data_Get_Struct(self, struct rb_libarchive_archive_container, pa); Check_Archive(pa); Data_Get_Struct(v_entry, struct rb_libarchive_entry_container, pae); Check_Entry(pae); if (archive_write_header(pa->ar, pae->ae) != ARCHIVE_OK) { rb_raise(rb_eArchiveError, "Write header failed: %s", archive_error_string(pa->ar)); } return Qnil; }
int libarchive_copy_data(archive *in, archive *out, archive_entry *entry) { const void *buff; size_t size; int64_t offset; int ret; while ((ret = archive_read_data_block( in, &buff, &size, &offset)) == ARCHIVE_OK) { if (archive_write_data_block(out, buff, size, offset) != ARCHIVE_OK) { LOGE("%s: Failed to write data: %s", archive_entry_pathname(entry), archive_error_string(out)); return ARCHIVE_FAILED; } } if (ret != ARCHIVE_EOF) { LOGE("%s: Data copy ended without reaching EOF: %s", archive_entry_pathname(entry), archive_error_string(in)); return ARCHIVE_FAILED; } return ARCHIVE_OK; }
/* ArchiveReader::readCurrentEntryData(count) {{{ * */ ZEND_METHOD(ArchiveReader, readCurrentEntryData){ zval *this = getThis(); archive_file_t *arch; char *buf; const char *error_string; size_t len; int r, error_num; long count; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, ce_ArchiveException, &error_handling TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &count) == FAILURE) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } if (!_archive_get_fd(this, &arch TSRMLS_CC)) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } if (arch->current_entry == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Current archive entry is not available"); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } Z_STRVAL_P(return_value) = emalloc(count+1); len = 0; while(count > 0){ r = archive_read_data(arch->arch, Z_STRVAL_P(return_value)+len, count); if(r < ARCHIVE_OK){ error_num = archive_errno(arch->arch); error_string = archive_error_string(arch->arch); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Decompress entry failed errno(%d):%s", error_num, error_string); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } if(r == 0){ break; } count -= r; len += r; } Z_STRVAL_P(return_value)[len] = 0; Z_STRLEN_P(return_value) = len; Z_TYPE_P(return_value) = IS_STRING; zend_restore_error_handling(&error_handling TSRMLS_CC); }
/* 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; }
int libarchive_copy_header_and_data(archive *in, archive *out, archive_entry *entry) { int ret = ARCHIVE_OK; if ((ret = archive_write_header(out, entry)) != ARCHIVE_OK) { LOGE("Failed to write header: %s", archive_error_string(out)); return ret; } if ((ret = libarchive_copy_data(in, out, entry)) != ARCHIVE_OK) { return ret; } return ret; }
static bool write_file(archive *in, archive *out, archive_entry *entry) { int ret; ret = archive_write_header(out, entry); if (ret != ARCHIVE_OK) { LOGE("%s: %s", archive_entry_pathname(entry), archive_error_string(out)); return false; } if (archive_entry_size(entry) > 0) { return util::libarchive_copy_data_disk_to_archive(in, out, entry); } return true; }
Writer *Writer::write_open_filename(const char *filename, int compression, int format) { std::string error_msg; struct archive *ar = archive_write_new(); try { switch(compression) { case Archive::COMPRESSION_BZIP2: archive_write_add_filter_bzip2(ar); break; case Archive::COMPRESSION_COMPRESS: archive_write_add_filter_compress(ar); break; case Archive::COMPRESSION_GZIP: archive_write_add_filter_gzip(ar); break; case Archive::COMPRESSION_LZMA: archive_write_add_filter_lzma(ar); break; case Archive::COMPRESSION_NONE: archive_write_add_filter_none(ar); break; case Archive::COMPRESSION_XZ: archive_write_add_filter_xz(ar); break; default: error_msg = "unknown or unsupported compression scheme"; throw Error(error_msg); } set_format_helper(ar, format); if(archive_write_open_filename(ar, filename) != ARCHIVE_OK) { error_msg = archive_error_string(ar); throw Error(error_msg); } } catch(...) { archive_write_free(ar); throw; } return new Writer(ar); }
void Writer::write_header(Entry *entry) { if(entry->nlink() > 1) { std::string path = _hardlinks[entry->dev()][entry->ino()]; if(path.empty()) { _hardlinks[entry->dev()][entry->ino()] = std::string(entry->pathname()); } else { entry->set_hardlink(path.c_str()); } } if(archive_write_header(_ar, entry->_entry) != ARCHIVE_OK) { std::string error_msg = archive_error_string(_ar); throw Error(error_msg); } }
static bool set_up_input(archive *in, const std::string &filename) { // Add more as needed //archive_read_support_format_all(in); //archive_read_support_filter_all(in); //archive_read_support_format_tar(in); archive_read_support_format_zip(in); //archive_read_support_filter_xz(in); if (archive_read_open_filename(in, filename.c_str(), 10240) != ARCHIVE_OK) { LOGE("%s: Failed to open archive: %s", filename.c_str(), archive_error_string(in)); return false; } return true; }
static int archive_entry_fill_buffer(stream_t *s, char *buffer, int max_len) { struct priv *p = s->priv; if (!p->mpa) return 0; locale_t oldlocale = uselocale(p->mpa->locale); int r = archive_read_data(p->mpa->arch, buffer, max_len); if (r < 0) { MP_ERR(s, "%s\n", archive_error_string(p->mpa->arch)); if (mp_archive_check_fatal(p->mpa, r)) { mp_archive_free(p->mpa); p->mpa = NULL; } } uselocale(oldlocale); return r; }
static int Seek( stream_extractor_t* p_extractor, uint64_t i_req ) { private_sys_t* p_sys = p_extractor->p_sys; if( !p_sys->p_entry || !p_sys->b_seekable_source ) return VLC_EGENERIC; if( archive_entry_size_is_set( p_sys->p_entry ) && (uint64_t)archive_entry_size( p_sys->p_entry ) <= i_req ) { p_sys->b_eof = true; return VLC_SUCCESS; } p_sys->b_eof = false; if( !p_sys->b_seekable_archive || p_sys->b_dead || archive_seek_data( p_sys->p_archive, i_req, SEEK_SET ) < 0 ) { msg_Dbg( p_extractor, "intrinsic seek failed: '%s' (falling back to dumb seek)", archive_error_string( p_sys->p_archive ) ); uint64_t i_skip = i_req - p_sys->i_offset; /* RECREATE LIBARCHIVE HANDLE IF WE ARE SEEKING BACKWARDS */ if( i_req < p_sys->i_offset ) { if( archive_extractor_reset( p_extractor ) ) { msg_Err( p_extractor, "unable to reset libarchive handle" ); return VLC_EGENERIC; } i_skip = i_req; } if( archive_skip_decompressed( p_extractor, i_skip ) ) msg_Dbg( p_extractor, "failed to skip to seek position" ); } p_sys->i_offset = i_req; return VLC_SUCCESS; }
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); } }
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 xbps_dictionary_t repo_get_dict(struct xbps_repo *repo) { struct archive_entry *entry; int rv; if (repo->ar == NULL) return NULL; rv = archive_read_next_header(repo->ar, &entry); if (rv != ARCHIVE_OK) { xbps_dbg_printf(repo->xhp, "%s: read_next_header %s\n", repo->uri, archive_error_string(repo->ar)); return NULL; } return xbps_archive_get_dictionary(repo->ar, entry); }
static int ar_write_data(lua_State *L) { struct archive* self; const char* data; size_t len; size_t wrote; self = *ar_write_check(L, 1); if ( NULL == self ) err("NULL archive{write}!"); data = lua_tolstring(L, 2, &len); wrote = archive_write_data(self, data, len); if ( -1 == wrote ) { err("archive_write_data: %s", archive_error_string(self)); } return 0; }
static QByteArray read_data( archive* a ){ QByteArray raw; //Read chuncks const char *buff; size_t size; int64_t offset; while( true ){ switch( archive_read_data_block( a, (const void**)&buff, &size, &offset ) ){ case ARCHIVE_OK: raw += QByteArray( buff, size ); break; case ARCHIVE_EOF: return raw; default: qCWarning(LOG) << "Error while reading zip data:" << archive_error_string(a); return raw; } } }
// Iterate entries. The first call establishes the first entry. Returns false // if no entry found, otherwise returns true and sets mpa->entry/entry_filename. bool mp_archive_next_entry(struct mp_archive *mpa) { mpa->entry = NULL; talloc_free(mpa->entry_filename); mpa->entry_filename = NULL; if (!mpa->arch) return false; locale_t oldlocale = uselocale(mpa->locale); bool success = false; while (!mp_cancel_test(mpa->primary_src->cancel)) { struct archive_entry *entry; int r = archive_read_next_header(mpa->arch, &entry); if (r == ARCHIVE_EOF) break; if (r < ARCHIVE_OK) MP_ERR(mpa, "%s\n", archive_error_string(mpa->arch)); if (r < ARCHIVE_WARN) { MP_FATAL(mpa, "could not read archive entry\n"); mp_archive_check_fatal(mpa, r); break; } if (archive_entry_filetype(entry) != AE_IFREG) continue; // Some archives may have no filenames, or libarchive won't return some. const char *fn = archive_entry_pathname(entry); char buf[64]; if (!fn || bstr_validate_utf8(bstr0(fn)) < 0) { snprintf(buf, sizeof(buf), "mpv_unknown#%d", mpa->entry_num); fn = buf; } mpa->entry = entry; mpa->entry_filename = talloc_strdup(mpa, fn); mpa->entry_num += 1; success = true; break; } uselocale(oldlocale); return success; }
static struct archive *file_type_archive_gen_archive(GBytes *data) {/*{{{*/ struct archive *archive = archive_read_new(); archive_read_support_format_zip(archive); archive_read_support_format_rar(archive); archive_read_support_format_7zip(archive); archive_read_support_format_tar(archive); archive_read_support_filter_all(archive); gsize data_size; char *data_ptr = (char *)g_bytes_get_data(data, &data_size); if(archive_read_open_memory(archive, data_ptr, data_size) != ARCHIVE_OK) { g_printerr("Failed to load archive: %s\n", archive_error_string(archive)); archive_read_free(archive); return NULL; } return archive; }/*}}}*/
static int ar_read_data(lua_State *L) { struct archive* self = *ar_read_check(L, 1); const void* buff; size_t buff_len; off_t offset; int result; if ( NULL == self ) err("NULL archive{read}!"); result = archive_read_data_block(self, &buff, &buff_len, &offset); if ( ARCHIVE_EOF == result ) { return 0; } else if ( ARCHIVE_OK != result ) { err("archive_read_data_block: %s", archive_error_string(self)); } lua_pushlstring(L, buff, buff_len); lua_pushnumber(L, offset); return 2; }
static int ar_read_next_header(lua_State *L) { struct archive_entry* entry; struct archive* self = *ar_read_check(L, 1); // {ud} int result; if ( NULL == self ) err("NULL archive{read}!"); lua_pushcfunction(L, ar_entry); // {ud}, ar_entry lua_call(L, 0, 1); // {ud}, header entry = *ar_entry_check(L, -1); result = archive_read_next_header2(self, entry); if ( ARCHIVE_EOF == result ) { lua_pop(L, 1); // {ud} lua_pushnil(L); // {ud}, nil } else if ( ARCHIVE_OK != result ) { err("archive_read_next_header2: %s", archive_error_string(self)); } return 1; }
/* Helper function to copy file to archive. */ static int write_file_data(struct bsdtar *bsdtar, struct archive *a, struct archive_entry *entry, int fd) { ssize_t bytes_read; ssize_t bytes_written; off_t progress = 0; bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN); while (bytes_read > 0) { disk_pause(bsdtar); if (network_select(0)) return (-1); siginfo_printinfo(bsdtar, progress); bytes_written = archive_write_data(a, bsdtar->buff, bytes_read); if (bytes_written < 0) { /* Write failed; this is bad */ bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a)); return (-1); } if (bytes_written < bytes_read) { /* Write was truncated; warn but continue. */ if (!bsdtar->option_quiet) bsdtar_warnc(bsdtar, 0, "%s: Truncated write; file may have " "grown while being archived.", archive_entry_pathname(entry)); return (0); } if (truncate_archive(bsdtar)) break; if (checkpoint_archive(bsdtar, 1)) exit(1); progress += bytes_written; bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN); } return 0; }
static void write_meta_file(struct memory_file *file, struct archive *archive) { struct archive_entry *entry; entry = archive_entry_new(); archive_entry_set_pathname(entry, file->name); archive_entry_copy_stat(entry, &file->st); archive_entry_set_uname(entry, file->owner); archive_entry_set_gname(entry, file->group); if (archive_write_header(archive, entry)) errx(2, "cannot write to archive: %s", archive_error_string(archive)); archive_write_data(archive, file->data, file->len); archive_entry_free(entry); }
int copy_data(struct archive *ar, struct archive *aw) { int r; const void *buff; size_t size; off_t offset; for (;;) { r = archive_read_data_block(ar, &buff, &size, &offset); if (r == ARCHIVE_EOF) return (ARCHIVE_OK); if (r < ARCHIVE_OK) return (r); r = archive_write_data_block(aw, buff, size, offset); if (r < ARCHIVE_OK) { fprintf(stderr, "%s\n", archive_error_string(aw)); return (r); } } }
bool archive_exists(const std::string &filename, std::vector<exists_info> &files) { if (files.empty()) { return false; } autoclose::archive in(archive_read_new(), archive_read_free); if (!in) { LOGE("Out of memory"); return false; } archive_entry *entry; int ret; for (exists_info &info : files) { info.exists = false; } if (!set_up_input(in.get(), filename)) { return false; } while ((ret = archive_read_next_header(in.get(), &entry)) == ARCHIVE_OK) { for (exists_info &info : files) { if (info.path == archive_entry_pathname(entry)) { info.exists = true; } } } if (ret != ARCHIVE_EOF) { LOGE("Archive extraction ended without reaching EOF: %s", archive_error_string(in.get())); return false; } return true; }
struct archive* ArchOpen( const char* FileName ) { struct archive* Arch = archive_read_new(); if ( Arch != nullptr ) { int Res = archive_read_support_filter_all( Arch ); if ( Res == ARCHIVE_OK ) { Res = archive_read_support_format_all( Arch ); if ( Res == ARCHIVE_OK ) { Res = archive_read_open_filename( Arch, FileName, 10240 ); if ( Res == ARCHIVE_OK ) { return Arch; } dbg_printf( "Couldn't open input archive: %s\n", archive_error_string( Arch ) ); } else { dbg_printf( "Couldn't enable read formats\n" ); } } else { dbg_printf( "Couldn't enable decompression\n" ); } } else { dbg_printf( "Couldn't create archive reader\n" ); } ArchClose( Arch ); return nullptr; }
static int archive_error(archive_wrapper *ar) { int eno = archive_errno(ar->archive); if ( eno != 0 ) { const char *s = archive_error_string(ar->archive); term_t ex = PL_new_term_ref(); if ( PL_unify_term(ex, PL_FUNCTOR, FUNCTOR_error2, PL_FUNCTOR, FUNCTOR_archive_error2, PL_INT, errno, PL_CHARS, s, PL_VARIABLE) ) return PL_raise_exception(ex); return FALSE; } return TRUE; }
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 VALUE rb_libarchive_reader_next_header(VALUE self) { struct rb_libarchive_archive_container *p; struct archive_entry *ae; int r; Data_Get_Struct(self, struct rb_libarchive_archive_container, p); Check_Archive(p); if (p->eof) { return Qnil; } r = archive_read_next_header(p->ar, &ae); if (r == ARCHIVE_EOF) { p->eof = 1; return Qnil; } else if (r != ARCHIVE_OK) { rb_raise(rb_eArchiveError, "Fetch entry failed: %s", archive_error_string(p->ar)); } return rb_libarchive_entry_new(ae, 0); }
static int copy_data(struct archive* ar, struct archive* aw) { const void* buff; size_t size; off_t offset; for (;;) { int r = archive_read_data_block(ar, &buff, &size, &offset); if (r == ARCHIVE_EOF) { return ARCHIVE_OK; } if (r != ARCHIVE_OK) { return r; } r = archive_write_data_block(aw, buff, size, offset); if (r != ARCHIVE_OK) { errmsg(archive_error_string(ar)); return r; } } return 0; }
/* Helper function to copy file to archive. */ static int write_file_data(struct bsdtar *bsdtar, struct archive *a, struct archive_entry *entry, int fd, size_t align) { ssize_t bytes_read; ssize_t bytes_written; int64_t progress = 0; size_t buff_size; char *buff = bsdtar->buff; /* Round 'buff' up to the next multiple of 'align' and reduce * 'buff_size' accordingly. */ buff = (char *)((((uintptr_t)buff + align - 1) / align) * align); buff_size = bsdtar->buff + bsdtar->buff_size - buff; buff_size = (buff_size / align) * align; bytes_read = read(fd, buff, buff_size); while (bytes_read > 0) { if (need_report()) report_write(bsdtar, a, entry, progress); bytes_written = archive_write_data(a, buff, bytes_read); if (bytes_written < 0) { /* Write failed; this is bad */ lafe_warnc(0, "%s", archive_error_string(a)); return (-1); } if (bytes_written < bytes_read) { /* Write was truncated; warn but continue. */ lafe_warnc(0, "%s: Truncated write; file may have grown while being archived.", archive_entry_pathname(entry)); return (0); } progress += bytes_written; bytes_read = read(fd, buff, buff_size); } return 0; }