blob: cdc6067e5b0d33af32e4ea5c9f2c6c9e2da2977c
1 | /* 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 | * by Glenn McGrath |
7 | * |
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 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 source tree. |
24 | */ |
25 | /* TODO: security with -C DESTDIR option can be enhanced. |
26 | * Consider tar file created via: |
27 | * $ tar cvf bug.tar anything.txt |
28 | * $ ln -s /tmp symlink |
29 | * $ tar --append -f bug.tar symlink |
30 | * $ rm symlink |
31 | * $ mkdir symlink |
32 | * $ tar --append -f bug.tar symlink/evil.py |
33 | * |
34 | * This will result in an archive which contains: |
35 | * $ tar --list -f bug.tar |
36 | * anything.txt |
37 | * symlink |
38 | * symlink/evil.py |
39 | * |
40 | * Untarring it puts evil.py in '/tmp' even if the -C DESTDIR is given. |
41 | * This doesn't feel right, and IIRC GNU tar doesn't do that. |
42 | */ |
43 | |
44 | //config:config TAR |
45 | //config: bool "tar" |
46 | //config: default y |
47 | //config: help |
48 | //config: tar is an archiving program. It's commonly used with gzip to |
49 | //config: create compressed archives. It's probably the most widely used |
50 | //config: UNIX archive program. |
51 | //config: |
52 | //config:config FEATURE_TAR_CREATE |
53 | //config: bool "Enable archive creation" |
54 | //config: default y |
55 | //config: depends on TAR |
56 | //config: help |
57 | //config: If you enable this option you'll be able to create |
58 | //config: tar archives using the `-c' option. |
59 | //config: |
60 | //config:config FEATURE_TAR_AUTODETECT |
61 | //config: bool "Autodetect compressed tarballs" |
62 | //config: default y |
63 | //config: depends on TAR && (FEATURE_SEAMLESS_Z || FEATURE_SEAMLESS_GZ || FEATURE_SEAMLESS_BZ2 || FEATURE_SEAMLESS_LZMA || FEATURE_SEAMLESS_XZ) |
64 | //config: help |
65 | //config: With this option tar can automatically detect compressed |
66 | //config: tarballs. Currently it works only on files (not pipes etc). |
67 | //config: |
68 | //config:config FEATURE_TAR_FROM |
69 | //config: bool "Enable -X (exclude from) and -T (include from) options)" |
70 | //config: default y |
71 | //config: depends on TAR |
72 | //config: help |
73 | //config: If you enable this option you'll be able to specify |
74 | //config: a list of files to include or exclude from an archive. |
75 | //config: |
76 | //config:config FEATURE_TAR_OLDGNU_COMPATIBILITY |
77 | //config: bool "Support for old tar header format" |
78 | //config: default y |
79 | //config: depends on TAR || DPKG |
80 | //config: help |
81 | //config: This option is required to unpack archives created in |
82 | //config: the old GNU format; help to kill this old format by |
83 | //config: repacking your ancient archives with the new format. |
84 | //config: |
85 | //config:config FEATURE_TAR_OLDSUN_COMPATIBILITY |
86 | //config: bool "Enable untarring of tarballs with checksums produced by buggy Sun tar" |
87 | //config: default y |
88 | //config: depends on TAR || DPKG |
89 | //config: help |
90 | //config: This option is required to unpack archives created by some old |
91 | //config: version of Sun's tar (it was calculating checksum using signed |
92 | //config: arithmetic). It is said to be fixed in newer Sun tar, but "old" |
93 | //config: tarballs still exist. |
94 | //config: |
95 | //config:config FEATURE_TAR_GNU_EXTENSIONS |
96 | //config: bool "Support for GNU tar extensions (long filenames)" |
97 | //config: default y |
98 | //config: depends on TAR || DPKG |
99 | //config: help |
100 | //config: With this option busybox supports GNU long filenames and |
101 | //config: linknames. |
102 | //config: |
103 | //config:config FEATURE_TAR_LONG_OPTIONS |
104 | //config: bool "Enable long options" |
105 | //config: default y |
106 | //config: depends on TAR && LONG_OPTS |
107 | //config: help |
108 | //config: Enable use of long options, increases size by about 400 Bytes |
109 | //config: |
110 | //config:config FEATURE_TAR_TO_COMMAND |
111 | //config: bool "Support for writing to an external program" |
112 | //config: default y |
113 | //config: depends on TAR && FEATURE_TAR_LONG_OPTIONS |
114 | //config: help |
115 | //config: If you enable this option you'll be able to instruct tar to send |
116 | //config: the contents of each extracted file to the standard input of an |
117 | //config: external program. |
118 | //config: |
119 | //config:config FEATURE_TAR_UNAME_GNAME |
120 | //config: bool "Enable use of user and group names" |
121 | //config: default y |
122 | //config: depends on TAR |
123 | //config: help |
124 | //config: Enables use of user and group names in tar. This affects contents |
125 | //config: listings (-t) and preserving permissions when unpacking (-p). |
126 | //config: +200 bytes. |
127 | //config: |
128 | //config:config FEATURE_TAR_NOPRESERVE_TIME |
129 | //config: bool "Enable -m (do not preserve time) option" |
130 | //config: default y |
131 | //config: depends on TAR |
132 | //config: help |
133 | //config: With this option busybox supports GNU tar -m |
134 | //config: (do not preserve time) option. |
135 | //config: |
136 | //config:config FEATURE_TAR_SELINUX |
137 | //config: bool "Support for extracting SELinux labels" |
138 | //config: default n |
139 | //config: depends on TAR && SELINUX |
140 | //config: help |
141 | //config: With this option busybox supports restoring SELinux labels |
142 | //config: when extracting files from tar archives. |
143 | |
144 | //applet:IF_TAR(APPLET(tar, BB_DIR_BIN, BB_SUID_DROP)) |
145 | //kbuild:lib-$(CONFIG_TAR) += tar.o |
146 | |
147 | #include <fnmatch.h> |
148 | #include "libbb.h" |
149 | #include "bb_archive.h" |
150 | /* FIXME: Stop using this non-standard feature */ |
151 | #ifndef FNM_LEADING_DIR |
152 | # define FNM_LEADING_DIR 0 |
153 | #endif |
154 | |
155 | |
156 | //#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__) |
157 | #define DBG(...) ((void)0) |
158 | |
159 | |
160 | #define block_buf bb_common_bufsiz1 |
161 | |
162 | |
163 | #if !ENABLE_FEATURE_SEAMLESS_GZ && !ENABLE_FEATURE_SEAMLESS_BZ2 |
164 | /* Do not pass gzip flag to writeTarFile() */ |
165 | #define writeTarFile(tar_fd, verboseFlag, optFlags, recurseFlags, include, exclude, gzip) \ |
166 | writeTarFile(tar_fd, verboseFlag, optFlags, recurseFlags, include, exclude) |
167 | #endif |
168 | |
169 | |
170 | #if ENABLE_FEATURE_TAR_CREATE |
171 | |
172 | /* |
173 | ** writeTarFile(), writeFileToTarball(), and writeTarHeader() are |
174 | ** the only functions that deal with the HardLinkInfo structure. |
175 | ** Even these functions use the xxxHardLinkInfo() functions. |
176 | */ |
177 | typedef struct HardLinkInfo { |
178 | struct HardLinkInfo *next; /* Next entry in list */ |
179 | dev_t dev; /* Device number */ |
180 | ino_t ino; /* Inode number */ |
181 | // short linkCount; /* (Hard) Link Count */ |
182 | char name[1]; /* Start of filename (must be last) */ |
183 | } HardLinkInfo; |
184 | |
185 | /* Some info to be carried along when creating a new tarball */ |
186 | typedef struct TarBallInfo { |
187 | int tarFd; /* Open-for-write file descriptor |
188 | * for the tarball */ |
189 | int verboseFlag; /* Whether to print extra stuff or not */ |
190 | unsigned optFlags; /* all command line flags */ |
191 | |
192 | const llist_t *excludeList; /* List of files to not include */ |
193 | HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */ |
194 | HardLinkInfo *hlInfo; /* Hard Link Info for the current file */ |
195 | //TODO: save only st_dev + st_ino |
196 | struct stat tarFileStatBuf; /* Stat info for the tarball, letting |
197 | * us know the inode and device that the |
198 | * tarball lives, so we can avoid trying |
199 | * to include the tarball into itself */ |
200 | } TarBallInfo; |
201 | |
202 | /* A nice enum with all the possible tar file content types */ |
203 | enum { |
204 | REGTYPE = '0', /* regular file */ |
205 | REGTYPE0 = '\0', /* regular file (ancient bug compat) */ |
206 | LNKTYPE = '1', /* hard link */ |
207 | SYMTYPE = '2', /* symbolic link */ |
208 | CHRTYPE = '3', /* character special */ |
209 | BLKTYPE = '4', /* block special */ |
210 | DIRTYPE = '5', /* directory */ |
211 | FIFOTYPE = '6', /* FIFO special */ |
212 | CONTTYPE = '7', /* reserved */ |
213 | GNULONGLINK = 'K', /* GNU long (>100 chars) link name */ |
214 | GNULONGNAME = 'L', /* GNU long (>100 chars) file name */ |
215 | EXTTYPE = 'x', /* ext metadata for next file, store selinux_context */ |
216 | }; |
217 | |
218 | /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */ |
219 | static void addHardLinkInfo(HardLinkInfo **hlInfoHeadPtr, |
220 | struct stat *statbuf, |
221 | const char *fileName) |
222 | { |
223 | /* Note: hlInfoHeadPtr can never be NULL! */ |
224 | HardLinkInfo *hlInfo; |
225 | |
226 | hlInfo = xmalloc(sizeof(HardLinkInfo) + strlen(fileName)); |
227 | hlInfo->next = *hlInfoHeadPtr; |
228 | *hlInfoHeadPtr = hlInfo; |
229 | hlInfo->dev = statbuf->st_dev; |
230 | hlInfo->ino = statbuf->st_ino; |
231 | // hlInfo->linkCount = statbuf->st_nlink; |
232 | strcpy(hlInfo->name, fileName); |
233 | } |
234 | |
235 | static void freeHardLinkInfo(HardLinkInfo **hlInfoHeadPtr) |
236 | { |
237 | HardLinkInfo *hlInfo; |
238 | HardLinkInfo *hlInfoNext; |
239 | |
240 | if (hlInfoHeadPtr) { |
241 | hlInfo = *hlInfoHeadPtr; |
242 | while (hlInfo) { |
243 | hlInfoNext = hlInfo->next; |
244 | free(hlInfo); |
245 | hlInfo = hlInfoNext; |
246 | } |
247 | *hlInfoHeadPtr = NULL; |
248 | } |
249 | } |
250 | |
251 | /* Might be faster (and bigger) if the dev/ino were stored in numeric order ;) */ |
252 | static HardLinkInfo *findHardLinkInfo(HardLinkInfo *hlInfo, struct stat *statbuf) |
253 | { |
254 | while (hlInfo) { |
255 | if (statbuf->st_ino == hlInfo->ino |
256 | && statbuf->st_dev == hlInfo->dev |
257 | ) { |
258 | DBG("found hardlink:'%s'", hlInfo->name); |
259 | break; |
260 | } |
261 | hlInfo = hlInfo->next; |
262 | } |
263 | return hlInfo; |
264 | } |
265 | |
266 | /* Put an octal string into the specified buffer. |
267 | * The number is zero padded and possibly null terminated. |
268 | * Stores low-order bits only if whole value does not fit. */ |
269 | static void putOctal(char *cp, int len, off_t value) |
270 | { |
271 | char tempBuffer[sizeof(off_t)*3 + 1]; |
272 | char *tempString = tempBuffer; |
273 | int width; |
274 | |
275 | width = sprintf(tempBuffer, "%0*"OFF_FMT"o", len, value); |
276 | tempString += (width - len); |
277 | |
278 | /* If string has leading zeroes, we can drop one */ |
279 | /* and field will have trailing '\0' */ |
280 | /* (increases chances of compat with other tars) */ |
281 | if (tempString[0] == '0') |
282 | tempString++; |
283 | |
284 | /* Copy the string to the field */ |
285 | memcpy(cp, tempString, len); |
286 | } |
287 | #define PUT_OCTAL(a, b) putOctal((a), sizeof(a), (b)) |
288 | |
289 | static void chksum_and_xwrite(int fd, struct tar_header_t* hp) |
290 | { |
291 | /* POSIX says that checksum is done on unsigned bytes |
292 | * (Sun and HP-UX gets it wrong... more details in |
293 | * GNU tar source) */ |
294 | const unsigned char *cp; |
295 | int chksum, size; |
296 | |
297 | strcpy(hp->magic, "ustar "); |
298 | |
299 | /* Calculate and store the checksum (i.e., the sum of all of the bytes of |
300 | * the header). The checksum field must be filled with blanks for the |
301 | * calculation. The checksum field is formatted differently from the |
302 | * other fields: it has 6 digits, a null, then a space -- rather than |
303 | * digits, followed by a null like the other fields... */ |
304 | memset(hp->chksum, ' ', sizeof(hp->chksum)); |
305 | cp = (const unsigned char *) hp; |
306 | chksum = 0; |
307 | size = sizeof(*hp); |
308 | do { chksum += *cp++; } while (--size); |
309 | putOctal(hp->chksum, sizeof(hp->chksum)-1, chksum); |
310 | |
311 | /* Now write the header out to disk */ |
312 | xwrite(fd, hp, sizeof(*hp)); |
313 | } |
314 | |
315 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
316 | static void writeLongname(int fd, int type, const char *name, int dir) |
317 | { |
318 | static const struct { |
319 | char mode[8]; /* 100-107 */ |
320 | char uid[8]; /* 108-115 */ |
321 | char gid[8]; /* 116-123 */ |
322 | char size[12]; /* 124-135 */ |
323 | char mtime[12]; /* 136-147 */ |
324 | } prefilled = { |
325 | "0000000", |
326 | "0000000", |
327 | "0000000", |
328 | "00000000000", |
329 | "00000000000", |
330 | }; |
331 | struct tar_header_t header; |
332 | int size; |
333 | |
334 | dir = !!dir; /* normalize: 0/1 */ |
335 | size = strlen(name) + 1 + dir; /* GNU tar uses strlen+1 */ |
336 | /* + dir: account for possible '/' */ |
337 | |
338 | memset(&header, 0, sizeof(header)); |
339 | strcpy(header.name, "././@LongLink"); |
340 | memcpy(header.mode, prefilled.mode, sizeof(prefilled)); |
341 | PUT_OCTAL(header.size, size); |
342 | header.typeflag = type; |
343 | chksum_and_xwrite(fd, &header); |
344 | |
345 | /* Write filename[/] and pad the block. */ |
346 | /* dir=0: writes 'name<NUL>', pads */ |
347 | /* dir=1: writes 'name', writes '/<NUL>', pads */ |
348 | dir *= 2; |
349 | xwrite(fd, name, size - dir); |
350 | xwrite(fd, "/", dir); |
351 | size = (-size) & (TAR_BLOCK_SIZE-1); |
352 | memset(&header, 0, size); |
353 | xwrite(fd, &header, size); |
354 | } |
355 | #endif |
356 | |
357 | #if ENABLE_FEATURE_TAR_SELINUX |
358 | # define SELINUX_CONTEXT_KEYWORD "RHT.security.selinux" |
359 | /* Write 2 blocks : extended file header + selinux context */ |
360 | static int writeSeHeader(int fd, const char *con, struct tar_header_t *header) |
361 | { |
362 | char block[TAR_BLOCK_SIZE]; |
363 | struct tar_header_t hd; |
364 | |
365 | int sz = sizeof(SELINUX_CONTEXT_KEYWORD) + 4 + strlen(con); |
366 | if (sz >= 100) sz++; /* another ascii digit for size */ |
367 | if (sz > TAR_BLOCK_SIZE) |
368 | return FALSE; |
369 | |
370 | memset(&block, 0, TAR_BLOCK_SIZE); |
371 | sprintf(block, "%d %s=%s\n", sz, SELINUX_CONTEXT_KEYWORD, con); |
372 | |
373 | /* write duplicated file entry */ |
374 | memcpy(&hd, header, sizeof(hd)); |
375 | hd.typeflag = EXTTYPE; |
376 | PUT_OCTAL(hd.size, sz); |
377 | chksum_and_xwrite(fd, &hd); |
378 | |
379 | /* write selinux context */ |
380 | xwrite(fd, &block, TAR_BLOCK_SIZE); |
381 | return TRUE; |
382 | } |
383 | #endif |
384 | |
385 | /* Write out a tar header for the specified file/directory/whatever */ |
386 | static int writeTarHeader(struct TarBallInfo *tbInfo, |
387 | const char *header_name, const char *fileName, struct stat *statbuf) |
388 | { |
389 | struct tar_header_t header; |
390 | |
391 | memset(&header, 0, sizeof(header)); |
392 | |
393 | strncpy(header.name, header_name, sizeof(header.name)); |
394 | |
395 | /* POSIX says to mask mode with 07777. */ |
396 | PUT_OCTAL(header.mode, statbuf->st_mode & 07777); |
397 | PUT_OCTAL(header.uid, statbuf->st_uid); |
398 | PUT_OCTAL(header.gid, statbuf->st_gid); |
399 | memset(header.size, '0', sizeof(header.size)-1); /* Regular file size is handled later */ |
400 | PUT_OCTAL(header.mtime, statbuf->st_mtime); |
401 | |
402 | /* Enter the user and group names */ |
403 | safe_strncpy(header.uname, get_cached_username(statbuf->st_uid), sizeof(header.uname)); |
404 | safe_strncpy(header.gname, get_cached_groupname(statbuf->st_gid), sizeof(header.gname)); |
405 | |
406 | if (tbInfo->hlInfo) { |
407 | /* This is a hard link */ |
408 | header.typeflag = LNKTYPE; |
409 | strncpy(header.linkname, tbInfo->hlInfo->name, |
410 | sizeof(header.linkname)); |
411 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
412 | /* Write out long linkname if needed */ |
413 | if (header.linkname[sizeof(header.linkname)-1]) |
414 | writeLongname(tbInfo->tarFd, GNULONGLINK, |
415 | tbInfo->hlInfo->name, 0); |
416 | #endif |
417 | } else if (S_ISLNK(statbuf->st_mode)) { |
418 | char *lpath = xmalloc_readlink_or_warn(fileName); |
419 | if (!lpath) |
420 | return FALSE; |
421 | header.typeflag = SYMTYPE; |
422 | strncpy(header.linkname, lpath, sizeof(header.linkname)); |
423 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
424 | /* Write out long linkname if needed */ |
425 | if (header.linkname[sizeof(header.linkname)-1]) |
426 | writeLongname(tbInfo->tarFd, GNULONGLINK, lpath, 0); |
427 | #else |
428 | /* If it is larger than 100 bytes, bail out */ |
429 | if (header.linkname[sizeof(header.linkname)-1]) { |
430 | free(lpath); |
431 | bb_error_msg("names longer than "NAME_SIZE_STR" chars not supported"); |
432 | return FALSE; |
433 | } |
434 | #endif |
435 | free(lpath); |
436 | } else if (S_ISDIR(statbuf->st_mode)) { |
437 | header.typeflag = DIRTYPE; |
438 | /* Append '/' only if there is a space for it */ |
439 | if (!header.name[sizeof(header.name)-1]) |
440 | header.name[strlen(header.name)] = '/'; |
441 | } else if (S_ISCHR(statbuf->st_mode)) { |
442 | header.typeflag = CHRTYPE; |
443 | PUT_OCTAL(header.devmajor, major(statbuf->st_rdev)); |
444 | PUT_OCTAL(header.devminor, minor(statbuf->st_rdev)); |
445 | } else if (S_ISBLK(statbuf->st_mode)) { |
446 | header.typeflag = BLKTYPE; |
447 | PUT_OCTAL(header.devmajor, major(statbuf->st_rdev)); |
448 | PUT_OCTAL(header.devminor, minor(statbuf->st_rdev)); |
449 | } else if (S_ISFIFO(statbuf->st_mode)) { |
450 | header.typeflag = FIFOTYPE; |
451 | } else if (S_ISREG(statbuf->st_mode)) { |
452 | /* header.size field is 12 bytes long */ |
453 | /* Does octal-encoded size fit? */ |
454 | uoff_t filesize = statbuf->st_size; |
455 | if (sizeof(filesize) <= 4 |
456 | || filesize <= (uoff_t)0777777777777LL |
457 | ) { |
458 | PUT_OCTAL(header.size, filesize); |
459 | } |
460 | /* Does base256-encoded size fit? |
461 | * It always does unless off_t is wider than 64 bits. |
462 | */ |
463 | else if (ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
464 | #if ULLONG_MAX > 0xffffffffffffffffLL /* 2^64-1 */ |
465 | && (filesize <= 0x3fffffffffffffffffffffffLL) |
466 | #endif |
467 | ) { |
468 | /* GNU tar uses "base-256 encoding" for very large numbers. |
469 | * Encoding is binary, with highest bit always set as a marker |
470 | * and sign in next-highest bit: |
471 | * 80 00 .. 00 - zero |
472 | * bf ff .. ff - largest positive number |
473 | * ff ff .. ff - minus 1 |
474 | * c0 00 .. 00 - smallest negative number |
475 | */ |
476 | char *p8 = header.size + sizeof(header.size); |
477 | do { |
478 | *--p8 = (uint8_t)filesize; |
479 | filesize >>= 8; |
480 | } while (p8 != header.size); |
481 | *p8 |= 0x80; |
482 | } else { |
483 | bb_error_msg_and_die("can't store file '%s' " |
484 | "of size %"FILESIZE_FMT"u, aborting", |
485 | fileName, statbuf->st_size); |
486 | } |
487 | header.typeflag = REGTYPE; |
488 | } else { |
489 | bb_error_msg("%s: unknown file type", fileName); |
490 | return FALSE; |
491 | } |
492 | |
493 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
494 | /* Write out long name if needed */ |
495 | /* (we, like GNU tar, output long linkname *before* long name) */ |
496 | if (header.name[sizeof(header.name)-1]) |
497 | writeLongname(tbInfo->tarFd, GNULONGNAME, |
498 | header_name, S_ISDIR(statbuf->st_mode)); |
499 | #endif |
500 | |
501 | #if ENABLE_FEATURE_TAR_SELINUX |
502 | if (is_selinux_enabled() && (tbInfo->optFlags & ARCHIVE_STORE_SELINUX)) { |
503 | security_context_t sid; |
504 | lgetfilecon(fileName, &sid); |
505 | if (sid) { |
506 | // optional extended block |
507 | writeSeHeader(tbInfo->tarFd, sid, &header); |
508 | freecon(sid); |
509 | } |
510 | } |
511 | #endif |
512 | |
513 | /* Now write the header out to disk */ |
514 | chksum_and_xwrite(tbInfo->tarFd, &header); |
515 | |
516 | /* Now do the verbose thing (or not) */ |
517 | if (tbInfo->verboseFlag) { |
518 | FILE *vbFd = stdout; |
519 | |
520 | /* If archive goes to stdout, verbose goes to stderr */ |
521 | if (tbInfo->tarFd == STDOUT_FILENO) |
522 | vbFd = stderr; |
523 | /* GNU "tar cvvf" prints "extended" listing a-la "ls -l" */ |
524 | /* We don't have such excesses here: for us "v" == "vv" */ |
525 | /* '/' is probably a GNUism */ |
526 | fprintf(vbFd, "%s%s\n", header_name, |
527 | S_ISDIR(statbuf->st_mode) ? "/" : ""); |
528 | } |
529 | |
530 | return TRUE; |
531 | } |
532 | |
533 | #if ENABLE_FEATURE_TAR_FROM |
534 | static int exclude_file(const llist_t *excluded_files, const char *file) |
535 | { |
536 | while (excluded_files) { |
537 | if (excluded_files->data[0] == '/') { |
538 | if (fnmatch(excluded_files->data, file, |
539 | FNM_PATHNAME | FNM_LEADING_DIR) == 0) |
540 | return 1; |
541 | } else { |
542 | const char *p; |
543 | |
544 | for (p = file; p[0] != '\0'; p++) { |
545 | if ((p == file || p[-1] == '/') |
546 | && p[0] != '/' |
547 | && fnmatch(excluded_files->data, p, |
548 | FNM_PATHNAME | FNM_LEADING_DIR) == 0 |
549 | ) { |
550 | return 1; |
551 | } |
552 | } |
553 | } |
554 | excluded_files = excluded_files->link; |
555 | } |
556 | |
557 | return 0; |
558 | } |
559 | #else |
560 | # define exclude_file(excluded_files, file) 0 |
561 | #endif |
562 | |
563 | static int FAST_FUNC writeFileToTarball(const char *fileName, struct stat *statbuf, |
564 | void *userData, int depth UNUSED_PARAM) |
565 | { |
566 | struct TarBallInfo *tbInfo = (struct TarBallInfo *) userData; |
567 | const char *header_name; |
568 | int inputFileFd = -1; |
569 | |
570 | DBG("writeFileToTarball('%s')", fileName); |
571 | |
572 | /* Strip leading '/' and such (must be before memorizing hardlink's name) */ |
573 | header_name = strip_unsafe_prefix(fileName); |
574 | |
575 | if (header_name[0] == '\0') |
576 | return TRUE; |
577 | |
578 | /* It is against the rules to archive a socket */ |
579 | if (S_ISSOCK(statbuf->st_mode)) { |
580 | bb_error_msg("%s: socket ignored", fileName); |
581 | return TRUE; |
582 | } |
583 | |
584 | /* |
585 | * Check to see if we are dealing with a hard link. |
586 | * If so - |
587 | * Treat the first occurance of a given dev/inode as a file while |
588 | * treating any additional occurances as hard links. This is done |
589 | * by adding the file information to the HardLinkInfo linked list. |
590 | */ |
591 | tbInfo->hlInfo = NULL; |
592 | if (!S_ISDIR(statbuf->st_mode) && statbuf->st_nlink > 1) { |
593 | DBG("'%s': st_nlink > 1", header_name); |
594 | tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf); |
595 | if (tbInfo->hlInfo == NULL) { |
596 | DBG("'%s': addHardLinkInfo", header_name); |
597 | addHardLinkInfo(&tbInfo->hlInfoHead, statbuf, header_name); |
598 | } |
599 | } |
600 | |
601 | /* It is a bad idea to store the archive we are in the process of creating, |
602 | * so check the device and inode to be sure that this particular file isn't |
603 | * the new tarball */ |
604 | if (tbInfo->tarFileStatBuf.st_dev == statbuf->st_dev |
605 | && tbInfo->tarFileStatBuf.st_ino == statbuf->st_ino |
606 | ) { |
607 | bb_error_msg("%s: file is the archive; skipping", fileName); |
608 | return TRUE; |
609 | } |
610 | |
611 | if (exclude_file(tbInfo->excludeList, header_name)) |
612 | return SKIP; |
613 | |
614 | #if !ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
615 | if (strlen(header_name) >= NAME_SIZE) { |
616 | bb_error_msg("names longer than "NAME_SIZE_STR" chars not supported"); |
617 | return TRUE; |
618 | } |
619 | #endif |
620 | |
621 | /* Is this a regular file? */ |
622 | if (tbInfo->hlInfo == NULL && S_ISREG(statbuf->st_mode)) { |
623 | /* open the file we want to archive, and make sure all is well */ |
624 | inputFileFd = open_or_warn(fileName, O_RDONLY); |
625 | if (inputFileFd < 0) { |
626 | return FALSE; |
627 | } |
628 | } |
629 | |
630 | /* Add an entry to the tarball */ |
631 | if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) { |
632 | return FALSE; |
633 | } |
634 | |
635 | /* If it was a regular file, write out the body */ |
636 | if (inputFileFd >= 0) { |
637 | size_t readSize; |
638 | /* Write the file to the archive. */ |
639 | /* We record size into header first, */ |
640 | /* and then write out file. If file shrinks in between, */ |
641 | /* tar will be corrupted. So we don't allow for that. */ |
642 | /* NB: GNU tar 1.16 warns and pads with zeroes */ |
643 | /* or even seeks back and updates header */ |
644 | bb_copyfd_exact_size(inputFileFd, tbInfo->tarFd, statbuf->st_size); |
645 | ////off_t readSize; |
646 | ////readSize = bb_copyfd_size(inputFileFd, tbInfo->tarFd, statbuf->st_size); |
647 | ////if (readSize != statbuf->st_size && readSize >= 0) { |
648 | //// bb_error_msg_and_die("short read from %s, aborting", fileName); |
649 | ////} |
650 | |
651 | /* Check that file did not grow in between? */ |
652 | /* if (safe_read(inputFileFd, 1) == 1) warn but continue? */ |
653 | |
654 | close(inputFileFd); |
655 | |
656 | /* Pad the file up to the tar block size */ |
657 | /* (a few tricks here in the name of code size) */ |
658 | readSize = (-(int)statbuf->st_size) & (TAR_BLOCK_SIZE-1); |
659 | memset(block_buf, 0, readSize); |
660 | xwrite(tbInfo->tarFd, block_buf, readSize); |
661 | } |
662 | |
663 | return TRUE; |
664 | } |
665 | |
666 | #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2 |
667 | # if !(ENABLE_FEATURE_SEAMLESS_GZ && ENABLE_FEATURE_SEAMLESS_BZ2) |
668 | # define vfork_compressor(tar_fd, gzip) vfork_compressor(tar_fd) |
669 | # endif |
670 | /* Don't inline: vfork scares gcc and pessimizes code */ |
671 | static void NOINLINE vfork_compressor(int tar_fd, int gzip) |
672 | { |
673 | pid_t gzipPid; |
674 | # if ENABLE_FEATURE_SEAMLESS_GZ && ENABLE_FEATURE_SEAMLESS_BZ2 |
675 | const char *zip_exec = (gzip == 1) ? "gzip" : "bzip2"; |
676 | # elif ENABLE_FEATURE_SEAMLESS_GZ |
677 | const char *zip_exec = "gzip"; |
678 | # else /* only ENABLE_FEATURE_SEAMLESS_BZ2 */ |
679 | const char *zip_exec = "bzip2"; |
680 | # endif |
681 | // On Linux, vfork never unpauses parent early, although standard |
682 | // allows for that. Do we want to waste bytes checking for it? |
683 | # define WAIT_FOR_CHILD 0 |
684 | volatile int vfork_exec_errno = 0; |
685 | struct fd_pair gzipDataPipe; |
686 | # if WAIT_FOR_CHILD |
687 | struct fd_pair gzipStatusPipe; |
688 | xpiped_pair(gzipStatusPipe); |
689 | # endif |
690 | xpiped_pair(gzipDataPipe); |
691 | |
692 | signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */ |
693 | |
694 | # if defined(__GNUC__) && __GNUC__ |
695 | /* Avoid vfork clobbering */ |
696 | (void) &zip_exec; |
697 | # endif |
698 | |
699 | gzipPid = xvfork(); |
700 | |
701 | if (gzipPid == 0) { |
702 | /* child */ |
703 | /* NB: close _first_, then move fds! */ |
704 | close(gzipDataPipe.wr); |
705 | # if WAIT_FOR_CHILD |
706 | close(gzipStatusPipe.rd); |
707 | /* gzipStatusPipe.wr will close only on exec - |
708 | * parent waits for this close to happen */ |
709 | fcntl(gzipStatusPipe.wr, F_SETFD, FD_CLOEXEC); |
710 | # endif |
711 | xmove_fd(gzipDataPipe.rd, 0); |
712 | xmove_fd(tar_fd, 1); |
713 | /* exec gzip/bzip2 program/applet */ |
714 | BB_EXECLP(zip_exec, zip_exec, "-f", (char *)0); |
715 | vfork_exec_errno = errno; |
716 | _exit(EXIT_FAILURE); |
717 | } |
718 | |
719 | /* parent */ |
720 | xmove_fd(gzipDataPipe.wr, tar_fd); |
721 | close(gzipDataPipe.rd); |
722 | # if WAIT_FOR_CHILD |
723 | close(gzipStatusPipe.wr); |
724 | while (1) { |
725 | char buf; |
726 | int n; |
727 | |
728 | /* Wait until child execs (or fails to) */ |
729 | n = full_read(gzipStatusPipe.rd, &buf, 1); |
730 | if (n < 0 /* && errno == EAGAIN */) |
731 | continue; /* try it again */ |
732 | } |
733 | close(gzipStatusPipe.rd); |
734 | # endif |
735 | if (vfork_exec_errno) { |
736 | errno = vfork_exec_errno; |
737 | bb_perror_msg_and_die("can't execute '%s'", zip_exec); |
738 | } |
739 | } |
740 | #endif /* ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2 */ |
741 | |
742 | |
743 | /* gcc 4.2.1 inlines it, making code bigger */ |
744 | static NOINLINE int writeTarFile(int tar_fd, int verboseFlag, |
745 | unsigned optFlags, |
746 | int recurseFlags, const llist_t *include, |
747 | const llist_t *exclude, int gzip) |
748 | { |
749 | int errorFlag = FALSE; |
750 | struct TarBallInfo tbInfo; |
751 | |
752 | tbInfo.hlInfoHead = NULL; |
753 | tbInfo.tarFd = tar_fd; |
754 | tbInfo.verboseFlag = verboseFlag; |
755 | tbInfo.optFlags = optFlags; |
756 | |
757 | /* Store the stat info for the tarball's file, so |
758 | * can avoid including the tarball into itself.... */ |
759 | xfstat(tbInfo.tarFd, &tbInfo.tarFileStatBuf, "can't stat tar file"); |
760 | |
761 | #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2 |
762 | if (gzip) |
763 | vfork_compressor(tbInfo.tarFd, gzip); |
764 | #endif |
765 | |
766 | tbInfo.excludeList = exclude; |
767 | |
768 | /* Read the directory/files and iterate over them one at a time */ |
769 | while (include) { |
770 | if (!recursive_action(include->data, recurseFlags, |
771 | writeFileToTarball, writeFileToTarball, &tbInfo, 0) |
772 | ) { |
773 | errorFlag = TRUE; |
774 | } |
775 | include = include->link; |
776 | } |
777 | /* Write two empty blocks to the end of the archive */ |
778 | memset(block_buf, 0, 2*TAR_BLOCK_SIZE); |
779 | xwrite(tbInfo.tarFd, block_buf, 2*TAR_BLOCK_SIZE); |
780 | |
781 | /* To be pedantically correct, we would check if the tarball |
782 | * is smaller than 20 tar blocks, and pad it if it was smaller, |
783 | * but that isn't necessary for GNU tar interoperability, and |
784 | * so is considered a waste of space */ |
785 | |
786 | /* Close so the child process (if any) will exit */ |
787 | close(tbInfo.tarFd); |
788 | |
789 | /* Hang up the tools, close up shop, head home */ |
790 | if (ENABLE_FEATURE_CLEAN_UP) |
791 | freeHardLinkInfo(&tbInfo.hlInfoHead); |
792 | |
793 | if (errorFlag) |
794 | bb_error_msg("error exit delayed from previous errors"); |
795 | |
796 | #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2 |
797 | if (gzip) { |
798 | int status; |
799 | if (safe_waitpid(-1, &status, 0) == -1) |
800 | bb_perror_msg("waitpid"); |
801 | else if (!WIFEXITED(status) || WEXITSTATUS(status)) |
802 | /* gzip was killed or has exited with nonzero! */ |
803 | errorFlag = TRUE; |
804 | } |
805 | #endif |
806 | return errorFlag; |
807 | } |
808 | #else |
809 | int writeTarFile(int tar_fd, int verboseFlag, |
810 | unsigned optFlags, |
811 | int recurseFlags, const llist_t *include, |
812 | const llist_t *exclude, int gzip); |
813 | #endif /* FEATURE_TAR_CREATE */ |
814 | |
815 | #if ENABLE_FEATURE_TAR_FROM |
816 | static llist_t *append_file_list_to_list(llist_t *list) |
817 | { |
818 | FILE *src_stream; |
819 | char *line; |
820 | llist_t *newlist = NULL; |
821 | |
822 | while (list) { |
823 | src_stream = xfopen_stdin(llist_pop(&list)); |
824 | while ((line = xmalloc_fgetline(src_stream)) != NULL) { |
825 | /* kill trailing '/' unless the string is just "/" */ |
826 | char *cp = last_char_is(line, '/'); |
827 | if (cp > line) |
828 | *cp = '\0'; |
829 | llist_add_to_end(&newlist, line); |
830 | } |
831 | fclose(src_stream); |
832 | } |
833 | return newlist; |
834 | } |
835 | #endif |
836 | |
837 | //usage:#define tar_trivial_usage |
838 | //usage: "-[" IF_FEATURE_TAR_CREATE("c") "xt" |
839 | //usage: IF_FEATURE_SEAMLESS_Z("Z") |
840 | //usage: IF_FEATURE_SEAMLESS_GZ("z") |
841 | //usage: IF_FEATURE_SEAMLESS_XZ("J") |
842 | //usage: IF_FEATURE_SEAMLESS_BZ2("j") |
843 | //usage: IF_FEATURE_SEAMLESS_LZMA("a") |
844 | //usage: IF_FEATURE_TAR_CREATE("h") |
845 | //usage: IF_FEATURE_TAR_NOPRESERVE_TIME("m") |
846 | //usage: IF_FEATURE_TAR_SELINUX("p") |
847 | //usage: "vO] " |
848 | //usage: IF_FEATURE_TAR_FROM("[-X FILE] [-T FILE] ") |
849 | //usage: "[-f TARFILE] [-C DIR] [FILE]..." |
850 | //usage:#define tar_full_usage "\n\n" |
851 | //usage: IF_FEATURE_TAR_CREATE("Create, extract, ") |
852 | //usage: IF_NOT_FEATURE_TAR_CREATE("Extract ") |
853 | //usage: "or list files from a tar file\n" |
854 | //usage: "\nOperation:" |
855 | //usage: IF_FEATURE_TAR_CREATE( |
856 | //usage: "\n c Create" |
857 | //usage: ) |
858 | //usage: "\n x Extract" |
859 | //usage: "\n t List" |
860 | //usage: "\n f Name of TARFILE ('-' for stdin/out)" |
861 | //usage: "\n C Change to DIR before operation" |
862 | //usage: "\n v Verbose" |
863 | //usage: IF_FEATURE_SEAMLESS_Z( |
864 | //usage: "\n Z (De)compress using compress" |
865 | //usage: ) |
866 | //usage: IF_FEATURE_SEAMLESS_GZ( |
867 | //usage: "\n z (De)compress using gzip" |
868 | //usage: ) |
869 | //usage: IF_FEATURE_SEAMLESS_XZ( |
870 | //usage: "\n J (De)compress using xz" |
871 | //usage: ) |
872 | //usage: IF_FEATURE_SEAMLESS_BZ2( |
873 | //usage: "\n j (De)compress using bzip2" |
874 | //usage: ) |
875 | //usage: IF_FEATURE_SEAMLESS_LZMA( |
876 | //usage: "\n a (De)compress using lzma" |
877 | //usage: ) |
878 | //usage: "\n O Extract to stdout" |
879 | //usage: IF_FEATURE_TAR_CREATE( |
880 | //usage: "\n h Follow symlinks" |
881 | //usage: ) |
882 | //usage: IF_FEATURE_TAR_NOPRESERVE_TIME( |
883 | //usage: "\n m Don't restore mtime" |
884 | //usage: ) |
885 | //usage: IF_FEATURE_TAR_FROM( |
886 | //usage: IF_FEATURE_TAR_LONG_OPTIONS( |
887 | //usage: "\n exclude File to exclude" |
888 | //usage: ) |
889 | //usage: "\n X File with names to exclude" |
890 | //usage: "\n T File with names to include" |
891 | //usage: ) |
892 | //usage: IF_FEATURE_TAR_SELINUX( |
893 | //usage: "\n p Store SELinux contexts" |
894 | //usage: ) |
895 | //usage: |
896 | //usage:#define tar_example_usage |
897 | //usage: "$ zcat /tmp/tarball.tar.gz | tar -xf -\n" |
898 | //usage: "$ tar -cf /tmp/tarball.tar /usr/local\n" |
899 | |
900 | // Supported but aren't in --help: |
901 | // o no-same-owner |
902 | // p same-permissions |
903 | // k keep-old |
904 | // no-recursion |
905 | // numeric-owner |
906 | // no-same-permissions |
907 | // overwrite |
908 | //IF_FEATURE_TAR_TO_COMMAND( |
909 | // to-command |
910 | //) |
911 | |
912 | enum { |
913 | OPTBIT_KEEP_OLD = 8, |
914 | IF_FEATURE_TAR_CREATE( OPTBIT_CREATE ,) |
915 | IF_FEATURE_TAR_CREATE( OPTBIT_DEREFERENCE ,) |
916 | IF_FEATURE_SEAMLESS_BZ2( OPTBIT_BZIP2 ,) |
917 | IF_FEATURE_SEAMLESS_LZMA(OPTBIT_LZMA ,) |
918 | IF_FEATURE_TAR_FROM( OPTBIT_INCLUDE_FROM,) |
919 | IF_FEATURE_TAR_FROM( OPTBIT_EXCLUDE_FROM,) |
920 | IF_FEATURE_SEAMLESS_GZ( OPTBIT_GZIP ,) |
921 | IF_FEATURE_SEAMLESS_XZ( OPTBIT_XZ ,) // 16th bit |
922 | IF_FEATURE_SEAMLESS_Z( OPTBIT_COMPRESS ,) |
923 | IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,) |
924 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS |
925 | OPTBIT_NORECURSION, |
926 | IF_FEATURE_TAR_TO_COMMAND(OPTBIT_2COMMAND ,) |
927 | OPTBIT_NUMERIC_OWNER, |
928 | OPTBIT_NOPRESERVE_PERM, |
929 | OPTBIT_OVERWRITE, |
930 | #endif |
931 | OPT_TEST = 1 << 0, // t |
932 | OPT_EXTRACT = 1 << 1, // x |
933 | OPT_BASEDIR = 1 << 2, // C |
934 | OPT_TARNAME = 1 << 3, // f |
935 | OPT_2STDOUT = 1 << 4, // O |
936 | OPT_NOPRESERVE_OWNER = 1 << 5, // o == no-same-owner |
937 | OPT_P = 1 << 6, // p |
938 | OPT_VERBOSE = 1 << 7, // v |
939 | OPT_KEEP_OLD = 1 << 8, // k |
940 | OPT_CREATE = IF_FEATURE_TAR_CREATE( (1 << OPTBIT_CREATE )) + 0, // c |
941 | OPT_DEREFERENCE = IF_FEATURE_TAR_CREATE( (1 << OPTBIT_DEREFERENCE )) + 0, // h |
942 | OPT_BZIP2 = IF_FEATURE_SEAMLESS_BZ2( (1 << OPTBIT_BZIP2 )) + 0, // j |
943 | OPT_LZMA = IF_FEATURE_SEAMLESS_LZMA((1 << OPTBIT_LZMA )) + 0, // a |
944 | OPT_INCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_INCLUDE_FROM)) + 0, // T |
945 | OPT_EXCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_EXCLUDE_FROM)) + 0, // X |
946 | OPT_GZIP = IF_FEATURE_SEAMLESS_GZ( (1 << OPTBIT_GZIP )) + 0, // z |
947 | OPT_XZ = IF_FEATURE_SEAMLESS_XZ( (1 << OPTBIT_XZ )) + 0, // J |
948 | OPT_COMPRESS = IF_FEATURE_SEAMLESS_Z( (1 << OPTBIT_COMPRESS )) + 0, // Z |
949 | OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m |
950 | OPT_NORECURSION = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NORECURSION )) + 0, // no-recursion |
951 | OPT_2COMMAND = IF_FEATURE_TAR_TO_COMMAND( (1 << OPTBIT_2COMMAND )) + 0, // to-command |
952 | OPT_NUMERIC_OWNER = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER )) + 0, // numeric-owner |
953 | OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions |
954 | OPT_OVERWRITE = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE )) + 0, // overwrite |
955 | |
956 | OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_COMPRESS), |
957 | }; |
958 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS |
959 | static const char tar_longopts[] ALIGN1 = |
960 | "list\0" No_argument "t" |
961 | "extract\0" No_argument "x" |
962 | "directory\0" Required_argument "C" |
963 | "file\0" Required_argument "f" |
964 | "to-stdout\0" No_argument "O" |
965 | /* do not restore owner */ |
966 | /* Note: GNU tar handles 'o' as no-same-owner only on extract, |
967 | * on create, 'o' is --old-archive. We do not support --old-archive. */ |
968 | "no-same-owner\0" No_argument "o" |
969 | "same-permissions\0" No_argument "p" |
970 | "verbose\0" No_argument "v" |
971 | "keep-old\0" No_argument "k" |
972 | # if ENABLE_FEATURE_TAR_CREATE |
973 | "create\0" No_argument "c" |
974 | "dereference\0" No_argument "h" |
975 | # endif |
976 | # if ENABLE_FEATURE_SEAMLESS_BZ2 |
977 | "bzip2\0" No_argument "j" |
978 | # endif |
979 | # if ENABLE_FEATURE_SEAMLESS_LZMA |
980 | "lzma\0" No_argument "a" |
981 | # endif |
982 | # if ENABLE_FEATURE_TAR_FROM |
983 | "files-from\0" Required_argument "T" |
984 | "exclude-from\0" Required_argument "X" |
985 | # endif |
986 | # if ENABLE_FEATURE_SEAMLESS_GZ |
987 | "gzip\0" No_argument "z" |
988 | # endif |
989 | # if ENABLE_FEATURE_SEAMLESS_XZ |
990 | "xz\0" No_argument "J" |
991 | # endif |
992 | # if ENABLE_FEATURE_SEAMLESS_Z |
993 | "compress\0" No_argument "Z" |
994 | # endif |
995 | # if ENABLE_FEATURE_TAR_NOPRESERVE_TIME |
996 | "touch\0" No_argument "m" |
997 | # endif |
998 | "no-recursion\0" No_argument "\xfa" |
999 | # if ENABLE_FEATURE_TAR_TO_COMMAND |
1000 | "to-command\0" Required_argument "\xfb" |
1001 | # endif |
1002 | /* use numeric uid/gid from tar header, not textual */ |
1003 | "numeric-owner\0" No_argument "\xfc" |
1004 | /* do not restore mode */ |
1005 | "no-same-permissions\0" No_argument "\xfd" |
1006 | /* on unpack, open with O_TRUNC and !O_EXCL */ |
1007 | "overwrite\0" No_argument "\xfe" |
1008 | /* --exclude takes next bit position in option mask, */ |
1009 | /* therefore we have to put it _after_ --no-same-permissions */ |
1010 | # if ENABLE_FEATURE_TAR_FROM |
1011 | "exclude\0" Required_argument "\xff" |
1012 | # endif |
1013 | ; |
1014 | #endif |
1015 | |
1016 | int tar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
1017 | int tar_main(int argc UNUSED_PARAM, char **argv) |
1018 | { |
1019 | archive_handle_t *tar_handle; |
1020 | char *base_dir = NULL; |
1021 | const char *tar_filename = "-"; |
1022 | unsigned opt; |
1023 | int verboseFlag = 0; |
1024 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM |
1025 | llist_t *excludes = NULL; |
1026 | #endif |
1027 | |
1028 | /* Initialise default values */ |
1029 | tar_handle = init_handle(); |
1030 | tar_handle->ah_flags = ARCHIVE_CREATE_LEADING_DIRS |
1031 | | ARCHIVE_RESTORE_DATE |
1032 | | ARCHIVE_UNLINK_OLD; |
1033 | |
1034 | /* Apparently only root's tar preserves perms (see bug 3844) */ |
1035 | if (getuid() != 0) |
1036 | tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_PERM; |
1037 | |
1038 | /* Prepend '-' to the first argument if required */ |
1039 | opt_complementary = "--:" // first arg is options |
1040 | "tt:vv:" // count -t,-v |
1041 | IF_FEATURE_TAR_FROM("X::T::") // cumulative lists |
1042 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM |
1043 | "\xff::" // cumulative lists for --exclude |
1044 | #endif |
1045 | IF_FEATURE_TAR_CREATE("c:") "t:x:" // at least one of these is reqd |
1046 | IF_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive |
1047 | IF_NOT_FEATURE_TAR_CREATE("t--x:x--t"); // mutually exclusive |
1048 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS |
1049 | applet_long_options = tar_longopts; |
1050 | #endif |
1051 | #if ENABLE_DESKTOP |
1052 | if (argv[1] && argv[1][0] != '-') { |
1053 | /* Compat: |
1054 | * 1st argument without dash handles options with parameters |
1055 | * differently from dashed one: it takes *next argv[i]* |
1056 | * as paramenter even if there are more chars in 1st argument: |
1057 | * "tar fx TARFILE" - "x" is not taken as f's param |
1058 | * but is interpreted as -x option |
1059 | * "tar -xf TARFILE" - dashed equivalent of the above |
1060 | * "tar -fx ..." - "x" is taken as f's param |
1061 | * getopt32 wouldn't handle 1st command correctly. |
1062 | * Unfortunately, people do use such commands. |
1063 | * We massage argv[1] to work around it by moving 'f' |
1064 | * to the end of the string. |
1065 | * More contrived "tar fCx TARFILE DIR" still fails, |
1066 | * but such commands are much less likely to be used. |
1067 | */ |
1068 | char *f = strchr(argv[1], 'f'); |
1069 | if (f) { |
1070 | while (f[1] != '\0') { |
1071 | *f = f[1]; |
1072 | f++; |
1073 | } |
1074 | *f = 'f'; |
1075 | } |
1076 | } |
1077 | #endif |
1078 | opt = getopt32(argv, |
1079 | "txC:f:Oopvk" |
1080 | IF_FEATURE_TAR_CREATE( "ch" ) |
1081 | IF_FEATURE_SEAMLESS_BZ2( "j" ) |
1082 | IF_FEATURE_SEAMLESS_LZMA("a" ) |
1083 | IF_FEATURE_TAR_FROM( "T:X:") |
1084 | IF_FEATURE_SEAMLESS_GZ( "z" ) |
1085 | IF_FEATURE_SEAMLESS_XZ( "J" ) |
1086 | IF_FEATURE_SEAMLESS_Z( "Z" ) |
1087 | IF_FEATURE_TAR_NOPRESERVE_TIME("m") |
1088 | , &base_dir // -C dir |
1089 | , &tar_filename // -f filename |
1090 | IF_FEATURE_TAR_FROM(, &(tar_handle->accept)) // T |
1091 | IF_FEATURE_TAR_FROM(, &(tar_handle->reject)) // X |
1092 | IF_FEATURE_TAR_TO_COMMAND(, &(tar_handle->tar__to_command)) // --to-command |
1093 | #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM |
1094 | , &excludes // --exclude |
1095 | #endif |
1096 | , &verboseFlag // combined count for -t and -v |
1097 | , &verboseFlag // combined count for -t and -v |
1098 | ); |
1099 | //bb_error_msg("opt:%08x", opt); |
1100 | argv += optind; |
1101 | |
1102 | if (verboseFlag) tar_handle->action_header = header_verbose_list; |
1103 | if (verboseFlag == 1) tar_handle->action_header = header_list; |
1104 | |
1105 | if (opt & OPT_EXTRACT) |
1106 | tar_handle->action_data = data_extract_all; |
1107 | |
1108 | if (opt & OPT_2STDOUT) |
1109 | tar_handle->action_data = data_extract_to_stdout; |
1110 | |
1111 | if (opt & OPT_2COMMAND) { |
1112 | putenv((char*)"TAR_FILETYPE=f"); |
1113 | signal(SIGPIPE, SIG_IGN); |
1114 | tar_handle->action_data = data_extract_to_command; |
1115 | IF_FEATURE_TAR_TO_COMMAND(tar_handle->tar__to_command_shell = xstrdup(get_shell_name());) |
1116 | } |
1117 | |
1118 | if (opt & OPT_KEEP_OLD) |
1119 | tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD; |
1120 | |
1121 | if (opt & OPT_NUMERIC_OWNER) |
1122 | tar_handle->ah_flags |= ARCHIVE_NUMERIC_OWNER; |
1123 | |
1124 | if (opt & OPT_NOPRESERVE_OWNER) |
1125 | tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_OWNER; |
1126 | |
1127 | if (opt & OPT_NOPRESERVE_PERM) |
1128 | tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_PERM; |
1129 | |
1130 | #if ENABLE_FEATURE_TAR_SELINUX |
1131 | if (opt & OPT_P) |
1132 | tar_handle->ah_flags |= ARCHIVE_STORE_SELINUX; |
1133 | #endif |
1134 | |
1135 | if (opt & OPT_OVERWRITE) { |
1136 | tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD; |
1137 | tar_handle->ah_flags |= ARCHIVE_O_TRUNC; |
1138 | } |
1139 | |
1140 | if (opt & OPT_NOPRESERVE_TIME) |
1141 | tar_handle->ah_flags &= ~ARCHIVE_RESTORE_DATE; |
1142 | |
1143 | #if ENABLE_FEATURE_TAR_FROM |
1144 | tar_handle->reject = append_file_list_to_list(tar_handle->reject); |
1145 | # if ENABLE_FEATURE_TAR_LONG_OPTIONS |
1146 | /* Append excludes to reject */ |
1147 | while (excludes) { |
1148 | llist_t *next = excludes->link; |
1149 | excludes->link = tar_handle->reject; |
1150 | tar_handle->reject = excludes; |
1151 | excludes = next; |
1152 | } |
1153 | # endif |
1154 | tar_handle->accept = append_file_list_to_list(tar_handle->accept); |
1155 | #endif |
1156 | |
1157 | /* Setup an array of filenames to work with */ |
1158 | /* TODO: This is the same as in ar, make a separate function? */ |
1159 | while (*argv) { |
1160 | /* kill trailing '/' unless the string is just "/" */ |
1161 | char *cp = last_char_is(*argv, '/'); |
1162 | if (cp > *argv) |
1163 | *cp = '\0'; |
1164 | llist_add_to_end(&tar_handle->accept, *argv); |
1165 | argv++; |
1166 | } |
1167 | |
1168 | if (tar_handle->accept || tar_handle->reject) |
1169 | tar_handle->filter = filter_accept_reject_list; |
1170 | |
1171 | /* Open the tar file */ |
1172 | { |
1173 | int tar_fd = STDIN_FILENO; |
1174 | int flags = O_RDONLY; |
1175 | |
1176 | if (opt & OPT_CREATE) { |
1177 | /* Make sure there is at least one file to tar up */ |
1178 | if (tar_handle->accept == NULL) |
1179 | bb_error_msg_and_die("empty archive"); |
1180 | |
1181 | tar_fd = STDOUT_FILENO; |
1182 | /* Mimicking GNU tar 1.15.1: */ |
1183 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
1184 | } |
1185 | |
1186 | if (LONE_DASH(tar_filename)) { |
1187 | tar_handle->src_fd = tar_fd; |
1188 | tar_handle->seek = seek_by_read; |
1189 | } else { |
1190 | if (ENABLE_FEATURE_TAR_AUTODETECT |
1191 | && flags == O_RDONLY |
1192 | && !(opt & OPT_ANY_COMPRESS) |
1193 | ) { |
1194 | tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0); |
1195 | if (tar_handle->src_fd < 0) |
1196 | bb_perror_msg_and_die("can't open '%s'", tar_filename); |
1197 | } else { |
1198 | tar_handle->src_fd = xopen(tar_filename, flags); |
1199 | } |
1200 | } |
1201 | } |
1202 | |
1203 | if (base_dir) |
1204 | xchdir(base_dir); |
1205 | |
1206 | //if (SEAMLESS_COMPRESSION || OPT_COMPRESS) |
1207 | // /* We need to know whether child (gzip/bzip/etc) exits abnormally */ |
1208 | // signal(SIGCHLD, check_errors_in_children); |
1209 | |
1210 | /* Create an archive */ |
1211 | if (opt & OPT_CREATE) { |
1212 | #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2 |
1213 | int zipMode = 0; |
1214 | if (ENABLE_FEATURE_SEAMLESS_GZ && (opt & OPT_GZIP)) |
1215 | zipMode = 1; |
1216 | if (ENABLE_FEATURE_SEAMLESS_BZ2 && (opt & OPT_BZIP2)) |
1217 | zipMode = 2; |
1218 | #endif |
1219 | /* NB: writeTarFile() closes tar_handle->src_fd */ |
1220 | return writeTarFile(tar_handle->src_fd, verboseFlag, |
1221 | tar_handle->ah_flags, |
1222 | (opt & OPT_DEREFERENCE ? ACTION_FOLLOWLINKS : 0) |
1223 | | (opt & OPT_NORECURSION ? 0 : ACTION_RECURSE), |
1224 | tar_handle->accept, |
1225 | tar_handle->reject, zipMode); |
1226 | } |
1227 | |
1228 | if (opt & OPT_ANY_COMPRESS) { |
1229 | USE_FOR_MMU(IF_DESKTOP(long long) int FAST_FUNC (*xformer)(transformer_aux_data_t *aux, int src_fd, int dst_fd);) |
1230 | USE_FOR_NOMMU(const char *xformer_prog;) |
1231 | |
1232 | if (opt & OPT_COMPRESS) |
1233 | USE_FOR_MMU(xformer = unpack_Z_stream;) |
1234 | USE_FOR_NOMMU(xformer_prog = "uncompress";) |
1235 | if (opt & OPT_GZIP) |
1236 | USE_FOR_MMU(xformer = unpack_gz_stream;) |
1237 | USE_FOR_NOMMU(xformer_prog = "gunzip";) |
1238 | if (opt & OPT_BZIP2) |
1239 | USE_FOR_MMU(xformer = unpack_bz2_stream;) |
1240 | USE_FOR_NOMMU(xformer_prog = "bunzip2";) |
1241 | if (opt & OPT_LZMA) |
1242 | USE_FOR_MMU(xformer = unpack_lzma_stream;) |
1243 | USE_FOR_NOMMU(xformer_prog = "unlzma";) |
1244 | if (opt & OPT_XZ) |
1245 | USE_FOR_MMU(xformer = unpack_xz_stream;) |
1246 | USE_FOR_NOMMU(xformer_prog = "unxz";) |
1247 | |
1248 | open_transformer_with_sig(tar_handle->src_fd, xformer, xformer_prog); |
1249 | /* Can't lseek over pipes */ |
1250 | tar_handle->seek = seek_by_read; |
1251 | /*tar_handle->offset = 0; - already is */ |
1252 | } |
1253 | |
1254 | /* Zero processed headers (== empty file) is not a valid tarball. |
1255 | * We (ab)use bb_got_signal as exitcode here, |
1256 | * because check_errors_in_children() uses _it_ as error indicator. |
1257 | */ |
1258 | bb_got_signal = EXIT_FAILURE; |
1259 | |
1260 | while (get_header_tar(tar_handle) == EXIT_SUCCESS) |
1261 | bb_got_signal = EXIT_SUCCESS; /* saw at least one header, good */ |
1262 | |
1263 | /* Check that every file that should have been extracted was */ |
1264 | while (tar_handle->accept) { |
1265 | if (!find_list_entry(tar_handle->reject, tar_handle->accept->data) |
1266 | && !find_list_entry(tar_handle->passed, tar_handle->accept->data) |
1267 | ) { |
1268 | bb_error_msg_and_die("%s: not found in archive", |
1269 | tar_handle->accept->data); |
1270 | } |
1271 | tar_handle->accept = tar_handle->accept->link; |
1272 | } |
1273 | if (ENABLE_FEATURE_CLEAN_UP /* && tar_handle->src_fd != STDIN_FILENO */) |
1274 | close(tar_handle->src_fd); |
1275 | |
1276 | if (SEAMLESS_COMPRESSION || OPT_COMPRESS) { |
1277 | /* Set bb_got_signal to 1 if a child died with !0 exitcode */ |
1278 | check_errors_in_children(0); |
1279 | } |
1280 | |
1281 | return bb_got_signal; |
1282 | } |
1283 |