Annotation of /trunk/mkinitrd-magellan/busybox/archival/tar.c
Parent Directory | Revision Log
Revision 984 -
(hide annotations)
(download)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 33252 byte(s)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 33252 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * Mini tar implementation for busybox | ||
4 | * | ||
5 | * Modified to use common extraction code used by ar, cpio, dpkg-deb, dpkg | ||
6 | niro | 816 | * by Glenn McGrath |
7 | niro | 532 | * |
8 | * Note, that as of BusyBox-0.43, tar has been completely rewritten from the | ||
9 | * ground up. It still has remnants of the old code lying about, but it is | ||
10 | * very different now (i.e., cleaner, less global variables, etc.) | ||
11 | * | ||
12 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
13 | * | ||
14 | * Based in part in the tar implementation in sash | ||
15 | * Copyright (c) 1999 by David I. Bell | ||
16 | * Permission is granted to use, distribute, or modify this source, | ||
17 | * provided that this copyright notice remains intact. | ||
18 | * Permission to distribute sash derived code under the GPL has been granted. | ||
19 | * | ||
20 | * Based in part on the tar implementation from busybox-0.28 | ||
21 | * Copyright (C) 1995 Bruce Perens | ||
22 | * | ||
23 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
24 | */ | ||
25 | |||
26 | #include <fnmatch.h> | ||
27 | niro | 816 | #include "libbb.h" |
28 | niro | 532 | #include "unarchive.h" |
29 | niro | 816 | /* FIXME: Stop using this non-standard feature */ |
30 | #ifndef FNM_LEADING_DIR | ||
31 | niro | 984 | # define FNM_LEADING_DIR 0 |
32 | niro | 816 | #endif |
33 | |||
34 | |||
35 | niro | 984 | //#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__) |
36 | #define DBG(...) ((void)0) | ||
37 | |||
38 | |||
39 | niro | 816 | #define block_buf bb_common_bufsiz1 |
40 | |||
41 | |||
42 | #if !ENABLE_FEATURE_SEAMLESS_GZ && !ENABLE_FEATURE_SEAMLESS_BZ2 | ||
43 | /* Do not pass gzip flag to writeTarFile() */ | ||
44 | #define writeTarFile(tar_fd, verboseFlag, dereferenceFlag, include, exclude, gzip) \ | ||
45 | writeTarFile(tar_fd, verboseFlag, dereferenceFlag, include, exclude) | ||
46 | #endif | ||
47 | |||
48 | |||
49 | niro | 532 | #if ENABLE_FEATURE_TAR_CREATE |
50 | |||
51 | /* Tar file constants */ | ||
52 | |||
53 | #define TAR_BLOCK_SIZE 512 | ||
54 | |||
55 | /* POSIX tar Header Block, from POSIX 1003.1-1990 */ | ||
56 | #define NAME_SIZE 100 | ||
57 | #define NAME_SIZE_STR "100" | ||
58 | niro | 984 | typedef struct TarHeader { /* byte offset */ |
59 | niro | 532 | char name[NAME_SIZE]; /* 0-99 */ |
60 | char mode[8]; /* 100-107 */ | ||
61 | char uid[8]; /* 108-115 */ | ||
62 | char gid[8]; /* 116-123 */ | ||
63 | char size[12]; /* 124-135 */ | ||
64 | char mtime[12]; /* 136-147 */ | ||
65 | char chksum[8]; /* 148-155 */ | ||
66 | char typeflag; /* 156-156 */ | ||
67 | char linkname[NAME_SIZE]; /* 157-256 */ | ||
68 | niro | 816 | /* POSIX: "ustar" NUL "00" */ |
69 | /* GNU tar: "ustar " NUL */ | ||
70 | /* Normally it's defined as magic[6] followed by | ||
71 | * version[2], but we put them together to save code. | ||
72 | */ | ||
73 | char magic[8]; /* 257-264 */ | ||
74 | niro | 532 | char uname[32]; /* 265-296 */ |
75 | char gname[32]; /* 297-328 */ | ||
76 | char devmajor[8]; /* 329-336 */ | ||
77 | char devminor[8]; /* 337-344 */ | ||
78 | char prefix[155]; /* 345-499 */ | ||
79 | niro | 816 | char padding[12]; /* 500-512 (pad to exactly TAR_BLOCK_SIZE) */ |
80 | niro | 984 | } TarHeader; |
81 | niro | 532 | |
82 | /* | ||
83 | ** writeTarFile(), writeFileToTarball(), and writeTarHeader() are | ||
84 | ** the only functions that deal with the HardLinkInfo structure. | ||
85 | ** Even these functions use the xxxHardLinkInfo() functions. | ||
86 | */ | ||
87 | niro | 984 | typedef struct HardLinkInfo { |
88 | struct HardLinkInfo *next; /* Next entry in list */ | ||
89 | dev_t dev; /* Device number */ | ||
90 | ino_t ino; /* Inode number */ | ||
91 | // short linkCount; /* (Hard) Link Count */ | ||
92 | char name[1]; /* Start of filename (must be last) */ | ||
93 | } HardLinkInfo; | ||
94 | niro | 532 | |
95 | /* Some info to be carried along when creating a new tarball */ | ||
96 | niro | 984 | typedef struct TarBallInfo { |
97 | niro | 816 | int tarFd; /* Open-for-write file descriptor |
98 | * for the tarball */ | ||
99 | int verboseFlag; /* Whether to print extra stuff or not */ | ||
100 | const llist_t *excludeList; /* List of files to not include */ | ||
101 | HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */ | ||
102 | HardLinkInfo *hlInfo; /* Hard Link Info for the current file */ | ||
103 | niro | 984 | //TODO: save only st_dev + st_ino |
104 | struct stat tarFileStatBuf; /* Stat info for the tarball, letting | ||
105 | * us know the inode and device that the | ||
106 | * tarball lives, so we can avoid trying | ||
107 | * to include the tarball into itself */ | ||
108 | } TarBallInfo; | ||
109 | niro | 532 | |
110 | /* A nice enum with all the possible tar file content types */ | ||
111 | niro | 984 | enum { |
112 | niro | 532 | REGTYPE = '0', /* regular file */ |
113 | REGTYPE0 = '\0', /* regular file (ancient bug compat) */ | ||
114 | LNKTYPE = '1', /* hard link */ | ||
115 | SYMTYPE = '2', /* symbolic link */ | ||
116 | CHRTYPE = '3', /* character special */ | ||
117 | BLKTYPE = '4', /* block special */ | ||
118 | DIRTYPE = '5', /* directory */ | ||
119 | FIFOTYPE = '6', /* FIFO special */ | ||
120 | CONTTYPE = '7', /* reserved */ | ||
121 | GNULONGLINK = 'K', /* GNU long (>100 chars) link name */ | ||
122 | GNULONGNAME = 'L', /* GNU long (>100 chars) file name */ | ||
123 | }; | ||
124 | |||
125 | /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */ | ||
126 | niro | 816 | static void addHardLinkInfo(HardLinkInfo **hlInfoHeadPtr, |
127 | niro | 532 | struct stat *statbuf, |
128 | const char *fileName) | ||
129 | { | ||
130 | /* Note: hlInfoHeadPtr can never be NULL! */ | ||
131 | HardLinkInfo *hlInfo; | ||
132 | |||
133 | hlInfo = xmalloc(sizeof(HardLinkInfo) + strlen(fileName)); | ||
134 | hlInfo->next = *hlInfoHeadPtr; | ||
135 | *hlInfoHeadPtr = hlInfo; | ||
136 | hlInfo->dev = statbuf->st_dev; | ||
137 | hlInfo->ino = statbuf->st_ino; | ||
138 | niro | 984 | // hlInfo->linkCount = statbuf->st_nlink; |
139 | niro | 532 | strcpy(hlInfo->name, fileName); |
140 | } | ||
141 | |||
142 | niro | 816 | static void freeHardLinkInfo(HardLinkInfo **hlInfoHeadPtr) |
143 | niro | 532 | { |
144 | HardLinkInfo *hlInfo; | ||
145 | HardLinkInfo *hlInfoNext; | ||
146 | |||
147 | if (hlInfoHeadPtr) { | ||
148 | hlInfo = *hlInfoHeadPtr; | ||
149 | while (hlInfo) { | ||
150 | hlInfoNext = hlInfo->next; | ||
151 | free(hlInfo); | ||
152 | hlInfo = hlInfoNext; | ||
153 | } | ||
154 | *hlInfoHeadPtr = NULL; | ||
155 | } | ||
156 | } | ||
157 | |||
158 | niro | 984 | /* Might be faster (and bigger) if the dev/ino were stored in numeric order ;) */ |
159 | niro | 816 | static HardLinkInfo *findHardLinkInfo(HardLinkInfo *hlInfo, struct stat *statbuf) |
160 | niro | 532 | { |
161 | while (hlInfo) { | ||
162 | niro | 984 | if (statbuf->st_ino == hlInfo->ino |
163 | && statbuf->st_dev == hlInfo->dev | ||
164 | ) { | ||
165 | DBG("found hardlink:'%s'", hlInfo->name); | ||
166 | niro | 532 | break; |
167 | niro | 984 | } |
168 | niro | 532 | hlInfo = hlInfo->next; |
169 | } | ||
170 | return hlInfo; | ||
171 | } | ||
172 | |||
173 | /* Put an octal string into the specified buffer. | ||
174 | * The number is zero padded and possibly null terminated. | ||
175 | * Stores low-order bits only if whole value does not fit. */ | ||
176 | static void putOctal(char *cp, int len, off_t value) | ||
177 | { | ||
178 | niro | 984 | char tempBuffer[sizeof(off_t)*3 + 1]; |
179 | niro | 532 | char *tempString = tempBuffer; |
180 | int width; | ||
181 | |||
182 | width = sprintf(tempBuffer, "%0*"OFF_FMT"o", len, value); | ||
183 | tempString += (width - len); | ||
184 | |||
185 | /* If string has leading zeroes, we can drop one */ | ||
186 | /* and field will have trailing '\0' */ | ||
187 | /* (increases chances of compat with other tars) */ | ||
188 | if (tempString[0] == '0') | ||
189 | tempString++; | ||
190 | |||
191 | /* Copy the string to the field */ | ||
192 | memcpy(cp, tempString, len); | ||
193 | } | ||
194 | #define PUT_OCTAL(a, b) putOctal((a), sizeof(a), (b)) | ||
195 | |||
196 | static void chksum_and_xwrite(int fd, struct TarHeader* hp) | ||
197 | { | ||
198 | niro | 816 | /* POSIX says that checksum is done on unsigned bytes |
199 | * (Sun and HP-UX gets it wrong... more details in | ||
200 | * GNU tar source) */ | ||
201 | niro | 532 | const unsigned char *cp; |
202 | int chksum, size; | ||
203 | |||
204 | strcpy(hp->magic, "ustar "); | ||
205 | |||
206 | /* Calculate and store the checksum (i.e., the sum of all of the bytes of | ||
207 | * the header). The checksum field must be filled with blanks for the | ||
208 | * calculation. The checksum field is formatted differently from the | ||
209 | * other fields: it has 6 digits, a null, then a space -- rather than | ||
210 | * digits, followed by a null like the other fields... */ | ||
211 | memset(hp->chksum, ' ', sizeof(hp->chksum)); | ||
212 | cp = (const unsigned char *) hp; | ||
213 | chksum = 0; | ||
214 | size = sizeof(*hp); | ||
215 | do { chksum += *cp++; } while (--size); | ||
216 | putOctal(hp->chksum, sizeof(hp->chksum)-1, chksum); | ||
217 | |||
218 | /* Now write the header out to disk */ | ||
219 | xwrite(fd, hp, sizeof(*hp)); | ||
220 | } | ||
221 | |||
222 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS | ||
223 | static void writeLongname(int fd, int type, const char *name, int dir) | ||
224 | { | ||
225 | static const struct { | ||
226 | char mode[8]; /* 100-107 */ | ||
227 | char uid[8]; /* 108-115 */ | ||
228 | char gid[8]; /* 116-123 */ | ||
229 | char size[12]; /* 124-135 */ | ||
230 | char mtime[12]; /* 136-147 */ | ||
231 | } prefilled = { | ||
232 | "0000000", | ||
233 | "0000000", | ||
234 | "0000000", | ||
235 | "00000000000", | ||
236 | "00000000000", | ||
237 | }; | ||
238 | struct TarHeader header; | ||
239 | int size; | ||
240 | |||
241 | dir = !!dir; /* normalize: 0/1 */ | ||
242 | size = strlen(name) + 1 + dir; /* GNU tar uses strlen+1 */ | ||
243 | /* + dir: account for possible '/' */ | ||
244 | |||
245 | memset(&header, 0, sizeof(header)); | ||
246 | strcpy(header.name, "././@LongLink"); | ||
247 | memcpy(header.mode, prefilled.mode, sizeof(prefilled)); | ||
248 | PUT_OCTAL(header.size, size); | ||
249 | header.typeflag = type; | ||
250 | chksum_and_xwrite(fd, &header); | ||
251 | |||
252 | /* Write filename[/] and pad the block. */ | ||
253 | /* dir=0: writes 'name<NUL>', pads */ | ||
254 | /* dir=1: writes 'name', writes '/<NUL>', pads */ | ||
255 | dir *= 2; | ||
256 | xwrite(fd, name, size - dir); | ||
257 | xwrite(fd, "/", dir); | ||
258 | size = (-size) & (TAR_BLOCK_SIZE-1); | ||
259 | memset(&header, 0, size); | ||
260 | xwrite(fd, &header, size); | ||
261 | } | ||
262 | #endif | ||
263 | |||
264 | /* Write out a tar header for the specified file/directory/whatever */ | ||
265 | void BUG_tar_header_size(void); | ||
266 | static int writeTarHeader(struct TarBallInfo *tbInfo, | ||
267 | const char *header_name, const char *fileName, struct stat *statbuf) | ||
268 | { | ||
269 | struct TarHeader header; | ||
270 | |||
271 | if (sizeof(header) != 512) | ||
272 | BUG_tar_header_size(); | ||
273 | |||
274 | memset(&header, 0, sizeof(struct TarHeader)); | ||
275 | |||
276 | strncpy(header.name, header_name, sizeof(header.name)); | ||
277 | |||
278 | /* POSIX says to mask mode with 07777. */ | ||
279 | PUT_OCTAL(header.mode, statbuf->st_mode & 07777); | ||
280 | PUT_OCTAL(header.uid, statbuf->st_uid); | ||
281 | PUT_OCTAL(header.gid, statbuf->st_gid); | ||
282 | memset(header.size, '0', sizeof(header.size)-1); /* Regular file size is handled later */ | ||
283 | PUT_OCTAL(header.mtime, statbuf->st_mtime); | ||
284 | |||
285 | /* Enter the user and group names */ | ||
286 | safe_strncpy(header.uname, get_cached_username(statbuf->st_uid), sizeof(header.uname)); | ||
287 | safe_strncpy(header.gname, get_cached_groupname(statbuf->st_gid), sizeof(header.gname)); | ||
288 | |||
289 | if (tbInfo->hlInfo) { | ||
290 | /* This is a hard link */ | ||
291 | header.typeflag = LNKTYPE; | ||
292 | strncpy(header.linkname, tbInfo->hlInfo->name, | ||
293 | sizeof(header.linkname)); | ||
294 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS | ||
295 | /* Write out long linkname if needed */ | ||
296 | if (header.linkname[sizeof(header.linkname)-1]) | ||
297 | writeLongname(tbInfo->tarFd, GNULONGLINK, | ||
298 | tbInfo->hlInfo->name, 0); | ||
299 | #endif | ||
300 | } else if (S_ISLNK(statbuf->st_mode)) { | ||
301 | niro | 816 | char *lpath = xmalloc_readlink_or_warn(fileName); |
302 | if (!lpath) | ||
303 | niro | 532 | return FALSE; |
304 | header.typeflag = SYMTYPE; | ||
305 | strncpy(header.linkname, lpath, sizeof(header.linkname)); | ||
306 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS | ||
307 | /* Write out long linkname if needed */ | ||
308 | if (header.linkname[sizeof(header.linkname)-1]) | ||
309 | writeLongname(tbInfo->tarFd, GNULONGLINK, lpath, 0); | ||
310 | #else | ||
311 | /* If it is larger than 100 bytes, bail out */ | ||
312 | if (header.linkname[sizeof(header.linkname)-1]) { | ||
313 | free(lpath); | ||
314 | bb_error_msg("names longer than "NAME_SIZE_STR" chars not supported"); | ||
315 | return FALSE; | ||
316 | } | ||
317 | #endif | ||
318 | free(lpath); | ||
319 | } else if (S_ISDIR(statbuf->st_mode)) { | ||
320 | header.typeflag = DIRTYPE; | ||
321 | /* Append '/' only if there is a space for it */ | ||
322 | if (!header.name[sizeof(header.name)-1]) | ||
323 | header.name[strlen(header.name)] = '/'; | ||
324 | } else if (S_ISCHR(statbuf->st_mode)) { | ||
325 | header.typeflag = CHRTYPE; | ||
326 | PUT_OCTAL(header.devmajor, major(statbuf->st_rdev)); | ||
327 | PUT_OCTAL(header.devminor, minor(statbuf->st_rdev)); | ||
328 | } else if (S_ISBLK(statbuf->st_mode)) { | ||
329 | header.typeflag = BLKTYPE; | ||
330 | PUT_OCTAL(header.devmajor, major(statbuf->st_rdev)); | ||
331 | PUT_OCTAL(header.devminor, minor(statbuf->st_rdev)); | ||
332 | } else if (S_ISFIFO(statbuf->st_mode)) { | ||
333 | header.typeflag = FIFOTYPE; | ||
334 | } else if (S_ISREG(statbuf->st_mode)) { | ||
335 | if (sizeof(statbuf->st_size) > 4 | ||
336 | && statbuf->st_size > (off_t)0777777777777LL | ||
337 | ) { | ||
338 | niro | 984 | bb_error_msg_and_die("can't store file '%s' " |
339 | "of size %"OFF_FMT"u, aborting", | ||
340 | niro | 532 | fileName, statbuf->st_size); |
341 | } | ||
342 | header.typeflag = REGTYPE; | ||
343 | PUT_OCTAL(header.size, statbuf->st_size); | ||
344 | } else { | ||
345 | bb_error_msg("%s: unknown file type", fileName); | ||
346 | return FALSE; | ||
347 | } | ||
348 | |||
349 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS | ||
350 | /* Write out long name if needed */ | ||
351 | /* (we, like GNU tar, output long linkname *before* long name) */ | ||
352 | if (header.name[sizeof(header.name)-1]) | ||
353 | writeLongname(tbInfo->tarFd, GNULONGNAME, | ||
354 | header_name, S_ISDIR(statbuf->st_mode)); | ||
355 | #endif | ||
356 | |||
357 | /* Now write the header out to disk */ | ||
358 | chksum_and_xwrite(tbInfo->tarFd, &header); | ||
359 | |||
360 | /* Now do the verbose thing (or not) */ | ||
361 | if (tbInfo->verboseFlag) { | ||
362 | FILE *vbFd = stdout; | ||
363 | |||
364 | niro | 984 | /* If archive goes to stdout, verbose goes to stderr */ |
365 | if (tbInfo->tarFd == STDOUT_FILENO) | ||
366 | niro | 532 | vbFd = stderr; |
367 | /* GNU "tar cvvf" prints "extended" listing a-la "ls -l" */ | ||
368 | /* We don't have such excesses here: for us "v" == "vv" */ | ||
369 | /* '/' is probably a GNUism */ | ||
370 | fprintf(vbFd, "%s%s\n", header_name, | ||
371 | S_ISDIR(statbuf->st_mode) ? "/" : ""); | ||
372 | } | ||
373 | |||
374 | return TRUE; | ||
375 | } | ||
376 | |||
377 | #if ENABLE_FEATURE_TAR_FROM | ||
378 | static int exclude_file(const llist_t *excluded_files, const char *file) | ||
379 | { | ||
380 | while (excluded_files) { | ||
381 | if (excluded_files->data[0] == '/') { | ||
382 | if (fnmatch(excluded_files->data, file, | ||
383 | niro | 984 | FNM_PATHNAME | FNM_LEADING_DIR) == 0) |
384 | niro | 532 | return 1; |
385 | } else { | ||
386 | const char *p; | ||
387 | |||
388 | for (p = file; p[0] != '\0'; p++) { | ||
389 | niro | 984 | if ((p == file || p[-1] == '/') |
390 | && p[0] != '/' | ||
391 | && fnmatch(excluded_files->data, p, | ||
392 | FNM_PATHNAME | FNM_LEADING_DIR) == 0 | ||
393 | ) { | ||
394 | niro | 532 | return 1; |
395 | niro | 984 | } |
396 | niro | 532 | } |
397 | } | ||
398 | excluded_files = excluded_files->link; | ||
399 | } | ||
400 | |||
401 | return 0; | ||
402 | } | ||
403 | #else | ||
404 | niro | 984 | # define exclude_file(excluded_files, file) 0 |
405 | niro | 532 | #endif |
406 | |||
407 | niro | 816 | static int FAST_FUNC writeFileToTarball(const char *fileName, struct stat *statbuf, |
408 | void *userData, int depth UNUSED_PARAM) | ||
409 | niro | 532 | { |
410 | struct TarBallInfo *tbInfo = (struct TarBallInfo *) userData; | ||
411 | const char *header_name; | ||
412 | int inputFileFd = -1; | ||
413 | |||
414 | niro | 984 | DBG("writeFileToTarball('%s')", fileName); |
415 | |||
416 | niro | 816 | /* Strip leading '/' (must be before memorizing hardlink's name) */ |
417 | header_name = fileName; | ||
418 | while (header_name[0] == '/') { | ||
419 | static smallint warned; | ||
420 | |||
421 | if (!warned) { | ||
422 | bb_error_msg("removing leading '/' from member names"); | ||
423 | warned = 1; | ||
424 | } | ||
425 | header_name++; | ||
426 | } | ||
427 | |||
428 | if (header_name[0] == '\0') | ||
429 | return TRUE; | ||
430 | |||
431 | /* It is against the rules to archive a socket */ | ||
432 | if (S_ISSOCK(statbuf->st_mode)) { | ||
433 | bb_error_msg("%s: socket ignored", fileName); | ||
434 | return TRUE; | ||
435 | } | ||
436 | |||
437 | niro | 532 | /* |
438 | * Check to see if we are dealing with a hard link. | ||
439 | * If so - | ||
440 | * Treat the first occurance of a given dev/inode as a file while | ||
441 | * treating any additional occurances as hard links. This is done | ||
442 | * by adding the file information to the HardLinkInfo linked list. | ||
443 | */ | ||
444 | tbInfo->hlInfo = NULL; | ||
445 | niro | 984 | if (!S_ISDIR(statbuf->st_mode) && statbuf->st_nlink > 1) { |
446 | DBG("'%s': st_nlink > 1", header_name); | ||
447 | niro | 532 | tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf); |
448 | niro | 984 | if (tbInfo->hlInfo == NULL) { |
449 | DBG("'%s': addHardLinkInfo", header_name); | ||
450 | niro | 816 | addHardLinkInfo(&tbInfo->hlInfoHead, statbuf, header_name); |
451 | niro | 984 | } |
452 | niro | 532 | } |
453 | |||
454 | /* It is a bad idea to store the archive we are in the process of creating, | ||
455 | * so check the device and inode to be sure that this particular file isn't | ||
456 | * the new tarball */ | ||
457 | niro | 984 | if (tbInfo->tarFileStatBuf.st_dev == statbuf->st_dev |
458 | && tbInfo->tarFileStatBuf.st_ino == statbuf->st_ino | ||
459 | niro | 816 | ) { |
460 | niro | 532 | bb_error_msg("%s: file is the archive; skipping", fileName); |
461 | return TRUE; | ||
462 | } | ||
463 | |||
464 | niro | 816 | if (exclude_file(tbInfo->excludeList, header_name)) |
465 | return SKIP; | ||
466 | niro | 532 | |
467 | #if !ENABLE_FEATURE_TAR_GNU_EXTENSIONS | ||
468 | niro | 816 | if (strlen(header_name) >= NAME_SIZE) { |
469 | niro | 532 | bb_error_msg("names longer than "NAME_SIZE_STR" chars not supported"); |
470 | return TRUE; | ||
471 | } | ||
472 | #endif | ||
473 | |||
474 | /* Is this a regular file? */ | ||
475 | if (tbInfo->hlInfo == NULL && S_ISREG(statbuf->st_mode)) { | ||
476 | /* open the file we want to archive, and make sure all is well */ | ||
477 | niro | 816 | inputFileFd = open_or_warn(fileName, O_RDONLY); |
478 | niro | 532 | if (inputFileFd < 0) { |
479 | return FALSE; | ||
480 | } | ||
481 | } | ||
482 | |||
483 | /* Add an entry to the tarball */ | ||
484 | if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) { | ||
485 | return FALSE; | ||
486 | } | ||
487 | |||
488 | /* If it was a regular file, write out the body */ | ||
489 | if (inputFileFd >= 0) { | ||
490 | size_t readSize; | ||
491 | niro | 816 | /* Write the file to the archive. */ |
492 | niro | 532 | /* We record size into header first, */ |
493 | /* and then write out file. If file shrinks in between, */ | ||
494 | /* tar will be corrupted. So we don't allow for that. */ | ||
495 | /* NB: GNU tar 1.16 warns and pads with zeroes */ | ||
496 | /* or even seeks back and updates header */ | ||
497 | bb_copyfd_exact_size(inputFileFd, tbInfo->tarFd, statbuf->st_size); | ||
498 | ////off_t readSize; | ||
499 | ////readSize = bb_copyfd_size(inputFileFd, tbInfo->tarFd, statbuf->st_size); | ||
500 | ////if (readSize != statbuf->st_size && readSize >= 0) { | ||
501 | //// bb_error_msg_and_die("short read from %s, aborting", fileName); | ||
502 | ////} | ||
503 | |||
504 | /* Check that file did not grow in between? */ | ||
505 | /* if (safe_read(inputFileFd, 1) == 1) warn but continue? */ | ||
506 | |||
507 | close(inputFileFd); | ||
508 | |||
509 | /* Pad the file up to the tar block size */ | ||
510 | /* (a few tricks here in the name of code size) */ | ||
511 | readSize = (-(int)statbuf->st_size) & (TAR_BLOCK_SIZE-1); | ||
512 | niro | 816 | memset(block_buf, 0, readSize); |
513 | xwrite(tbInfo->tarFd, block_buf, readSize); | ||
514 | niro | 532 | } |
515 | |||
516 | return TRUE; | ||
517 | } | ||
518 | |||
519 | niro | 816 | #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2 |
520 | niro | 984 | # if !(ENABLE_FEATURE_SEAMLESS_GZ && ENABLE_FEATURE_SEAMLESS_BZ2) |
521 | # define vfork_compressor(tar_fd, gzip) vfork_compressor(tar_fd) | ||
522 | # endif | ||
523 | niro | 816 | /* Don't inline: vfork scares gcc and pessimizes code */ |
524 | static void NOINLINE vfork_compressor(int tar_fd, int gzip) | ||
525 | niro | 532 | { |
526 | niro | 816 | pid_t gzipPid; |
527 | niro | 984 | # if ENABLE_FEATURE_SEAMLESS_GZ && ENABLE_FEATURE_SEAMLESS_BZ2 |
528 | niro | 816 | const char *zip_exec = (gzip == 1) ? "gzip" : "bzip2"; |
529 | niro | 984 | # elif ENABLE_FEATURE_SEAMLESS_GZ |
530 | niro | 816 | const char *zip_exec = "gzip"; |
531 | niro | 984 | # else /* only ENABLE_FEATURE_SEAMLESS_BZ2 */ |
532 | niro | 816 | const char *zip_exec = "bzip2"; |
533 | niro | 984 | # endif |
534 | niro | 816 | // On Linux, vfork never unpauses parent early, although standard |
535 | // allows for that. Do we want to waste bytes checking for it? | ||
536 | niro | 984 | # define WAIT_FOR_CHILD 0 |
537 | niro | 816 | volatile int vfork_exec_errno = 0; |
538 | struct fd_pair gzipDataPipe; | ||
539 | niro | 984 | # if WAIT_FOR_CHILD |
540 | niro | 816 | struct fd_pair gzipStatusPipe; |
541 | xpiped_pair(gzipStatusPipe); | ||
542 | niro | 984 | # endif |
543 | niro | 816 | xpiped_pair(gzipDataPipe); |
544 | |||
545 | signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */ | ||
546 | |||
547 | niro | 984 | # if defined(__GNUC__) && __GNUC__ |
548 | niro | 816 | /* Avoid vfork clobbering */ |
549 | (void) &zip_exec; | ||
550 | niro | 984 | # endif |
551 | niro | 816 | |
552 | gzipPid = vfork(); | ||
553 | if (gzipPid < 0) | ||
554 | bb_perror_msg_and_die("vfork"); | ||
555 | |||
556 | if (gzipPid == 0) { | ||
557 | /* child */ | ||
558 | /* NB: close _first_, then move fds! */ | ||
559 | close(gzipDataPipe.wr); | ||
560 | niro | 984 | # if WAIT_FOR_CHILD |
561 | niro | 816 | close(gzipStatusPipe.rd); |
562 | /* gzipStatusPipe.wr will close only on exec - | ||
563 | * parent waits for this close to happen */ | ||
564 | fcntl(gzipStatusPipe.wr, F_SETFD, FD_CLOEXEC); | ||
565 | niro | 984 | # endif |
566 | niro | 816 | xmove_fd(gzipDataPipe.rd, 0); |
567 | xmove_fd(tar_fd, 1); | ||
568 | /* exec gzip/bzip2 program/applet */ | ||
569 | BB_EXECLP(zip_exec, zip_exec, "-f", NULL); | ||
570 | vfork_exec_errno = errno; | ||
571 | _exit(EXIT_FAILURE); | ||
572 | } | ||
573 | |||
574 | /* parent */ | ||
575 | xmove_fd(gzipDataPipe.wr, tar_fd); | ||
576 | close(gzipDataPipe.rd); | ||
577 | niro | 984 | # if WAIT_FOR_CHILD |
578 | niro | 816 | close(gzipStatusPipe.wr); |
579 | while (1) { | ||
580 | char buf; | ||
581 | int n; | ||
582 | |||
583 | /* Wait until child execs (or fails to) */ | ||
584 | n = full_read(gzipStatusPipe.rd, &buf, 1); | ||
585 | if (n < 0 /* && errno == EAGAIN */) | ||
586 | continue; /* try it again */ | ||
587 | } | ||
588 | close(gzipStatusPipe.rd); | ||
589 | niro | 984 | # endif |
590 | niro | 816 | if (vfork_exec_errno) { |
591 | errno = vfork_exec_errno; | ||
592 | niro | 984 | bb_perror_msg_and_die("can't execute '%s'", zip_exec); |
593 | niro | 816 | } |
594 | } | ||
595 | #endif /* ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2 */ | ||
596 | |||
597 | |||
598 | /* gcc 4.2.1 inlines it, making code bigger */ | ||
599 | static NOINLINE int writeTarFile(int tar_fd, int verboseFlag, | ||
600 | int dereferenceFlag, const llist_t *include, | ||
601 | const llist_t *exclude, int gzip) | ||
602 | { | ||
603 | niro | 532 | int errorFlag = FALSE; |
604 | struct TarBallInfo tbInfo; | ||
605 | |||
606 | tbInfo.hlInfoHead = NULL; | ||
607 | tbInfo.tarFd = tar_fd; | ||
608 | tbInfo.verboseFlag = verboseFlag; | ||
609 | |||
610 | /* Store the stat info for the tarball's file, so | ||
611 | * can avoid including the tarball into itself.... */ | ||
612 | niro | 984 | if (fstat(tbInfo.tarFd, &tbInfo.tarFileStatBuf) < 0) |
613 | bb_perror_msg_and_die("can't stat tar file"); | ||
614 | niro | 532 | |
615 | niro | 816 | #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2 |
616 | if (gzip) | ||
617 | vfork_compressor(tbInfo.tarFd, gzip); | ||
618 | niro | 532 | #endif |
619 | |||
620 | tbInfo.excludeList = exclude; | ||
621 | |||
622 | /* Read the directory/files and iterate over them one at a time */ | ||
623 | while (include) { | ||
624 | niro | 816 | if (!recursive_action(include->data, ACTION_RECURSE | |
625 | (dereferenceFlag ? ACTION_FOLLOWLINKS : 0), | ||
626 | niro | 984 | writeFileToTarball, writeFileToTarball, &tbInfo, 0) |
627 | ) { | ||
628 | niro | 532 | errorFlag = TRUE; |
629 | } | ||
630 | include = include->link; | ||
631 | } | ||
632 | /* Write two empty blocks to the end of the archive */ | ||
633 | niro | 816 | memset(block_buf, 0, 2*TAR_BLOCK_SIZE); |
634 | xwrite(tbInfo.tarFd, block_buf, 2*TAR_BLOCK_SIZE); | ||
635 | niro | 532 | |
636 | /* To be pedantically correct, we would check if the tarball | ||
637 | * is smaller than 20 tar blocks, and pad it if it was smaller, | ||
638 | * but that isn't necessary for GNU tar interoperability, and | ||
639 | * so is considered a waste of space */ | ||
640 | |||
641 | /* Close so the child process (if any) will exit */ | ||
642 | close(tbInfo.tarFd); | ||
643 | |||
644 | /* Hang up the tools, close up shop, head home */ | ||
645 | if (ENABLE_FEATURE_CLEAN_UP) | ||
646 | freeHardLinkInfo(&tbInfo.hlInfoHead); | ||
647 | |||
648 | if (errorFlag) | ||
649 | bb_error_msg("error exit delayed from previous errors"); | ||
650 | |||
651 | niro | 816 | #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2 |
652 | if (gzip) { | ||
653 | niro | 532 | int status; |
654 | niro | 816 | if (safe_waitpid(-1, &status, 0) == -1) |
655 | niro | 532 | bb_perror_msg("waitpid"); |
656 | else if (!WIFEXITED(status) || WEXITSTATUS(status)) | ||
657 | /* gzip was killed or has exited with nonzero! */ | ||
658 | errorFlag = TRUE; | ||
659 | } | ||
660 | niro | 816 | #endif |
661 | niro | 532 | return errorFlag; |
662 | } | ||
663 | #else | ||
664 | niro | 816 | int writeTarFile(int tar_fd, int verboseFlag, |
665 | int dereferenceFlag, const llist_t *include, | ||
666 | const llist_t *exclude, int gzip); | ||
667 | niro | 532 | #endif /* FEATURE_TAR_CREATE */ |
668 | |||
669 | #if ENABLE_FEATURE_TAR_FROM | ||
670 | static llist_t *append_file_list_to_list(llist_t *list) | ||
671 | { | ||
672 | FILE *src_stream; | ||
673 | char *line; | ||
674 | llist_t *newlist = NULL; | ||
675 | |||
676 | niro | 816 | while (list) { |
677 | src_stream = xfopen_for_read(llist_pop(&list)); | ||
678 | while ((line = xmalloc_fgetline(src_stream)) != NULL) { | ||
679 | niro | 532 | /* kill trailing '/' unless the string is just "/" */ |
680 | char *cp = last_char_is(line, '/'); | ||
681 | if (cp > line) | ||
682 | *cp = '\0'; | ||
683 | llist_add_to(&newlist, line); | ||
684 | } | ||
685 | fclose(src_stream); | ||
686 | } | ||
687 | return newlist; | ||
688 | } | ||
689 | #else | ||
690 | niro | 984 | # define append_file_list_to_list(x) 0 |
691 | niro | 532 | #endif |
692 | |||
693 | niro | 816 | #if ENABLE_FEATURE_SEAMLESS_Z |
694 | static char FAST_FUNC get_header_tar_Z(archive_handle_t *archive_handle) | ||
695 | niro | 532 | { |
696 | /* Can't lseek over pipes */ | ||
697 | archive_handle->seek = seek_by_read; | ||
698 | |||
699 | /* do the decompression, and cleanup */ | ||
700 | if (xread_char(archive_handle->src_fd) != 0x1f | ||
701 | || xread_char(archive_handle->src_fd) != 0x9d | ||
702 | ) { | ||
703 | bb_error_msg_and_die("invalid magic"); | ||
704 | } | ||
705 | |||
706 | niro | 816 | open_transformer(archive_handle->src_fd, unpack_Z_stream, "uncompress"); |
707 | niro | 532 | archive_handle->offset = 0; |
708 | while (get_header_tar(archive_handle) == EXIT_SUCCESS) | ||
709 | niro | 816 | continue; |
710 | niro | 532 | |
711 | /* Can only do one file at a time */ | ||
712 | return EXIT_FAILURE; | ||
713 | } | ||
714 | #else | ||
715 | niro | 984 | # define get_header_tar_Z NULL |
716 | niro | 532 | #endif |
717 | |||
718 | #ifdef CHECK_FOR_CHILD_EXITCODE | ||
719 | /* Looks like it isn't needed - tar detects malformed (truncated) | ||
720 | * archive if e.g. bunzip2 fails */ | ||
721 | static int child_error; | ||
722 | |||
723 | static void handle_SIGCHLD(int status) | ||
724 | { | ||
725 | /* Actually, 'status' is a signo. We reuse it for other needs */ | ||
726 | |||
727 | /* Wait for any child without blocking */ | ||
728 | niro | 816 | if (wait_any_nohang(&status) < 0) |
729 | niro | 532 | /* wait failed?! I'm confused... */ |
730 | return; | ||
731 | |||
732 | niro | 984 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) |
733 | niro | 532 | /* child exited with 0 */ |
734 | return; | ||
735 | /* Cannot happen? | ||
736 | niro | 816 | if (!WIFSIGNALED(status) && !WIFEXITED(status)) return; */ |
737 | niro | 532 | child_error = 1; |
738 | } | ||
739 | #endif | ||
740 | |||
741 | enum { | ||
742 | niro | 984 | OPTBIT_KEEP_OLD = 8, |
743 | IF_FEATURE_TAR_CREATE( OPTBIT_CREATE ,) | ||
744 | IF_FEATURE_TAR_CREATE( OPTBIT_DEREFERENCE ,) | ||
745 | IF_FEATURE_SEAMLESS_BZ2( OPTBIT_BZIP2 ,) | ||
746 | IF_FEATURE_SEAMLESS_LZMA(OPTBIT_LZMA ,) | ||
747 | IF_FEATURE_TAR_FROM( OPTBIT_INCLUDE_FROM,) | ||
748 | IF_FEATURE_TAR_FROM( OPTBIT_EXCLUDE_FROM,) | ||
749 | IF_FEATURE_SEAMLESS_GZ( OPTBIT_GZIP ,) | ||
750 | IF_FEATURE_SEAMLESS_Z( OPTBIT_COMPRESS ,) // 16th bit | ||
751 | IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,) | ||
752 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS | ||
753 | OPTBIT_NUMERIC_OWNER, | ||
754 | niro | 532 | OPTBIT_NOPRESERVE_PERM, |
755 | niro | 984 | OPTBIT_OVERWRITE, |
756 | #endif | ||
757 | niro | 532 | OPT_TEST = 1 << 0, // t |
758 | OPT_EXTRACT = 1 << 1, // x | ||
759 | OPT_BASEDIR = 1 << 2, // C | ||
760 | OPT_TARNAME = 1 << 3, // f | ||
761 | OPT_2STDOUT = 1 << 4, // O | ||
762 | niro | 984 | OPT_NOPRESERVE_OWNER = 1 << 5, // o == no-same-owner |
763 | OPT_P = 1 << 6, // p | ||
764 | OPT_VERBOSE = 1 << 7, // v | ||
765 | OPT_KEEP_OLD = 1 << 8, // k | ||
766 | OPT_CREATE = IF_FEATURE_TAR_CREATE( (1 << OPTBIT_CREATE )) + 0, // c | ||
767 | OPT_DEREFERENCE = IF_FEATURE_TAR_CREATE( (1 << OPTBIT_DEREFERENCE )) + 0, // h | ||
768 | OPT_BZIP2 = IF_FEATURE_SEAMLESS_BZ2( (1 << OPTBIT_BZIP2 )) + 0, // j | ||
769 | OPT_LZMA = IF_FEATURE_SEAMLESS_LZMA((1 << OPTBIT_LZMA )) + 0, // a | ||
770 | OPT_INCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_INCLUDE_FROM)) + 0, // T | ||
771 | OPT_EXCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_EXCLUDE_FROM)) + 0, // X | ||
772 | OPT_GZIP = IF_FEATURE_SEAMLESS_GZ( (1 << OPTBIT_GZIP )) + 0, // z | ||
773 | OPT_COMPRESS = IF_FEATURE_SEAMLESS_Z( (1 << OPTBIT_COMPRESS )) + 0, // Z | ||
774 | OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m | ||
775 | OPT_NUMERIC_OWNER = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER )) + 0, // numeric-owner | ||
776 | OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions | ||
777 | OPT_OVERWRITE = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE )) + 0, // overwrite | ||
778 | niro | 532 | }; |
779 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS | ||
780 | niro | 816 | static const char tar_longopts[] ALIGN1 = |
781 | "list\0" No_argument "t" | ||
782 | "extract\0" No_argument "x" | ||
783 | "directory\0" Required_argument "C" | ||
784 | "file\0" Required_argument "f" | ||
785 | "to-stdout\0" No_argument "O" | ||
786 | niro | 984 | /* do not restore owner */ |
787 | /* Note: GNU tar handles 'o' as no-same-owner only on extract, | ||
788 | * on create, 'o' is --old-archive. We do not support --old-archive. */ | ||
789 | "no-same-owner\0" No_argument "o" | ||
790 | niro | 816 | "same-permissions\0" No_argument "p" |
791 | "verbose\0" No_argument "v" | ||
792 | "keep-old\0" No_argument "k" | ||
793 | niro | 532 | # if ENABLE_FEATURE_TAR_CREATE |
794 | niro | 816 | "create\0" No_argument "c" |
795 | "dereference\0" No_argument "h" | ||
796 | niro | 532 | # endif |
797 | niro | 816 | # if ENABLE_FEATURE_SEAMLESS_BZ2 |
798 | "bzip2\0" No_argument "j" | ||
799 | niro | 532 | # endif |
800 | niro | 816 | # if ENABLE_FEATURE_SEAMLESS_LZMA |
801 | "lzma\0" No_argument "a" | ||
802 | niro | 532 | # endif |
803 | # if ENABLE_FEATURE_TAR_FROM | ||
804 | niro | 816 | "files-from\0" Required_argument "T" |
805 | "exclude-from\0" Required_argument "X" | ||
806 | niro | 532 | # endif |
807 | niro | 816 | # if ENABLE_FEATURE_SEAMLESS_GZ |
808 | "gzip\0" No_argument "z" | ||
809 | niro | 532 | # endif |
810 | niro | 816 | # if ENABLE_FEATURE_SEAMLESS_Z |
811 | "compress\0" No_argument "Z" | ||
812 | niro | 532 | # endif |
813 | niro | 984 | # if ENABLE_FEATURE_TAR_NOPRESERVE_TIME |
814 | "touch\0" No_argument "m" | ||
815 | # endif | ||
816 | /* use numeric uid/gid from tar header, not textual */ | ||
817 | "numeric-owner\0" No_argument "\xfc" | ||
818 | /* do not restore mode */ | ||
819 | "no-same-permissions\0" No_argument "\xfd" | ||
820 | /* on unpack, open with O_TRUNC and !O_EXCL */ | ||
821 | "overwrite\0" No_argument "\xfe" | ||
822 | niro | 532 | /* --exclude takes next bit position in option mask, */ |
823 | niro | 984 | /* therefore we have to put it _after_ --no-same-permissions */ |
824 | niro | 532 | # if ENABLE_FEATURE_TAR_FROM |
825 | niro | 816 | "exclude\0" Required_argument "\xff" |
826 | niro | 532 | # endif |
827 | niro | 816 | ; |
828 | niro | 532 | #endif |
829 | |||
830 | niro | 816 | int tar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
831 | int tar_main(int argc UNUSED_PARAM, char **argv) | ||
832 | niro | 532 | { |
833 | niro | 816 | char FAST_FUNC (*get_header_ptr)(archive_handle_t *) = get_header_tar; |
834 | niro | 532 | archive_handle_t *tar_handle; |
835 | char *base_dir = NULL; | ||
836 | const char *tar_filename = "-"; | ||
837 | unsigned opt; | ||
838 | int verboseFlag = 0; | ||
839 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM | ||
840 | llist_t *excludes = NULL; | ||
841 | #endif | ||
842 | |||
843 | /* Initialise default values */ | ||
844 | tar_handle = init_handle(); | ||
845 | niro | 816 | tar_handle->ah_flags = ARCHIVE_CREATE_LEADING_DIRS |
846 | niro | 984 | | ARCHIVE_RESTORE_DATE |
847 | | ARCHIVE_UNLINK_OLD; | ||
848 | niro | 532 | |
849 | niro | 816 | /* Apparently only root's tar preserves perms (see bug 3844) */ |
850 | if (getuid() != 0) | ||
851 | niro | 984 | tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_PERM; |
852 | niro | 816 | |
853 | niro | 532 | /* Prepend '-' to the first argument if required */ |
854 | opt_complementary = "--:" // first arg is options | ||
855 | "tt:vv:" // count -t,-v | ||
856 | "?:" // bail out with usage instead of error return | ||
857 | "X::T::" // cumulative lists | ||
858 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM | ||
859 | "\xff::" // cumulative lists for --exclude | ||
860 | #endif | ||
861 | niro | 984 | IF_FEATURE_TAR_CREATE("c:") "t:x:" // at least one of these is reqd |
862 | IF_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive | ||
863 | IF_NOT_FEATURE_TAR_CREATE("t--x:x--t"); // mutually exclusive | ||
864 | niro | 532 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS |
865 | niro | 816 | applet_long_options = tar_longopts; |
866 | niro | 532 | #endif |
867 | niro | 984 | #if ENABLE_DESKTOP |
868 | if (argv[1] && argv[1][0] != '-') { | ||
869 | /* Compat: | ||
870 | * 1st argument without dash handles options with parameters | ||
871 | * differently from dashed one: it takes *next argv[i]* | ||
872 | * as paramenter even if there are more chars in 1st argument: | ||
873 | * "tar fx TARFILE" - "x" is not taken as f's param | ||
874 | * but is interpreted as -x option | ||
875 | * "tar -xf TARFILE" - dashed equivalent of the above | ||
876 | * "tar -fx ..." - "x" is taken as f's param | ||
877 | * getopt32 wouldn't handle 1st command correctly. | ||
878 | * Unfortunately, people do use such commands. | ||
879 | * We massage argv[1] to work around it by moving 'f' | ||
880 | * to the end of the string. | ||
881 | * More contrived "tar fCx TARFILE DIR" still fails, | ||
882 | * but such commands are much less likely to be used. | ||
883 | */ | ||
884 | char *f = strchr(argv[1], 'f'); | ||
885 | if (f) { | ||
886 | while (f[1] != '\0') { | ||
887 | *f = f[1]; | ||
888 | f++; | ||
889 | } | ||
890 | *f = 'f'; | ||
891 | } | ||
892 | } | ||
893 | #endif | ||
894 | niro | 816 | opt = getopt32(argv, |
895 | niro | 984 | "txC:f:Oopvk" |
896 | IF_FEATURE_TAR_CREATE( "ch" ) | ||
897 | IF_FEATURE_SEAMLESS_BZ2( "j" ) | ||
898 | IF_FEATURE_SEAMLESS_LZMA("a" ) | ||
899 | IF_FEATURE_TAR_FROM( "T:X:") | ||
900 | IF_FEATURE_SEAMLESS_GZ( "z" ) | ||
901 | IF_FEATURE_SEAMLESS_Z( "Z" ) | ||
902 | IF_FEATURE_TAR_NOPRESERVE_TIME("m") | ||
903 | niro | 532 | , &base_dir // -C dir |
904 | , &tar_filename // -f filename | ||
905 | niro | 984 | IF_FEATURE_TAR_FROM(, &(tar_handle->accept)) // T |
906 | IF_FEATURE_TAR_FROM(, &(tar_handle->reject)) // X | ||
907 | niro | 532 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM |
908 | , &excludes // --exclude | ||
909 | #endif | ||
910 | , &verboseFlag // combined count for -t and -v | ||
911 | , &verboseFlag // combined count for -t and -v | ||
912 | ); | ||
913 | niro | 984 | //bb_error_msg("opt:%08x", opt); |
914 | niro | 816 | argv += optind; |
915 | niro | 532 | |
916 | if (verboseFlag) tar_handle->action_header = header_verbose_list; | ||
917 | if (verboseFlag == 1) tar_handle->action_header = header_list; | ||
918 | |||
919 | if (opt & OPT_EXTRACT) | ||
920 | tar_handle->action_data = data_extract_all; | ||
921 | |||
922 | if (opt & OPT_2STDOUT) | ||
923 | tar_handle->action_data = data_extract_to_stdout; | ||
924 | |||
925 | if (opt & OPT_KEEP_OLD) | ||
926 | niro | 984 | tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD; |
927 | niro | 532 | |
928 | niro | 984 | if (opt & OPT_NUMERIC_OWNER) |
929 | tar_handle->ah_flags |= ARCHIVE_NUMERIC_OWNER; | ||
930 | niro | 532 | |
931 | niro | 984 | if (opt & OPT_NOPRESERVE_OWNER) |
932 | tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_OWNER; | ||
933 | |||
934 | niro | 532 | if (opt & OPT_NOPRESERVE_PERM) |
935 | niro | 984 | tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_PERM; |
936 | niro | 532 | |
937 | niro | 984 | if (opt & OPT_OVERWRITE) { |
938 | tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD; | ||
939 | tar_handle->ah_flags |= ARCHIVE_O_TRUNC; | ||
940 | } | ||
941 | |||
942 | niro | 532 | if (opt & OPT_GZIP) |
943 | get_header_ptr = get_header_tar_gz; | ||
944 | |||
945 | if (opt & OPT_BZIP2) | ||
946 | get_header_ptr = get_header_tar_bz2; | ||
947 | |||
948 | if (opt & OPT_LZMA) | ||
949 | get_header_ptr = get_header_tar_lzma; | ||
950 | |||
951 | if (opt & OPT_COMPRESS) | ||
952 | get_header_ptr = get_header_tar_Z; | ||
953 | |||
954 | niro | 984 | if (opt & OPT_NOPRESERVE_TIME) |
955 | tar_handle->ah_flags &= ~ARCHIVE_RESTORE_DATE; | ||
956 | |||
957 | niro | 532 | #if ENABLE_FEATURE_TAR_FROM |
958 | tar_handle->reject = append_file_list_to_list(tar_handle->reject); | ||
959 | niro | 984 | # if ENABLE_FEATURE_TAR_LONG_OPTIONS |
960 | niro | 532 | /* Append excludes to reject */ |
961 | while (excludes) { | ||
962 | llist_t *next = excludes->link; | ||
963 | excludes->link = tar_handle->reject; | ||
964 | tar_handle->reject = excludes; | ||
965 | excludes = next; | ||
966 | } | ||
967 | niro | 984 | # endif |
968 | niro | 532 | tar_handle->accept = append_file_list_to_list(tar_handle->accept); |
969 | #endif | ||
970 | |||
971 | /* Setup an array of filenames to work with */ | ||
972 | niro | 984 | /* TODO: This is the same as in ar, make a separate function? */ |
973 | niro | 816 | while (*argv) { |
974 | niro | 532 | /* kill trailing '/' unless the string is just "/" */ |
975 | niro | 816 | char *cp = last_char_is(*argv, '/'); |
976 | if (cp > *argv) | ||
977 | niro | 532 | *cp = '\0'; |
978 | niro | 816 | llist_add_to_end(&tar_handle->accept, *argv); |
979 | argv++; | ||
980 | niro | 532 | } |
981 | |||
982 | if (tar_handle->accept || tar_handle->reject) | ||
983 | tar_handle->filter = filter_accept_reject_list; | ||
984 | |||
985 | /* Open the tar file */ | ||
986 | { | ||
987 | niro | 984 | int tar_fd = STDIN_FILENO; |
988 | int flags = O_RDONLY; | ||
989 | niro | 532 | |
990 | if (opt & OPT_CREATE) { | ||
991 | niro | 984 | /* Make sure there is at least one file to tar up */ |
992 | niro | 532 | if (tar_handle->accept == NULL) |
993 | bb_error_msg_and_die("empty archive"); | ||
994 | |||
995 | niro | 984 | tar_fd = STDOUT_FILENO; |
996 | niro | 532 | /* Mimicking GNU tar 1.15.1: */ |
997 | niro | 816 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
998 | niro | 532 | } |
999 | |||
1000 | if (LONE_DASH(tar_filename)) { | ||
1001 | niro | 984 | tar_handle->src_fd = tar_fd; |
1002 | niro | 532 | tar_handle->seek = seek_by_read; |
1003 | } else { | ||
1004 | niro | 984 | if (ENABLE_FEATURE_TAR_AUTODETECT && flags == O_RDONLY) { |
1005 | get_header_ptr = get_header_tar; | ||
1006 | niro | 816 | tar_handle->src_fd = open_zipped(tar_filename); |
1007 | if (tar_handle->src_fd < 0) | ||
1008 | bb_perror_msg_and_die("can't open '%s'", tar_filename); | ||
1009 | } else { | ||
1010 | tar_handle->src_fd = xopen(tar_filename, flags); | ||
1011 | } | ||
1012 | niro | 532 | } |
1013 | } | ||
1014 | |||
1015 | if (base_dir) | ||
1016 | xchdir(base_dir); | ||
1017 | |||
1018 | #ifdef CHECK_FOR_CHILD_EXITCODE | ||
1019 | /* We need to know whether child (gzip/bzip/etc) exits abnormally */ | ||
1020 | signal(SIGCHLD, handle_SIGCHLD); | ||
1021 | #endif | ||
1022 | |||
1023 | niro | 984 | /* Create an archive */ |
1024 | niro | 532 | if (opt & OPT_CREATE) { |
1025 | niro | 816 | #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2 |
1026 | niro | 532 | int zipMode = 0; |
1027 | niro | 816 | if (ENABLE_FEATURE_SEAMLESS_GZ && (opt & OPT_GZIP)) |
1028 | niro | 532 | zipMode = 1; |
1029 | niro | 816 | if (ENABLE_FEATURE_SEAMLESS_BZ2 && (opt & OPT_BZIP2)) |
1030 | niro | 532 | zipMode = 2; |
1031 | niro | 816 | #endif |
1032 | niro | 532 | /* NB: writeTarFile() closes tar_handle->src_fd */ |
1033 | return writeTarFile(tar_handle->src_fd, verboseFlag, opt & OPT_DEREFERENCE, | ||
1034 | tar_handle->accept, | ||
1035 | tar_handle->reject, zipMode); | ||
1036 | } | ||
1037 | |||
1038 | while (get_header_ptr(tar_handle) == EXIT_SUCCESS) | ||
1039 | niro | 816 | continue; |
1040 | niro | 532 | |
1041 | /* Check that every file that should have been extracted was */ | ||
1042 | while (tar_handle->accept) { | ||
1043 | if (!find_list_entry(tar_handle->reject, tar_handle->accept->data) | ||
1044 | && !find_list_entry(tar_handle->passed, tar_handle->accept->data) | ||
1045 | ) { | ||
1046 | bb_error_msg_and_die("%s: not found in archive", | ||
1047 | tar_handle->accept->data); | ||
1048 | } | ||
1049 | tar_handle->accept = tar_handle->accept->link; | ||
1050 | } | ||
1051 | if (ENABLE_FEATURE_CLEAN_UP /* && tar_handle->src_fd != STDIN_FILENO */) | ||
1052 | close(tar_handle->src_fd); | ||
1053 | |||
1054 | return EXIT_SUCCESS; | ||
1055 | } |