static const char * packing_set_format(struct archive *a, pkg_formats format) { switch (format) { case TXZ: if (archive_write_set_compression_xz(a) == ARCHIVE_OK) { return ("txz"); } else { pkg_emit_event(PKG_EVENT_ARCHIVE_COMP_UNSUP, /*argc*/2, "xz", "bzip2"); } case TBZ: if (archive_write_set_compression_bzip2(a) == ARCHIVE_OK) { return ("tbz"); } else { pkg_emit_event(PKG_EVENT_ARCHIVE_COMP_UNSUP, /*argc*/2, "bzip2", "gzip"); } case TGZ: if (archive_write_set_compression_gzip(a) == ARCHIVE_OK) { return ("tgz"); } else { pkg_emit_event(PKG_EVENT_ARCHIVE_COMP_UNSUP, /*argc*/2, "gzip", "plain tar"); } case TAR: archive_write_set_compression_none(a); return ("tar"); } return (NULL); }
static const char * packing_set_format(struct archive *a, pkg_formats format) { const char *notsupp_fmt = "%s is not supported, trying %s"; switch (format) { case TXZ: if (archive_write_set_compression_xz(a) == ARCHIVE_OK) return ("txz"); else pkg_emit_error(notsupp_fmt, "xz", "bzip2"); case TBZ: if (archive_write_set_compression_bzip2(a) == ARCHIVE_OK) return ("tbz"); else pkg_emit_error(notsupp_fmt, "bzip2", "gzip"); case TGZ: if (archive_write_set_compression_gzip(a) == ARCHIVE_OK) return ("tgz"); else pkg_emit_error(notsupp_fmt, "gzip", "plain tar"); case TAR: archive_write_set_compression_none(a); return ("tar"); } return (NULL); }
/* LZMA is handled identically, we just need a different compression * code set. (The liblzma setup looks at the code to determine * the one place that XZ and LZMA require different handling.) */ int archive_write_set_compression_lzma(struct archive *_a) { struct archive_write *a = (struct archive_write *)_a; int r = archive_write_set_compression_xz(_a); if (r != ARCHIVE_OK) return (r); a->archive.compression_code = ARCHIVE_COMPRESSION_LZMA; a->archive.compression_name = "lzma"; return (ARCHIVE_OK); }
static void make_dist(const char *pkg, const char *suffix, const package_t *plist) { char *archive_name; const char *owner, *group; const plist_t *p; struct archive *archive; struct archive_entry *entry, *sparse_entry; struct archive_entry_linkresolver *resolver; char *initial_cwd; archive = archive_write_new(); archive_write_set_format_pax_restricted(archive); if ((resolver = archive_entry_linkresolver_new()) == NULL) errx(2, "cannot create link resolver"); archive_entry_linkresolver_set_strategy(resolver, archive_format(archive)); if (CompressionType == NULL) { if (strcmp(suffix, "tbz") == 0 || strcmp(suffix, "tar.bz2") == 0) CompressionType = "bzip2"; else if (strcmp(suffix, "tgz") == 0 || strcmp(suffix, "tar.gz") == 0) CompressionType = "gzip"; else CompressionType = "none"; } if (strcmp(CompressionType, "bzip2") == 0) archive_write_set_compression_bzip2(archive); else if (strcmp(CompressionType, "gzip") == 0) archive_write_set_compression_gzip(archive); else if (strcmp(CompressionType, "xz") == 0) archive_write_set_compression_xz(archive); else if (strcmp(CompressionType, "none") == 0) archive_write_set_compression_none(archive); else errx(1, "Unspported compression type for -F: %s", CompressionType); archive_name = xasprintf("%s.%s", pkg, suffix); if (archive_write_open_file(archive, archive_name)) errx(2, "cannot create archive: %s", archive_error_string(archive)); free(archive_name); owner = DefaultOwner; group = DefaultGroup; write_meta_file(contents_file, archive); write_meta_file(comment_file, archive); write_meta_file(desc_file, archive); if (Install) write_meta_file(install_file, archive); if (DeInstall) write_meta_file(deinstall_file, archive); if (Display) write_meta_file(display_file, archive); if (BuildVersion) write_meta_file(build_version_file, archive); if (BuildInfo) write_meta_file(build_info_file, archive); if (SizePkg) write_meta_file(size_pkg_file, archive); if (SizeAll) write_meta_file(size_all_file, archive); if (Preserve) write_meta_file(preserve_file, archive); if (create_views) write_meta_file(views_file, archive); initial_cwd = getcwd(NULL, 0); for (p = plist->head; p; p = p->next) { if (p->type == PLIST_FILE) { write_normal_file(p->name, archive, resolver, owner, group); } else if (p->type == PLIST_CWD) { chdir(p->name); } else if (p->type == PLIST_IGNORE) { p = p->next; } else if (p->type == PLIST_CHOWN) { if (p->name != NULL) owner = p->name; else owner = DefaultOwner; } else if (p->type == PLIST_CHGRP) { if (p->name != NULL) group = p->name; else group = DefaultGroup; } } entry = NULL; archive_entry_linkify(resolver, &entry, &sparse_entry); while (entry != NULL) { write_entry(archive, entry); entry = NULL; archive_entry_linkify(resolver, &entry, &sparse_entry); } archive_entry_linkresolver_free(resolver); if (archive_write_close(archive)) errx(2, "cannot finish archive: %s", archive_error_string(archive)); archive_write_finish(archive); free(initial_cwd); }
static void mode_out(struct cpio *cpio) { struct archive_entry *entry, *spare; struct lafe_line_reader *lr; const char *p; int r; if (cpio->option_append) lafe_errc(1, 0, "Append mode not yet supported."); cpio->archive_read_disk = archive_read_disk_new(); if (cpio->archive_read_disk == NULL) lafe_errc(1, 0, "Failed to allocate archive object"); if (cpio->option_follow_links) archive_read_disk_set_symlink_logical(cpio->archive_read_disk); else archive_read_disk_set_symlink_physical(cpio->archive_read_disk); archive_read_disk_set_standard_lookup(cpio->archive_read_disk); cpio->archive = archive_write_new(); if (cpio->archive == NULL) lafe_errc(1, 0, "Failed to allocate archive object"); switch (cpio->compress) { case 'J': r = archive_write_set_compression_xz(cpio->archive); break; case OPTION_LZMA: r = archive_write_set_compression_lzma(cpio->archive); break; case 'j': case 'y': r = archive_write_set_compression_bzip2(cpio->archive); break; case 'z': r = archive_write_set_compression_gzip(cpio->archive); break; case 'Z': r = archive_write_set_compression_compress(cpio->archive); break; default: r = archive_write_set_compression_none(cpio->archive); break; } if (r < ARCHIVE_WARN) lafe_errc(1, 0, "Requested compression not available"); r = archive_write_set_format_by_name(cpio->archive, cpio->format); if (r != ARCHIVE_OK) lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); archive_write_set_bytes_per_block(cpio->archive, cpio->bytes_per_block); cpio->linkresolver = archive_entry_linkresolver_new(); archive_entry_linkresolver_set_strategy(cpio->linkresolver, archive_format(cpio->archive)); /* * The main loop: Copy each file into the output archive. */ r = archive_write_open_file(cpio->archive, cpio->filename); if (r != ARCHIVE_OK) lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); lr = lafe_line_reader("-", cpio->option_null); while ((p = lafe_line_reader_next(lr)) != NULL) file_to_archive(cpio, p); lafe_line_reader_free(lr); /* * The hardlink detection may have queued up a couple of entries * that can now be flushed. */ entry = NULL; archive_entry_linkify(cpio->linkresolver, &entry, &spare); while (entry != NULL) { entry_to_archive(cpio, entry); archive_entry_free(entry); entry = NULL; archive_entry_linkify(cpio->linkresolver, &entry, &spare); } r = archive_write_close(cpio->archive); if (r != ARCHIVE_OK) lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); if (!cpio->quiet) { int64_t blocks = (archive_position_uncompressed(cpio->archive) + 511) / 512; fprintf(stderr, "%lu %s\n", (unsigned long)blocks, blocks == 1 ? "block" : "blocks"); } archive_write_finish(cpio->archive); }
bool LibArchiveInterface::addFiles(const QStringList& files, const CompressionOptions& options) { const bool creatingNewFile = !QFileInfo(filename()).exists(); const QString tempFilename = filename() + QLatin1String( ".arkWriting" ); const QString globalWorkDir = options.value(QLatin1String( "GlobalWorkDir" )).toString(); if (!globalWorkDir.isEmpty()) { kDebug() << "GlobalWorkDir is set, changing dir to " << globalWorkDir; m_workDir.setPath(globalWorkDir); QDir::setCurrent(globalWorkDir); } m_writtenFiles.clear(); ArchiveRead arch_reader; if (!creatingNewFile) { arch_reader.reset(archive_read_new()); if (!(arch_reader.data())) { emit error(i18n("The archive reader could not be initialized.")); 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(i18n("The source file could not be read.")); return false; } } ArchiveWrite arch_writer(archive_write_new()); if (!(arch_writer.data())) { emit error(i18n("The archive writer could not be initialized.")); return false; } //pax_restricted is the libarchive default, let's go with that. archive_write_set_format_pax_restricted(arch_writer.data()); int ret; if (creatingNewFile) { if (filename().right(2).toUpper() == QLatin1String( "GZ" )) { kDebug() << "Detected gzip compression for new file"; ret = archive_write_set_compression_gzip(arch_writer.data()); } else if (filename().right(3).toUpper() == QLatin1String( "BZ2" )) { kDebug() << "Detected bzip2 compression for new file"; ret = archive_write_set_compression_bzip2(arch_writer.data()); #ifdef HAVE_LIBARCHIVE_XZ_SUPPORT } else if (filename().right(2).toUpper() == QLatin1String( "XZ" )) { kDebug() << "Detected xz compression for new file"; ret = archive_write_set_compression_xz(arch_writer.data()); #endif #ifdef HAVE_LIBARCHIVE_LZMA_SUPPORT } else if (filename().right(4).toUpper() == QLatin1String( "LZMA" )) { kDebug() << "Detected lzma compression for new file"; ret = archive_write_set_compression_lzma(arch_writer.data()); #endif } else if (filename().right(3).toUpper() == QLatin1String( "TAR" )) { kDebug() << "Detected no compression for new file (pure tar)"; ret = archive_write_set_compression_none(arch_writer.data()); } else { kDebug() << "Falling back to gzip"; ret = archive_write_set_compression_gzip(arch_writer.data()); } if (ret != ARCHIVE_OK) { emit error(i18nc("@info", "Setting the compression method failed with the following error: <message>%1</message>", QLatin1String(archive_error_string(arch_writer.data())))); return false; } } else { switch (archive_compression(arch_reader.data())) { case ARCHIVE_COMPRESSION_GZIP: ret = archive_write_set_compression_gzip(arch_writer.data()); break; case ARCHIVE_COMPRESSION_BZIP2: ret = archive_write_set_compression_bzip2(arch_writer.data()); break; #ifdef HAVE_LIBARCHIVE_XZ_SUPPORT case ARCHIVE_COMPRESSION_XZ: ret = archive_write_set_compression_xz(arch_writer.data()); break; #endif #ifdef HAVE_LIBARCHIVE_LZMA_SUPPORT case ARCHIVE_COMPRESSION_LZMA: ret = archive_write_set_compression_lzma(arch_writer.data()); break; #endif case ARCHIVE_COMPRESSION_NONE: ret = archive_write_set_compression_none(arch_writer.data()); break; default: emit error(i18n("The compression type '%1' is not supported by Ark.", QLatin1String(archive_compression_name(arch_reader.data())))); return false; } if (ret != ARCHIVE_OK) { emit error(i18nc("@info", "Setting the compression method failed with the following error: <message>%1</message>", QLatin1String(archive_error_string(arch_writer.data())))); return false; } } ret = archive_write_open_filename(arch_writer.data(), QFile::encodeName(tempFilename)); if (ret != ARCHIVE_OK) { emit error(i18nc("@info", "Opening the archive for writing failed with the following error: <message>%1</message>", QLatin1String(archive_error_string(arch_writer.data())))); return false; } //**************** first write the new files foreach(const QString& selectedFile, files) { bool success; success = writeFile(selectedFile, arch_writer.data()); if (!success) { QFile::remove(tempFilename); return false; } if (QFileInfo(selectedFile).isDir()) { QDirIterator it(selectedFile, QDir::AllEntries | QDir::Readable | QDir::Hidden | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); while (it.hasNext()) { const QString path = it.next(); if ((it.fileName() == QLatin1String("..")) || (it.fileName() == QLatin1String("."))) { continue; } const bool isRealDir = it.fileInfo().isDir() && !it.fileInfo().isSymLink(); success = writeFile(path + (isRealDir ? QLatin1String( "/" ) : QLatin1String( "" )), arch_writer.data()); if (!success) { QFile::remove(tempFilename); return false; } } } }
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; }
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; }
void tar_mode_c(struct bsdtar *bsdtar) { struct archive *a; int r; if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL) lafe_errc(1, 0, "no files or directories specified"); a = archive_write_new(); /* Support any format that the library supports. */ if (bsdtar->create_format == NULL) { r = archive_write_set_format_pax_restricted(a); bsdtar->create_format = "pax restricted"; } else { r = archive_write_set_format_by_name(a, bsdtar->create_format); } if (r != ARCHIVE_OK) { fprintf(stderr, "Can't use format %s: %s\n", bsdtar->create_format, archive_error_string(a)); usage(); } archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block); archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block); if (bsdtar->compress_program) { archive_write_set_compression_program(a, bsdtar->compress_program); } else { switch (bsdtar->create_compression) { case 0: r = ARCHIVE_OK; break; case 'j': case 'y': r = archive_write_set_compression_bzip2(a); break; case 'J': r = archive_write_set_compression_xz(a); break; case OPTION_LZIP: r = archive_write_set_compression_lzip(a); break; case OPTION_LZMA: r = archive_write_set_compression_lzma(a); break; case 'z': r = archive_write_set_compression_gzip(a); break; case 'Z': r = archive_write_set_compression_compress(a); break; default: lafe_errc(1, 0, "Unrecognized compression option -%c", bsdtar->create_compression); } if (r != ARCHIVE_OK) { lafe_errc(1, 0, "Unsupported compression option -%c", bsdtar->create_compression); } } if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options)) lafe_errc(1, 0, "%s", archive_error_string(a)); if (ARCHIVE_OK != archive_write_open_file(a, bsdtar->filename)) lafe_errc(1, 0, "%s", archive_error_string(a)); write_archive(a, bsdtar); }
void tar_mode_c(struct bsdtar *bsdtar) { struct archive *a; int r; if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL) bsdtar_errc(bsdtar, 1, 0, "no files or directories specified"); a = archive_write_new(); /* Support any format that the library supports. */ if (bsdtar->create_format == NULL) { r = archive_write_set_format_pax_restricted(a); bsdtar->create_format = "pax restricted"; } else { r = archive_write_set_format_by_name(a, bsdtar->create_format); } if (r != ARCHIVE_OK) { fprintf(stderr, "Can't use format %s: %s\n", bsdtar->create_format, archive_error_string(a)); usage(bsdtar); } /* * If user explicitly set the block size, then assume they * want the last block padded as well. Otherwise, use the * default block size and accept archive_write_open_file()'s * default padding decisions. */ if (bsdtar->bytes_per_block != 0) { archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block); archive_write_set_bytes_in_last_block(a, bsdtar->bytes_per_block); } else archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK); if (bsdtar->compress_program) { archive_write_set_compression_program(a, bsdtar->compress_program); } else { switch (bsdtar->create_compression) { case 0: archive_write_set_compression_none(a); break; #ifdef HAVE_LIBBZ2 case 'j': case 'y': archive_write_set_compression_bzip2(a); break; #endif #ifdef HAVE_LIBLZMA case 'J': archive_write_set_compression_xz(a); break; case OPTION_LZMA: archive_write_set_compression_lzma(a); break; #endif #ifdef HAVE_LIBZ case 'z': archive_write_set_compression_gzip(a); break; #endif case 'Z': archive_write_set_compression_compress(a); break; default: bsdtar_errc(bsdtar, 1, 0, "Unrecognized compression option -%c", bsdtar->create_compression); } } if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options)) bsdtar_errc(bsdtar, 1, 0, archive_error_string(a)); if (ARCHIVE_OK != archive_write_open_file(a, bsdtar->filename)) bsdtar_errc(bsdtar, 1, 0, archive_error_string(a)); write_archive(a, bsdtar); }