1 /* vi: set sw=4 ts=4: */
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #include <sys/types.h>
29 #include "unarchive.h"
31 void data_extract_all(archive_handle_t *archive_handle)
33 file_header_t *file_header = archive_handle->file_header;
37 if (archive_handle->flags & ARCHIVE_CREATE_LEADING_DIRS) {
38 char *name = bb_xstrdup(file_header->name);
39 bb_make_directory (dirname(name), -1, FILEUTILS_RECUR);
43 /* Check if the file already exists */
44 if (archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) {
45 /* Remove the existing entry if it exists */
46 if (((file_header->mode & S_IFMT) != S_IFDIR) && (unlink(file_header->name) == -1) && (errno != ENOENT)) {
47 bb_perror_msg_and_die("Couldnt remove old file");
50 else if (archive_handle->flags & ARCHIVE_EXTRACT_NEWER) {
51 /* Remove the existing entry if its older than the extracted entry */
53 if (lstat(file_header->name, &statbuf) == -1) {
54 if (errno != ENOENT) {
55 bb_perror_msg_and_die("Couldnt stat old file");
58 else if (statbuf.st_mtime <= file_header->mtime) {
59 if (!(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
60 bb_error_msg("%s not created: newer or same age file exists", file_header->name);
62 data_skip(archive_handle);
65 else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) {
66 bb_perror_msg_and_die("Couldnt remove old file %s", file_header->name);
70 /* Handle hard links separately
71 * We identified hard links as regular files of size 0 with a symlink */
72 if (S_ISREG(file_header->mode) && (file_header->link_name) && (file_header->size == 0)) {
74 res = link(file_header->link_name, file_header->name);
75 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
76 bb_perror_msg("Couldnt create hard link");
79 /* Create the filesystem entry */
80 switch(file_header->mode & S_IFMT) {
83 dst_fd = bb_xopen(file_header->name, O_WRONLY | O_CREAT | O_EXCL);
84 bb_copyfd_size(archive_handle->src_fd, dst_fd, file_header->size);
89 res = mkdir(file_header->name, file_header->mode);
90 if ((errno != EISDIR) && (res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
91 bb_perror_msg("extract_archive: %s", file_header->name);
96 res = symlink(file_header->link_name, file_header->name);
97 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
98 bb_perror_msg("Cannot create symlink from %s to '%s'", file_header->name, file_header->link_name);
105 res = mknod(file_header->name, file_header->mode, file_header->device);
106 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
107 bb_perror_msg("Cannot create node %s", file_header->name);
111 bb_error_msg_and_die("Unrecognised file type");
115 if (!(archive_handle->flags & ARCHIVE_NOPRESERVE_OWN)) {
116 lchown(file_header->name, file_header->uid, file_header->gid);
118 if (!(archive_handle->flags & ARCHIVE_NOPRESERVE_PERM) &&
119 (file_header->mode & S_IFMT) != S_IFLNK)
121 chmod(file_header->name, file_header->mode);
124 if (archive_handle->flags & ARCHIVE_PRESERVE_DATE) {
126 t.actime = t.modtime = file_header->mtime;
127 utime(file_header->name, &t);