/* Transform all path components of the given entry. This includes hardlink and * symlink paths as well as the destination path. * * Returns 0 on success, 1 where the file does not need to be extracted and <0 * on error. */ static int transform_all_paths(struct archive_entry *entry, const char *dest) { char *path; const char *filename; int r; r = transform_dest_path(entry, dest); if (r) return r; /* Next transform hardlink and symlink destinations. */ filename = archive_entry_hardlink(entry); if (filename) { /* Apply the same transform to the hardlink path as was applied * to the destination path. */ path = join_paths(dest, filename); if (!path) { opkg_msg(ERROR, "Not extracting '%s': Hardlink to nowhere.\n", archive_entry_pathname(entry)); return 1; } archive_entry_set_hardlink(entry, path); free(path); } /* Currently no transform to perform for symlinks. */ return 0; }
static void record_hardlink(struct cpio *cpio, struct archive_entry *entry) { struct links_entry *le; dev_t dev; ino_t ino; dev = archive_entry_dev(entry); ino = archive_entry_ino(entry); /* * First look in the list of multiply-linked files. If we've * already dumped it, convert this entry to a hard link entry. */ for (le = cpio->links_head; le; le = le->next) { if (le->dev == dev && le->ino == ino) { archive_entry_set_hardlink(entry, le->name); if (--le->links <= 0) { if (le->previous != NULL) le->previous->next = le->next; if (le->next != NULL) le->next->previous = le->previous; if (cpio->links_head == le) cpio->links_head = le->next; free(le); } return; } } le = (struct links_entry *)malloc(sizeof(struct links_entry)); if (le == NULL) __archive_errx(1, "Out of memory adding file to list"); if (cpio->links_head != NULL) cpio->links_head->previous = le; le->next = cpio->links_head; le->previous = NULL; cpio->links_head = le; le->dev = dev; le->ino = ino; le->links = archive_entry_nlink(entry) - 1; le->name = strdup(archive_entry_pathname(entry)); if (le->name == NULL) __archive_errx(1, "Out of memory adding file to list"); }
static int process_package(rpmts ts, char * filename) { FD_t fdi; FD_t gzdi; Header h; int rc = 0; char * rpmio_flags = NULL; struct archive *a; struct archive_entry *entry; if (!strcmp(filename, "-")) { fdi = fdDup(STDIN_FILENO); } else { fdi = Fopen(filename, "r.ufdio"); } if (Ferror(fdi)) { fprintf(stderr, "rpm2archive: %s: %s\n", filename, Fstrerror(fdi)); exit(EXIT_FAILURE); } rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h); switch (rc) { case RPMRC_OK: case RPMRC_NOKEY: case RPMRC_NOTTRUSTED: break; case RPMRC_NOTFOUND: fprintf(stderr, _("argument is not an RPM package\n")); exit(EXIT_FAILURE); break; case RPMRC_FAIL: default: fprintf(stderr, _("error reading header from package\n")); exit(EXIT_FAILURE); break; } /* Retrieve payload size and compression type. */ { const char *compr = headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR); rpmio_flags = rstrscat(NULL, "r.", compr ? compr : "gzip", NULL); } gzdi = Fdopen(fdi, rpmio_flags); /* XXX gzdi == fdi */ free(rpmio_flags); if (gzdi == NULL) { fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi)); exit(EXIT_FAILURE); } rpmfiles files = rpmfilesNew(NULL, h, 0, RPMFI_KEEPHEADER); rpmfi fi = rpmfiNewArchiveReader(gzdi, files, RPMFI_ITER_READ_ARCHIVE_CONTENT_FIRST); /* create archive */ a = archive_write_new(); archive_write_add_filter_gzip(a); archive_write_set_format_pax_restricted(a); if (!strcmp(filename, "-")) { if (isatty(STDOUT_FILENO)) { fprintf(stderr, "Error: refusing to output archive data to a terminal.\n"); exit(EXIT_FAILURE); } archive_write_open_fd(a, STDOUT_FILENO); } else { char * outname = rstrscat(NULL, filename, ".tgz", NULL); archive_write_open_filename(a, outname); _free(outname); // XXX error handling } entry = archive_entry_new(); char * buf = xmalloc(BUFSIZE); char * hardlink = NULL; rc = 0; while (rc >= 0) { rc = rpmfiNext(fi); if (rc == RPMERR_ITER_END) { break; } rpm_mode_t mode = rpmfiFMode(fi); int nlink = rpmfiFNlink(fi); fill_archive_entry(a, entry, fi); if (nlink > 1) { if (rpmfiArchiveHasContent(fi)) { _free(hardlink); hardlink = rstrscat(NULL, ".", rpmfiFN(fi), NULL); } else { archive_entry_set_hardlink(entry, hardlink); } } archive_write_header(a, entry); if (S_ISREG(mode) && (nlink == 1 || rpmfiArchiveHasContent(fi))) { write_file_content(a, buf, fi); } } /* End of iteration is not an error */ if (rc == RPMERR_ITER_END) { rc = 0; } _free(hardlink); Fclose(gzdi); /* XXX gzdi == fdi */ archive_entry_free(entry); archive_write_close(a); archive_write_free(a); buf = _free(buf); rpmfilesFree(files); rpmfiFree(fi); headerFree(h); return rc; }
static int entry_to_archive(struct cpio *cpio, struct archive_entry *entry) { const char *destpath = archive_entry_pathname(entry); const char *srcpath = archive_entry_sourcepath(entry); int fd = -1; ssize_t bytes_read; int r; /* Print out the destination name to the user. */ if (cpio->verbose) fprintf(stderr,"%s", destpath); /* * Option_link only makes sense in pass mode and for * regular files. Also note: if a link operation fails * because of cross-device restrictions, we'll fall back * to copy mode for that entry. * * TODO: Test other cpio implementations to see if they * hard-link anything other than regular files here. */ if (cpio->option_link && archive_entry_filetype(entry) == AE_IFREG) { struct archive_entry *t; /* Save the original entry in case we need it later. */ t = archive_entry_clone(entry); if (t == NULL) lafe_errc(1, ENOMEM, "Can't create link"); /* Note: link(2) doesn't create parent directories, * so we use archive_write_header() instead as a * convenience. */ archive_entry_set_hardlink(t, srcpath); /* This is a straight link that carries no data. */ archive_entry_set_size(t, 0); r = archive_write_header(cpio->archive, t); archive_entry_free(t); if (r != ARCHIVE_OK) lafe_warnc(archive_errno(cpio->archive), "%s", archive_error_string(cpio->archive)); if (r == ARCHIVE_FATAL) exit(1); #ifdef EXDEV if (r != ARCHIVE_OK && archive_errno(cpio->archive) == EXDEV) { /* Cross-device link: Just fall through and use * the original entry to copy the file over. */ lafe_warnc(0, "Copying file instead"); } else #endif return (0); } /* * Make sure we can open the file (if necessary) before * trying to write the header. */ if (archive_entry_filetype(entry) == AE_IFREG) { if (archive_entry_size(entry) > 0) { fd = open(srcpath, O_RDONLY | O_BINARY); if (fd < 0) { lafe_warnc(errno, "%s: could not open file", srcpath); goto cleanup; } } } else { archive_entry_set_size(entry, 0); } r = archive_write_header(cpio->archive, entry); if (r != ARCHIVE_OK) lafe_warnc(archive_errno(cpio->archive), "%s: %s", srcpath, archive_error_string(cpio->archive)); if (r == ARCHIVE_FATAL) exit(1); if (r >= ARCHIVE_WARN && fd >= 0) { bytes_read = read(fd, cpio->buff, cpio->buff_size); while (bytes_read > 0) { r = archive_write_data(cpio->archive, cpio->buff, bytes_read); if (r < 0) lafe_errc(1, archive_errno(cpio->archive), "%s", archive_error_string(cpio->archive)); if (r < bytes_read) { lafe_warnc(0, "Truncated write; file may have grown while being archived."); } bytes_read = read(fd, cpio->buff, cpio->buff_size); } } fd = restore_time(cpio, entry, srcpath, fd); cleanup: if (cpio->verbose) fprintf(stderr,"\n"); if (fd >= 0) close(fd); return (0); }
static int archive_read_format_iso9660_read_header(struct archive_read *a, struct archive_entry *entry) { struct iso9660 *iso9660; struct file_info *file; ssize_t bytes_read; int r; iso9660 = (struct iso9660 *)(a->format->data); if (!a->archive.archive_format) { a->archive.archive_format = ARCHIVE_FORMAT_ISO9660; a->archive.archive_format_name = "ISO9660"; } /* Get the next entry that appears after the current offset. */ r = next_entry_seek(a, iso9660, &file); if (r != ARCHIVE_OK) return (r); iso9660->entry_bytes_remaining = file->size; iso9660->entry_sparse_offset = 0; /* Offset for sparse-file-aware clients. */ /* Set up the entry structure with information about this entry. */ archive_entry_set_mode(entry, file->mode); archive_entry_set_uid(entry, file->uid); archive_entry_set_gid(entry, file->gid); archive_entry_set_nlink(entry, file->nlinks); archive_entry_set_ino(entry, file->inode); archive_entry_set_mtime(entry, file->mtime, 0); archive_entry_set_ctime(entry, file->ctime, 0); archive_entry_set_atime(entry, file->atime, 0); archive_entry_set_size(entry, iso9660->entry_bytes_remaining); archive_string_empty(&iso9660->pathname); archive_entry_set_pathname(entry, build_pathname(&iso9660->pathname, file)); if (file->symlink.s != NULL) archive_entry_copy_symlink(entry, file->symlink.s); /* If this entry points to the same data as the previous * entry, convert this into a hardlink to that entry. * But don't bother for zero-length files. */ if (file->offset == iso9660->previous_offset && file->size == iso9660->previous_size && file->size > 0) { archive_entry_set_hardlink(entry, iso9660->previous_pathname.s); iso9660->entry_bytes_remaining = 0; iso9660->entry_sparse_offset = 0; release_file(iso9660, file); return (ARCHIVE_OK); } /* If the offset is before our current position, we can't * seek backwards to extract it, so issue a warning. */ if (file->offset < iso9660->current_position) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Ignoring out-of-order file"); iso9660->entry_bytes_remaining = 0; iso9660->entry_sparse_offset = 0; release_file(iso9660, file); return (ARCHIVE_WARN); } iso9660->previous_size = file->size; iso9660->previous_offset = file->offset; archive_strcpy(&iso9660->previous_pathname, iso9660->pathname.s); /* If this is a directory, read in all of the entries right now. */ if (archive_entry_filetype(entry) == AE_IFDIR) { while (iso9660->entry_bytes_remaining > 0) { const void *block; const unsigned char *p; ssize_t step = iso9660->logical_block_size; if (step > iso9660->entry_bytes_remaining) step = iso9660->entry_bytes_remaining; bytes_read = (a->decompressor->read_ahead)(a, &block, step); if (bytes_read < step) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Failed to read full block when scanning ISO9660 directory list"); release_file(iso9660, file); return (ARCHIVE_FATAL); } if (bytes_read > step) bytes_read = step; (a->decompressor->consume)(a, bytes_read); iso9660->current_position += bytes_read; iso9660->entry_bytes_remaining -= bytes_read; for (p = (const unsigned char *)block; *p != 0 && p < (const unsigned char *)block + bytes_read; p += *p) { struct file_info *child; /* Skip '.' entry. */ if (*(p + DR_name_len_offset) == 1 && *(p + DR_name_offset) == '\0') continue; /* Skip '..' entry. */ if (*(p + DR_name_len_offset) == 1 && *(p + DR_name_offset) == '\001') continue; child = parse_file_info(iso9660, file, p); add_entry(iso9660, child); if (iso9660->seenRockridge) { a->archive.archive_format = ARCHIVE_FORMAT_ISO9660_ROCKRIDGE; a->archive.archive_format_name = "ISO9660 with Rockridge extensions"; } } } } release_file(iso9660, file); return (ARCHIVE_OK); }
static void test_linkify_tar(void) { struct archive_entry *entry, *e2; struct archive_entry_linkresolver *resolver; /* Initialize the resolver. */ assert(NULL != (resolver = archive_entry_linkresolver_new())); archive_entry_linkresolver_set_strategy(resolver, ARCHIVE_FORMAT_TAR_USTAR); /* Create an entry with only 1 link and try to linkify it. */ assert(NULL != (entry = archive_entry_new())); archive_entry_set_pathname(entry, "test1"); archive_entry_set_ino(entry, 1); archive_entry_set_dev(entry, 2); archive_entry_set_nlink(entry, 1); archive_entry_set_size(entry, 10); archive_entry_linkify(resolver, &entry, &e2); /* Shouldn't have been changed. */ assert(e2 == NULL); assertEqualInt(10, archive_entry_size(entry)); assertEqualString("test1", archive_entry_pathname(entry)); /* Now, try again with an entry that has 2 links. */ archive_entry_set_pathname(entry, "test2"); archive_entry_set_nlink(entry, 2); archive_entry_set_ino(entry, 2); archive_entry_linkify(resolver, &entry, &e2); /* Shouldn't be altered, since it wasn't seen before. */ assert(e2 == NULL); assertEqualString("test2", archive_entry_pathname(entry)); assertEqualString(NULL, archive_entry_hardlink(entry)); assertEqualInt(10, archive_entry_size(entry)); /* Match again and make sure it does get altered. */ archive_entry_linkify(resolver, &entry, &e2); assert(e2 == NULL); assertEqualString("test2", archive_entry_pathname(entry)); assertEqualString("test2", archive_entry_hardlink(entry)); assertEqualInt(0, archive_entry_size(entry)); /* Dirs should never be matched as hardlinks, regardless. */ archive_entry_set_pathname(entry, "test3"); archive_entry_set_nlink(entry, 2); archive_entry_set_filetype(entry, AE_IFDIR); archive_entry_set_ino(entry, 3); archive_entry_set_hardlink(entry, NULL); archive_entry_linkify(resolver, &entry, &e2); /* Shouldn't be altered, since it wasn't seen before. */ assert(e2 == NULL); assertEqualString("test3", archive_entry_pathname(entry)); assertEqualString(NULL, archive_entry_hardlink(entry)); /* Dir, so it shouldn't get matched. */ archive_entry_linkify(resolver, &entry, &e2); assert(e2 == NULL); assertEqualString("test3", archive_entry_pathname(entry)); assertEqualString(NULL, archive_entry_hardlink(entry)); archive_entry_free(entry); archive_entry_linkresolver_free(resolver); }
int archive_extract_all( char *arch_file, char *dest_dir, char *suffix ) { int ret, flags; char *pwd = NULL, *filename = NULL, *filename_new = NULL, *hardlink = NULL; struct archive *arch_r = NULL, *arch_w = NULL; struct archive_entry *entry = NULL; if( !arch_file ) return -1; arch_r = archive_read_new(); archive_read_support_format_all( arch_r ); archive_read_support_compression_all( arch_r ); if( archive_read_open_filename( arch_r, arch_file, 10240 ) != ARCHIVE_OK ) goto errout; if( dest_dir ) { if( util_mkdir( dest_dir ) == -1 ) { if( errno == EEXIST ) { if( access( dest_dir, R_OK | W_OK | X_OK ) == -1 ) { goto errout; } } else { goto errout; } } pwd = getcwd( NULL, 0 ); chdir( dest_dir ); } flags = ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_OWNER; arch_w = archive_write_disk_new(); archive_write_disk_set_options( arch_w, flags ); archive_write_disk_set_standard_lookup( arch_w ); while( archive_read_next_header( arch_r, &entry ) == ARCHIVE_OK ) { if( suffix ) filename = (char *)archive_entry_pathname( entry ); #ifdef DEBUG if( !filename ) filename = (char *)archive_entry_pathname( entry ); printf("extract:%s\n", filename ); #endif if( suffix && archive_entry_filetype( entry ) != AE_IFDIR ) { filename_new = util_strcat( filename, suffix, NULL ); archive_entry_set_pathname( entry, filename_new ); free( filename_new ); if( archive_entry_nlink( entry ) > 0 ) { hardlink = (char *)archive_entry_hardlink( entry ); if( hardlink ) { filename_new = util_strcat( hardlink, suffix, NULL ); archive_entry_set_hardlink( entry, filename_new ); free( filename_new ); } } } ret = archive_read_extract2( arch_r, entry, arch_w ); if( ret != ARCHIVE_OK && ret != ARCHIVE_WARN ) { goto errout; } #ifdef DEBUG if( ret != ARCHIVE_OK) { printf("ret:%d, file:%s, link:%d, size:%d, read_err:%s, write_err:%s\n", ret, filename, archive_entry_nlink(entry), archive_entry_size(entry), archive_error_string(arch_r), archive_error_string(arch_w)); } #endif } archive_read_finish( arch_r ); archive_write_finish( arch_w ); if( pwd ) { chdir( pwd ); free( pwd ); } return 0; errout: if( arch_r ) archive_read_finish( arch_r ); if( arch_w ) archive_write_finish( arch_w ); if( pwd ) { chdir( pwd ); free( pwd ); } return -1; }