ar: heh, there *was* "-1" already...
[oweals/busybox.git] / archival / tar.c
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 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 #include "libbb.h"
28 #include "unarchive.h"
29 /* FIXME: Stop using this non-standard feature */
30 #ifndef FNM_LEADING_DIR
31 # define FNM_LEADING_DIR 0
32 #endif
33
34
35 //#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
36 #define DBG(...) ((void)0)
37
38
39 #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 #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 typedef struct TarHeader {       /* byte offset */
59         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         /* 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         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         char padding[12];         /* 500-512 (pad to exactly TAR_BLOCK_SIZE) */
80 } TarHeader;
81
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 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
95 /* Some info to be carried along when creating a new tarball */
96 typedef struct TarBallInfo {
97         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 //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
110 /* A nice enum with all the possible tar file content types */
111 enum {
112         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 static void addHardLinkInfo(HardLinkInfo **hlInfoHeadPtr,
127                                         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 //      hlInfo->linkCount = statbuf->st_nlink;
139         strcpy(hlInfo->name, fileName);
140 }
141
142 static void freeHardLinkInfo(HardLinkInfo **hlInfoHeadPtr)
143 {
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 /* Might be faster (and bigger) if the dev/ino were stored in numeric order ;) */
159 static HardLinkInfo *findHardLinkInfo(HardLinkInfo *hlInfo, struct stat *statbuf)
160 {
161         while (hlInfo) {
162                 if (statbuf->st_ino == hlInfo->ino
163                  && statbuf->st_dev == hlInfo->dev
164                 ) {
165                         DBG("found hardlink:'%s'", hlInfo->name);
166                         break;
167                 }
168                 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         char tempBuffer[sizeof(off_t)*3 + 1];
179         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         /* 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         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                 char *lpath = xmalloc_readlink_or_warn(fileName);
302                 if (!lpath)
303                         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                         bb_error_msg_and_die("can't store file '%s' "
339                                 "of size %"OFF_FMT"u, aborting",
340                                 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                 /* If archive goes to stdout, verbose goes to stderr */
365                 if (tbInfo->tarFd == STDOUT_FILENO)
366                         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                                         FNM_PATHNAME | FNM_LEADING_DIR) == 0)
384                                 return 1;
385                 } else {
386                         const char *p;
387
388                         for (p = file; p[0] != '\0'; p++) {
389                                 if ((p == file || p[-1] == '/')
390                                  && p[0] != '/'
391                                  && fnmatch(excluded_files->data, p,
392                                                 FNM_PATHNAME | FNM_LEADING_DIR) == 0
393                                 ) {
394                                         return 1;
395                                 }
396                         }
397                 }
398                 excluded_files = excluded_files->link;
399         }
400
401         return 0;
402 }
403 #else
404 # define exclude_file(excluded_files, file) 0
405 #endif
406
407 static int FAST_FUNC writeFileToTarball(const char *fileName, struct stat *statbuf,
408                         void *userData, int depth UNUSED_PARAM)
409 {
410         struct TarBallInfo *tbInfo = (struct TarBallInfo *) userData;
411         const char *header_name;
412         int inputFileFd = -1;
413
414         DBG("writeFileToTarball('%s')", fileName);
415
416         /* 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         /*
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         if (!S_ISDIR(statbuf->st_mode) && statbuf->st_nlink > 1) {
446                 DBG("'%s': st_nlink > 1", header_name);
447                 tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf);
448                 if (tbInfo->hlInfo == NULL) {
449                         DBG("'%s': addHardLinkInfo", header_name);
450                         addHardLinkInfo(&tbInfo->hlInfoHead, statbuf, header_name);
451                 }
452         }
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         if (tbInfo->tarFileStatBuf.st_dev == statbuf->st_dev
458          && tbInfo->tarFileStatBuf.st_ino == statbuf->st_ino
459         ) {
460                 bb_error_msg("%s: file is the archive; skipping", fileName);
461                 return TRUE;
462         }
463
464         if (exclude_file(tbInfo->excludeList, header_name))
465                 return SKIP;
466
467 #if !ENABLE_FEATURE_TAR_GNU_EXTENSIONS
468         if (strlen(header_name) >= NAME_SIZE) {
469                 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                 inputFileFd = open_or_warn(fileName, O_RDONLY);
478                 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                 /* Write the file to the archive. */
492                 /* 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                 memset(block_buf, 0, readSize);
513                 xwrite(tbInfo->tarFd, block_buf, readSize);
514         }
515
516         return TRUE;
517 }
518
519 #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2
520 # if !(ENABLE_FEATURE_SEAMLESS_GZ && ENABLE_FEATURE_SEAMLESS_BZ2)
521 #  define vfork_compressor(tar_fd, gzip) vfork_compressor(tar_fd)
522 # endif
523 /* Don't inline: vfork scares gcc and pessimizes code */
524 static void NOINLINE vfork_compressor(int tar_fd, int gzip)
525 {
526         pid_t gzipPid;
527 # if ENABLE_FEATURE_SEAMLESS_GZ && ENABLE_FEATURE_SEAMLESS_BZ2
528         const char *zip_exec = (gzip == 1) ? "gzip" : "bzip2";
529 # elif ENABLE_FEATURE_SEAMLESS_GZ
530         const char *zip_exec = "gzip";
531 # else /* only ENABLE_FEATURE_SEAMLESS_BZ2 */
532         const char *zip_exec = "bzip2";
533 # endif
534         // On Linux, vfork never unpauses parent early, although standard
535         // allows for that. Do we want to waste bytes checking for it?
536 # define WAIT_FOR_CHILD 0
537         volatile int vfork_exec_errno = 0;
538         struct fd_pair gzipDataPipe;
539 # if WAIT_FOR_CHILD
540         struct fd_pair gzipStatusPipe;
541         xpiped_pair(gzipStatusPipe);
542 # endif
543         xpiped_pair(gzipDataPipe);
544
545         signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */
546
547 # if defined(__GNUC__) && __GNUC__
548         /* Avoid vfork clobbering */
549         (void) &zip_exec;
550 # endif
551
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 # if WAIT_FOR_CHILD
561                 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 # endif
566                 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 # if WAIT_FOR_CHILD
578         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 # endif
590         if (vfork_exec_errno) {
591                 errno = vfork_exec_errno;
592                 bb_perror_msg_and_die("can't execute '%s'", zip_exec);
593         }
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         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         if (fstat(tbInfo.tarFd, &tbInfo.tarFileStatBuf) < 0)
613                 bb_perror_msg_and_die("can't stat tar file");
614
615 #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2
616         if (gzip)
617                 vfork_compressor(tbInfo.tarFd, gzip);
618 #endif
619
620         tbInfo.excludeList = exclude;
621
622         /* Read the directory/files and iterate over them one at a time */
623         while (include) {
624                 if (!recursive_action(include->data, ACTION_RECURSE |
625                                 (dereferenceFlag ? ACTION_FOLLOWLINKS : 0),
626                                 writeFileToTarball, writeFileToTarball, &tbInfo, 0)
627                 ) {
628                         errorFlag = TRUE;
629                 }
630                 include = include->link;
631         }
632         /* Write two empty blocks to the end of the archive */
633         memset(block_buf, 0, 2*TAR_BLOCK_SIZE);
634         xwrite(tbInfo.tarFd, block_buf, 2*TAR_BLOCK_SIZE);
635
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 #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2
652         if (gzip) {
653                 int status;
654                 if (safe_waitpid(-1, &status, 0) == -1)
655                         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 #endif
661         return errorFlag;
662 }
663 #else
664 int writeTarFile(int tar_fd, int verboseFlag,
665         int dereferenceFlag, const llist_t *include,
666         const llist_t *exclude, int gzip);
667 #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         while (list) {
677                 src_stream = xfopen_for_read(llist_pop(&list));
678                 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
679                         /* 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 # define append_file_list_to_list(x) 0
691 #endif
692
693 #if ENABLE_FEATURE_SEAMLESS_Z
694 static char FAST_FUNC get_header_tar_Z(archive_handle_t *archive_handle)
695 {
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         open_transformer(archive_handle->src_fd, unpack_Z_stream, "uncompress");
707         archive_handle->offset = 0;
708         while (get_header_tar(archive_handle) == EXIT_SUCCESS)
709                 continue;
710
711         /* Can only do one file at a time */
712         return EXIT_FAILURE;
713 }
714 #else
715 # define get_header_tar_Z NULL
716 #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         if (wait_any_nohang(&status) < 0)
729                 /* wait failed?! I'm confused... */
730                 return;
731
732         if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
733                 /* child exited with 0 */
734                 return;
735         /* Cannot happen?
736         if (!WIFSIGNALED(status) && !WIFEXITED(status)) return; */
737         child_error = 1;
738 }
739 #endif
740
741 enum {
742         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 ENABLE_FEATURE_TAR_LONG_OPTIONS
752         OPTBIT_NUMERIC_OWNER,
753         OPTBIT_NOPRESERVE_PERM,
754         OPTBIT_OVERWRITE,
755 #endif
756         OPT_TEST         = 1 << 0, // t
757         OPT_EXTRACT      = 1 << 1, // x
758         OPT_BASEDIR      = 1 << 2, // C
759         OPT_TARNAME      = 1 << 3, // f
760         OPT_2STDOUT      = 1 << 4, // O
761         OPT_NOPRESERVE_OWNER = 1 << 5, // o == no-same-owner
762         OPT_P            = 1 << 6, // p
763         OPT_VERBOSE      = 1 << 7, // v
764         OPT_KEEP_OLD     = 1 << 8, // k
765         OPT_CREATE       = IF_FEATURE_TAR_CREATE(   (1 << OPTBIT_CREATE      )) + 0, // c
766         OPT_DEREFERENCE  = IF_FEATURE_TAR_CREATE(   (1 << OPTBIT_DEREFERENCE )) + 0, // h
767         OPT_BZIP2        = IF_FEATURE_SEAMLESS_BZ2( (1 << OPTBIT_BZIP2       )) + 0, // j
768         OPT_LZMA         = IF_FEATURE_SEAMLESS_LZMA((1 << OPTBIT_LZMA        )) + 0, // a
769         OPT_INCLUDE_FROM = IF_FEATURE_TAR_FROM(     (1 << OPTBIT_INCLUDE_FROM)) + 0, // T
770         OPT_EXCLUDE_FROM = IF_FEATURE_TAR_FROM(     (1 << OPTBIT_EXCLUDE_FROM)) + 0, // X
771         OPT_GZIP         = IF_FEATURE_SEAMLESS_GZ(  (1 << OPTBIT_GZIP        )) + 0, // z
772         OPT_COMPRESS     = IF_FEATURE_SEAMLESS_Z(   (1 << OPTBIT_COMPRESS    )) + 0, // Z
773         OPT_NUMERIC_OWNER   = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER  )) + 0, // numeric-owner
774         OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
775         OPT_OVERWRITE       = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE      )) + 0, // overwrite
776 };
777 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
778 static const char tar_longopts[] ALIGN1 =
779         "list\0"                No_argument       "t"
780         "extract\0"             No_argument       "x"
781         "directory\0"           Required_argument "C"
782         "file\0"                Required_argument "f"
783         "to-stdout\0"           No_argument       "O"
784         /* do not restore owner */
785         /* Note: GNU tar handles 'o' as no-same-owner only on extract,
786          * on create, 'o' is --old-archive. We do not support --old-archive. */
787         "no-same-owner\0"       No_argument       "o"
788         "same-permissions\0"    No_argument       "p"
789         "verbose\0"             No_argument       "v"
790         "keep-old\0"            No_argument       "k"
791 # if ENABLE_FEATURE_TAR_CREATE
792         "create\0"              No_argument       "c"
793         "dereference\0"         No_argument       "h"
794 # endif
795 # if ENABLE_FEATURE_SEAMLESS_BZ2
796         "bzip2\0"               No_argument       "j"
797 # endif
798 # if ENABLE_FEATURE_SEAMLESS_LZMA
799         "lzma\0"                No_argument       "a"
800 # endif
801 # if ENABLE_FEATURE_TAR_FROM
802         "files-from\0"          Required_argument "T"
803         "exclude-from\0"        Required_argument "X"
804 # endif
805 # if ENABLE_FEATURE_SEAMLESS_GZ
806         "gzip\0"                No_argument       "z"
807 # endif
808 # if ENABLE_FEATURE_SEAMLESS_Z
809         "compress\0"            No_argument       "Z"
810 # endif
811         /* use numeric uid/gid from tar header, not textual */
812         "numeric-owner\0"       No_argument       "\xfc"
813         /* do not restore mode */
814         "no-same-permissions\0" No_argument       "\xfd"
815         /* on unpack, open with O_TRUNC and !O_EXCL */
816         "overwrite\0"           No_argument       "\xfe"
817         /* --exclude takes next bit position in option mask, */
818         /* therefore we have to put it _after_ --no-same-permissions */
819 # if ENABLE_FEATURE_TAR_FROM
820         "exclude\0"             Required_argument "\xff"
821 # endif
822         ;
823 #endif
824
825 int tar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
826 int tar_main(int argc UNUSED_PARAM, char **argv)
827 {
828         char FAST_FUNC (*get_header_ptr)(archive_handle_t *) = get_header_tar;
829         archive_handle_t *tar_handle;
830         char *base_dir = NULL;
831         const char *tar_filename = "-";
832         unsigned opt;
833         int verboseFlag = 0;
834 #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
835         llist_t *excludes = NULL;
836 #endif
837
838         /* Initialise default values */
839         tar_handle = init_handle();
840         tar_handle->ah_flags = ARCHIVE_CREATE_LEADING_DIRS
841                              | ARCHIVE_RESTORE_DATE
842                              | ARCHIVE_UNLINK_OLD;
843
844         /* Apparently only root's tar preserves perms (see bug 3844) */
845         if (getuid() != 0)
846                 tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_PERM;
847
848         /* Prepend '-' to the first argument if required */
849         opt_complementary = "--:" // first arg is options
850                 "tt:vv:" // count -t,-v
851                 "?:" // bail out with usage instead of error return
852                 "X::T::" // cumulative lists
853 #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
854                 "\xff::" // cumulative lists for --exclude
855 #endif
856                 IF_FEATURE_TAR_CREATE("c:") "t:x:" // at least one of these is reqd
857                 IF_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive
858                 IF_NOT_FEATURE_TAR_CREATE("t--x:x--t"); // mutually exclusive
859 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
860         applet_long_options = tar_longopts;
861 #endif
862 #if ENABLE_DESKTOP
863         if (argv[1] && argv[1][0] != '-') {
864                 /* Compat:
865                  * 1st argument without dash handles options with parameters
866                  * differently from dashed one: it takes *next argv[i]*
867                  * as paramenter even if there are more chars in 1st argument:
868                  *  "tar fx TARFILE" - "x" is not taken as f's param
869                  *  but is interpreted as -x option
870                  *  "tar -xf TARFILE" - dashed equivalent of the above
871                  *  "tar -fx ..." - "x" is taken as f's param
872                  * getopt32 wouldn't handle 1st command correctly.
873                  * Unfortunately, people do use such commands.
874                  * We massage argv[1] to work around it by moving 'f'
875                  * to the end of the string.
876                  * More contrived "tar fCx TARFILE DIR" still fails,
877                  * but such commands are much less likely to be used.
878                  */
879                 char *f = strchr(argv[1], 'f');
880                 if (f) {
881                         while (f[1] != '\0') {
882                                 *f = f[1];
883                                 f++;
884                         }
885                         *f = 'f';
886                 }
887         }
888 #endif
889         opt = getopt32(argv,
890                 "txC:f:Oopvk"
891                 IF_FEATURE_TAR_CREATE(   "ch"  )
892                 IF_FEATURE_SEAMLESS_BZ2( "j"   )
893                 IF_FEATURE_SEAMLESS_LZMA("a"   )
894                 IF_FEATURE_TAR_FROM(     "T:X:")
895                 IF_FEATURE_SEAMLESS_GZ(  "z"   )
896                 IF_FEATURE_SEAMLESS_Z(   "Z"   )
897                 , &base_dir // -C dir
898                 , &tar_filename // -f filename
899                 IF_FEATURE_TAR_FROM(, &(tar_handle->accept)) // T
900                 IF_FEATURE_TAR_FROM(, &(tar_handle->reject)) // X
901 #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
902                 , &excludes // --exclude
903 #endif
904                 , &verboseFlag // combined count for -t and -v
905                 , &verboseFlag // combined count for -t and -v
906                 );
907         //bb_error_msg("opt:%08x", opt);
908         argv += optind;
909
910         if (verboseFlag) tar_handle->action_header = header_verbose_list;
911         if (verboseFlag == 1) tar_handle->action_header = header_list;
912
913         if (opt & OPT_EXTRACT)
914                 tar_handle->action_data = data_extract_all;
915
916         if (opt & OPT_2STDOUT)
917                 tar_handle->action_data = data_extract_to_stdout;
918
919         if (opt & OPT_KEEP_OLD)
920                 tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD;
921
922         if (opt & OPT_NUMERIC_OWNER)
923                 tar_handle->ah_flags |= ARCHIVE_NUMERIC_OWNER;
924
925         if (opt & OPT_NOPRESERVE_OWNER)
926                 tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_OWNER;
927
928         if (opt & OPT_NOPRESERVE_PERM)
929                 tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_PERM;
930
931         if (opt & OPT_OVERWRITE) {
932                 tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD;
933                 tar_handle->ah_flags |= ARCHIVE_O_TRUNC;
934         }
935
936         if (opt & OPT_GZIP)
937                 get_header_ptr = get_header_tar_gz;
938
939         if (opt & OPT_BZIP2)
940                 get_header_ptr = get_header_tar_bz2;
941
942         if (opt & OPT_LZMA)
943                 get_header_ptr = get_header_tar_lzma;
944
945         if (opt & OPT_COMPRESS)
946                 get_header_ptr = get_header_tar_Z;
947
948 #if ENABLE_FEATURE_TAR_FROM
949         tar_handle->reject = append_file_list_to_list(tar_handle->reject);
950 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
951         /* Append excludes to reject */
952         while (excludes) {
953                 llist_t *next = excludes->link;
954                 excludes->link = tar_handle->reject;
955                 tar_handle->reject = excludes;
956                 excludes = next;
957         }
958 #endif
959         tar_handle->accept = append_file_list_to_list(tar_handle->accept);
960 #endif
961
962         /* Setup an array of filenames to work with */
963         /* TODO: This is the same as in ar, separate function ? */
964         while (*argv) {
965                 /* kill trailing '/' unless the string is just "/" */
966                 char *cp = last_char_is(*argv, '/');
967                 if (cp > *argv)
968                         *cp = '\0';
969                 llist_add_to_end(&tar_handle->accept, *argv);
970                 argv++;
971         }
972
973         if (tar_handle->accept || tar_handle->reject)
974                 tar_handle->filter = filter_accept_reject_list;
975
976         /* Open the tar file */
977         {
978                 int tar_fd = STDIN_FILENO;
979                 int flags = O_RDONLY;
980
981                 if (opt & OPT_CREATE) {
982                         /* Make sure there is at least one file to tar up.  */
983                         if (tar_handle->accept == NULL)
984                                 bb_error_msg_and_die("empty archive");
985
986                         tar_fd = STDOUT_FILENO;
987                         /* Mimicking GNU tar 1.15.1: */
988                         flags = O_WRONLY | O_CREAT | O_TRUNC;
989                 }
990
991                 if (LONE_DASH(tar_filename)) {
992                         tar_handle->src_fd = tar_fd;
993                         tar_handle->seek = seek_by_read;
994                 } else {
995                         if (ENABLE_FEATURE_TAR_AUTODETECT && flags == O_RDONLY) {
996                                 get_header_ptr = get_header_tar;
997                                 tar_handle->src_fd = open_zipped(tar_filename);
998                                 if (tar_handle->src_fd < 0)
999                                         bb_perror_msg_and_die("can't open '%s'", tar_filename);
1000                         } else {
1001                                 tar_handle->src_fd = xopen(tar_filename, flags);
1002                         }
1003                 }
1004         }
1005
1006         if (base_dir)
1007                 xchdir(base_dir);
1008
1009 #ifdef CHECK_FOR_CHILD_EXITCODE
1010         /* We need to know whether child (gzip/bzip/etc) exits abnormally */
1011         signal(SIGCHLD, handle_SIGCHLD);
1012 #endif
1013
1014         /* create an archive */
1015         if (opt & OPT_CREATE) {
1016 #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2
1017                 int zipMode = 0;
1018                 if (ENABLE_FEATURE_SEAMLESS_GZ && (opt & OPT_GZIP))
1019                         zipMode = 1;
1020                 if (ENABLE_FEATURE_SEAMLESS_BZ2 && (opt & OPT_BZIP2))
1021                         zipMode = 2;
1022 #endif
1023                 /* NB: writeTarFile() closes tar_handle->src_fd */
1024                 return writeTarFile(tar_handle->src_fd, verboseFlag, opt & OPT_DEREFERENCE,
1025                                 tar_handle->accept,
1026                                 tar_handle->reject, zipMode);
1027         }
1028
1029         while (get_header_ptr(tar_handle) == EXIT_SUCCESS)
1030                 continue;
1031
1032         /* Check that every file that should have been extracted was */
1033         while (tar_handle->accept) {
1034                 if (!find_list_entry(tar_handle->reject, tar_handle->accept->data)
1035                  && !find_list_entry(tar_handle->passed, tar_handle->accept->data)
1036                 ) {
1037                         bb_error_msg_and_die("%s: not found in archive",
1038                                 tar_handle->accept->data);
1039                 }
1040                 tar_handle->accept = tar_handle->accept->link;
1041         }
1042         if (ENABLE_FEATURE_CLEAN_UP /* && tar_handle->src_fd != STDIN_FILENO */)
1043                 close(tar_handle->src_fd);
1044
1045         return EXIT_SUCCESS;
1046 }