1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 #include "unarchive.h"
19 void data_extract_all(archive_handle_t *archive_handle)
21 file_header_t *file_header = archive_handle->file_header;
25 if (archive_handle->flags & ARCHIVE_CREATE_LEADING_DIRS) {
26 char *name = bb_xstrdup(file_header->name);
27 bb_make_directory (dirname(name), -1, FILEUTILS_RECUR);
31 /* Check if the file already exists */
32 if (archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) {
33 /* Remove the existing entry if it exists */
34 if (((file_header->mode & S_IFMT) != S_IFDIR) && (unlink(file_header->name) == -1) && (errno != ENOENT)) {
35 bb_perror_msg_and_die("Couldnt remove old file");
38 else if (archive_handle->flags & ARCHIVE_EXTRACT_NEWER) {
39 /* Remove the existing entry if its older than the extracted entry */
41 if (lstat(file_header->name, &statbuf) == -1) {
42 if (errno != ENOENT) {
43 bb_perror_msg_and_die("Couldnt stat old file");
46 else if (statbuf.st_mtime <= file_header->mtime) {
47 if (!(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
48 bb_error_msg("%s not created: newer or same age file exists", file_header->name);
50 data_skip(archive_handle);
53 else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) {
54 bb_perror_msg_and_die("Couldnt remove old file %s", file_header->name);
58 /* Handle hard links separately
59 * We identified hard links as regular files of size 0 with a symlink */
60 if (S_ISREG(file_header->mode) && (file_header->link_name) && (file_header->size == 0)) {
62 res = link(file_header->link_name, file_header->name);
63 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
64 bb_perror_msg("Couldnt create hard link");
67 /* Create the filesystem entry */
68 switch(file_header->mode & S_IFMT) {
71 dst_fd = bb_xopen(file_header->name, O_WRONLY | O_CREAT | O_EXCL);
72 bb_copyfd_size(archive_handle->src_fd, dst_fd, file_header->size);
77 res = mkdir(file_header->name, file_header->mode);
78 if ((errno != EISDIR) && (res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
79 bb_perror_msg("extract_archive: %s", file_header->name);
84 res = symlink(file_header->link_name, file_header->name);
85 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
86 bb_perror_msg("Cannot create symlink from %s to '%s'", file_header->name, file_header->link_name);
93 res = mknod(file_header->name, file_header->mode, file_header->device);
94 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
95 bb_perror_msg("Cannot create node %s", file_header->name);
99 bb_error_msg_and_die("Unrecognised file type");
103 if (!(archive_handle->flags & ARCHIVE_NOPRESERVE_OWN)) {
104 lchown(file_header->name, file_header->uid, file_header->gid);
106 if (!(archive_handle->flags & ARCHIVE_NOPRESERVE_PERM) &&
107 (file_header->mode & S_IFMT) != S_IFLNK)
109 chmod(file_header->name, file_header->mode);
112 if (archive_handle->flags & ARCHIVE_PRESERVE_DATE) {
114 t.actime = t.modtime = file_header->mtime;
115 utime(file_header->name, &t);