1
2
3
4
5#include "libbb.h"
6#include "bb_archive.h"
7
8void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
9{
10 file_header_t *file_header = archive_handle->file_header;
11 int dst_fd;
12 int res;
13 char *hard_link;
14#if ENABLE_FEATURE_TAR_LONG_OPTIONS
15 char *dst_name;
16#else
17# define dst_name (file_header->name)
18#endif
19
20#if ENABLE_FEATURE_TAR_SELINUX
21 char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
22 if (!sctx)
23 sctx = archive_handle->tar__sctx[PAX_GLOBAL];
24 if (sctx) {
25 setfscreatecon(sctx);
26 free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
27 archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
28 }
29#endif
30
31
32
33 hard_link = NULL;
34 if (S_ISREG(file_header->mode) && file_header->size == 0)
35 hard_link = file_header->link_target;
36
37#if ENABLE_FEATURE_TAR_LONG_OPTIONS
38 dst_name = file_header->name;
39 if (archive_handle->tar__strip_components) {
40 unsigned n = archive_handle->tar__strip_components;
41 do {
42 dst_name = strchr(dst_name, '/');
43 if (!dst_name || dst_name[1] == '\0') {
44 data_skip(archive_handle);
45 goto ret;
46 }
47 dst_name++;
48
49
50
51
52 if (hard_link) {
53
54
55
56
57
58 hard_link = strchr(hard_link, '/');
59 if (!hard_link || hard_link[1] == '\0') {
60 data_skip(archive_handle);
61 goto ret;
62 }
63 hard_link++;
64 }
65 } while (--n != 0);
66 }
67#endif
68
69 if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
70 char *slash = strrchr(dst_name, '/');
71 if (slash) {
72 *slash = '\0';
73 bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
74 *slash = '/';
75 }
76 }
77
78 if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
79
80 if (!S_ISDIR(file_header->mode)) {
81 if (hard_link) {
82
83
84
85
86
87
88
89 if (strcmp(hard_link, dst_name) == 0)
90 goto ret;
91 }
92
93 if (unlink(dst_name) == -1
94 && errno != ENOENT
95 ) {
96 bb_perror_msg_and_die("can't remove old file %s",
97 dst_name);
98 }
99 }
100 }
101 else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
102
103 struct stat existing_sb;
104 if (lstat(dst_name, &existing_sb) == -1) {
105 if (errno != ENOENT) {
106 bb_simple_perror_msg_and_die("can't stat old file");
107 }
108 }
109 else if (existing_sb.st_mtime >= file_header->mtime) {
110 if (!S_ISDIR(file_header->mode)) {
111 bb_error_msg("%s not created: newer or "
112 "same age file exists", dst_name);
113 }
114 data_skip(archive_handle);
115 goto ret;
116 }
117 else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
118 bb_perror_msg_and_die("can't remove old file %s",
119 dst_name);
120 }
121 }
122
123
124 if (hard_link) {
125 create_or_remember_link(&archive_handle->link_placeholders,
126 hard_link,
127 dst_name,
128 1);
129
130 goto ret;
131 }
132
133
134 switch (file_header->mode & S_IFMT) {
135 case S_IFREG: {
136
137 char *dst_nameN;
138 int flags = O_WRONLY | O_CREAT | O_EXCL;
139 if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
140 flags = O_WRONLY | O_CREAT | O_TRUNC;
141 dst_nameN = dst_name;
142#ifdef ARCHIVE_REPLACE_VIA_RENAME
143 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
144
145 dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
146#endif
147 dst_fd = xopen3(dst_nameN,
148 flags,
149 file_header->mode
150 );
151 bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
152 close(dst_fd);
153#ifdef ARCHIVE_REPLACE_VIA_RENAME
154 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
155 xrename(dst_nameN, dst_name);
156 free(dst_nameN);
157 }
158#endif
159 break;
160 }
161 case S_IFDIR:
162
163
164
165
166 res = mkdir(dst_name, file_header->mode);
167 if ((res != 0)
168 && (errno != EISDIR)
169 && (errno != EEXIST)
170 ) {
171 bb_perror_msg("can't make dir %s", dst_name);
172 }
173 break;
174 case S_IFLNK:
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 create_or_remember_link(&archive_handle->link_placeholders,
200 file_header->link_target,
201 dst_name,
202 0);
203 break;
204 case S_IFSOCK:
205 case S_IFBLK:
206 case S_IFCHR:
207 case S_IFIFO:
208 res = mknod(dst_name, file_header->mode, file_header->device);
209 if (res != 0) {
210 bb_perror_msg("can't create node %s", dst_name);
211 }
212 break;
213 default:
214 bb_simple_error_msg_and_die("unrecognized file type");
215 }
216
217 if (!S_ISLNK(file_header->mode)) {
218 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
219 uid_t uid = file_header->uid;
220 gid_t gid = file_header->gid;
221#if ENABLE_FEATURE_TAR_UNAME_GNAME
222 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
223 if (file_header->tar__uname) {
224
225 struct passwd *pwd = getpwnam(file_header->tar__uname);
226 if (pwd) uid = pwd->pw_uid;
227 }
228 if (file_header->tar__gname) {
229 struct group *grp = getgrnam(file_header->tar__gname);
230 if (grp) gid = grp->gr_gid;
231 }
232 }
233#endif
234
235 chown(dst_name, uid, gid);
236 }
237
238
239
240 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
241 chmod(dst_name, file_header->mode);
242 }
243 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
244 struct timeval t[2];
245
246 t[1].tv_sec = t[0].tv_sec = file_header->mtime;
247 t[1].tv_usec = t[0].tv_usec = 0;
248 utimes(dst_name, t);
249 }
250 }
251
252 ret: ;
253#if ENABLE_FEATURE_TAR_SELINUX
254 if (sctx) {
255
256 setfscreatecon(NULL);
257 }
258#endif
259}
260