summaryrefslogtreecommitdiff
path: root/archival/libarchive/data_extract_all.c (plain)
blob: 5d8e57a78e19e90078f6548d2fc4ff1c6e17f733
1/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 */
5
6#include "libbb.h"
7#include "bb_archive.h"
8
9void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
10{
11 file_header_t *file_header = archive_handle->file_header;
12 int dst_fd;
13 int res;
14 char *hard_link;
15#if ENABLE_FEATURE_TAR_LONG_OPTIONS
16 char *dst_name;
17#else
18# define dst_name (file_header->name)
19#endif
20
21#if ENABLE_FEATURE_TAR_SELINUX
22 char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
23#ifdef __BIONIC__
24 matchpathcon_init(NULL);
25#endif
26 if (!sctx)
27 sctx = archive_handle->tar__sctx[PAX_GLOBAL];
28 if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
29 setfscreatecon(sctx);
30 free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
31 archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
32 }
33#endif
34
35 /* Hard links are encoded as regular files of size 0
36 * with a nonempty link field */
37 hard_link = NULL;
38 if (S_ISREG(file_header->mode) && file_header->size == 0)
39 hard_link = file_header->link_target;
40
41#if ENABLE_FEATURE_TAR_LONG_OPTIONS
42 dst_name = file_header->name;
43 if (archive_handle->tar__strip_components) {
44 unsigned n = archive_handle->tar__strip_components;
45 do {
46 dst_name = strchr(dst_name, '/');
47 if (!dst_name || dst_name[1] == '\0') {
48 data_skip(archive_handle);
49 goto ret;
50 }
51 dst_name++;
52 /*
53 * Link target is shortened only for hardlinks:
54 * softlinks restored unchanged.
55 */
56 if (hard_link) {
57// GNU tar 1.26 does not check that we reached end of link name:
58// if "dir/hardlink" is hardlinked to "file",
59// tar xvf a.tar --strip-components=1 says:
60// tar: hardlink: Cannot hard link to '': No such file or directory
61// and continues processing. We silently skip such entries.
62 hard_link = strchr(hard_link, '/');
63 if (!hard_link || hard_link[1] == '\0') {
64 data_skip(archive_handle);
65 goto ret;
66 }
67 hard_link++;
68 }
69 } while (--n != 0);
70 }
71#endif
72
73 if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
74 char *slash = strrchr(dst_name, '/');
75 if (slash) {
76 *slash = '\0';
77 bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
78 *slash = '/';
79 }
80 }
81
82 if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
83 /* Remove the entry if it exists */
84 if (!S_ISDIR(file_header->mode)) {
85 if (hard_link) {
86 /* Ugly special case:
87 * tar cf t.tar hardlink1 hardlink2 hardlink1
88 * results in this tarball structure:
89 * hardlink1
90 * hardlink2 -> hardlink1
91 * hardlink1 -> hardlink1 <== !!!
92 */
93 if (strcmp(hard_link, dst_name) == 0)
94 goto ret;
95 }
96 /* Proceed with deleting */
97 if (unlink(dst_name) == -1
98 && errno != ENOENT
99 ) {
100 bb_perror_msg_and_die("can't remove old file %s",
101 dst_name);
102 }
103 }
104 }
105 else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
106 /* Remove the existing entry if its older than the extracted entry */
107 struct stat existing_sb;
108 if (lstat(dst_name, &existing_sb) == -1) {
109 if (errno != ENOENT) {
110 bb_perror_msg_and_die("can't stat old file");
111 }
112 }
113 else if ((time_t) existing_sb.st_mtime >= (time_t) file_header->mtime) {
114 if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
115 && !S_ISDIR(file_header->mode)
116 ) {
117 bb_error_msg("%s not created: newer or "
118 "same age file exists", dst_name);
119 }
120 data_skip(archive_handle);
121 goto ret;
122 }
123 else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
124 bb_perror_msg_and_die("can't remove old file %s",
125 dst_name);
126 }
127 }
128
129 /* Handle hard links separately */
130 if (hard_link) {
131 res = link(hard_link, dst_name);
132 if (res != 0 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
133 bb_perror_msg("can't create %slink "
134 "from %s to %s", "hard",
135 dst_name,
136 hard_link);
137 }
138 /* Hardlinks have no separate mode/ownership, skip chown/chmod */
139 goto ret;
140 }
141
142 /* Create the filesystem entry */
143 switch (file_header->mode & S_IFMT) {
144 case S_IFREG: {
145 /* Regular file */
146 char *dst_nameN;
147 int flags = O_WRONLY | O_CREAT | O_EXCL;
148 if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
149 flags = O_WRONLY | O_CREAT | O_TRUNC;
150 dst_nameN = dst_name;
151#ifdef ARCHIVE_REPLACE_VIA_RENAME
152 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
153 /* rpm-style temp file name */
154 dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
155#endif
156 dst_fd = xopen3(dst_nameN,
157 flags,
158 file_header->mode
159 );
160 bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
161 close(dst_fd);
162#ifdef ARCHIVE_REPLACE_VIA_RENAME
163 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
164 xrename(dst_nameN, dst_name);
165 free(dst_nameN);
166 }
167#endif
168 break;
169 }
170 case S_IFDIR:
171 res = mkdir(dst_name, file_header->mode);
172 if ((res == -1)
173 && (errno != EISDIR) /* btw, Linux doesn't return this */
174 && (errno != EEXIST)
175 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
176 ) {
177 bb_perror_msg("can't make dir %s", dst_name);
178 }
179 break;
180 case S_IFLNK:
181 /* Symlink */
182//TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
183 res = symlink(file_header->link_target, dst_name);
184 if (res != 0
185 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
186 ) {
187 bb_perror_msg("can't create %slink "
188 "from %s to %s", "sym",
189 dst_name,
190 file_header->link_target);
191 }
192 break;
193 case S_IFSOCK:
194 case S_IFBLK:
195 case S_IFCHR:
196 case S_IFIFO:
197 res = mknod(dst_name, file_header->mode, file_header->device);
198 if ((res == -1)
199 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
200 ) {
201 bb_perror_msg("can't create node %s", dst_name);
202 }
203 break;
204 default:
205 bb_error_msg_and_die("unrecognized file type");
206 }
207
208 if (!S_ISLNK(file_header->mode)) {
209 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
210 uid_t uid = file_header->uid;
211 gid_t gid = file_header->gid;
212#if ENABLE_FEATURE_TAR_UNAME_GNAME
213 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
214 if (file_header->tar__uname) {
215//TODO: cache last name/id pair?
216 struct passwd *pwd = getpwnam(file_header->tar__uname);
217 if (pwd) uid = pwd->pw_uid;
218 }
219 if (file_header->tar__gname) {
220 struct group *grp = getgrnam(file_header->tar__gname);
221 if (grp) gid = grp->gr_gid;
222 }
223 }
224#endif
225 /* GNU tar 1.15.1 uses chown, not lchown */
226 chown(dst_name, uid, gid);
227 }
228 /* uclibc has no lchmod, glibc is even stranger -
229 * it has lchmod which seems to do nothing!
230 * so we use chmod... */
231 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
232 chmod(dst_name, file_header->mode);
233 }
234 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
235 struct timeval t[2];
236
237 t[1].tv_sec = t[0].tv_sec = file_header->mtime;
238 t[1].tv_usec = t[0].tv_usec = 0;
239 utimes(dst_name, t);
240 }
241 }
242
243 ret: ;
244#if ENABLE_FEATURE_TAR_SELINUX
245 if (sctx) {
246 /* reset the context after creating an entry */
247 setfscreatecon(NULL);
248 }
249#endif
250}
251