int fwfile_add_meta_conf_str(const char *configtxt, int configtxt_len, struct archive *a, const unsigned char *signing_key) { struct archive_entry *entry; // If the user passed in a signing key, sign the meta.conf. if (signing_key) { unsigned char signature[crypto_sign_BYTES]; crypto_sign_detached(signature, NULL, (unsigned char *) configtxt, configtxt_len, signing_key); entry = archive_entry_new(); archive_entry_set_pathname(entry, "meta.conf.ed25519"); archive_entry_set_size(entry, sizeof(signature)); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(a, entry); archive_write_data(a, signature, sizeof(signature)); archive_entry_free(entry); } // Add meta.conf entry = archive_entry_new(); archive_entry_set_pathname(entry, "meta.conf"); archive_entry_set_size(entry, configtxt_len); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(a, entry); archive_write_data(a, configtxt, configtxt_len); archive_entry_free(entry); return 0; }
int StudioFrame::WriteDefsToZip(struct archive *zip) { tinyxml2::XMLDocument xmlDoc; tinyxml2::XMLPrinter printer; CreateDefs(xmlDoc, true); xmlDoc.Accept(&printer); const char *buffer = printer.CStr(); struct archive_entry *entry = archive_entry_new(); if (entry == NULL) { wxMessageBox(_("archive_entry_new error")); return -1; } archive_entry_set_pathname(entry, "oaml.defs"); archive_entry_set_size(entry, strlen(buffer)); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(zip, entry); if (archive_write_data(zip, buffer, strlen(buffer)) != strlen(buffer)) { wxMessageBox(_("archive_write_data error")); return -1; } archive_entry_free(entry); return 0; }
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; }
int packing_append_buffer(struct packing *pack, const char *buffer, const char *path, int size) { struct archive_entry *entry; int ret = EPKG_OK; entry = archive_entry_new(); archive_entry_clear(entry); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_entry_set_gname(entry, "wheel"); archive_entry_set_uname(entry, "root"); archive_entry_set_pathname(entry, path); archive_entry_set_size(entry, size); if (archive_write_header(pack->awrite, entry) == -1) { pkg_emit_errno("archive_write_header", path); ret = EPKG_FATAL; goto cleanup; } if (archive_write_data(pack->awrite, buffer, size) == -1) { pkg_emit_errno("archive_write_data", path); ret = EPKG_FATAL; } cleanup: archive_entry_free(entry); return (ret); }
static void _addFileToArchive(void* item, void* userData) { char* itemPath = (char*)item; struct archive* outArchive = (struct archive*)userData; struct archive_entry* entry = archive_entry_new(); struct stat fileStat; FILE* filePointer; size_t bytesRead; byte* fileBuffer = (byte*)malloc(8192); stat(itemPath, &fileStat); archive_entry_set_pathname(entry, itemPath); archive_entry_set_size(entry, fileStat.st_size); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(outArchive, entry); filePointer = fopen(itemPath, "rb"); do { bytesRead = fread(fileBuffer, 1, 8192, filePointer); if(bytesRead > 0) { archive_write_data(outArchive, fileBuffer, bytesRead); } } while(bytesRead > 0); fclose(filePointer); archive_entry_free(entry); }
void write_archive(const char *outname, const char **filename) { struct archive *a; struct archive_entry *entry; struct stat st; char buff[8192]; int len; int fd; a = archive_write_new(); archive_write_set_compression_gzip(a); archive_write_set_format_pax_restricted(a); // Note 1 archive_write_open_filename(a, outname, 10240); while (*filename) { stat(*filename, &st); entry = archive_entry_new(); // Note 2 archive_entry_set_pathname(entry, *filename); archive_entry_set_size(entry, st.st_size); // Note 3 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++; } archive_write_close(a); // Note 4 archive_write_free(a); // Note 5 }
int xbps_archive_append_buf(struct archive *ar, const void *buf, const size_t buflen, const char *fname, const mode_t mode, const char *uname, const char *gname) { struct archive_entry *entry; assert(ar); assert(buf); assert(fname); assert(uname); assert(gname); entry = archive_entry_new(); assert(entry); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, mode); archive_entry_set_uname(entry, uname); archive_entry_set_gname(entry, gname); archive_entry_set_pathname(entry, fname); archive_entry_set_size(entry, buflen); if (archive_write_header(ar, entry) != ARCHIVE_OK) { archive_entry_free(entry); return archive_errno(ar); } if (archive_write_data(ar, buf, buflen) != ARCHIVE_OK) { archive_entry_free(entry); return archive_errno(ar); } archive_write_finish_entry(ar); archive_entry_free(entry); return 0; }
void GuiZipper::packageSingleFile(const char *absFilename, const char *filename, struct archive *a, bool binary) throw (ZipperException*) { struct stat st; char buff[8192]; int fd; stat(absFilename, &st); struct archive_entry *entry = archive_entry_new(); archive_entry_copy_stat(entry, &st); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_pathname(entry, filename); archive_entry_set_perm(entry, 0644); int r = archive_write_header(a, entry); checkForErrors("Error writing header", a, r); int flags = O_RDONLY; #ifdef __WIN32__ if (binary) { flags |= O_BINARY; } #endif fd = open(absFilename, flags); ssize_t 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); }
int myarch_set_entry(const char *filename, size_t len, struct archive_entry *entry) { time_t tm; archive_entry_set_pathname(entry, filename); archive_entry_set_size(entry, len); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_entry_set_mtime(entry, time(&tm), 0); return 0; }
int test1() { struct archive *a = archive_write_new(); int ret = archive_write_set_format_zip(a); unsigned char buffer[100]; int size; ret = archive_write_open_memory(a, buffer, 100, &size); // add a file struct archive_entry *entry = archive_entry_new(); archive_entry_set_pathname(entry, "hello.txt"); archive_entry_set_size(entry, 5); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); ret = archive_write_header(a, entry); printf("archive_write_header: ret=%d\n", ret); la_ssize_t n = archive_write_data(a, "world", 5); printf("archive_write_data: n=%d, error=%s\n", n, archive_error_string(a)); // ad another file // archive_entry_clear(entry); archive_entry_set_pathname(entry, "somedir/yo.txt"); archive_entry_set_size(entry, 5); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(a, entry); n = archive_write_data(a, "12345", 5); printf("archive_write_data: n=%d, error=%s\n", n, archive_error_string(a)); archive_entry_free(entry); ret = archive_write_free(a); printf("archive_write_free: ret=%d, error=%s\n", ret, archive_error_string(a)); printf("size=%d\n", size); write(1, buffer, size); }
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); }
static gboolean asb_utils_write_archive (const gchar *filename, const gchar *path_orig, GPtrArray *files, GError **error) { const gchar *tmp; gboolean ret = TRUE; gsize len; guint i; struct archive *a; struct archive_entry *entry; struct stat st; a = archive_write_new (); if (g_str_has_suffix (filename, ".gz")) { archive_write_add_filter_gzip (a); archive_write_set_filter_option (a, "gzip", "timestamp", NULL); } if (g_str_has_suffix (filename, ".bz2")) archive_write_add_filter_bzip2 (a); if (g_str_has_suffix (filename, ".xz")) archive_write_add_filter_xz (a); archive_write_set_format_pax_restricted (a); archive_write_open_filename (a, filename); for (i = 0; i < files->len; i++) { g_autofree gchar *data = NULL; g_autofree gchar *filename_full = NULL; tmp = g_ptr_array_index (files, i); filename_full = g_build_filename (path_orig, tmp, NULL); if (stat (filename_full, &st) != 0) continue; entry = archive_entry_new (); archive_entry_set_pathname (entry, tmp); 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); ret = g_file_get_contents (filename_full, &data, &len, error); if (!ret) { archive_entry_free (entry); break; } archive_write_data (a, data, len); archive_entry_free (entry); } archive_write_close (a); archive_write_free (a); return ret; }
int packing_append_buffer(struct packing *pack, const char *buffer, const char *path, int size) { archive_entry_clear(pack->entry); archive_entry_set_filetype(pack->entry, AE_IFREG); archive_entry_set_perm(pack->entry, 0644); archive_entry_set_gname(pack->entry, "wheel"); archive_entry_set_uname(pack->entry, "root"); archive_entry_set_pathname(pack->entry, path); archive_entry_set_size(pack->entry, size); archive_write_header(pack->awrite, pack->entry); archive_write_data(pack->awrite, buffer, size); archive_entry_clear(pack->entry); return (EPKG_OK); }
int StudioFrame::WriteFileToZip(struct archive *zip, std::string file) { const char *filename = file.c_str(); void *fd = studioCbs.open(filename); if (fd == NULL) { wxString str; str.Printf(wxT("Error creating file %s"), filename); wxMessageBox(str); return -1; } studioCbs.seek(fd, 0, SEEK_END); size_t size = studioCbs.tell(fd); studioCbs.seek(fd, 0, SEEK_SET); wxFileName fname(filename); struct archive_entry *entry = archive_entry_new(); if (entry == NULL) { wxMessageBox(_("archive_entry_new error")); return -1; } archive_entry_set_pathname(entry, fname.GetFullName().ToStdString().c_str()); archive_entry_set_size(entry, size); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(zip, entry); char buffer[4096]; while (size > 0) { int bytes = studioCbs.read(buffer, 1, 4096, fd); if (bytes == 0) break; if (archive_write_data(zip, buffer, bytes) != bytes) { studioCbs.close(fd); wxMessageBox(_("archive_write_data error")); return -1; } } studioCbs.close(fd); archive_entry_free(entry); return 0; }
Status Carver::compress(const std::set<boost::filesystem::path>& paths) { auto arch = archive_write_new(); if (arch == nullptr) { return Status(1, "Failed to create tar archive"); } // Zipping doesn't seem to be working currently // archive_write_set_format_zip(arch); archive_write_set_format_pax_restricted(arch); auto ret = archive_write_open_filename(arch, archivePath_.string().c_str()); if (ret == ARCHIVE_FATAL) { archive_write_free(arch); return Status(1, "Failed to open tar archive for writing"); } for (const auto& f : paths) { PlatformFile pFile(f.string(), PF_OPEN_EXISTING | PF_READ); auto entry = archive_entry_new(); archive_entry_set_pathname(entry, f.leaf().string().c_str()); archive_entry_set_size(entry, pFile.size()); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); // archive_entry_set_atime(); // archive_entry_set_ctime(); // archive_entry_set_mtime(); archive_write_header(arch, entry); // TODO: Chunking or a max file size. std::ifstream in(f.string(), std::ios::binary); std::stringstream buffer; buffer << in.rdbuf(); archive_write_data(arch, buffer.str().c_str(), buffer.str().size()); in.close(); archive_entry_free(entry); } archive_write_free(arch); PlatformFile archFile(archivePath_.string(), PF_OPEN_EXISTING | PF_READ); updateCarveValue(carveGuid_, "size", std::to_string(archFile.size())); updateCarveValue( carveGuid_, "sha256", hashFromFile(HashType::HASH_TYPE_SHA256, archivePath_.string())); return Status(0, "Ok"); };
/* * Mock up a fake header. */ static int archive_read_format_raw_read_header(struct archive_read *a, struct archive_entry *entry) { struct raw_info *info; info = (struct raw_info *)(a->format->data); if (info->end_of_file) return (ARCHIVE_EOF); a->archive.archive_format = ARCHIVE_FORMAT_RAW; a->archive.archive_format_name = "raw"; archive_entry_set_pathname(entry, "data"); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); /* I'm deliberately leaving most fields unset here. */ return (ARCHIVE_OK); }
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 }
void Package::pack() { struct archive *a; char buf[8192]; std::string package_base_filename(mManifest.packageName() + "-" + std::to_string(mManifest.packageVersion()) + "-" + mManifest.targetArch()); std::string tbz2_path = mWorkDir.string() + "/" + package_base_filename + ".rps"; a = archive_write_new(); archive_write_add_filter_bzip2(a); archive_write_set_format_pax_restricted(a); archive_write_open_filename(a, tbz2_path.c_str()); struct stat st; struct archive_entry *entry; for (auto &f: mManifest.files()) { std::string source = mUnpackedDir.string() + std::string("/data/") + f.name(); std::string dest = std::string("data/") + f.name(); stat(source.c_str(), &st); entry = archive_entry_new(); archive_entry_set_pathname(entry, dest.c_str()); 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); int fd = open(source.c_str(), O_RDONLY); int len = read(fd, buf, sizeof(buf)); while (len > 0) { archive_write_data(a, buf, len); len = read(fd, buf, sizeof(buf)); } close(fd); archive_entry_free(entry); } archive_write_close(a); archive_write_free(a); }
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); }
static void record_entry(db_writer_t *writer, const char *root, const char *entry) { time_t now = time(NULL); char entry_path[PATH_MAX]; snprintf(entry_path, PATH_MAX, "%s/%s", root, entry); archive_entry_set_pathname(writer->entry, entry_path); archive_entry_set_filetype(writer->entry, AE_IFREG); archive_entry_set_size(writer->entry, writer->buf.len); archive_entry_set_perm(writer->entry, 0644); archive_entry_set_ctime(writer->entry, now, 0); archive_entry_set_mtime(writer->entry, now, 0); archive_entry_set_atime(writer->entry, now, 0); archive_write_header(writer->archive, writer->entry); archive_write_data(writer->archive, writer->buf.data, writer->buf.len); archive_entry_clear(writer->entry); buffer_clear(&writer->buf); }
static void fill_archive_entry(struct archive * a, struct archive_entry * entry, rpmfi fi) { archive_entry_clear(entry); char * filename = rstrscat(NULL, ".", rpmfiDN(fi), rpmfiBN(fi), NULL); archive_entry_copy_pathname(entry, filename); _free(filename); archive_entry_set_size(entry, rpmfiFSize(fi)); rpm_mode_t mode = rpmfiFMode(fi); archive_entry_set_filetype(entry, mode & S_IFMT); archive_entry_set_perm(entry, mode); archive_entry_set_uname(entry, rpmfiFUser(fi)); archive_entry_set_gname(entry, rpmfiFGroup(fi)); archive_entry_set_rdev(entry, rpmfiFRdev(fi)); archive_entry_set_mtime(entry, rpmfiFMtime(fi), 0); if (S_ISLNK(mode)) archive_entry_set_symlink(entry, rpmfiFLink(fi)); }
int test2_with_write_callbacks() { struct archive *a = archive_write_new(); archive_write_set_format_zip(a); int ret = archive_write_open(a, NULL, myopen, mywrite, myclose); // add a file struct archive_entry *entry = archive_entry_new(); archive_entry_set_pathname(entry, "123.d/hello.txt"); archive_entry_set_size(entry, 5); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); ret = archive_write_header(a, entry); fprintf(stderr, "archive_write_header: ret=%d\n", ret); la_ssize_t n = archive_write_data(a, "world", 5); fprintf(stderr, "archive_write_data: n=%d, error=%s\n", n, archive_error_string(a)); archive_entry_free(entry); ret = archive_write_free(a); fprintf(stderr, "archive_write_free: ret=%d, error=%s\n", ret, archive_error_string(a)); }
static int extract_db_file(alpm_handle_t *handle, struct archive *archive, struct archive_entry *entry, alpm_pkg_t *newpkg, const char *entryname) { char filename[PATH_MAX]; /* the actual file we're extracting */ const char *dbfile = NULL; if(strcmp(entryname, ".INSTALL") == 0) { dbfile = "install"; } else if(strcmp(entryname, ".CHANGELOG") == 0) { dbfile = "changelog"; } else if(strcmp(entryname, ".MTREE") == 0) { dbfile = "mtree"; } else if(*entryname == '.') { /* reserve all files starting with '.' for future possibilities */ _alpm_log(handle, ALPM_LOG_DEBUG, "skipping extraction of '%s'\n", entryname); archive_read_data_skip(archive); return 0; } archive_entry_set_perm(entry, 0644); snprintf(filename, PATH_MAX, "%s%s-%s/%s", _alpm_db_path(handle->db_local), newpkg->name, newpkg->version, dbfile); return perform_extraction(handle, archive, entry, filename); }
// This function will create a gzipped tar file (outname) // which contains the files matched by the function pattern_match. // Pattern_match should return 0 on no match and 1 on match. int create_archive(char *outname, char *path, const std::vector<std::string> &filenames) { struct archive *a; struct archive_entry *entry; struct stat st; char buff[BUFFER_SIZE]; int len; int fd; a = archive_write_new(); archive_write_set_compression_gzip(a); archive_write_set_format_pax_restricted(a); archive_write_open_filename(a, outname); for (unsigned int i=0;i<filenames.size();i++) { stat(filenames[i].c_str(), &st); entry = archive_entry_new(); archive_entry_set_pathname(entry, filenames[i].c_str()); 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(filenames[i].c_str(), 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_write_close(a); archive_write_finish(a); return 0; }
int packing_append_file_attr(struct packing *pack, const char *filepath, const char *newpath, const char *uname, const char *gname, mode_t perm, u_long fflags) { int fd; int retcode = EPKG_OK; int ret; time_t source_time; struct stat st; struct archive_entry *entry, *sparse_entry; bool unset_timestamp; const char *source_date_epoch; char buf[32768]; int len; entry = archive_entry_new(); archive_entry_copy_sourcepath(entry, filepath); pkg_debug(2, "Packing file '%s'", filepath); if (lstat(filepath, &st) != 0) { pkg_emit_errno("lstat", filepath); retcode = EPKG_FATAL; goto cleanup; } ret = archive_read_disk_entry_from_file(pack->aread, entry, -1, &st); if (ret != ARCHIVE_OK) { pkg_emit_error("%s: %s", filepath, archive_error_string(pack->aread)); retcode = EPKG_FATAL; goto cleanup; } if (newpath != NULL) archive_entry_set_pathname(entry, newpath); if (archive_entry_filetype(entry) != AE_IFREG) { archive_entry_set_size(entry, 0); } if (uname != NULL && uname[0] != '\0') { archive_entry_set_uname(entry, uname); } if (gname != NULL && gname[0] != '\0') { archive_entry_set_gname(entry, gname); } if (fflags > 0) archive_entry_set_fflags(entry, fflags, 0); if (perm != 0) archive_entry_set_perm(entry, perm); unset_timestamp = pkg_object_bool(pkg_config_get("UNSET_TIMESTAMP")); if (unset_timestamp) { archive_entry_unset_atime(entry); archive_entry_unset_ctime(entry); archive_entry_unset_mtime(entry); archive_entry_unset_birthtime(entry); } if ((source_date_epoch = getenv("SOURCE_DATE_EPOCH")) != NULL) { if (source_date_epoch[strspn(source_date_epoch, "0123456789")] != '\0') { pkg_emit_error("Bad environment variable " "SOURCE_DATE_EPOCH: %s", source_date_epoch); retcode = EPKG_FATAL; goto cleanup; } source_time = strtoll(source_date_epoch, NULL, 10); archive_entry_set_atime(entry, source_time, 0); archive_entry_set_ctime(entry, source_time, 0); archive_entry_set_mtime(entry, source_time, 0); archive_entry_set_birthtime(entry, source_time, 0); } archive_entry_linkify(pack->resolver, &entry, &sparse_entry); if (sparse_entry != NULL && entry == NULL) entry = sparse_entry; archive_write_header(pack->awrite, entry); if (archive_entry_size(entry) <= 0) goto cleanup; if ((fd = open(filepath, O_RDONLY)) < 0) { pkg_emit_errno("open", filepath); retcode = EPKG_FATAL; goto cleanup; } while ((len = read(fd, buf, sizeof(buf))) > 0) { if (archive_write_data(pack->awrite, buf, len) == -1) { pkg_emit_errno("archive_write_data", "archive write error"); retcode = EPKG_FATAL; break; } } if (len == -1) { pkg_emit_errno("read", "file read error"); retcode = EPKG_FATAL; } close(fd); cleanup: archive_entry_free(entry); return (retcode); }
int pkg_update(const char *name, const char *packagesite, bool force) { char url[MAXPATHLEN]; struct archive *a = NULL; struct archive_entry *ae = NULL; char repofile[MAXPATHLEN]; char repofile_unchecked[MAXPATHLEN]; char tmp[MAXPATHLEN]; const char *dbdir = NULL; const char *repokey; unsigned char *sig = NULL; int siglen = 0; int fd, rc = EPKG_FATAL, ret; struct stat st; time_t t = 0; sqlite3 *sqlite; char *archreq = NULL; const char *myarch; int64_t res; const char *tmpdir; snprintf(url, MAXPATHLEN, "%s/repo.txz", packagesite); tmpdir = getenv("TMPDIR"); if (tmpdir == NULL) tmpdir = "/tmp"; strlcpy(tmp, tmpdir, sizeof(tmp)); strlcat(tmp, "/repo.txz.XXXXXX", sizeof(tmp)); fd = mkstemp(tmp); if (fd == -1) { pkg_emit_error("Could not create temporary file %s, " "aborting update.\n", tmp); return (EPKG_FATAL); } if (pkg_config_string(PKG_CONFIG_DBDIR, &dbdir) != EPKG_OK) { pkg_emit_error("Cant get dbdir config entry"); return (EPKG_FATAL); } snprintf(repofile, sizeof(repofile), "%s/%s.sqlite", dbdir, name); if (force) t = 0; /* Always fetch */ else { if (stat(repofile, &st) != -1) { t = st.st_mtime; /* add 1 minute to the timestamp because * repo.sqlite is always newer than repo.txz, * 1 minute should be enough. */ t += 60; } } rc = pkg_fetch_file_to_fd(url, fd, t); close(fd); if (rc != EPKG_OK) { goto cleanup; } if (eaccess(repofile, W_OK) == -1) { pkg_emit_error("Insufficient privilege to update %s\n", repofile); rc = EPKG_ENOACCESS; goto cleanup; } a = archive_read_new(); archive_read_support_compression_all(a); archive_read_support_format_tar(a); archive_read_open_filename(a, tmp, 4096); while (archive_read_next_header(a, &ae) == ARCHIVE_OK) { if (strcmp(archive_entry_pathname(ae), "repo.sqlite") == 0) { snprintf(repofile_unchecked, sizeof(repofile_unchecked), "%s.unchecked", repofile); archive_entry_set_pathname(ae, repofile_unchecked); /* * The repo should be owned by root and not writable */ archive_entry_set_uid(ae, 0); archive_entry_set_gid(ae, 0); archive_entry_set_perm(ae, 0644); archive_read_extract(a, ae, EXTRACT_ARCHIVE_FLAGS); } if (strcmp(archive_entry_pathname(ae), "signature") == 0) { siglen = archive_entry_size(ae); sig = malloc(siglen); archive_read_data(a, sig, siglen); } } if (pkg_config_string(PKG_CONFIG_REPOKEY, &repokey) != EPKG_OK) { free(sig); return (EPKG_FATAL); } if (repokey != NULL) { if (sig != NULL) { ret = rsa_verify(repofile_unchecked, repokey, sig, siglen - 1); if (ret != EPKG_OK) { pkg_emit_error("Invalid signature, " "removing repository.\n"); unlink(repofile_unchecked); free(sig); rc = EPKG_FATAL; goto cleanup; } free(sig); } else { pkg_emit_error("No signature found in the repository. " "Can not validate against %s key.", repokey); rc = EPKG_FATAL; unlink(repofile_unchecked); goto cleanup; } } /* check is the repository is for valid architecture */ sqlite3_initialize(); if (sqlite3_open(repofile_unchecked, &sqlite) != SQLITE_OK) { unlink(repofile_unchecked); pkg_emit_error("Corrupted repository"); rc = EPKG_FATAL; goto cleanup; } pkg_config_string(PKG_CONFIG_ABI, &myarch); archreq = sqlite3_mprintf("select count(arch) from packages " "where arch not GLOB '%q'", myarch); if (get_pragma(sqlite, archreq, &res) != EPKG_OK) { sqlite3_free(archreq); pkg_emit_error("Unable to query repository"); rc = EPKG_FATAL; sqlite3_close(sqlite); goto cleanup; } if (res > 0) { pkg_emit_error("At least one of the packages provided by" "the repository is not compatible with your abi: %s", myarch); rc = EPKG_FATAL; sqlite3_close(sqlite); goto cleanup; } sqlite3_close(sqlite); sqlite3_shutdown(); if (rename(repofile_unchecked, repofile) != 0) { pkg_emit_errno("rename", ""); rc = EPKG_FATAL; goto cleanup; } if ((rc = remote_add_indexes(name)) != EPKG_OK) goto cleanup; rc = EPKG_OK; cleanup: if (a != NULL) archive_read_finish(a); (void)unlink(tmp); return (rc); }
//******************************************************************************************************** void write_archive(const char* const outname, char** filename, int nFiles) { // Adapted from // https://github.com/libarchive/libarchive/wiki/Examples#A_Basic_Write_Example struct archive *a; struct archive_entry *entry; struct stat st; int err; char buff[8192]; int len; FILE* fd; //printf("Writing outname='%s'\n",outname); a = archive_write_new(); //Constructs the archive in memory? If so, may be problematic for large archives. //For tar.gz: // archive_write_add_filter_gzip(a); // archive_write_set_format_pax_restricted(a); // Note 1 //For .zip: archive_write_set_format_zip(a); archive_write_open_filename(a, outname); entry = archive_entry_new(); for (int i=0;i<nFiles;i++){ //printf("Compressing filename='%s'... ",filename[i]); //Write the header #ifdef __WIN32 LARGE_INTEGER filesize_union; HANDLE hFile = CreateFile(filename[i], GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) continue; if(!GetFileSizeEx(hFile, &filesize_union)) { fprintf(stderr, "CRITICAL ERROR in write_archive(): GetFileSizeEx failed."); exit(EXIT_FAILURE); } CloseHandle(hFile); LONGLONG filesize = filesize_union.QuadPart; #else off_t filesize = 0; err = stat(filename[i], &st); //printf("stat reported err=%i\n", err); if(err != 0) continue; filesize = st.st_size; #endif archive_entry_set_pathname(entry, filename[i]); archive_entry_set_size(entry, filesize); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); archive_write_header(a, entry); //Write the data int len_total = 0; fd = fopen(filename[i], "rb"); len = fread(buff, 1,sizeof(buff), fd); len_total = len; while ( len > 0 ) { err=archive_write_data(a, buff, len); if (err < 0){ fprintf(stderr, "CRITICAL ERROR in write_archive(): When writing file, got err=%i\n",err); fprintf(stderr, "CRITICAL ERROR in write_archive(): %s\n",archive_error_string(a)); exit(EXIT_FAILURE); } len = fread(buff, 1,sizeof(buff), fd); len_total += len; } fclose(fd); archive_entry_clear(entry); //printf("Wrote file '%s', len_total = %i, filesize = %i\n", filename[i], len_total, filesize); if (len_total != filesize) { fprintf(stderr, "CRITICAL ERROR in write_archive(): When writing file '%s', got len_total = %i but filesize = %i\n", filename[i], len_total, filesize); } } archive_entry_free(entry); //printf("Complete!\n"); archive_write_close(a); // Note 4 archive_write_free(a); // called archive_write_finish() in old versions of libarchive }
RESULTCODE ArchiveProcessorImpl::Compress() { const char* tpath = GetArchivePath(); if(tpath==NULL) { return AG_FAILURE; } struct archive *a; struct archive_entry *entry; struct stat st; char buff[AUGE_BUFFER_MAX]; int len; int fd; a = archive_write_new(); if(a==NULL) { return AG_FAILURE; } //archive_write_add_filter_gzip(a); archive_write_set_format_zip(a); archive_write_set_format_pax_restricted(a); archive_write_open_filename(a,tpath); const char* fpath = NULL; char name[AUGE_NAME_MAX]; char ext[AUGE_EXT_MAX]; char fname[AUGE_NAME_MAX]; std::vector<std::string>::iterator iter; for(iter=m_paths.begin(); iter!=m_paths.end();iter++) { fpath = (*iter).c_str(); memset(fname,0,AUGE_NAME_MAX); memset(name,0,AUGE_NAME_MAX); memset(name,0,AUGE_EXT_MAX); auge_split_path(fpath, NULL, NULL, name, ext); auge_make_path(fname, NULL, NULL, name, ext); if(strlen(fname)==0) { continue; } stat(fpath,&st); entry = archive_entry_new(); archive_entry_set_pathname(entry,fname); 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(fpath,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); } archive_write_close(a); archive_write_free(a); return AG_SUCCESS; }
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); }
int main(int argc, char **argv) { int i; unsigned int version = 0, help = 0; char cpiotmpfile[] = CPIOTMPFILE; struct archive *archive; struct archive_entry *entry; struct stat st; char buff[64]; int len, fdfile, fdarchive; DIR * dir; struct dirent * ent; char * filename, * path; off_t pathlength = 0; int8_t rc = EXIT_FAILURE; /* get command line options */ while ((i = getopt_long(argc, argv, optstring, options_long, NULL)) != -1) switch (i) { case 'h': help++; break; case 'V': version++; break; } if (version > 0) printf("%s: %s v%s (compiled: " __DATE__ ", " __TIME__ ")\n", argv[0], PROGNAME, VERSION); if (help > 0) fprintf(stderr, "usage: %s [-h|--help] [-V|--version]\n", argv[0]); if (version > 0 || help > 0) return EXIT_SUCCESS; if ((fdarchive = mkstemp(cpiotmpfile)) < 0) { perror("mkstemp() failed"); goto out10; } if ((archive = archive_write_new()) == NULL) { fprintf(stderr, "archive_write_new() failed.\n"); goto out10; } if (archive_write_set_format_cpio_newc(archive) != ARCHIVE_OK) { fprintf(stderr, "archive_write_set_format_cpio_newc() failed.\n"); goto out10; } if (archive_write_open_fd(archive, fdarchive) != ARCHIVE_OK) { fprintf(stderr, "archive_write_open_fd() failed.\n"); goto out10; } while (1) { path = strdup(CHALLENGEDIR + 1); if (strstr(path + pathlength, "/") == NULL) break; *strstr(path + pathlength, "/") = 0; pathlength = strlen(path) + 1; if (add_dir(archive, path) < 0) { fprintf(stderr, "add_dir() failed"); goto out10; } free(path); } if ((dir = opendir(CHALLENGEDIR)) == NULL) { perror("opendir() failed"); goto out10; } while ((ent = readdir(dir)) != NULL) { filename = malloc(sizeof(CHALLENGEDIR) + strlen(ent->d_name) + 1); sprintf(filename, CHALLENGEDIR "%s", ent->d_name); if (stat(filename, &st) < 0) { perror("stat() failed"); goto out10; } if (S_ISREG(st.st_mode)) { if ((entry = archive_entry_new()) == NULL) { fprintf(stderr, "archive_entry_new() failed.\n"); goto out10; } /* these do not return exit code */ archive_entry_set_pathname(entry, filename + 1); archive_entry_set_size(entry, st.st_size); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_perm(entry, 0644); if (archive_write_header(archive, entry) != ARCHIVE_OK) { fprintf(stderr, "archive_write_header() failed"); goto out10; } if ((fdfile = open(filename, O_RDONLY)) < 0) { perror("open() failed"); goto out10; } if ((len = read(fdfile, buff, sizeof(buff))) < 0) { perror("read() failed"); goto out10; } while (len > 0) { if (archive_write_data(archive, buff, len) < 0) { fprintf(stderr, "archive_write_data() failed"); goto out10; } if ((len = read(fdfile, buff, sizeof(buff))) < 0) { perror("read() failed"); goto out10; } } if (close(fdfile) < 0) { perror("close() failed"); goto out10; } archive_entry_free(entry); } free(filename); } if (closedir(dir) < 0) { perror("closedir() failed"); goto out10; } if (archive_write_close(archive) != ARCHIVE_OK) { fprintf(stderr, "archive_write_close() failed"); goto out10; } if (archive_write_free(archive) != ARCHIVE_OK) { fprintf(stderr, "archive_write_free() failed"); goto out10; } if (access(CPIOFILE, F_OK) == 0 && unlink(CPIOFILE) < 0) { perror("unkink() failed"); goto out10; } if (rename(cpiotmpfile, CPIOFILE) < 0) { perror("rename() failed"); goto out10; } rc = EXIT_SUCCESS; out10: if (access(cpiotmpfile, F_OK) == 0) unlink(cpiotmpfile); return rc; }