• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* tar.c - create/extract archives
2  *
3  * Copyright 2014 Ashwini Kumar <ak.ashwini81@gmail.com>
4  *
5  * For the command, see
6  *   http://pubs.opengroup.org/onlinepubs/007908799/xcu/tar.html
7  * For the modern file format, see
8  *   http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_06
9  *   https://en.wikipedia.org/wiki/Tar_(computing)#File_format
10  *   https://www.gnu.org/software/tar/manual/html_node/Tar-Internals.html
11  *
12  * For writing to external program
13  * http://www.gnu.org/software/tar/manual/html_node/Writing-to-an-External-Program.html
14  *
15  * Toybox will never implement the "pax" command as a matter of policy.
16  *
17  * TODO: --wildcard state changes aren't positional.
18  * We always --verbatim-files-from
19  * Why --exclude pattern but no --include? tar cvzf a.tgz dir --include '*.txt'
20  * No --no-null because the args infrastructure isn't ready.
21  * Until args.c learns about no- toggles, --no-thingy always wins over --thingy
22 
23 USE_TAR(NEWTOY(tar, "&(no-ignore-case)(ignore-case)(no-anchored)(anchored)(no-wildcards)(wildcards)(no-wildcards-match-slash)(wildcards-match-slash)(show-transformed-names)(selinux)(restrict)(full-time)(no-recursion)(null)(numeric-owner)(no-same-permissions)(overwrite)(exclude)*(sort);:(mode):(mtime):(group):(owner):(to-command):~(strip-components)(strip)#~(transform)(xform)*o(no-same-owner)p(same-permissions)k(keep-old)c(create)|h(dereference)x(extract)|t(list)|v(verbose)J(xz)j(bzip2)z(gzip)S(sparse)O(to-stdout)P(absolute-names)m(touch)X(exclude-from)*T(files-from)*I(use-compress-program):C(directory):f(file):as[!txc][!jzJa]", TOYFLAG_USR|TOYFLAG_BIN))
24 
25 config TAR
26   bool "tar"
27   default y
28   help
29     usage: tar [-cxt] [-fvohmjkOS] [-XTCf NAME] [--selinux] [FILE...]
30 
31     Create, extract, or list files in a .tar (or compressed t?z) file.
32 
33     Options:
34     c  Create                x  Extract               t  Test (list)
35     f  tar FILE (default -)  C  Change to DIR first   v  Verbose display
36     J  xz compression        j  bzip2 compression     z  gzip compression
37     o  Ignore owner          h  Follow symlinks       m  Ignore mtime
38     O  Extract to stdout     X  exclude names in FILE T  include names in FILE
39     s  Sort dirs (--sort)
40 
41     --exclude        FILENAME to exclude  --full-time         Show seconds with -tv
42     --mode MODE      Adjust permissions   --owner NAME[:UID]  Set file ownership
43     --mtime TIME     Override timestamps  --group NAME[:GID]  Set file group
44     --sparse         Record sparse files  --selinux           Save/restore labels
45     --restrict       All under one dir    --no-recursion      Skip dir contents
46     --numeric-owner  Use numeric uid/gid, not user/group names
47     --null           Filenames in -T FILE are null-separated, not newline
48     --strip-components NUM  Ignore first NUM directory components when extracting
49     --xform=SED      Modify filenames via SED expression (ala s/find/replace/g)
50     -I PROG          Filter through PROG to compress or PROG -d to decompress
51 
52     Filename filter types. Create command line args aren't filtered, extract
53     defaults to --anchored, --exclude defaults to --wildcards-match-slash,
54     use no- prefix to disable:
55 
56     --anchored  Match name not path       --ignore-case       Case insensitive
57     --wildcards Expand *?[] like shell    --wildcards-match-slash
58 */
59 
60 #define FOR_tar
61 #include "toys.h"
62 
63 GLOBALS(
64   char *f, *C, *I;
65   struct arg_list *T, *X, *xform;
66   long strip;
67   char *to_command, *owner, *group, *mtime, *mode, *sort;
68   struct arg_list *exclude;
69 
70   struct double_list *incl, *excl, *seen;
71   struct string_list *dirs;
72   char *cwd, **xfsed;
73   int fd, ouid, ggid, hlc, warn, sparselen, pid, xfpipe[2];
74   struct dev_ino archive_di;
75   long long *sparse;
76   time_t mtt;
77 
78   // hardlinks seen so far (hlc many)
79   struct {
80     char *arg;
81     struct dev_ino di;
82   } *hlx;
83 
84   // Parsed information about a tar header.
85   struct tar_header {
86     char *name, *link_target, *uname, *gname;
87     long long size, ssize;
88     uid_t uid;
89     gid_t gid;
90     mode_t mode;
91     time_t mtime;
92     dev_t device;
93   } hdr;
94 )
95 
96 // The on-disk 512 byte record structure.
97 struct tar_hdr {
98   char name[100], mode[8], uid[8], gid[8], size[12], mtime[12], chksum[8],
99        type, link[100], magic[8], uname[32], gname[32], major[8], minor[8],
100        prefix[155], padd[12];
101 };
102 
103 // Tar uses ASCII octal when it fits, base-256 otherwise.
ascii_fits(unsigned long long val,int len)104 static int ascii_fits(unsigned long long val, int len)
105 {
106   return !(val>>(3*(len-1)));
107 }
108 
109 // convert from int to octal (or base-256)
itoo(char * str,int len,unsigned long long val)110 static void itoo(char *str, int len, unsigned long long val)
111 {
112   if (ascii_fits(val, len)) sprintf(str, "%0*llo", len-1, val);
113   else {
114     for (str += len; len--; val >>= 8) *--str = val;
115     *str = 128;
116   }
117 }
118 #define ITOO(x, y) itoo(x, sizeof(x), y)
119 
120 // convert octal (or base-256) to int
otoi(char * str,unsigned len)121 static unsigned long long otoi(char *str, unsigned len)
122 {
123   unsigned long long val = 0;
124 
125   // When tar value too big or octal, use binary encoding with high bit set
126   if (128&*str) while (--len) {
127     if (val<<8 < val) error_exit("bad header");
128     val = (val<<8)+*++str;
129   } else {
130     while (len && *str == ' ') str++;
131     while (len && *str>='0' && *str<='7') val = val*8+*str++-'0', len--;
132     if (len && *str && *str != ' ') error_exit("bad header");
133   }
134 
135   return val;
136 }
137 #define OTOI(x) otoi(x, sizeof(x))
138 
write_prefix_block(char * data,int len,char type)139 static void write_prefix_block(char *data, int len, char type)
140 {
141   struct tar_hdr tmp;
142 
143   memset(&tmp, 0, sizeof(tmp));
144   sprintf(tmp.name, "././@%s", type=='x' ? "PaxHeaders" : "LongLink");
145   ITOO(tmp.uid, 0);
146   ITOO(tmp.gid, 0);
147   ITOO(tmp.size, len);
148   ITOO(tmp.mtime, 0);
149   tmp.type = type;
150   strcpy(tmp.magic, "ustar  ");
151 
152   // Historical nonsense to match other implementations. Never used.
153   ITOO(tmp.mode, 0644);
154   strcpy(tmp.uname, "root");
155   strcpy(tmp.gname, "root");
156 
157   // Calculate checksum. Since 512*255 = 0377000 in octal, this can never
158   // use more than 6 digits. The last byte is ' ' for historical reasons.
159   itoo(tmp.chksum, sizeof(tmp.chksum)-1, tar_cksum(&tmp));
160   tmp.chksum[7] = ' ';
161 
162   // write header and name, padded with NUL to block size
163   xwrite(TT.fd, &tmp, 512);
164   xwrite(TT.fd, data, len);
165   if (len%512) xwrite(TT.fd, toybuf, 512-(len%512));
166 }
167 
maybe_prefix_block(char * data,int check,int type)168 static void maybe_prefix_block(char *data, int check, int type)
169 {
170   int len = strlen(data);
171 
172   if (len>check) write_prefix_block(data, len+1, type);
173 }
174 
do_filter(char * pattern,char * name,long long flags)175 static int do_filter(char *pattern, char *name, long long flags)
176 {
177   int ign = !!(flags&FLAG_ignore_case), wild = !!(flags&FLAG_wildcards),
178       slash = !!(flags&FLAG_wildcards_match_slash), len;
179 
180   if (wild || slash) {
181     // 1) match can end with / 2) maybe case insensitive 2) maybe * matches /
182     if (!fnmatch(pattern, name, FNM_LEADING_DIR+FNM_CASEFOLD*ign+FNM_PATHNAME*slash))
183       return 1;
184   } else {
185     len = strlen(pattern);
186     if (!(ign ? strncasecmp : strncmp)(pattern, name, len))
187       if (!name[len] || name[len]=='/') return 1;
188   }
189 
190   return 0;
191 }
192 
filter(struct double_list * lst,char * name)193 static struct double_list *filter(struct double_list *lst, char *name)
194 {
195   struct double_list *end = lst;
196   long long flags = toys.optflags;
197   char *ss;
198 
199   if (!lst || !*name) return 0;
200 
201   // --wildcards-match-slash implies --wildcards because I couldn't figure
202   // out a graceful way to explain why it DIDN'T in the help text. We don't
203   // do the positional enable/disable thing (would need to annotate at list
204   // creation, maybe a TODO item).
205 
206   // Set defaults for filter type, and apply --no-flags
207   if (lst == TT.excl) flags |= FLAG_wildcards_match_slash;
208   else flags |= FLAG_anchored;
209   flags &= (~(flags&(FLAG_no_ignore_case|FLAG_no_anchored|FLAG_no_wildcards|FLAG_no_wildcards_match_slash)))>>1;
210   if (flags&FLAG_no_wildcards) flags &= ~FLAG_wildcards_match_slash;
211 
212   // The +1 instead of ++ is in case of conseutive slashes
213   do {
214     if (do_filter(lst->data, name, flags)) return lst;
215     if (!(flags & FLAG_anchored)) for (ss = name; *ss; ss++) {
216       if (*ss!='/' || !ss[1]) continue;
217       if (do_filter(lst->data, ss+1, flags)) return lst;
218     }
219   } while (end != (lst = lst->next));
220 
221   return 0;
222 }
223 
skippy(long long len)224 static void skippy(long long len)
225 {
226   if (lskip(TT.fd, len)) perror_exit("EOF");
227 }
228 
229 // allocate and read data from TT.fd
alloread(void * buf,int len)230 static void alloread(void *buf, int len)
231 {
232   // actually void **, but automatic typecasting doesn't work with void ** :(
233   char **b = buf;
234 
235   free(*b);
236   *b = xmalloc(len+1);
237   xreadall(TT.fd, *b, len);
238   (*b)[len] = 0;
239 }
240 
xform(char ** name,char type)241 static char *xform(char **name, char type)
242 {
243   char buf[9], *end;
244   off_t len;
245 
246   if (!TT.xform) return 0;
247 
248   buf[8] = 0;
249   if (dprintf(TT.xfpipe[0], "%s%c%c", *name, type, 0) != strlen(*name)+2
250     || readall(TT.xfpipe[1], buf, 8) != 8
251     || !(len = estrtol(buf, &end, 16)) || errno ||*end) error_exit("bad xform");
252   xreadall(TT.xfpipe[1], *name = xmalloc(len+1), len);
253   (*name)[len] = 0;
254 
255   return *name;
256 }
257 
dirtree_sort(struct dirtree ** aa,struct dirtree ** bb)258 int dirtree_sort(struct dirtree **aa, struct dirtree **bb)
259 {
260   return (FLAG(ignore_case) ? strcasecmp : strcmp)(aa[0]->name, bb[0]->name);
261 }
262 
263 // callback from dirtree to create archive
add_to_tar(struct dirtree * node)264 static int add_to_tar(struct dirtree *node)
265 {
266   struct stat *st = &(node->st);
267   struct tar_hdr hdr;
268   struct passwd *pw = pw;
269   struct group *gr = gr;
270   int i, fd = -1, recurse = 0;
271   char *name, *lnk, *hname, *xfname = 0;
272 
273   if (!dirtree_notdotdot(node)) return 0;
274   if (same_dev_ino(st, &TT.archive_di)) {
275     error_msg("'%s' file is the archive; not dumped", node->name);
276     return 0;
277   }
278 
279   i = 1;
280   name = hname = dirtree_path(node, &i);
281   if (filter(TT.excl, name)) goto done;
282 
283   if ((FLAG(s)|FLAG(sort)) && !FLAG(no_recursion)) {
284     if (S_ISDIR(st->st_mode) && !node->again) {
285       free(name);
286 
287       return DIRTREE_BREADTH;
288     } else if ((node->again&DIRTREE_BREADTH) && node->child) {
289       struct dirtree *dt, **sort = xmalloc(sizeof(void *)*node->extra);
290 
291       for (node->extra = 0, dt = node->child; dt; dt = dt->next)
292         sort[node->extra++] = dt;
293       qsort(sort, node->extra--, sizeof(void *), (void *)dirtree_sort);
294       node->child = *sort;
295       for (i = 0; i<node->extra; i++) sort[i]->next = sort[i+1];
296       sort[i]->next = 0;
297       free(sort);
298 
299       // fall through to add directory
300     }
301   }
302 
303   // Consume the 1 extra byte alocated in dirtree_path()
304   if (S_ISDIR(st->st_mode) && (lnk = name+strlen(name))[-1] != '/')
305     strcpy(lnk, "/");
306 
307   // remove leading / and any .. entries from saved name
308   if (!FLAG(P)) {
309     while (*hname == '/') hname++;
310     for (lnk = hname;;) {
311       if (!(lnk = strstr(lnk, ".."))) break;
312       if (lnk == hname || lnk[-1] == '/') {
313         if (!lnk[2]) goto done;
314         if (lnk[2]=='/') {
315           lnk = hname = lnk+3;
316           continue;
317         }
318       }
319       lnk += 2;
320     }
321     if (!*hname) hname = "./";
322   }
323   if (!*hname) goto done;
324 
325   if (TT.warn && hname != name) {
326     dprintf(2, "removing leading '%.*s' from member names\n",
327            (int)(hname-name), name);
328     TT.warn = 0;
329   }
330 
331   // Override dentry data from command line and fill out header data
332   if (TT.owner) st->st_uid = TT.ouid;
333   if (TT.group) st->st_gid = TT.ggid;
334   if (TT.mode) st->st_mode = string_to_mode(TT.mode, st->st_mode);
335   if (TT.mtime) st->st_mtime = TT.mtt;
336   memset(&hdr, 0, sizeof(hdr));
337   ITOO(hdr.mode, st->st_mode &07777);
338   ITOO(hdr.uid, st->st_uid);
339   ITOO(hdr.gid, st->st_gid);
340   ITOO(hdr.size, 0); //set size later
341   ITOO(hdr.mtime, st->st_mtime);
342   strcpy(hdr.magic, "ustar  ");
343 
344   // Are there hardlinks to a non-directory entry?
345   lnk = 0;
346   if (st->st_nlink>1 && !S_ISDIR(st->st_mode)) {
347     // Have we seen this dev&ino before?
348     for (i = 0; i<TT.hlc; i++) if (same_dev_ino(st, &TT.hlx[i].di)) break;
349     if (i != TT.hlc) lnk = TT.hlx[i].arg;
350     else {
351       // first time we've seen it. Store as normal file, but remember it.
352       if (!(TT.hlc&255))
353         TT.hlx = xrealloc(TT.hlx, sizeof(*TT.hlx)*(TT.hlc+256));
354       TT.hlx[TT.hlc].arg = xstrdup(hname);
355       TT.hlx[TT.hlc].di.ino = st->st_ino;
356       TT.hlx[TT.hlc].di.dev = st->st_dev;
357       TT.hlc++;
358     }
359   }
360 
361   xfname = xform(&hname, 'r');
362   strncpy(hdr.name, hname, sizeof(hdr.name));
363 
364   // Handle file types: 0=reg, 1=hardlink, 2=sym, 3=chr, 4=blk, 5=dir, 6=fifo
365   if (lnk || S_ISLNK(st->st_mode)) {
366     hdr.type = '1'+!lnk;
367     if (lnk) {
368       if (!xform(&lnk, 'h')) lnk = xstrdup(lnk);
369     } else if (!(lnk = xreadlink(name))) {
370       perror_msg("readlink");
371       goto done;
372     } else xform(&lnk, 's');
373 
374     maybe_prefix_block(lnk, sizeof(hdr.link), 'K');
375     strncpy(hdr.link, lnk, sizeof(hdr.link));
376     free(lnk);
377   } else if (S_ISREG(st->st_mode)) {
378     hdr.type = '0';
379     ITOO(hdr.size, st->st_size);
380   } else if (S_ISDIR(st->st_mode)) hdr.type = '5';
381   else if (S_ISFIFO(st->st_mode)) hdr.type = '6';
382   else if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
383     hdr.type = (S_ISCHR(st->st_mode))?'3':'4';
384     ITOO(hdr.major, dev_major(st->st_rdev));
385     ITOO(hdr.minor, dev_minor(st->st_rdev));
386   } else {
387     error_msg("unknown file type '%o'", st->st_mode & S_IFMT);
388     goto done;
389   }
390 
391   // write out 'x' prefix header for --selinux data
392   if (FLAG(selinux)) {
393     int start = 0, sz = 0, temp, len = 0;
394     char *buf = 0, *sec = "security.selinux";
395 
396     for (;;) {
397       // First time get length, second time read data into prepared buffer
398       len = (S_ISLNK(st->st_mode) ? xattr_lget : xattr_get)
399         (name, sec, buf+start, sz);
400 
401       // Handle data or error
402       if (len>999999 || (sz && len>sz)) len = -1, errno = E2BIG;
403       if (buf || len<1) {
404         if (len>0) {
405           strcpy(buf+start+sz, "\n");
406           write_prefix_block(buf, start+sz+2, 'x');
407         } else if (errno==ENODATA || errno==ENOTSUP) len = 0;
408         if (len) perror_msg("getfilecon %s", name);
409 
410         free(buf);
411         break;
412       }
413 
414       // Allocate buffer. Length includes prefix: calculate twice (wrap 99->100)
415       temp = snprintf(0, 0, "%d", sz = (start = 22)+len+1);
416       start += temp + (temp != snprintf(0, 0, "%d", temp+sz));
417       buf = xmprintf("%u RHT.%s=%.*s", start+len+1, sec, sz = len, "");
418     }
419   }
420 
421   maybe_prefix_block(hname, sizeof(hdr.name), 'L');
422   if (!FLAG(numeric_owner)) {
423     if ((TT.owner || (pw = bufgetpwuid(st->st_uid))) &&
424         ascii_fits(st->st_uid, sizeof(hdr.uid)))
425       strncpy(hdr.uname, TT.owner ? : pw->pw_name, sizeof(hdr.uname));
426     if ((TT.group || (gr = bufgetgrgid(st->st_gid))) &&
427         ascii_fits(st->st_gid, sizeof(hdr.gid)))
428       strncpy(hdr.gname, TT.group ? : gr->gr_name, sizeof(hdr.gname));
429   }
430 
431   TT.sparselen = 0;
432   if (hdr.type == '0') {
433     // Before we write the header, make sure we can read the file
434     if ((fd = open(name, O_RDONLY)) < 0) {
435       perror_msg("can't open '%s'", name);
436       free(name);
437 
438       return 0;
439     }
440     if (FLAG(S)) {
441       long long lo, ld = 0, len = 0;
442 
443       // Enumerate the extents
444       while ((lo = lseek(fd, ld, SEEK_HOLE)) != -1) {
445         if (!(TT.sparselen&511))
446           TT.sparse = xrealloc(TT.sparse, (TT.sparselen+514)*sizeof(long long));
447         if (ld != lo) {
448           TT.sparse[TT.sparselen++] = ld;
449           len += TT.sparse[TT.sparselen++] = lo-ld;
450         }
451         if (lo == st->st_size || (ld = lseek(fd, lo, SEEK_DATA)) < lo) break;
452       }
453 
454       // If there were extents, change type to S record
455       if (TT.sparselen>2) {
456         TT.sparse[TT.sparselen++] = st->st_size;
457         TT.sparse[TT.sparselen++] = 0;
458         hdr.type = 'S';
459         lnk = (char *)&hdr;
460         for (i = 0; i<TT.sparselen && i<8; i++)
461           itoo(lnk+386+12*i, 12, TT.sparse[i]);
462 
463         // Record if there's overflow records, change length to sparse length,
464         // record apparent length
465         if (TT.sparselen>8) lnk[482] = 1;
466         itoo(lnk+483, 12, st->st_size);
467         ITOO(hdr.size, len);
468       } else TT.sparselen = 0;
469       lseek(fd, 0, SEEK_SET);
470     }
471   }
472 
473   itoo(hdr.chksum, sizeof(hdr.chksum)-1, tar_cksum(&hdr));
474   hdr.chksum[7] = ' ';
475 
476   if (FLAG(v)) dprintf(1+(TT.fd==1), "%s\n", hname);
477 
478   // Write header and data to archive
479   xwrite(TT.fd, &hdr, 512);
480   if (TT.sparselen>8) {
481     char buf[512];
482 
483     // write extent overflow blocks
484     for (i=8;;i++) {
485       int j = (i-8)%42;
486 
487       if (!j || i==TT.sparselen) {
488         if (i!=8) {
489           if (i!=TT.sparselen) buf[504] = 1;
490           xwrite(TT.fd, buf, 512);
491         }
492         if (i==TT.sparselen) break;
493         memset(buf, 0, sizeof(buf));
494       }
495       itoo(buf+12*j, 12, TT.sparse[i]);
496     }
497   }
498   TT.sparselen >>= 1;
499   if (hdr.type == '0' || hdr.type == 'S') {
500     if (hdr.type == '0') xsendfile_pad(fd, TT.fd, st->st_size);
501     else for (i = 0; i<TT.sparselen; i++) {
502       if (TT.sparse[i*2] != lseek(fd, TT.sparse[i*2], SEEK_SET))
503         perror_msg("%s: seek %lld", name, TT.sparse[i*2]);
504       xsendfile_pad(fd, TT.fd, TT.sparse[i*2+1]);
505     }
506     if (st->st_size%512) writeall(TT.fd, toybuf, (512-(st->st_size%512)));
507     close(fd);
508   }
509   recurse = !FLAG(no_recursion);
510 
511 done:
512   free(xfname);
513   free(name);
514 
515   return recurse*(DIRTREE_RECURSE|DIRTREE_SYMFOLLOW*FLAG(h));
516 }
517 
wsettime(char * s,long long sec)518 static void wsettime(char *s, long long sec)
519 {
520   struct timespec times[2] = {{sec, 0},{sec, 0}};
521 
522   if (utimensat(AT_FDCWD, s, times, AT_SYMLINK_NOFOLLOW))
523     perror_msg("settime %lld %s", sec, s);
524 }
525 
526 // Do pending directory utimes(), NULL to flush all.
dirflush(char * name,int isdir)527 static int dirflush(char *name, int isdir)
528 {
529   char *s = 0, *ss;
530 
531   // Barf if name not in TT.cwd
532   if (name) {
533     if (!(ss = s = xabspath(name, isdir ? ABS_LAST : 0))) {
534       error_msg("'%s' bad symlink", name);
535 
536       return 1;
537     }
538     if (TT.cwd[1] && (!strstart(&ss, TT.cwd) || (*ss && *ss!='/'))) {
539       error_msg("'%s' %s not under '%s'", name, s, TT.cwd);
540       free(s);
541 
542       return 1;
543     }
544 
545     // --restrict means first entry extracted is what everything must be under
546     if (FLAG(restrict)) {
547       free(TT.cwd);
548       TT.cwd = strdup(s);
549       toys.optflags ^= FLAG_restrict;
550     }
551     // use resolved name so trailing / is stripped
552     if (isdir) unlink(s);
553   }
554 
555   // Set deferred utimes() for directories this file isn't under.
556   // (Files must be depth-first ordered in tarball for this to matter.)
557   while (TT.dirs) {
558 
559     // If next file is under (or equal to) this dir, keep waiting
560     if (name && strstart(&ss, ss = s) && (!*ss || *ss=='/')) break;
561 
562     wsettime(TT.dirs->str+sizeof(long long), *(long long *)TT.dirs->str);
563     free(llist_pop(&TT.dirs));
564   }
565   free(s);
566 
567   // name was under TT.cwd
568   return 0;
569 }
570 
571 // write data to file
sendfile_sparse(int fd)572 static void sendfile_sparse(int fd)
573 {
574   long long len, used = 0, sent;
575   int i = 0, j;
576 
577   do {
578     if (TT.sparselen) {
579       // Seek past holes or fill output with zeroes.
580       if (-1 == lseek(fd, len = TT.sparse[i*2], SEEK_SET)) {
581         sent = 0;
582         while (len) {
583           // first/last 512 bytes used, rest left zeroes
584           j = (len>3072) ? 3072 : len;
585           if (j != writeall(fd, toybuf+512, j)) goto error;
586           len -= j;
587         }
588       } else {
589         sent = len;
590         if (!(len = TT.sparse[i*2+1]) && ftruncate(fd, sent+len))
591           perror_msg("ftruncate");
592       }
593       if (len+used>TT.hdr.size) error_exit("sparse overflow");
594     } else len = TT.hdr.size;
595 
596     len -= sendfile_len(TT.fd, fd, len, &sent);
597     used += sent;
598     if (len) {
599 error:
600       if (fd!=1) perror_msg(0);
601       skippy(TT.hdr.size-used);
602 
603       break;
604     }
605   } while (++i<TT.sparselen);
606 
607   close(fd);
608 }
609 
extract_to_disk(char * name)610 static void extract_to_disk(char *name)
611 {
612   int ala = TT.hdr.mode;
613 
614   if (dirflush(name, S_ISDIR(ala))) {
615     if (S_ISREG(ala) && !TT.hdr.link_target) skippy(TT.hdr.size);
616 
617     return;
618   }
619 
620   // create path before file if necessary
621   if (strrchr(name, '/') && mkpath(name) && errno!=EEXIST)
622       return perror_msg(":%s: can't mkdir", name);
623 
624   // remove old file, if exists
625   if (!FLAG(k) && !S_ISDIR(ala) && rmdir(name) && errno!=ENOENT && unlink(name))
626     return perror_msg("can't remove: %s", name);
627 
628   if (S_ISREG(ala)) {
629     // hardlink?
630     if (TT.hdr.link_target) {
631       if (link(TT.hdr.link_target, name))
632         return perror_msg("can't link '%s' -> '%s'", name, TT.hdr.link_target);
633     // write contents
634     } else {
635       int fd = WARN_ONLY|O_WRONLY|O_CREAT|(FLAG(overwrite) ? O_TRUNC : O_EXCL);
636 
637       if ((fd = xcreate(name, fd, ala&07777)) != -1) sendfile_sparse(fd);
638       else return skippy(TT.hdr.size);
639     }
640   } else if (S_ISDIR(ala)) {
641     if ((mkdir(name, 0700) == -1) && errno != EEXIST)
642       return perror_msg("%s: can't create", name);
643   } else if (S_ISLNK(ala)) {
644     if (symlink(TT.hdr.link_target, name))
645       return perror_msg("can't link '%s' -> '%s'", name, TT.hdr.link_target);
646   } else if (mknod(name, ala, TT.hdr.device))
647     return perror_msg("can't create '%s'", name);
648 
649   // Set ownership
650   if (!FLAG(o) && !geteuid()) {
651     int u = TT.hdr.uid, g = TT.hdr.gid;
652 
653     if (TT.owner) TT.hdr.uid = TT.ouid;
654     else if (!FLAG(numeric_owner) && *TT.hdr.uname) {
655       struct passwd *pw = bufgetpwnamuid(TT.hdr.uname, 0);
656       if (pw) TT.hdr.uid = pw->pw_uid;
657     }
658 
659     if (TT.group) TT.hdr.gid = TT.ggid;
660     else if (!FLAG(numeric_owner) && *TT.hdr.uname) {
661       struct group *gr = bufgetgrnamgid(TT.hdr.gname, 0);
662       if (gr) TT.hdr.gid = gr->gr_gid;
663     }
664 
665     if (lchown(name, u, g)) perror_msg("chown %d:%d '%s'", u, g, name);;
666   }
667 
668   if (!S_ISLNK(ala)) chmod(name, FLAG(p) ? ala : ala&0777);
669 
670   // Apply mtime.
671   if (!FLAG(m)) {
672     if (S_ISDIR(ala)) {
673       struct string_list *sl;
674 
675       // Writing files into a directory changes directory timestamps, so
676       // defer mtime updates until contents written.
677 
678       sl = xmalloc(sizeof(struct string_list)+sizeof(long long)+strlen(name)+1);
679       *(long long *)sl->str = TT.hdr.mtime;
680       strcpy(sl->str+sizeof(long long), name);
681       sl->next = TT.dirs;
682       TT.dirs = sl;
683     } else wsettime(name, TT.hdr.mtime);
684   }
685 }
686 
unpack_tar(char * first)687 static void unpack_tar(char *first)
688 {
689   struct double_list *walk, *delete;
690   struct tar_hdr tar;
691   int i, sefd = -1, and = 0;
692   unsigned maj, min;
693   char *s, *name;
694 
695   for (;;) {
696     if (first) {
697       memcpy(&tar, first, i = 512);
698       first = 0;
699     } else {
700       // align to next block and read it
701       if (TT.hdr.size%512) skippy(512-TT.hdr.size%512);
702       i = readall(TT.fd, &tar, 512);
703     }
704 
705     if (i && i!=512) error_exit("short header");
706 
707     // Two consecutive empty headers ends tar even if there's more data
708     if (!i || !*tar.name) {
709       if (!i || and++) return;
710       TT.hdr.size = 0;
711       continue;
712     }
713     // ensure null temination even of pathological packets
714     tar.padd[0] = and = 0;
715 
716     // Is this a valid TAR header?
717     if (!is_tar_header(&tar)) error_exit("bad header");
718     TT.hdr.size = OTOI(tar.size);
719 
720     // If this header isn't writing something to the filesystem
721     if ((tar.type<'0' || tar.type>'7') && tar.type!='S'
722         && (*tar.magic && tar.type))
723     {
724       // Skip to next record if unknown type or payload > 1 megabyte
725       if (!strchr("KLx", tar.type) || TT.hdr.size>1<<20) skippy(TT.hdr.size);
726       // Read link or long name
727       else if (tar.type != 'x')
728         alloread(tar.type=='K'?&TT.hdr.link_target:&TT.hdr.name, TT.hdr.size);
729       // Loop through 'x' payload records in "LEN NAME=VALUE\n" format
730       else {
731         char *p, *pp, *buf = 0;
732         unsigned i, len, n;
733 
734         alloread(&buf, TT.hdr.size);
735         for (p = buf; (p-buf)<TT.hdr.size; p += len) {
736           i = TT.hdr.size-(p-buf);
737           if (1!=sscanf(p, "%u %n", &len, &n) || len<n+4 || len>i || n>i) {
738             error_msg("bad header");
739             break;
740           }
741           p[len-1] = 0;
742           pp = p+n;
743           // Ignore "RHT." prefix, if any.
744           strstart(&pp, "RHT.");
745           if ((FLAG(selinux) && !(FLAG(t)|FLAG(O)))
746               && strstart(&pp, "security.selinux="))
747           {
748             i = strlen(pp);
749             sefd = xopen("/proc/self/attr/fscreate", O_WRONLY|WARN_ONLY);
750             if (sefd==-1 ||  i!=write(sefd, pp, i))
751               perror_msg("setfscreatecon %s", pp);
752           } else if (strstart(&pp, "path=")) {
753             free(TT.hdr.name);
754             TT.hdr.name = xstrdup(pp);
755             break;
756           }
757         }
758         free(buf);
759       }
760 
761       continue;
762     }
763 
764     // Handle sparse file type
765     TT.sparselen = 0;
766     if (tar.type == 'S') {
767       char sparse[512];
768       int max = 8;
769 
770       // Load 4 pairs of offset/len from S block, plus 21 pairs from each
771       // continuation block, list says where to seek/write sparse file contents
772       s = 386+(char *)&tar;
773       *sparse = i = 0;
774 
775       for (;;) {
776         if (!(TT.sparselen&511))
777           TT.sparse = xrealloc(TT.sparse, (TT.sparselen+512)*sizeof(long long));
778 
779         // If out of data in block check continue flag, stop or load next block
780         if (++i>max || !*s) {
781           if (!(*sparse ? sparse[504] : ((char *)&tar)[482])) break;
782           xreadall(TT.fd, s = sparse, 512);
783           max = 41;
784           i = 0;
785         }
786         // Load next entry
787         TT.sparse[TT.sparselen++] = otoi(s, 12);
788         s += 12;
789       }
790 
791       // Odd number of entries (from corrupted tar) would be dropped here
792       TT.sparselen /= 2;
793       if (TT.sparselen)
794         TT.hdr.ssize = TT.sparse[2*TT.sparselen-1]+TT.sparse[2*TT.sparselen-2];
795     } else TT.hdr.ssize = TT.hdr.size;
796 
797     // At this point, we have something to output. Convert metadata.
798     TT.hdr.mode = OTOI(tar.mode)&0xfff;
799     if (tar.type == 'S' || !tar.type) TT.hdr.mode |= 0x8000;
800     else TT.hdr.mode |= (char []){8,8,10,2,6,4,1,8}[tar.type-'0']<<12;
801     TT.hdr.uid = OTOI(tar.uid);
802     TT.hdr.gid = OTOI(tar.gid);
803     TT.hdr.mtime = OTOI(tar.mtime);
804     maj = OTOI(tar.major);
805     min = OTOI(tar.minor);
806     TT.hdr.device = dev_makedev(maj, min);
807     TT.hdr.uname = xstrndup(TT.owner ? : tar.uname, sizeof(tar.uname));
808     TT.hdr.gname = xstrndup(TT.group ? : tar.gname, sizeof(tar.gname));
809 
810     if (TT.owner) TT.hdr.uid = TT.ouid;
811     else if (!FLAG(numeric_owner)) {
812       struct passwd *pw = bufgetpwnamuid(TT.hdr.uname, 0);
813       if (pw && (TT.owner || !FLAG(numeric_owner))) TT.hdr.uid = pw->pw_uid;
814     }
815 
816     if (TT.group) TT.hdr.gid = TT.ggid;
817     else if (!FLAG(numeric_owner)) {
818       struct group *gr = bufgetgrnamgid(TT.hdr.gname, 0);
819       if (gr) TT.hdr.gid = gr->gr_gid;
820     }
821 
822     if (!TT.hdr.link_target && *tar.link)
823       TT.hdr.link_target = xstrndup(tar.link, sizeof(tar.link));
824     if (!TT.hdr.name) {
825       // Glue prefix and name fields together with / if necessary
826       i = (tar.type=='S') ? 0 : strnlen(tar.prefix, sizeof(tar.prefix));
827       TT.hdr.name = xmprintf("%.*s%s%.*s", i, tar.prefix,
828         (i && tar.prefix[i-1] != '/') ? "/" : "",
829         (int)sizeof(tar.name), tar.name);
830     }
831 
832     // Old broken tar recorded dir as "file with trailing slash"
833     if (S_ISREG(TT.hdr.mode) && (s = strend(TT.hdr.name, "/"))) {
834       *s = 0;
835       TT.hdr.mode = (TT.hdr.mode & ~S_IFMT) | S_IFDIR;
836     }
837 
838     // Non-regular files don't have contents stored in archive.
839     if ((TT.hdr.link_target && *TT.hdr.link_target)
840       || (tar.type && !S_ISREG(TT.hdr.mode)))
841         TT.hdr.size = 0;
842 
843     // Files are seen even if excluded, so check them here.
844     // TT.seen points to first seen entry in TT.incl, or NULL if none yet.
845 
846     if ((delete = filter(TT.incl, TT.hdr.name)) && TT.incl != TT.seen) {
847       if (!TT.seen) TT.seen = delete;
848 
849       // Move seen entry to end of list.
850       if (TT.incl == delete) TT.incl = TT.incl->next;
851       else for (walk = TT.incl; walk != TT.seen; walk = walk->next) {
852         if (walk == delete) {
853           dlist_pop(&walk);
854           dlist_add_nomalloc(&TT.incl, delete);
855         }
856       }
857     }
858 
859     // Skip excluded files, filtering on the untransformed name.
860     if (filter(TT.excl, name = TT.hdr.name) || (TT.incl && !delete)) {
861       skippy(TT.hdr.size);
862       goto done;
863     }
864 
865     // We accept --show-transformed but always do, so it's a NOP.
866     name = TT.hdr.name;
867     if (xform(&name, 'r')) {
868       free(TT.hdr.name);
869       TT.hdr.name = name;
870     }
871     if ((i = "\0hs"[stridx("12", tar.type)+1])) xform(&TT.hdr.link_target, i);
872 
873     for (i = 0; i<TT.strip; i++) {
874       char *s = strchr(name, '/');
875 
876       if (s && s[1]) name = s+1;
877       else {
878         if (S_ISDIR(TT.hdr.mode)) *name = 0;
879         break;
880       }
881     }
882 
883     if (!*name) skippy(TT.hdr.size);
884     else if (FLAG(t)) {
885       if (FLAG(v)) {
886         struct tm *lc = localtime(TT.mtime ? &TT.mtt : &TT.hdr.mtime);
887         char perm[12], gname[12];
888 
889         mode_to_string(TT.hdr.mode, perm);
890         printf("%s", perm);
891         sprintf(perm, "%u", TT.hdr.uid);
892         sprintf(gname, "%u", TT.hdr.gid);
893         printf(" %s/%s ", *TT.hdr.uname ? TT.hdr.uname : perm,
894           *TT.hdr.gname ? TT.hdr.gname : gname);
895         if (tar.type=='3' || tar.type=='4') printf("%u,%u", maj, min);
896         else printf("%9lld", TT.hdr.ssize);
897         sprintf(perm, ":%02d", lc->tm_sec);
898         printf("  %d-%02d-%02d %02d:%02d%s ", 1900+lc->tm_year, 1+lc->tm_mon,
899           lc->tm_mday, lc->tm_hour, lc->tm_min, FLAG(full_time) ? perm : "");
900       }
901       printf("%s", name);
902       if (TT.hdr.link_target)
903         printf(" %s %s", tar.type=='2' ? "->" : "link to", TT.hdr.link_target);
904       xputc('\n');
905       skippy(TT.hdr.size);
906     } else {
907       if (FLAG(v)) printf("%s\n", name);
908       if (FLAG(O)) sendfile_sparse(1);
909       else if (FLAG(to_command)) {
910         if (S_ISREG(TT.hdr.mode)) {
911           int fd, pid;
912 
913           xsetenv("TAR_FILETYPE", "f");
914           xsetenv(xmprintf("TAR_MODE=%o", TT.hdr.mode), 0);
915           xsetenv(xmprintf("TAR_SIZE=%lld", TT.hdr.ssize), 0);
916           xsetenv("TAR_FILENAME", name);
917           xsetenv("TAR_UNAME", TT.hdr.uname);
918           xsetenv("TAR_GNAME", TT.hdr.gname);
919           xsetenv(xmprintf("TAR_MTIME=%llo", (long long)TT.hdr.mtime), 0);
920           xsetenv(xmprintf("TAR_UID=%o", TT.hdr.uid), 0);
921           xsetenv(xmprintf("TAR_GID=%o", TT.hdr.gid), 0);
922 
923           pid = xpopen((char *[]){"sh", "-c", TT.to_command, NULL}, &fd, 0);
924           // TODO: short write exits tar here, other skips data.
925           sendfile_sparse(fd);
926           fd = xpclose_both(pid, 0);
927           if (fd) error_msg("%d: Child returned %d", pid, fd);
928         }
929       } else extract_to_disk(name);
930     }
931 
932 done:
933     if (sefd != -1) {
934       // zero length write resets fscreate context to default
935       (void)write(sefd, 0, 0);
936       close(sefd);
937       sefd = -1;
938     }
939     free(TT.hdr.name);
940     free(TT.hdr.link_target);
941     free(TT.hdr.uname);
942     free(TT.hdr.gname);
943     TT.hdr.name = TT.hdr.link_target = 0;
944   }
945 }
946 
947 // Add copy of filename (minus trailing \n and /) to dlist **
trim2list(void * list,char * pline)948 static void trim2list(void *list, char *pline)
949 {
950   char *n = xstrdup(pline);
951   int i = strlen(n);
952 
953   dlist_add(list, n);
954   if (i && n[i-1]=='\n') i--;
955   while (i>1 && n[i-1] == '/') i--;
956   n[i] = 0;
957 }
958 
959 // do_lines callback, selects TT.incl or TT.excl based on call order
do_XT(char ** pline,long len)960 static void do_XT(char **pline, long len)
961 {
962   if (pline) trim2list(TT.X ? &TT.excl : &TT.incl, *pline);
963 }
964 
get_archiver()965 static  char *get_archiver()
966 {
967   return TT.I ? : FLAG(z) ? "gzip" : FLAG(j) ? "bzip2" : "xz";
968 }
969 
tar_main(void)970 void tar_main(void)
971 {
972   char *s, **xfsed, **args = toys.optargs;
973   int len = 0, ii;
974 
975   // Needed when extracting to command
976   signal(SIGPIPE, SIG_IGN);
977 
978   // Get possible early errors out of the way
979   if (!geteuid()) toys.optflags |= FLAG_p;
980   if (TT.owner) {
981     if (!(s = strchr(TT.owner, ':'))) TT.ouid = xgetuid(TT.owner);
982     else {
983       TT.owner = xstrndup(TT.owner, s++-TT.owner);
984       TT.ouid = atolx_range(s, 0, INT_MAX);
985     }
986   }
987   if (TT.group) {
988     if (!(s = strchr(TT.group, ':'))) TT.ggid = xgetgid(TT.group);
989     else {
990       TT.group = xstrndup(TT.group, s++-TT.group);
991       TT.ggid = atolx_range(s, 0, INT_MAX);
992     }
993   }
994   if (TT.mtime) xparsedate(TT.mtime, &TT.mtt, (void *)&s, 1);
995 
996   // TODO: collect filter types here and annotate saved include/exclude?
997 
998   // Collect file list.
999   for (; TT.exclude; TT.exclude = TT.exclude->next)
1000     trim2list(&TT.excl, TT.exclude->arg);
1001   for (;TT.X; TT.X = TT.X->next) do_lines(xopenro(TT.X->arg), '\n', do_XT);
1002   for (args = toys.optargs; *args; args++) trim2list(&TT.incl, *args);
1003   // -T is always --verbatim-files-from: no quote removal or -arg handling
1004   for (;TT.T; TT.T = TT.T->next)
1005     do_lines(xopenro(TT.T->arg), '\n'*!FLAG(null), do_XT);
1006 
1007   // If include file list empty, don't create empty archive
1008   if (FLAG(c)) {
1009     if (!TT.incl) error_exit("empty archive");
1010     TT.fd = 1;
1011   }
1012 
1013   if (TT.xform) {
1014     struct arg_list *al;
1015 
1016     for (ii = 0, al = TT.xform; al; al = al->next) ii++;
1017     xfsed = xmalloc((ii+2)*2*sizeof(char *));
1018     xfsed[0] = "sed";
1019     xfsed[1] = "--tarxform";
1020     for (ii = 2, al = TT.xform; al; al = al->next) {
1021       xfsed[ii++] = "-e";
1022       xfsed[ii++] = al->arg;
1023     }
1024     xfsed[ii] = 0;
1025     TT.xfpipe[0] = TT.xfpipe[1] = -1;
1026     xpopen_both(xfsed, TT.xfpipe);
1027     free(xfsed);
1028   }
1029 
1030   // nommu reentry for nonseekable input skips this, parent did it for us
1031   if (toys.stacktop) {
1032     if (TT.f && strcmp(TT.f, "-"))
1033       TT.fd = xcreate(TT.f, TT.fd*(O_WRONLY|O_CREAT|O_TRUNC), 0666);
1034     // Get destination directory
1035     if (TT.C) xchdir(TT.C);
1036   }
1037 
1038   // Get destination directory
1039   TT.cwd = xabspath(s = xgetcwd(), ABS_PATH);
1040   free(s);
1041 
1042   // Remember archive inode so we don't overwrite it or add it to itself
1043   {
1044     struct stat st;
1045 
1046     if (!fstat(TT.fd, &st)) {
1047       TT.archive_di.ino = st.st_ino;
1048       TT.archive_di.dev = st.st_dev;
1049     }
1050   }
1051 
1052   // Are we reading?
1053   if (FLAG(x)||FLAG(t)) {
1054     char *hdr = 0;
1055 
1056     // autodetect compression type when not specified
1057     if (!(FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J))) {
1058       len = xread(TT.fd, hdr = toybuf+sizeof(toybuf)-512, 512);
1059       if (len!=512 || !is_tar_header(hdr)) {
1060         // detect gzip and bzip signatures
1061         if (SWAP_BE16(*(short *)hdr)==0x1f8b) toys.optflags |= FLAG_z;
1062         else if (!smemcmp(hdr, "BZh", 3)) toys.optflags |= FLAG_j;
1063         else if (peek_be(hdr, 7) == 0xfd377a585a0000UL) toys.optflags |= FLAG_J;
1064         else error_exit("Not tar");
1065 
1066         // if we can seek back we don't need to loop and copy data
1067         if (!lseek(TT.fd, -len, SEEK_CUR)) hdr = 0;
1068       }
1069     }
1070 
1071     if (FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J)) {
1072       int pipefd[2] = {hdr ? -1 : TT.fd, -1}, i, pid;
1073       struct string_list *zcat = FLAG(I) ? 0 : find_in_path(getenv("PATH"),
1074         FLAG(z) ? "zcat" : FLAG(j) ? "bzcat" : "xzcat");
1075 
1076       // Toybox provides more decompressors than compressors, so try them first
1077       TT.pid = xpopen_both(zcat ? (char *[]){zcat->str, 0} :
1078         (char *[]){get_archiver(), "-d", 0}, pipefd);
1079       if (CFG_TOYBOX_FREE) llist_traverse(zcat, free);
1080 
1081       if (!hdr) {
1082         // If we could seek, child gzip inherited fd and we read its output
1083         close(TT.fd);
1084         TT.fd = pipefd[1];
1085 
1086       } else {
1087 
1088         // If we autodetected type but then couldn't lseek to put the data back
1089         // we have to loop reading data from TT.fd and pass it to gzip ourselves
1090         // (starting with the block of data we read to autodetect).
1091 
1092         // dirty trick: move gzip input pipe to stdin so child closes spare copy
1093         dup2(pipefd[0], 0);
1094         if (pipefd[0]) close(pipefd[0]);
1095 
1096         // Fork a copy of ourselves to handle extraction (reads from zip output
1097         // pipe, writes to stdout).
1098         pipefd[0] = pipefd[1];
1099         pipefd[1] = 1;
1100         pid = xpopen_both(0, pipefd);
1101         close(pipefd[1]);
1102 
1103         // loop writing collated data to zip proc
1104         xwrite(0, hdr, len);
1105         for (;;) {
1106           if ((i = read(TT.fd, toybuf, sizeof(toybuf)))<1) {
1107             close(0);
1108             xwaitpid(pid);
1109             return;
1110           }
1111           xwrite(0, toybuf, i);
1112         }
1113       }
1114     }
1115 
1116     unpack_tar(hdr);
1117     dirflush(0, 0);
1118     // Shut up archiver about inability to write all trailing NULs to pipe buf
1119     while (0<read(TT.fd, toybuf, sizeof(toybuf)));
1120 
1121     // Each time a TT.incl entry is seen it's moved to the end of the list,
1122     // with TT.seen pointing to first seen list entry. Anything between
1123     // TT.incl and TT.seen wasn't encountered in archive..
1124     if (TT.seen != TT.incl) {
1125       if (!TT.seen) TT.seen = TT.incl;
1126       while (TT.incl != TT.seen) {
1127         error_msg("'%s' not in archive", TT.incl->data);
1128         TT.incl = TT.incl->next;
1129       }
1130     }
1131 
1132   // are we writing? (Don't have to test flag here, one of 3 must be set)
1133   } else {
1134     struct double_list *dl = TT.incl;
1135 
1136     // autodetect compression type based on -f name. (Use > to avoid.)
1137     if (TT.f && !FLAG(j) && !FLAG(z) && !FLAG(I) && !FLAG(J)) {
1138       char *tbz[] = {".tbz", ".tbz2", ".tar.bz", ".tar.bz2"};
1139       if (strend(TT.f, ".tgz") || strend(TT.f, ".tar.gz"))
1140         toys.optflags |= FLAG_z;
1141       if (strend(TT.f, ".txz") || strend(TT.f, ".tar.xz"))
1142         toys.optflags |= FLAG_J;
1143       else for (len = 0; len<ARRAY_LEN(tbz); len++)
1144         if (strend(TT.f, tbz[len])) toys.optflags |= FLAG_j;
1145     }
1146 
1147     if (FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J)) {
1148       int pipefd[2] = {-1, TT.fd};
1149 
1150       TT.pid = xpopen_both((char *[]){get_archiver(), 0}, pipefd);
1151       close(TT.fd);
1152       TT.fd = pipefd[0];
1153     }
1154     do {
1155       TT.warn = 1;
1156       dirtree_flagread(dl->data,
1157         DIRTREE_SYMFOLLOW*FLAG(h)|DIRTREE_BREADTH*(FLAG(sort)|FLAG(s)),
1158         add_to_tar);
1159     } while (TT.incl != (dl = dl->next));
1160 
1161     writeall(TT.fd, toybuf, 1024);
1162     close(TT.fd);
1163   }
1164   if (TT.pid) {
1165     TT.pid = xpclose_both(TT.pid, 0);
1166     if (TT.pid) toys.exitval = TT.pid;
1167   }
1168   if (toys.exitval) error_msg("had errors");
1169 
1170   if (CFG_TOYBOX_FREE) {
1171     llist_traverse(TT.excl, llist_free_double);
1172     llist_traverse(TT.incl, llist_free_double);
1173     while(TT.hlc) free(TT.hlx[--TT.hlc].arg);
1174     free(TT.hlx);
1175     free(TT.cwd);
1176     close(TT.fd);
1177   }
1178 }
1179