int write_archive_from_mem(char *outname, MagickWand *wand) { int archiveSize = 0; int pageNumber = 1; struct archive *a; struct archive_entry *entry; a = archive_write_new(); archive_write_set_format_ustar(a); archive_write_open_filename(a, outname); char filename[13]; MagickResetIterator(wand); MagickNextImage(wand); // Has to be called after MagickResetIterator to set the first picture as the current do { unsigned char *data; size_t size; data = MagickWriteImageBlob(wand, &size); entry = archive_entry_new(); snprintf(filename, 13, "page_%d", pageNumber++); archive_entry_set_pathname(entry, filename); archive_entry_set_size(entry, size); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(a, entry); archiveSize += archive_write_data(a, data, size); archive_entry_free(entry); } while (MagickNextImage(wand)); archive_write_close(a); archive_write_free(a); return archiveSize; }
void DTAR_writer_init() { char compress=DTAR_user_opts->compress; char filename=DTAR_user_opts->dest_path; DTAR_writer->flags= O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_CLOEXEC; DTAR_writer->a=archive_write_new(); switch (compress) { case 'j': case 'y': archive_write_add_filter_bzip2(a); break; case 'Z': archive_write_add_filter_compress(a); break; case 'z': archive_write_add_filter_gzip(a); break; default: archive_write_add_filter_none(a); break; } archive_write_set_format_ustar(a); archive_write_set_bytes_per_block(a,0); if (strcmp(filename, "-") == 0) filename = NULL; DTAR_writer->fd_tar=open(filename,DTAR_writer->flags,0666); archive_write_open_fd(a, DTAR_writer->fd_tar); }
void write_archive(const char *outname, const char **filename) { struct mydata *mydata = malloc(sizeof(struct mydata)); struct archive *a; struct archive_entry *entry; struct stat st; char buff[8192]; int len; int fd; a = archive_write_new(); mydata->name = outname; archive_write_set_compression_gzip(a); archive_write_set_format_ustar(a); archive_write_open(a, mydata, myopen, mywrite, myclose); while (*filename) { stat(*filename, &st); entry = archive_entry_new(); archive_entry_copy_stat(entry, &st); archive_entry_set_pathname(entry, *filename); archive_entry_set_size(entry, st.st_size); archive_clear_error(a); archive_write_header(a, entry); fd = open(*filename, O_RDONLY); len = read(fd, buff, sizeof(buff)); while ( len > 0 ) { archive_write_data(a, buff, len); len = read(fd, buff, sizeof(buff)); } archive_entry_free(entry); filename++; } archive_write_finish(a); }
void Writer::set_format_helper(struct archive *ar, int format) { std::string error_msg; switch(format) { case Archive::FORMAT_AR_BSD: archive_write_set_format_ar_bsd(ar); break; case Archive::FORMAT_AR_SVR4: archive_write_set_format_ar_svr4(ar); break; case Archive::FORMAT_CPIO: archive_write_set_format_cpio(ar); break; case Archive::FORMAT_CPIO_NEWC: archive_write_set_format_cpio_newc(ar); break; case Archive::FORMAT_MTREE: archive_write_set_format_mtree(ar); break; case Archive::FORMAT_TAR: case Archive::FORMAT_TAR_PAX_RESTRICTED: archive_write_set_format_pax_restricted(ar); break; case Archive::FORMAT_TAR_PAX_INTERCHANGE: archive_write_set_format_pax(ar); break; case Archive::FORMAT_TAR_USTAR: archive_write_set_format_ustar(ar); break; default: error_msg = "unknown or unsupported archive format"; throw Error(error_msg); } }
int createArchiveofFilesPC(char** files, unsigned long * size, unsigned int fileCount, const char* filename, const char* tarHostDir) { unsigned int ctr = 0; struct timespec ts; struct archive_entry* entry; struct archive* archive = archive_write_new(); int dirlen = strlen(tarHostDir); if ((archive_write_set_compression_gzip(archive) != ARCHIVE_OK) || (archive_write_set_format_ustar(archive) != ARCHIVE_OK) || (archive_write_open_filename(archive, filename) != ARCHIVE_OK)) { printf("%s\n", archive_error_string(archive)); return -1; } int tarHostDirLen = strlen(tarHostDir); for (ctr = 0; ctr < fileCount; ctr++) { entry = archive_entry_new(); clock_gettime(CLOCK_REALTIME, &ts); //Set entry to be stored under the tarHostDir directory const char* path = files[ctr]; int pathlength = dirlen + strlen(path) + 2; //One for / and the other for '\0' char newPath[pathlength]; if(tarHostDirLen>0) snprintf(newPath, pathlength, "%s/%s", tarHostDir, boost::filesystem::path(path).filename().c_str()); else snprintf(newPath, pathlength, "%s", boost::filesystem::path(path).filename().c_str()); archive_entry_set_pathname(entry, newPath); archive_entry_set_size(entry, size[ctr]); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0444); archive_entry_set_atime(entry, ts.tv_sec, ts.tv_nsec); archive_entry_set_birthtime(entry, ts.tv_sec, ts.tv_nsec); archive_entry_set_ctime(entry, ts.tv_sec, ts.tv_nsec); archive_entry_set_mtime(entry, ts.tv_sec, ts.tv_nsec); int rc = archive_write_header(archive, entry); char *contents = new char[size[ctr]+1]; FILE* fp = fopen(files[ctr],"rb"); fread((void *)contents, size[ctr], 1, fp); fclose(fp); archive_write_data(archive, contents, size[ctr]); archive_entry_free(entry); entry = NULL; delete[] contents; if (ARCHIVE_OK != rc) { printf("%s\n", archive_error_string(archive)); return -1; } } archive_write_finish(archive); }
int archive_create_tar_bzip2(const char **pathnames) { int error = 0; struct archive *a = archive_write_new(); archive_write_set_compression_bzip2(a); archive_write_set_format_ustar(a); archive_write_open_file(a, NULL); while (*pathnames) { append_path_recursively(*pathnames, a); pathnames++; } archive_write_close(a); archive_write_finish(a); return error; }
static void create_archive(const char *filepath, const char *tarfile) { struct archive *a; struct archive_entry *entry; struct stat st; char buff[8192]; int len; int fd; char **filenames, **filename; filenames = list_filenames(filepath); filename = filenames; a = archive_write_new(); archive_write_set_compression_bzip2(a); archive_write_set_format_ustar(a); archive_write_open_filename(a, tarfile); while (*filename) { stat(*filename, &st); entry = archive_entry_new(); archive_entry_set_pathname(entry, *filename); archive_entry_set_size(entry, st.st_size); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(a, entry); fd = open(*filename, O_RDONLY); len = read(fd, buff, sizeof(buff)); while ( len > 0 ) { archive_write_data(a, buff, len); len = read(fd, buff, sizeof(buff)); } close(fd); archive_entry_free(entry); filename++; } free_str_array(filenames); archive_write_close(a); #if ARCHIVE_VERSION_NUMBER < 4000000 archive_write_finish(a); #else archive_write_free(a); #endif }
int createArchiveofFiles(char** files, unsigned long * size, unsigned int fileCount, const char* filename) { unsigned int ctr = 0; struct timespec ts; struct archive_entry* entry; struct archive* archive = archive_write_new(); if ((archive_write_set_compression_gzip(archive) != ARCHIVE_OK) || (archive_write_set_format_ustar(archive) != ARCHIVE_OK) || (archive_write_open_filename(archive, filename) != ARCHIVE_OK)) { printf("%s\n", archive_error_string(archive)); return -1; } for (ctr = 0; ctr < fileCount; ctr++) { entry = archive_entry_new(); clock_gettime(CLOCK_REALTIME, &ts); archive_entry_set_pathname(entry, files[ctr]); archive_entry_set_size(entry, size[ctr]); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0444); archive_entry_set_atime(entry, ts.tv_sec, ts.tv_nsec); archive_entry_set_birthtime(entry, ts.tv_sec, ts.tv_nsec); archive_entry_set_ctime(entry, ts.tv_sec, ts.tv_nsec); archive_entry_set_mtime(entry, ts.tv_sec, ts.tv_nsec); int rc = archive_write_header(archive, entry); char *contents = new char[size[ctr]+1]; FILE* fp = fopen(files[ctr],"rb"); fread((void *)contents, size[ctr], 1, fp); fclose(fp); archive_write_data(archive, contents, size[ctr]); archive_entry_free(entry); entry = NULL; delete[] contents; if (ARCHIVE_OK != rc) { printf("%s\n", archive_error_string(archive)); return -1; } } archive_write_finish(archive); }
MemoryWriteArchive::MemoryWriteArchive(std::vector< char >& buffer) : m_buffer(buffer) { int ret; void* userData; m_archive = archive_write_new(); archive_write_add_filter_none(m_archive); archive_write_set_format_ustar(m_archive); archive_write_set_bytes_in_last_block(m_archive, 1); userData = reinterpret_cast<void*>(&m_buffer); ret = archive_write_open(m_archive, userData, &MemoryWriteArchive::open, &MemoryWriteArchive::write, &MemoryWriteArchive::close); if (ret != ARCHIVE_OK) { throw ::fwZip::exception::Write("Cannot open archive in write mode"); } }
static void test_filter_by_name(const char *filter_name, int filter_code, int (*can_filter_prog)(void)) { struct archive_entry *ae; struct archive *a; size_t used; size_t buffsize = 1024 * 128; char *buff; int r; assert((buff = malloc(buffsize)) != NULL); if (buff == NULL) return; /* Create a new archive in memory. */ assert((a = archive_write_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); r = archive_write_add_filter_by_name(a, filter_name); if (r == ARCHIVE_WARN) { if (!can_filter_prog()) { skipping("%s filter not suported on this platform", filter_name); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); free(buff); return; } } else if (r == ARCHIVE_FATAL && (strcmp(archive_error_string(a), "lzma compression not supported on this platform") == 0 || strcmp(archive_error_string(a), "xz compression not supported on this platform") == 0)) { skipping("%s filter not suported on this platform", filter_name); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); free(buff); return; } else { if (!assertEqualIntA(a, ARCHIVE_OK, r)) { assertEqualInt(ARCHIVE_OK, archive_write_free(a)); free(buff); return; } } assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 10)); assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used)); /* * Write a file to it. */ assert((ae = archive_entry_new()) != NULL); archive_entry_set_mtime(ae, 1, 0); assertEqualInt(1, archive_entry_mtime(ae)); archive_entry_set_ctime(ae, 1, 0); assertEqualInt(1, archive_entry_ctime(ae)); archive_entry_set_atime(ae, 1, 0); assertEqualInt(1, archive_entry_atime(ae)); archive_entry_copy_pathname(ae, "file"); assertEqualString("file", archive_entry_pathname(ae)); archive_entry_set_mode(ae, AE_IFREG | 0755); assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae)); archive_entry_set_size(ae, 8); assertEqualInt(0, archive_write_header(a, ae)); archive_entry_free(ae); assertEqualInt(8, archive_write_data(a, "12345678", 8)); /* Close out the archive. */ assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* * Now, read the data back. */ assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used)); /* * Read and verify the file. */ assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualInt(1, archive_entry_mtime(ae)); assertEqualString("file", archive_entry_pathname(ae)); assertEqualInt(AE_IFREG, archive_entry_filetype(ae)); assertEqualInt(8, archive_entry_size(ae)); /* Verify the end of the archive. */ assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); /* Verify archive format. */ assertEqualIntA(a, filter_code, archive_filter_code(a, 0)); assertEqualIntA(a, ARCHIVE_FORMAT_TAR_USTAR, archive_format(a)); assertEqualInt(ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); free(buff); }
void TarUtils::write(std::ostream &output, const io::ObservedFile &root, const std::set<ObservedFile> &files_to_send) { bool processed = false; //create new archive, set format to tar, use callbacks (above this method) struct archive *a; a = archive_write_new(); archive_write_set_format_ustar(a); archive_write_open(a, &output, &__tar_utils_open_callback, &__tar_utils_write_callback, &__tar_utils_close_callback); for(std::set<ObservedFile>::const_iterator of_iter = files_to_send.begin(); of_iter != files_to_send.end(); ++of_iter) { const ObservedFile &of = (*of_iter); const ibrcommon::File &file = of.getFile(); struct archive_entry *entry; entry = archive_entry_new(); archive_entry_set_size(entry, file.size()); if(file.isDirectory()) { archive_entry_set_filetype(entry, AE_IFDIR); archive_entry_set_perm(entry, 0755); } else { archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); } archive_entry_set_pathname(entry, rel_filename(root, of).c_str()); //set timestamps struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); archive_entry_set_atime(entry, ts.tv_sec, ts.tv_nsec); //accesstime archive_entry_set_birthtime(entry, ts.tv_sec, ts.tv_nsec); //creationtime archive_entry_set_ctime(entry, ts.tv_sec, ts.tv_nsec); //time, inode changed archive_entry_set_mtime(entry, ts.tv_sec, ts.tv_nsec); //modification time archive_write_header(a, entry); try { #ifdef HAVE_LIBTFFS //read file on vfat-image try { const FATFile &ffile = dynamic_cast<const FATFile&>(file); processed = true; // get image reader const FatImageReader &reader = ffile.getReader(); // open fat file io::FatImageReader::FileHandle fh = reader.open(ffile); char buff[BUFF_SIZE]; ssize_t ret = 0; size_t len = 0; // read file len = fh.read((unsigned char*)&buff, BUFF_SIZE); //write buffer to archive while (len > 0) { if( (ret = archive_write_data(a, buff, len)) < 0) { IBRCOMMON_LOGGER_TAG("TarUtils", error) << "archive_write_data failed" << IBRCOMMON_LOGGER_ENDL; break; } // read next chunk len = fh.read((unsigned char*)&buff, BUFF_SIZE); } } catch (const std::bad_cast&) { }; #endif if (!processed) { char buff[BUFF_SIZE]; ssize_t ret = 0; // open file for reading std::ifstream fs(file.getPath().c_str()); // write buffer to archive while (fs.good()) { // read bytes fs.read(buff, BUFF_SIZE); // write bytes to archive if( (ret = archive_write_data(a, buff, fs.gcount())) < 0) { IBRCOMMON_LOGGER_TAG("TarUtils", error) << "archive write failed" << IBRCOMMON_LOGGER_ENDL; break; } } } } catch (const ibrcommon::IOException &e) { // write failed IBRCOMMON_LOGGER_TAG("TarUtils", error) << "archive write failed: " << e.what() << IBRCOMMON_LOGGER_ENDL; archive_entry_free(entry); archive_write_close(a); archive_write_free(a); throw; } archive_entry_free(entry); } archive_write_close(a); archive_write_free(a); }
static void create(const char *filename, int compress, const char **argv) { struct archive *a; struct archive *disk; struct archive_entry *entry; ssize_t len; int fd; a = archive_write_new(); switch (compress) { #ifndef NO_BZIP2_CREATE case 'j': case 'y': archive_write_add_filter_bzip2(a); break; #endif #ifndef NO_COMPRESS_CREATE case 'Z': archive_write_add_filter_compress(a); break; #endif #ifndef NO_GZIP_CREATE case 'z': archive_write_add_filter_gzip(a); break; #endif default: archive_write_add_filter_none(a); break; } archive_write_set_format_ustar(a); if (filename != NULL && strcmp(filename, "-") == 0) filename = NULL; archive_write_open_filename(a, filename); disk = archive_read_disk_new(); #ifndef NO_LOOKUP archive_read_disk_set_standard_lookup(disk); #endif while (*argv != NULL) { struct archive *disk = archive_read_disk_new(); int r; r = archive_read_disk_open(disk, *argv); if (r != ARCHIVE_OK) { errmsg(archive_error_string(disk)); errmsg("\n"); exit(1); } for (;;) { int needcr = 0; entry = archive_entry_new(); r = archive_read_next_header2(disk, entry); if (r == ARCHIVE_EOF) break; if (r != ARCHIVE_OK) { errmsg(archive_error_string(disk)); errmsg("\n"); exit(1); } archive_read_disk_descend(disk); if (verbose) { msg("a "); msg(archive_entry_pathname(entry)); needcr = 1; } r = archive_write_header(a, entry); if (r < ARCHIVE_OK) { errmsg(": "); errmsg(archive_error_string(a)); needcr = 1; } if (r == ARCHIVE_FATAL) exit(1); if (r > ARCHIVE_FAILED) { #if 0 /* Ideally, we would be able to use * the same code to copy a body from * an archive_read_disk to an * archive_write that we use for * copying data from an archive_read * to an archive_write_disk. * Unfortunately, this doesn't quite * work yet. */ copy_data(disk, a); #else /* For now, we use a simpler loop to copy data * into the target archive. */ fd = open(archive_entry_sourcepath(entry), O_RDONLY); len = read(fd, buff, sizeof(buff)); while (len > 0) { archive_write_data(a, buff, len); len = read(fd, buff, sizeof(buff)); } close(fd); #endif } archive_entry_free(entry); if (needcr) msg("\n"); } archive_read_close(disk); archive_read_free(disk); argv++; } archive_write_close(a); archive_write_free(a); }
/* ArchiveWriter::__construct {{{ * */ ZEND_METHOD(ArchiveWriter, __construct) { archive_file_t *arch = NULL; int resource_id; zval *this = getThis(); const char *error_string = NULL; char *filename; long error_num, filename_len, result, format=0, compression=0; 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, "s|ll", &filename, &filename_len, &format, &compression) == FAILURE) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } #if PHP_API_VERSION < 20100412 if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } #endif if (php_check_open_basedir(filename TSRMLS_CC)) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } arch = (archive_file_t *) emalloc(sizeof(archive_file_t)); arch->stream = NULL; ALLOC_HASHTABLE(arch->entries); zend_hash_init(arch->entries, 10, NULL, _archive_entries_hash_dtor, 0); arch->mode = PHP_ARCHIVE_WRITE_MODE; arch->buf = emalloc(PHP_ARCHIVE_BUF_LEN + 1); arch->filename = estrndup(filename, filename_len); arch->arch = archive_write_new(); switch (compression) { case PHP_ARCHIVE_COMPRESSION_GZIP: if (archive_write_add_filter_gzip(arch->arch) != ARCHIVE_OK) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Gzip compression is not supported in this build"); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } break; case PHP_ARCHIVE_COMPRESSION_BZIP2: if (archive_write_add_filter_bzip2(arch->arch) != ARCHIVE_OK) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bzip2 compression is not supported in this build"); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } break; case 0: /* default value */ case PHP_ARCHIVE_COMPRESSION_NONE: /* always supported */ break; default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported compression type %ld", compression); zend_restore_error_handling(&error_handling TSRMLS_CC); return; break; } switch (format) { case 0: /* default value */ case PHP_ARCHIVE_FORMAT_TAR: case PHP_ARCHIVE_FORMAT_PAX_RESTRICTED: archive_write_set_format_pax_restricted(arch->arch); break; case PHP_ARCHIVE_FORMAT_PAX: archive_write_set_format_pax(arch->arch); break; case PHP_ARCHIVE_FORMAT_CPIO: archive_write_set_format_cpio(arch->arch); break; case PHP_ARCHIVE_FORMAT_SHAR: archive_write_set_format_shar(arch->arch); break; case PHP_ARCHIVE_FORMAT_USTAR: archive_write_set_format_ustar(arch->arch); break; default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported archive format: %ld", format); zend_restore_error_handling(&error_handling TSRMLS_CC); return; break; } archive_write_set_bytes_per_block(arch->arch, DEFAULT_BYTES_PER_BLOCK); result = archive_write_open(arch->arch, arch, _archive_open_clbk, _archive_write_clbk, _archive_close_clbk); /* do not pad the last block */ archive_write_set_bytes_in_last_block(arch->arch, 1); if (result) { error_num = archive_errno(arch->arch); error_string = archive_error_string(arch->arch); efree(arch->filename); efree(arch->buf); efree(arch); if (error_num && error_string) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open file %s for writing: error #%ld, %s", filename, error_num, error_string); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open file %s for writing: unknown error %ld", filename, result); } zend_restore_error_handling(&error_handling TSRMLS_CC); return; } resource_id = zend_list_insert(arch,le_archive); add_property_resource(this, "fd", resource_id); zend_restore_error_handling(&error_handling TSRMLS_CC); return; }
int archive_create( char *arch_file, int compress, int format, char *src_dir, char **exclude ) { int fd, len, ret, skip; char *pwd, **files; DIR *dir; struct dirent *dir_entry; struct archive *arch_w = NULL, *arch_r = NULL; struct archive_entry *arch_entry = NULL; char buf[8192]; if( !arch_file || !src_dir ) return -1; dir = opendir( src_dir ); if( !dir ) return -1; arch_w = archive_write_new(); switch( compress ) { case 'j': //bz2 //archive_write_add_filter_bzip2( arch_w ); //libarchive 3 archive_write_set_compression_bzip2( arch_w ); break; case 'J': //xz //archive_write_add_filter_xz( arch_w ); archive_write_set_compression_xz( arch_w ); break; case 'z': //gzip //archive_write_add_filter_gzip( arch_w ); archive_write_set_compression_gzip( arch_w ); break; } switch( format ) { case 'c': //cpio archive_write_set_format_cpio( arch_w ); break; case 't': //tar archive_write_set_format_ustar( arch_w ); break; default: //tar archive_write_set_format_ustar( arch_w ); break; } ret = archive_write_open_filename( arch_w, arch_file ); if( ret != ARCHIVE_OK ) { archive_write_finish( arch_w ); return -1; } pwd = getcwd( NULL, 0 ); chdir( src_dir ); while( (dir_entry = readdir( dir )) ) { if( !strcmp( dir_entry->d_name, "." ) || !strcmp( dir_entry->d_name, ".." ) ) { continue; } if( exclude ) { files = exclude; skip = 0; while( *files ) { if( !strcmp( *files, dir_entry->d_name ) ) { skip = 1; break; } files++; } if( skip ) continue; } arch_r = archive_read_disk_new(); ret = archive_read_disk_open( arch_r, dir_entry->d_name ); if( ret != ARCHIVE_OK ) { #ifdef DEBUG printf( "%s\n", archive_error_string( arch_r ) ); #endif archive_write_finish( arch_w ); return -1; } for( ; ; ) { arch_entry = archive_entry_new(); ret = archive_read_next_header2( arch_r, arch_entry ); if( ret == ARCHIVE_EOF ) break; if( ret != ARCHIVE_OK ) { #ifdef DEBUG printf( "%s\n", archive_error_string( arch_r ) ); #endif archive_entry_free( arch_entry ); archive_write_finish( arch_r ); archive_write_finish( arch_w ); return -1; } #ifdef DEBUG printf( "%s\n", archive_entry_pathname( arch_entry ) ); #endif archive_read_disk_descend( arch_r ); ret = archive_write_header( arch_w, arch_entry ); if( ret < ARCHIVE_OK ) { #ifdef DEBUG printf( "%s\n", archive_error_string( arch_w ) ); #endif archive_entry_free( arch_entry ); archive_write_finish( arch_r ); archive_write_finish( arch_w ); return -1; } if( ( fd = open( archive_entry_sourcepath( arch_entry ), O_RDONLY ) ) != -1 ) { len = read( fd, buf, 8192 ); while( len > 0 ) { archive_write_data( arch_w, buf, len ); len = read( fd, buf, 8192 ); } close( fd ); } archive_entry_free( arch_entry ); } /* libarchive 3 archive_read_close( arch_r ); archive_read_free( arch_r ); */ archive_read_finish( arch_r ); } if( arch_w ) { /* libarchive 3 archive_write_close( arch_r ); archive_write_free( arch_r ); */ archive_write_finish( arch_w ); } if( pwd ) { chdir( pwd ); free( pwd ); } return 0; }
static void create(const char *filename, int compress, const char **argv) { struct archive *a; struct archive *disk; struct archive_entry *entry; ssize_t len; int fd; a = archive_write_new(); switch (compress) { #ifndef NO_BZIP2_CREATE case 'j': case 'y': archive_write_set_compression_bzip2(a); break; #endif #ifndef NO_COMPRESS_CREATE case 'Z': archive_write_set_compression_compress(a); break; #endif #ifndef NO_GZIP_CREATE case 'z': archive_write_set_compression_gzip(a); break; #endif default: archive_write_set_compression_none(a); break; } archive_write_set_format_ustar(a); if (strcmp(filename, "-") == 0) filename = NULL; archive_write_open_file(a, filename); disk = archive_read_disk_new(); #ifndef NO_LOOKUP archive_read_disk_set_standard_lookup(disk); #endif while (*argv != NULL) { struct tree *t = tree_open(*argv); while (tree_next(t)) { entry = archive_entry_new(); archive_entry_set_pathname(entry, tree_current_path(t)); archive_read_disk_entry_from_file(disk, entry, -1, tree_current_stat(t)); if (verbose) { msg("a "); msg(tree_current_path(t)); } archive_write_header(a, entry); fd = open(tree_current_access_path(t), O_RDONLY); len = read(fd, buff, sizeof(buff)); while (len > 0) { archive_write_data(a, buff, len); len = read(fd, buff, sizeof(buff)); } close(fd); archive_entry_free(entry); if (verbose) msg("\n"); } argv++; } archive_write_close(a); archive_write_finish(a); }
static void test_options(const char *options) { struct archive_entry *ae; struct archive* a; char *buff, *data; size_t buffsize, datasize; char path[16]; size_t used1; int i, r, use_prog = 0, filecount; assert((a = archive_write_new()) != NULL); r = archive_write_add_filter_lz4(a); if (archive_liblz4_version() == NULL) { if (!canLz4()) { skipping("lz4 writing not supported on this platform"); assertEqualInt(ARCHIVE_WARN, r); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); return; } else { assertEqualInt(ARCHIVE_WARN, r); use_prog = 1; } } else { assertEqualInt(ARCHIVE_OK, r); } buffsize = 2000000; assert(NULL != (buff = (char *)malloc(buffsize))); datasize = 10000; assert(NULL != (data = (char *)calloc(1, datasize))); filecount = 10; /* * Write a filecount files and read them all back. */ assert((a = archive_write_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); assertEqualIntA(a, (use_prog)?ARCHIVE_WARN:ARCHIVE_OK, archive_write_add_filter_lz4(a)); assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, options)); assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1024)); assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1024)); assertEqualInt(ARCHIVE_FILTER_LZ4, archive_filter_code(a, 0)); assertEqualString("lz4", archive_filter_name(a, 0)); assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used1)); assert((ae = archive_entry_new()) != NULL); archive_entry_set_filetype(ae, AE_IFREG); archive_entry_set_size(ae, datasize); for (i = 0; i < filecount; i++) { sprintf(path, "file%03d", i); archive_entry_copy_pathname(ae, path); assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); assertA(datasize == (size_t)archive_write_data(a, data, datasize)); } archive_entry_free(ae); assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); r = archive_read_support_filter_lz4(a); if (r == ARCHIVE_WARN) { skipping("Can't verify lz4 writing by reading back;" " lz4 reading not fully supported on this platform"); } else { assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used1)); for (i = 0; i < filecount; i++) { sprintf(path, "file%03d", i); if (!assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &ae))) break; assertEqualString(path, archive_entry_pathname(ae)); assertEqualInt((int)datasize, archive_entry_size(ae)); } assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); } assertEqualInt(ARCHIVE_OK, archive_read_free(a)); /* * Clean up. */ free(data); free(buff); }
int archive_create2( char *arch_file, int compress, int format, char **files ) { int fd, len, ret; struct archive *arch_w = NULL, *arch_r; struct archive_entry *arch_entry = NULL; char buf[8192]; if( !arch_file || !files ) return -1; arch_w = archive_write_new(); switch( compress ) { case 'j': //bz2 //archive_write_add_filter_bzip2( arch_w ); archive_write_set_compression_bzip2( arch_w ); break; case 'J': //xz //archive_write_add_filter_xz( arch_w ); archive_write_set_compression_xz( arch_w ); break; case 'z': //gzip //archive_write_add_filter_gzip( arch_w ); archive_write_set_compression_gzip( arch_w ); break; } switch( format ) { case 'c': //cpio archive_write_set_format_cpio( arch_w ); break; case 't': //tar archive_write_set_format_ustar( arch_w ); break; default: //tar archive_write_set_format_ustar( arch_w ); break; } ret = archive_write_open_filename( arch_w, arch_file ); if( ret != ARCHIVE_OK ) { archive_write_finish( arch_w ); return -1; } while( *files ) { arch_r = archive_read_disk_new(); ret = archive_read_disk_open( arch_r, *files ); if( ret != ARCHIVE_OK ) { #ifdef DEBUG printf( "%s\n", archive_error_string( arch_r ) ); #endif archive_write_finish( arch_w ); return -1; } for( ; ; ) { arch_entry = archive_entry_new(); ret = archive_read_next_header2( arch_r, arch_entry ); if( ret == ARCHIVE_EOF ) break; if( ret != ARCHIVE_OK ) { #ifdef DEBUG printf( "%s\n", archive_error_string( arch_r ) ); #endif archive_entry_free( arch_entry ); archive_write_finish( arch_r ); archive_write_finish( arch_w ); return -1; } archive_read_disk_descend( arch_r ); ret = archive_write_header( arch_w, arch_entry ); if( ret < ARCHIVE_OK ) { #ifdef DEBUG printf( "%s\n", archive_error_string( arch_w ) ); #endif archive_entry_free( arch_entry ); archive_write_finish( arch_r ); archive_write_finish( arch_w ); return -1; } if( ( fd = open( archive_entry_sourcepath( arch_entry ), O_RDONLY ) ) != -1 ) { len = read( fd, buf, 8192 ); while( len > 0 ) { archive_write_data( arch_w, buf, len ); len = read( fd, buf, 8192 ); } close( fd ); } archive_entry_free( arch_entry ); } /* libarchive 3 archive_read_close( arch_r ); archive_read_free( arch_r ); */ archive_read_finish( arch_r ); } if( arch_w ) archive_write_finish( arch_w ); return 0; }
static void test_open_filename_mbs(void) { char buff[64]; struct archive_entry *ae; struct archive *a; /* Write an archive through this FILE *. */ assert((a = archive_write_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_none(a)); assertEqualIntA(a, ARCHIVE_OK, archive_write_open_filename(a, "test.tar")); /* * Write a file to it. */ assert((ae = archive_entry_new()) != NULL); archive_entry_set_mtime(ae, 1, 0); archive_entry_copy_pathname(ae, "file"); archive_entry_set_mode(ae, S_IFREG | 0755); archive_entry_set_size(ae, 8); assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); archive_entry_free(ae); assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9)); /* * Write a second file to it. */ assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "file2"); archive_entry_set_mode(ae, S_IFREG | 0755); archive_entry_set_size(ae, 819200); assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); archive_entry_free(ae); /* Close out the archive. */ assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* * Now, read the data back. */ assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, "test.tar", 512)); assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualInt(1, archive_entry_mtime(ae)); assertEqualInt(0, archive_entry_mtime_nsec(ae)); assertEqualInt(0, archive_entry_atime(ae)); assertEqualInt(0, archive_entry_ctime(ae)); assertEqualString("file", archive_entry_pathname(ae)); assert((S_IFREG | 0755) == archive_entry_mode(ae)); assertEqualInt(8, archive_entry_size(ae)); assertEqualIntA(a, 8, archive_read_data(a, buff, 10)); assertEqualMem(buff, "12345678", 8); assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); assertEqualString("file2", archive_entry_pathname(ae)); assert((S_IFREG | 0755) == archive_entry_mode(ae)); assertEqualInt(819200, archive_entry_size(ae)); assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a)); /* Verify the end of the archive. */ assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); /* * Verify some of the error handling. */ assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_filename(a, "nonexistent.tar", 512)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
static void test_filename(const char *prefix, int dlen, int flen) { char buff[8192]; char filename[400]; char dirname[400]; struct archive_entry *ae; struct archive *a; size_t used; int separator = 0; int i = 0; if (prefix != NULL) { strcpy(filename, prefix); i = (int)strlen(prefix); } if (dlen > 0) { for (; i < dlen; i++) filename[i] = 'a'; filename[i++] = '/'; separator = 1; } for (; i < dlen + flen + separator; i++) filename[i] = 'b'; filename[i] = '\0'; strcpy(dirname, filename); /* Create a new archive in memory. */ assert((a = archive_write_new()) != NULL); assertA(0 == archive_write_set_format_ustar(a)); assertA(0 == archive_write_add_filter_none(a)); assertA(0 == archive_write_set_bytes_per_block(a,0)); assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used)); /* * Write a file to it. */ assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, filename); archive_entry_set_mode(ae, S_IFREG | 0755); failure("dlen=%d, flen=%d", dlen, flen); if (flen > 100) { assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae)); } else { assertEqualIntA(a, 0, archive_write_header(a, ae)); } archive_entry_free(ae); /* * Write a dir to it (without trailing '/'). */ assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, dirname); archive_entry_set_mode(ae, S_IFDIR | 0755); failure("dlen=%d, flen=%d", dlen, flen); if (flen >= 100) { assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae)); } else { assertEqualIntA(a, 0, archive_write_header(a, ae)); } archive_entry_free(ae); /* Tar adds a '/' to directory names. */ strcat(dirname, "/"); /* * Write a dir to it (with trailing '/'). */ assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, dirname); archive_entry_set_mode(ae, S_IFDIR | 0755); failure("dlen=%d, flen=%d", dlen, flen); if (flen >= 100) { assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae)); } else { assertEqualIntA(a, 0, archive_write_header(a, ae)); } archive_entry_free(ae); /* Close out the archive. */ assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); assertEqualInt(ARCHIVE_OK, archive_write_free(a)); /* * Now, read the data back. */ assert((a = archive_read_new()) != NULL); assertA(0 == archive_read_support_format_all(a)); assertA(0 == archive_read_support_filter_all(a)); assertA(0 == archive_read_open_memory(a, buff, used)); if (flen <= 100) { /* Read the file and check the filename. */ assertA(0 == archive_read_next_header(a, &ae)); failure("dlen=%d, flen=%d", dlen, flen); assertEqualString(filename, archive_entry_pathname(ae)); assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae)); } /* * Read the two dirs and check the names. * * Both dirs should read back with the same name, since * tar should add a trailing '/' to any dir that doesn't * already have one. */ if (flen <= 99) { assertA(0 == archive_read_next_header(a, &ae)); assert((S_IFDIR | 0755) == archive_entry_mode(ae)); failure("dlen=%d, flen=%d", dlen, flen); assertEqualString(dirname, archive_entry_pathname(ae)); } if (flen <= 99) { assertA(0 == archive_read_next_header(a, &ae)); assert((S_IFDIR | 0755) == archive_entry_mode(ae)); assertEqualString(dirname, archive_entry_pathname(ae)); } /* Verify the end of the archive. */ failure("This fails if entries were written that should not have been written. dlen=%d, flen=%d", dlen, flen); assertEqualInt(1, archive_read_next_header(a, &ae)); assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); assertEqualInt(ARCHIVE_OK, archive_read_free(a)); }
const gchar* archive_create(const char* archive_name, GSList* files, COMPRESS_METHOD method, ARCHIVE_FORMAT format) { struct archive* arch; struct archive_entry* entry; char* buf = NULL; ssize_t len; int fd; struct stat st; struct file_info* file; gchar* filename = NULL; gchar* msg = NULL; #ifndef _TEST gint num = 0; gint total = g_slist_length (files); #endif g_return_val_if_fail(files != NULL, "No files for archiving"); debug_print("File: %s\n", archive_name); arch = archive_write_new(); switch (method) { case ZIP: if (archive_write_set_compression_gzip(arch) != ARCHIVE_OK) return archive_error_string(arch); break; case BZIP2: if (archive_write_set_compression_bzip2(arch) != ARCHIVE_OK) return archive_error_string(arch); break; #if NEW_ARCHIVE_API case COMPRESS: if (archive_write_set_compression_compress(arch) != ARCHIVE_OK) return archive_error_string(arch); break; #endif case NO_COMPRESS: if (archive_write_set_compression_none(arch) != ARCHIVE_OK) return archive_error_string(arch); break; } switch (format) { case TAR: if (archive_write_set_format_ustar(arch) != ARCHIVE_OK) return archive_error_string(arch); break; case SHAR: if (archive_write_set_format_shar(arch) != ARCHIVE_OK) return archive_error_string(arch); break; case PAX: if (archive_write_set_format_pax(arch) != ARCHIVE_OK) return archive_error_string(arch); break; case CPIO: if (archive_write_set_format_cpio(arch) != ARCHIVE_OK) return archive_error_string(arch); break; case NO_FORMAT: return "Missing archive format"; } if (archive_write_open_file(arch, archive_name) != ARCHIVE_OK) return archive_error_string(arch); while (files && ! stop_action) { #ifndef _TEST set_progress_print_all(num++, total, 30); #endif file = (struct file_info *) files->data; if (!file) continue; filename = get_full_path(file); /* libarchive will crash if instructed to add archive to it self */ if (g_utf8_collate(archive_name, filename) == 0) { buf = NULL; buf = g_strdup_printf( "%s: Not dumping to %s", archive_name, filename); g_warning("%s\n", buf); #ifndef _TEST debug_print("%s\n", buf); #endif g_free(buf); } else { #ifndef _TEST debug_print("Adding: %s\n", filename); msg = g_strdup_printf("%s", filename); set_progress_file_label(msg); g_free(msg); #endif entry = archive_entry_new(); lstat(filename, &st); if ((fd = open(filename, O_RDONLY)) == -1) { perror("open file"); } else { archive_entry_copy_stat(entry, &st); archive_entry_set_pathname(entry, filename); if (S_ISLNK(st.st_mode)) { buf = NULL; buf = malloc(PATH_MAX + 1); if ((len = readlink(filename, buf, PATH_MAX)) < 0) perror("error in readlink"); else buf[len] = '\0'; archive_entry_set_symlink(entry, buf); g_free(buf); archive_entry_set_size(entry, 0); archive_write_header(arch, entry); } else { if (archive_write_header(arch, entry) != ARCHIVE_OK) g_warning("%s", archive_error_string(arch)); buf = NULL; buf = malloc(READ_BLOCK_SIZE); len = read(fd, buf, READ_BLOCK_SIZE); while (len > 0) { if (archive_write_data(arch, buf, len) == -1) g_warning("%s", archive_error_string(arch)); memset(buf, 0, READ_BLOCK_SIZE); len = read(fd, buf, READ_BLOCK_SIZE); } g_free(buf); } close(fd); archive_entry_free(entry); } } g_free(filename); files = g_slist_next(files); } #ifndef _TEST if (stop_action) unlink(archive_name); stop_action = FALSE; #endif archive_write_close(arch); archive_write_finish(arch); return NULL; }