Standardize on the vi editing directives being on the first line.
[oweals/busybox.git] / archival / libunarchive / data_extract_all.c
1 /* vi: set sw=4 ts=4: */
2 /*
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.
7  *
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.
12  *
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.
16  */
17
18 #include <sys/types.h>
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <utime.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27
28 #include "libbb.h"
29 #include "unarchive.h"
30
31 void data_extract_all(archive_handle_t *archive_handle)
32 {
33         file_header_t *file_header = archive_handle->file_header;
34         int dst_fd;
35         int res;
36
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);
40                 free(name);
41         }
42
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");
48                 }
49         }
50         else if (archive_handle->flags & ARCHIVE_EXTRACT_NEWER) {
51                 /* Remove the existing entry if its older than the extracted entry */
52                 struct stat statbuf;
53                 if (lstat(file_header->name, &statbuf) == -1) {
54                         if (errno != ENOENT) {
55                                 bb_perror_msg_and_die("Couldnt stat old file");
56                         }
57                 }
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);
61                         }
62                         data_skip(archive_handle);
63                         return;
64                 }
65                 else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) {
66                         bb_perror_msg_and_die("Couldnt remove old file %s", file_header->name);
67                 }
68         }
69
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)) {
73                 /* hard link */
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");
77                 }
78         } else {
79                 /* Create the filesystem entry */
80                 switch(file_header->mode & S_IFMT) {
81                         case S_IFREG: {
82                                 /* Regular file */
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);
85                                 close(dst_fd);
86                                 break;
87                                 }
88                         case S_IFDIR:
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);
92                                 }
93                                 break;
94                         case S_IFLNK:
95                                 /* Symlink */
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);
99                                 }
100                                 break;
101                         case S_IFSOCK:
102                         case S_IFBLK:
103                         case S_IFCHR:
104                         case S_IFIFO:
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);
108                                 }
109                                 break;
110                         default:
111                                 bb_error_msg_and_die("Unrecognised file type");
112                 }
113         }
114
115         if (!(archive_handle->flags & ARCHIVE_NOPRESERVE_OWN)) {
116                 lchown(file_header->name, file_header->uid, file_header->gid);
117         }
118         if (!(archive_handle->flags & ARCHIVE_NOPRESERVE_PERM) &&
119                  (file_header->mode & S_IFMT) != S_IFLNK)
120         {
121                 chmod(file_header->name, file_header->mode);
122         }
123
124         if (archive_handle->flags & ARCHIVE_PRESERVE_DATE) {
125                 struct utimbuf t;
126                 t.actime = t.modtime = file_header->mtime;
127                 utime(file_header->name, &t);
128         }
129 }