xref: /aosp_15_r20/external/toybox/toys/posix/tar.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* tar.c - create/extract archives
2  *
3  * Copyright 2014 Ashwini Kumar <[email protected]>
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, "&(one-file-system)(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|TOYFLAG_UMASK))
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, oldsparse;
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|DIRTREE_SYMFOLLOW*FLAG(h);
288 
289     } else if ((node->again&DIRTREE_BREADTH) && node->child) {
290       struct dirtree *dt, **sort = xmalloc(sizeof(void *)*node->extra);
291 
292       for (node->extra = 0, dt = node->child; dt; dt = dt->next)
293         sort[node->extra++] = dt;
294       qsort(sort, node->extra--, sizeof(void *), (void *)dirtree_sort);
295       node->child = *sort;
296       for (i = 0; i<node->extra; i++) sort[i]->next = sort[i+1];
297       sort[i]->next = 0;
298       free(sort);
299 
300       // fall through to add directory
301     }
302   }
303 
304   // Consume the 1 extra byte alocated in dirtree_path()
305   if (S_ISDIR(st->st_mode) && (lnk = name+strlen(name))[-1] != '/')
306     strcpy(lnk, "/");
307 
308   // remove leading / and any .. entries from saved name
309   if (!FLAG(P)) {
310     while (*hname == '/') hname++;
311     for (lnk = hname;;) {
312       if (!(lnk = strstr(lnk, ".."))) break;
313       if (lnk == hname || lnk[-1] == '/') {
314         if (!lnk[2]) goto done;
315         if (lnk[2]=='/') {
316           lnk = hname = lnk+3;
317           continue;
318         }
319       }
320       lnk += 2;
321     }
322     if (!*hname) hname = "./";
323   }
324   if (!*hname) goto done;
325 
326   if (TT.warn && hname != name) {
327     dprintf(2, "removing leading '%.*s' from member names\n",
328            (int)(hname-name), name);
329     TT.warn = 0;
330   }
331 
332   // Override dentry data from command line and fill out header data
333   if (TT.owner) st->st_uid = TT.ouid;
334   if (TT.group) st->st_gid = TT.ggid;
335   if (TT.mode) st->st_mode = string_to_mode(TT.mode, st->st_mode);
336   if (TT.mtime) st->st_mtime = TT.mtt;
337   memset(&hdr, 0, sizeof(hdr));
338   ITOO(hdr.mode, st->st_mode &07777);
339   ITOO(hdr.uid, st->st_uid);
340   ITOO(hdr.gid, st->st_gid);
341   ITOO(hdr.size, 0); //set size later
342   ITOO(hdr.mtime, st->st_mtime);
343   strcpy(hdr.magic, "ustar  ");
344 
345   // Are there hardlinks to a non-directory entry?
346   lnk = 0;
347   if ((st->st_nlink>1 || FLAG(h)) && !S_ISDIR(st->st_mode)) {
348     // Have we seen this dev&ino before?
349     for (i = 0; i<TT.hlc; i++) if (same_dev_ino(st, &TT.hlx[i].di)) break;
350     if (i != TT.hlc) lnk = TT.hlx[i].arg;
351     else {
352       // first time we've seen it. Store as normal file, but remember it.
353       if (!(TT.hlc&255))
354         TT.hlx = xrealloc(TT.hlx, sizeof(*TT.hlx)*(TT.hlc+256));
355       TT.hlx[TT.hlc].arg = xstrdup(hname);
356       TT.hlx[TT.hlc].di.ino = st->st_ino;
357       TT.hlx[TT.hlc].di.dev = st->st_dev;
358       TT.hlc++;
359     }
360   }
361 
362   xfname = xform(&hname, 'r');
363   strncpy(hdr.name, hname, sizeof(hdr.name));
364 
365   // Handle file types: 0=reg, 1=hardlink, 2=sym, 3=chr, 4=blk, 5=dir, 6=fifo
366   if (lnk || S_ISLNK(st->st_mode)) {
367     hdr.type = '1'+!lnk;
368     if (lnk) {
369       if (!xform(&lnk, 'h')) lnk = xstrdup(lnk);
370     } else if (!(lnk = xreadlink(name))) {
371       perror_msg("readlink");
372       goto done;
373     } else xform(&lnk, 's');
374 
375     maybe_prefix_block(lnk, sizeof(hdr.link), 'K');
376     strncpy(hdr.link, lnk, sizeof(hdr.link));
377     free(lnk);
378   } else if (S_ISREG(st->st_mode)) {
379     hdr.type = '0';
380     ITOO(hdr.size, st->st_size);
381   } else if (S_ISDIR(st->st_mode)) hdr.type = '5';
382   else if (S_ISFIFO(st->st_mode)) hdr.type = '6';
383   else if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
384     hdr.type = (S_ISCHR(st->st_mode))?'3':'4';
385     ITOO(hdr.major, dev_major(st->st_rdev));
386     ITOO(hdr.minor, dev_minor(st->st_rdev));
387   } else {
388     error_msg("unknown file type '%o'", st->st_mode & S_IFMT);
389     goto done;
390   }
391 
392   // write out 'x' prefix header for --selinux data
393   if (FLAG(selinux)) {
394     int start = 0, sz = 0, temp, len = 0;
395     char *buf = 0, *sec = "security.selinux";
396 
397     for (;;) {
398       // First time get length, second time read data into prepared buffer
399       len = (S_ISLNK(st->st_mode) ? xattr_lget : xattr_get)
400         (name, sec, buf+start, sz);
401 
402       // Handle data or error
403       if (len>999999 || (sz && len>sz)) len = -1, errno = E2BIG;
404       if (buf || len<1) {
405         if (len>0) {
406           strcpy(buf+start+sz, "\n");
407           write_prefix_block(buf, start+sz+2, 'x');
408         } else if (errno==ENODATA || errno==ENOTSUP) len = 0;
409         if (len) perror_msg("getfilecon %s", name);
410 
411         free(buf);
412         break;
413       }
414 
415       // Allocate buffer. Length includes prefix: calculate twice (wrap 99->100)
416       temp = snprintf(0, 0, "%d", sz = (start = 22)+len+1);
417       start += temp + (temp != snprintf(0, 0, "%d", temp+sz));
418       buf = xmprintf("%u RHT.%s=%.*s", start+len+1, sec, sz = len, "");
419     }
420   }
421 
422   maybe_prefix_block(hname, sizeof(hdr.name), 'L');
423   if (!FLAG(numeric_owner)) {
424     if ((TT.owner || (pw = bufgetpwuid(st->st_uid))) &&
425         ascii_fits(st->st_uid, sizeof(hdr.uid)))
426       strncpy(hdr.uname, TT.owner ? : pw->pw_name, sizeof(hdr.uname));
427     if ((TT.group || (gr = bufgetgrgid(st->st_gid))) &&
428         ascii_fits(st->st_gid, sizeof(hdr.gid)))
429       strncpy(hdr.gname, TT.group ? : gr->gr_name, sizeof(hdr.gname));
430   }
431 
432   TT.sparselen = 0;
433   if (hdr.type == '0') {
434     // Before we write the header, make sure we can read the file
435     if ((fd = open(name, O_RDONLY)) < 0) {
436       perror_msg("can't open '%s'", name);
437       free(name);
438 
439       return 0;
440     }
441     if (FLAG(S)) {
442       long long lo, ld = 0, len = 0;
443 
444       // Enumerate the extents
445       while ((lo = lseek(fd, ld, SEEK_HOLE)) != -1) {
446         if (!(TT.sparselen&511))
447           TT.sparse = xrealloc(TT.sparse, (TT.sparselen+514)*sizeof(long long));
448         if (ld != lo) {
449           TT.sparse[TT.sparselen++] = ld;
450           len += TT.sparse[TT.sparselen++] = lo-ld;
451         }
452         if (lo == st->st_size || (ld = lseek(fd, lo, SEEK_DATA)) < lo) break;
453       }
454 
455       // If there were extents, change type to S record
456       if (TT.sparselen>2) {
457         TT.sparse[TT.sparselen++] = st->st_size;
458         TT.sparse[TT.sparselen++] = 0;
459         hdr.type = 'S';
460         lnk = (char *)&hdr;
461         for (i = 0; i<TT.sparselen && i<8; i++)
462           itoo(lnk+386+12*i, 12, TT.sparse[i]);
463 
464         // Record if there's overflow records, change length to sparse length,
465         // record apparent length
466         if (TT.sparselen>8) lnk[482] = 1;
467         itoo(lnk+483, 12, st->st_size);
468         ITOO(hdr.size, len);
469       } else TT.sparselen = 0;
470       lseek(fd, 0, SEEK_SET);
471     }
472   }
473 
474   itoo(hdr.chksum, sizeof(hdr.chksum)-1, tar_cksum(&hdr));
475   hdr.chksum[7] = ' ';
476 
477   if (FLAG(v)) dprintf(1+(TT.fd==1), "%s\n", hname);
478 
479   // Write header and data to archive
480   xwrite(TT.fd, &hdr, 512);
481   if (TT.sparselen>8) {
482     char buf[512];
483 
484     // write extent overflow blocks
485     for (i=8;;i++) {
486       int j = (i-8)%42;
487 
488       if (!j || i==TT.sparselen) {
489         if (i!=8) {
490           if (i!=TT.sparselen) buf[504] = 1;
491           xwrite(TT.fd, buf, 512);
492         }
493         if (i==TT.sparselen) break;
494         memset(buf, 0, sizeof(buf));
495       }
496       itoo(buf+12*j, 12, TT.sparse[i]);
497     }
498   }
499   TT.sparselen >>= 1;
500   if (hdr.type == '0' || hdr.type == 'S') {
501     if (hdr.type == '0') xsendfile_pad(fd, TT.fd, st->st_size);
502     else for (i = 0; i<TT.sparselen; i++) {
503       if (TT.sparse[i*2] != lseek(fd, TT.sparse[i*2], SEEK_SET))
504         perror_msg("%s: seek %lld", name, TT.sparse[i*2]);
505       xsendfile_pad(fd, TT.fd, TT.sparse[i*2+1]);
506     }
507     if (st->st_size%512) writeall(TT.fd, toybuf, (512-(st->st_size%512)));
508     close(fd);
509   }
510   recurse = !FLAG(no_recursion);
511 
512 done:
513   free(xfname);
514   free(name);
515 
516   if (FLAG(one_file_system) && node->parent
517       && node->parent->st.st_dev != node->st.st_dev) recurse = 0;
518   return recurse*(DIRTREE_RECURSE|DIRTREE_SYMFOLLOW*FLAG(h));
519 }
520 
wsettime(char * s,long long sec)521 static void wsettime(char *s, long long sec)
522 {
523   struct timespec times[2] = {{sec, 0},{sec, 0}};
524 
525   if (utimensat(AT_FDCWD, s, times, AT_SYMLINK_NOFOLLOW))
526     perror_msg("settime %lld %s", sec, s);
527 }
528 
freedup(char ** to,char * from)529 static void freedup(char **to, char *from)
530 {
531   free(*to);
532   *to = xstrdup(from);
533 }
534 
535 // Do pending directory utimes(), NULL to flush all.
dirflush(char * name,int isdir)536 static int dirflush(char *name, int isdir)
537 {
538   char *s = 0, *ss;
539 
540   // Barf if name not in TT.cwd
541   if (name) {
542     if (!(ss = s = xabspath(name, isdir ? ABS_LAST : 0))) {
543       error_msg("'%s' bad symlink", name);
544 
545       return 1;
546     }
547     if (TT.cwd[1] && (!strstart(&ss, TT.cwd) || (*ss && *ss!='/'))) {
548       error_msg("'%s' %s not under '%s'", name, s, TT.cwd);
549       free(s);
550 
551       return 1;
552     }
553 
554     // --restrict means first entry extracted is what everything must be under
555     if (FLAG(restrict)) {
556       freedup(&TT.cwd, s);
557       toys.optflags ^= FLAG_restrict;
558     }
559     // use resolved name so trailing / is stripped
560     if (isdir) unlink(s);
561   }
562 
563   // Set deferred utimes() for directories this file isn't under.
564   // (Files must be depth-first ordered in tarball for this to matter.)
565   while (TT.dirs) {
566 
567     // If next file is under (or equal to) this dir, keep waiting
568     if (name && strstart(&ss, ss = s) && (!*ss || *ss=='/')) break;
569 
570     wsettime(TT.dirs->str+sizeof(long long), *(long long *)TT.dirs->str);
571     free(llist_pop(&TT.dirs));
572   }
573   free(s);
574 
575   // name was under TT.cwd
576   return 0;
577 }
578 
579 // write data to file
sendfile_sparse(int fd)580 static void sendfile_sparse(int fd)
581 {
582   long long len, used = 0, sent;
583   int i = 0, j;
584 
585   do {
586     if (TT.sparselen) {
587       // Seek past holes or fill output with zeroes.
588       if (-1 == lseek(fd, len = TT.sparse[i*2], SEEK_SET)) {
589         sent = 0;
590         while (len) {
591           // first/last 512 bytes used, rest left zeroes
592           j = (len>3072) ? 3072 : len;
593           if (j != writeall(fd, toybuf+512, j)) goto error;
594           len -= j;
595         }
596       } else {
597         sent = len;
598         if (!(len = TT.sparse[i*2+1]) && ftruncate(fd, sent))
599           perror_msg("ftruncate");
600       }
601       if (len+used>TT.hdr.size) error_exit("sparse overflow");
602     } else len = TT.hdr.size;
603 
604     len -= sendfile_len(TT.fd, fd, len, &sent);
605     used += sent;
606     if (len) {
607 error:
608       if (fd!=1) perror_msg(0);
609       skippy(TT.hdr.size-used);
610 
611       break;
612     }
613   } while (++i<TT.sparselen);
614 
615   close(fd);
616 }
617 
extract_to_disk(char * name)618 static void extract_to_disk(char *name)
619 {
620   int ala = TT.hdr.mode;
621 
622   if (dirflush(name, S_ISDIR(ala))) {
623     if (S_ISREG(ala) && !TT.hdr.link_target) skippy(TT.hdr.size);
624 
625     return;
626   }
627 
628   // create path before file if necessary
629   if (strrchr(name, '/') && mkpath(name) && errno!=EEXIST)
630       return perror_msg(":%s: can't mkdir", name);
631 
632   // remove old file, if exists
633   if (!FLAG(k) && !S_ISDIR(ala) && rmdir(name) && errno!=ENOENT && unlink(name))
634     return perror_msg("can't remove: %s", name);
635 
636   if (S_ISREG(ala)) {
637     // hardlink?
638     if (TT.hdr.link_target) {
639       if (link(TT.hdr.link_target, name))
640         return perror_msg("can't link '%s' -> '%s'", name, TT.hdr.link_target);
641     // write contents
642     } else {
643       int fd = WARN_ONLY|O_WRONLY|O_CREAT|(FLAG(overwrite) ? O_TRUNC : O_EXCL);
644 
645       if ((fd = xcreate(name, fd, 0700)) != -1) sendfile_sparse(fd);
646       else return skippy(TT.hdr.size);
647     }
648   } else if (S_ISDIR(ala)) {
649     if ((mkdir(name, 0700) == -1) && errno != EEXIST)
650       return perror_msg("%s: can't create", name);
651   } else if (S_ISLNK(ala)) {
652     if (symlink(TT.hdr.link_target, name))
653       return perror_msg("can't link '%s' -> '%s'", name, TT.hdr.link_target);
654   } else if (mknod(name, ala&~toys.old_umask, TT.hdr.device))
655     return perror_msg("can't create '%s'", name);
656 
657   // Set ownership
658   if (!FLAG(o) && !geteuid()) {
659     int u = TT.hdr.uid, g = TT.hdr.gid;
660 
661     if (TT.owner) TT.hdr.uid = TT.ouid;
662     else if (!FLAG(numeric_owner) && *TT.hdr.uname) {
663       struct passwd *pw = bufgetpwnamuid(TT.hdr.uname, 0);
664       if (pw) TT.hdr.uid = pw->pw_uid;
665     }
666 
667     if (TT.group) TT.hdr.gid = TT.ggid;
668     else if (!FLAG(numeric_owner) && *TT.hdr.uname) {
669       struct group *gr = bufgetgrnamgid(TT.hdr.gname, 0);
670       if (gr) TT.hdr.gid = gr->gr_gid;
671     }
672 
673     if (lchown(name, u, g)) perror_msg("chown %d:%d '%s'", u, g, name);;
674   }
675 
676   if (!S_ISLNK(ala)) chmod(name, FLAG(p) ? ala : ala&0777&~toys.old_umask);
677 
678   // Apply mtime.
679   if (!FLAG(m)) {
680     if (S_ISDIR(ala)) {
681       struct string_list *sl;
682 
683       // Writing files into a directory changes directory timestamps, so
684       // defer mtime updates until contents written.
685 
686       sl = xmalloc(sizeof(struct string_list)+sizeof(long long)+strlen(name)+1);
687       *(long long *)sl->str = TT.hdr.mtime;
688       strcpy(sl->str+sizeof(long long), name);
689       sl->next = TT.dirs;
690       TT.dirs = sl;
691     } else wsettime(name, TT.hdr.mtime);
692   }
693 }
694 
unpack_tar(char * first)695 static void unpack_tar(char *first)
696 {
697   struct double_list *walk, *delete;
698   struct tar_hdr tar;
699   int i, sefd = -1, and = 0;
700   unsigned maj, min;
701   char *s, *name;
702 
703   for (;;) {
704     if (first) {
705       memcpy(&tar, first, i = 512);
706       first = 0;
707     } else {
708       // align to next block and read it
709       if (TT.hdr.size%512) skippy(512-TT.hdr.size%512);
710       i = readall(TT.fd, &tar, 512);
711     }
712 
713     if (i && i!=512) error_exit("short header");
714 
715     // Two consecutive empty headers ends tar even if there's more data
716     if (!i || !*tar.name) {
717       if (!i || and++) return;
718       TT.hdr.size = 0;
719       continue;
720     }
721     // ensure null temination even of pathological packets
722     tar.padd[0] = and = 0;
723 
724     // Is this a valid TAR header?
725     if (!is_tar_header(&tar)) error_exit("bad header");
726     TT.hdr.size = OTOI(tar.size);
727 
728     // If this header isn't writing something to the filesystem
729     if ((tar.type<'0' || tar.type>'7') && tar.type!='S'
730         && (*tar.magic && tar.type))
731     {
732       // Skip to next record if unknown type or payload > 1 megabyte
733       if (!strchr("KLx", tar.type) || TT.hdr.size>1<<20) skippy(TT.hdr.size);
734       // Read link or long name
735       else if (tar.type != 'x')
736         alloread(tar.type=='K'?&TT.hdr.link_target:&TT.hdr.name, TT.hdr.size);
737       // Loop through 'x' payload records in "LEN NAME=VALUE\n" format
738       else {
739         char *p, *pp, *buf = 0;
740         unsigned i, len, n;
741 
742         alloread(&buf, TT.hdr.size);
743         for (p = buf; (p-buf)<TT.hdr.size; p += len) {
744           i = TT.hdr.size-(p-buf);
745           if (1!=sscanf(p, "%u %n", &len, &n) || len<n+4 || len>i || n>i) {
746             error_msg("bad header");
747             break;
748           }
749           p[len-1] = 0;
750           pp = p+n;
751           // Ignore "RHT." prefix, if any.
752           strstart(&pp, "RHT.");
753           if ((FLAG(selinux) && !(FLAG(t)|FLAG(O)))
754               && strstart(&pp, "security.selinux="))
755           {
756             i = strlen(pp);
757             sefd = xopen("/proc/self/attr/fscreate", O_WRONLY|WARN_ONLY);
758             if (sefd==-1 ||  i!=write(sefd, pp, i))
759               perror_msg("setfscreatecon %s", pp);
760           } else if (strstart(&pp, "path=")) freedup(&TT.hdr.name, pp);
761           // legacy sparse format circa 2005
762           else if (strstart(&pp, "GNU.sparse.name=")) freedup(&TT.hdr.name, pp);
763           else if (strstart(&pp, "GNU.sparse.realsize="))
764             TT.hdr.oldsparse = atoll(pp);
765         }
766         free(buf);
767       }
768 
769       continue;
770     }
771 
772     // Handle sparse file type
773     TT.sparselen = 0;
774     if (tar.type == 'S') {
775       char sparse[512];
776       int max = 8;
777 
778       // Load 4 pairs of offset/len from S block, plus 21 pairs from each
779       // continuation block, list says where to seek/write sparse file contents
780       s = 386+(char *)&tar;
781       *sparse = i = 0;
782 
783       for (;;) {
784         if (!(TT.sparselen&511))
785           TT.sparse = xrealloc(TT.sparse, (TT.sparselen+512)*sizeof(long long));
786 
787         // If out of data in block check continue flag, stop or load next block
788         if (++i>max || !*s) {
789           if (!(*sparse ? sparse[504] : ((char *)&tar)[482])) break;
790           xreadall(TT.fd, s = sparse, 512);
791           max = 41;
792           i = 0;
793         }
794         // Load next entry
795         TT.sparse[TT.sparselen++] = otoi(s, 12);
796         s += 12;
797       }
798 
799       // Odd number of entries (from corrupted tar) would be dropped here
800       TT.sparselen /= 2;
801       if (TT.sparselen)
802         TT.hdr.ssize = TT.sparse[2*TT.sparselen-1]+TT.sparse[2*TT.sparselen-2];
803     } else {
804       TT.hdr.ssize = TT.hdr.size;
805 
806       // Handle obsolete sparse format
807       if (TT.hdr.oldsparse>0) {
808         char sparse[512], c;
809         long long ll = 0;
810 
811         s = sparse+512;
812         for (i = 0;;) {
813           if (s == sparse+512) {
814             if (TT.hdr.size<512) break;
815             xreadall(TT.fd, s = sparse, 512);
816             TT.hdr.size -= 512;
817           } else if (!(c = *s++)) break;
818           else if (isdigit(c)) ll = (10*ll)+c-'0';
819           else {
820             if (!TT.sparselen)
821               TT.sparse = xzalloc(((TT.sparselen = ll)+1)*2*sizeof(long long));
822             else TT.sparse[i++] = ll;
823             ll = 0;
824             if (i == TT.sparselen*2) break;
825           }
826         }
827         if (TT.sparselen) {
828           ll = TT.sparse[2*(TT.sparselen-1)]+TT.sparse[2*TT.sparselen-1];
829           if (TT.hdr.oldsparse>ll)
830             TT.sparse[2*TT.sparselen++] = TT.hdr.oldsparse;
831         }
832         TT.hdr.oldsparse = 0;
833       }
834     }
835 
836     // At this point, we have something to output. Convert metadata.
837     TT.hdr.mode = OTOI(tar.mode)&0xfff;
838     if (tar.type == 'S' || !tar.type || !*tar.magic) TT.hdr.mode |= 0x8000;
839     else TT.hdr.mode |= (char []){8,8,10,2,6,4,1,8}[tar.type-'0']<<12;
840     TT.hdr.uid = OTOI(tar.uid);
841     TT.hdr.gid = OTOI(tar.gid);
842     TT.hdr.mtime = OTOI(tar.mtime);
843     maj = OTOI(tar.major);
844     min = OTOI(tar.minor);
845     TT.hdr.device = dev_makedev(maj, min);
846     TT.hdr.uname = xstrndup(TT.owner ? : tar.uname, sizeof(tar.uname));
847     TT.hdr.gname = xstrndup(TT.group ? : tar.gname, sizeof(tar.gname));
848 
849     if (TT.owner) TT.hdr.uid = TT.ouid;
850     else if (!FLAG(numeric_owner)) {
851       struct passwd *pw = bufgetpwnamuid(TT.hdr.uname, 0);
852       if (pw && (TT.owner || !FLAG(numeric_owner))) TT.hdr.uid = pw->pw_uid;
853     }
854 
855     if (TT.group) TT.hdr.gid = TT.ggid;
856     else if (!FLAG(numeric_owner)) {
857       struct group *gr = bufgetgrnamgid(TT.hdr.gname, 0);
858       if (gr) TT.hdr.gid = gr->gr_gid;
859     }
860 
861     if (!TT.hdr.link_target && *tar.link)
862       TT.hdr.link_target = xstrndup(tar.link, sizeof(tar.link));
863     if (!TT.hdr.name) {
864       // Glue prefix and name fields together with / if necessary
865       i = (tar.type=='S') ? 0 : strnlen(tar.prefix, sizeof(tar.prefix));
866       TT.hdr.name = xmprintf("%.*s%s%.*s", i, tar.prefix,
867         (i && tar.prefix[i-1] != '/') ? "/" : "",
868         (int)sizeof(tar.name), tar.name);
869     }
870 
871     // Old broken tar recorded dir as "file with trailing slash"
872     if (S_ISREG(TT.hdr.mode) && (s = strend(TT.hdr.name, "/"))) {
873       *s = 0;
874       TT.hdr.mode = (TT.hdr.mode & ~S_IFMT) | S_IFDIR;
875     }
876 
877     // Non-regular files don't have contents stored in archive.
878     if ((TT.hdr.link_target && *TT.hdr.link_target)
879       || (tar.type && !S_ISREG(TT.hdr.mode)))
880         TT.hdr.size = 0;
881 
882     // Files are seen even if excluded, so check them here.
883     // TT.seen points to first seen entry in TT.incl, or NULL if none yet.
884 
885     if ((delete = filter(TT.incl, TT.hdr.name)) && TT.incl != TT.seen) {
886       if (!TT.seen) TT.seen = delete;
887 
888       // Move seen entry to end of list.
889       if (TT.incl == delete) TT.incl = TT.incl->next;
890       else for (walk = TT.incl; walk != TT.seen; walk = walk->next) {
891         if (walk == delete) {
892           dlist_pop(&walk);
893           dlist_add_nomalloc(&TT.incl, delete);
894         }
895       }
896     }
897 
898     // Skip excluded files, filtering on the untransformed name.
899     if (filter(TT.excl, name = TT.hdr.name) || (TT.incl && !delete)) {
900       skippy(TT.hdr.size);
901       goto done;
902     }
903 
904     // We accept --show-transformed but always do, so it's a NOP.
905     name = TT.hdr.name;
906     if (xform(&name, 'r')) {
907       free(TT.hdr.name);
908       TT.hdr.name = name;
909     }
910     if ((i = "\0hs"[stridx("12", tar.type)+1])) xform(&TT.hdr.link_target, i);
911 
912     for (i = 0; i<TT.strip; i++) {
913       char *s = strchr(name, '/');
914 
915       if (s && s[1]) name = s+1;
916       else {
917         if (S_ISDIR(TT.hdr.mode)) *name = 0;
918         break;
919       }
920     }
921 
922     if (!*name) skippy(TT.hdr.size);
923     else if (FLAG(t)) {
924       if (FLAG(v)) {
925         struct tm *lc = localtime(TT.mtime ? &TT.mtt : &TT.hdr.mtime);
926         char perm[12], gname[12];
927 
928         mode_to_string(TT.hdr.mode, perm);
929         printf("%s", perm);
930         sprintf(perm, "%u", TT.hdr.uid);
931         sprintf(gname, "%u", TT.hdr.gid);
932         printf(" %s/%s ", *TT.hdr.uname ? TT.hdr.uname : perm,
933           *TT.hdr.gname ? TT.hdr.gname : gname);
934         if (tar.type=='3' || tar.type=='4') printf("%u,%u", maj, min);
935         else printf("%9lld", TT.hdr.ssize);
936         sprintf(perm, ":%02d", lc->tm_sec);
937         printf("  %d-%02d-%02d %02d:%02d%s ", 1900+lc->tm_year, 1+lc->tm_mon,
938           lc->tm_mday, lc->tm_hour, lc->tm_min, FLAG(full_time) ? perm : "");
939       }
940       printf("%s", name);
941       if (TT.hdr.link_target)
942         printf(" %s %s", tar.type=='2' ? "->" : "link to", TT.hdr.link_target);
943       xputc('\n');
944       skippy(TT.hdr.size);
945     } else {
946       if (FLAG(v)) printf("%s\n", name);
947       if (FLAG(O)) sendfile_sparse(1);
948       else if (FLAG(to_command)) {
949         if (S_ISREG(TT.hdr.mode)) {
950           int fd, pid;
951 
952           xsetenv("TAR_FILETYPE", "f");
953           xsetenv(xmprintf("TAR_MODE=%o", TT.hdr.mode), 0);
954           xsetenv(xmprintf("TAR_SIZE=%lld", TT.hdr.ssize), 0);
955           xsetenv("TAR_FILENAME", name);
956           xsetenv("TAR_UNAME", TT.hdr.uname);
957           xsetenv("TAR_GNAME", TT.hdr.gname);
958           xsetenv(xmprintf("TAR_MTIME=%llo", (long long)TT.hdr.mtime), 0);
959           xsetenv(xmprintf("TAR_UID=%o", TT.hdr.uid), 0);
960           xsetenv(xmprintf("TAR_GID=%o", TT.hdr.gid), 0);
961 
962           pid = xpopen((char *[]){"sh", "-c", TT.to_command, NULL}, &fd, 0);
963           // TODO: short write exits tar here, other skips data.
964           sendfile_sparse(fd);
965           fd = xpclose_both(pid, 0);
966           if (fd) error_msg("%d: Child returned %d", pid, fd);
967         }
968       } else extract_to_disk(name);
969     }
970 
971 done:
972     if (sefd != -1) {
973       // zero length write resets fscreate context to default
974       (void)write(sefd, 0, 0);
975       close(sefd);
976       sefd = -1;
977     }
978     free(TT.hdr.name);
979     free(TT.hdr.link_target);
980     free(TT.hdr.uname);
981     free(TT.hdr.gname);
982     TT.hdr.name = TT.hdr.link_target = 0;
983   }
984 }
985 
986 // Add copy of filename (minus trailing \n and /) to dlist **
trim2list(void * list,char * pline)987 static void trim2list(void *list, char *pline)
988 {
989   char *n = xstrdup(pline);
990   int i = strlen(n);
991 
992   dlist_add(list, n);
993   if (i && n[i-1]=='\n') i--;
994   while (i>1 && n[i-1] == '/') i--;
995   n[i] = 0;
996 }
997 
998 // do_lines callback, selects TT.incl or TT.excl based on call order
do_XT(char ** pline,long len)999 static void do_XT(char **pline, long len)
1000 {
1001   if (pline) trim2list(TT.X ? &TT.excl : &TT.incl, *pline);
1002 }
1003 
get_archiver()1004 static  char *get_archiver()
1005 {
1006   return TT.I ? : FLAG(z) ? "gzip" : FLAG(j) ? "bzip2" : "xz";
1007 }
1008 
tar_main(void)1009 void tar_main(void)
1010 {
1011   char *s, **xfsed, **args = toys.optargs;
1012   int len = 0, ii;
1013 
1014   // Needed when extracting to command
1015   signal(SIGPIPE, SIG_IGN);
1016 
1017   // Get possible early errors out of the way
1018   if (!geteuid()) toys.optflags |= FLAG_p;
1019   if (TT.owner) {
1020     if (!(s = strchr(TT.owner, ':'))) TT.ouid = xgetuid(TT.owner);
1021     else {
1022       TT.owner = xstrndup(TT.owner, s++-TT.owner);
1023       TT.ouid = atolx_range(s, 0, INT_MAX);
1024     }
1025   }
1026   if (TT.group) {
1027     if (!(s = strchr(TT.group, ':'))) TT.ggid = xgetgid(TT.group);
1028     else {
1029       TT.group = xstrndup(TT.group, s++-TT.group);
1030       TT.ggid = atolx_range(s, 0, INT_MAX);
1031     }
1032   }
1033   if (TT.mtime) xparsedate(TT.mtime, &TT.mtt, (void *)&s, 1);
1034 
1035   // TODO: collect filter types here and annotate saved include/exclude?
1036 
1037   // Collect file list.
1038   for (; TT.exclude; TT.exclude = TT.exclude->next)
1039     trim2list(&TT.excl, TT.exclude->arg);
1040   for (;TT.X; TT.X = TT.X->next) do_lines(xopenro(TT.X->arg), '\n', do_XT);
1041   for (args = toys.optargs; *args; args++) trim2list(&TT.incl, *args);
1042   // -T is always --verbatim-files-from: no quote removal or -arg handling
1043   for (;TT.T; TT.T = TT.T->next)
1044     do_lines(xopenro(TT.T->arg), '\n'*!FLAG(null), do_XT);
1045 
1046   // If include file list empty, don't create empty archive
1047   if (FLAG(c)) {
1048     if (!TT.incl) error_exit("empty archive");
1049     TT.fd = 1;
1050   }
1051 
1052   if (TT.xform) {
1053     struct arg_list *al;
1054 
1055     for (ii = 0, al = TT.xform; al; al = al->next) ii++;
1056     xfsed = xmalloc((ii+2)*2*sizeof(char *));
1057     xfsed[0] = "sed";
1058     xfsed[1] = "--tarxform";
1059     for (ii = 2, al = TT.xform; al; al = al->next) {
1060       xfsed[ii++] = "-e";
1061       xfsed[ii++] = al->arg;
1062     }
1063     xfsed[ii] = 0;
1064     TT.xfpipe[0] = TT.xfpipe[1] = -1;
1065     xpopen_both(xfsed, TT.xfpipe);
1066     free(xfsed);
1067   }
1068 
1069   // nommu reentry for nonseekable input skips this, parent did it for us
1070   if (toys.stacktop) {
1071     if (TT.f && strcmp(TT.f, "-"))
1072       TT.fd = xcreate(TT.f, TT.fd*(O_WRONLY|O_CREAT|O_TRUNC),
1073                       0666&~toys.old_umask);
1074     // Get destination directory
1075     if (TT.C) xchdir(TT.C);
1076   }
1077 
1078   // Get destination directory
1079   TT.cwd = xabspath(s = xgetcwd(), ABS_PATH);
1080   free(s);
1081 
1082   // Remember archive inode so we don't overwrite it or add it to itself
1083   {
1084     struct stat st;
1085 
1086     if (!fstat(TT.fd, &st)) {
1087       TT.archive_di.ino = st.st_ino;
1088       TT.archive_di.dev = st.st_dev;
1089     }
1090   }
1091 
1092   // Are we reading?
1093   if (FLAG(x)||FLAG(t)) {
1094     char *hdr = 0;
1095 
1096     // autodetect compression type when not specified
1097     if (!(FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J))) {
1098       len = xread(TT.fd, hdr = toybuf+sizeof(toybuf)-512, 512);
1099       if (len!=512 || !is_tar_header(hdr)) {
1100         // detect gzip and bzip signatures
1101         if (SWAP_BE16(*(short *)hdr)==0x1f8b) toys.optflags |= FLAG_z;
1102         else if (!smemcmp(hdr, "BZh", 3)) toys.optflags |= FLAG_j;
1103         else if (peek_be(hdr, 7) == 0xfd377a585a0000ULL) toys.optflags |= FLAG_J;
1104         else error_exit("Not tar");
1105 
1106         // if we can seek back we don't need to loop and copy data
1107         if (!lseek(TT.fd, -len, SEEK_CUR)) hdr = 0;
1108       }
1109     }
1110 
1111     if (FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J)) {
1112       int pipefd[2] = {hdr ? -1 : TT.fd, -1}, i, pid;
1113       struct string_list *zcat = FLAG(I) ? 0 : find_in_path(getenv("PATH"),
1114         FLAG(z) ? "zcat" : FLAG(j) ? "bzcat" : "xzcat");
1115 
1116       // Toybox provides more decompressors than compressors, so try them first
1117       TT.pid = xpopen_both(zcat ? (char *[]){zcat->str, 0} :
1118         (char *[]){get_archiver(), "-d", 0}, pipefd);
1119       if (CFG_TOYBOX_FREE) llist_traverse(zcat, free);
1120 
1121       if (!hdr) {
1122         // If we could seek, child gzip inherited fd and we read its output
1123         close(TT.fd);
1124         TT.fd = pipefd[1];
1125 
1126       } else {
1127 
1128         // If we autodetected type but then couldn't lseek to put the data back
1129         // we have to loop reading data from TT.fd and pass it to gzip ourselves
1130         // (starting with the block of data we read to autodetect).
1131 
1132         // dirty trick: move gzip input pipe to stdin so child closes spare copy
1133         dup2(pipefd[0], 0);
1134         if (pipefd[0]) close(pipefd[0]);
1135 
1136         // Fork a copy of ourselves to handle extraction (reads from zip output
1137         // pipe, writes to stdout).
1138         pipefd[0] = pipefd[1];
1139         pipefd[1] = 1;
1140         pid = xpopen_both(0, pipefd);
1141         close(pipefd[1]);
1142 
1143         // loop writing collated data to zip proc
1144         xwrite(0, hdr, len);
1145         for (;;) {
1146           if ((i = read(TT.fd, toybuf, sizeof(toybuf)))<1) {
1147             close(0);
1148             xwaitpid(pid);
1149             return;
1150           }
1151           xwrite(0, toybuf, i);
1152         }
1153       }
1154     }
1155 
1156     unpack_tar(hdr);
1157     dirflush(0, 0);
1158     // Shut up archiver about inability to write all trailing NULs to pipe buf
1159     while (0<read(TT.fd, toybuf, sizeof(toybuf)));
1160 
1161     // Each time a TT.incl entry is seen it's moved to the end of the list,
1162     // with TT.seen pointing to first seen list entry. Anything between
1163     // TT.incl and TT.seen wasn't encountered in archive..
1164     if (TT.seen != TT.incl) {
1165       if (!TT.seen) TT.seen = TT.incl;
1166       while (TT.incl != TT.seen) {
1167         error_msg("'%s' not in archive", TT.incl->data);
1168         TT.incl = TT.incl->next;
1169       }
1170     }
1171 
1172   // are we writing? (Don't have to test flag here, one of 3 must be set)
1173   } else {
1174     struct double_list *dl = TT.incl;
1175 
1176     // autodetect compression type based on -f name. (Use > to avoid.)
1177     if (TT.f && !FLAG(j) && !FLAG(z) && !FLAG(I) && !FLAG(J)) {
1178       char *tbz[] = {".tbz", ".tbz2", ".tar.bz", ".tar.bz2"};
1179       if (strend(TT.f, ".tgz") || strend(TT.f, ".tar.gz"))
1180         toys.optflags |= FLAG_z;
1181       if (strend(TT.f, ".txz") || strend(TT.f, ".tar.xz"))
1182         toys.optflags |= FLAG_J;
1183       else for (len = 0; len<ARRAY_LEN(tbz); len++)
1184         if (strend(TT.f, tbz[len])) toys.optflags |= FLAG_j;
1185     }
1186 
1187     if (FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J)) {
1188       int pipefd[2] = {-1, TT.fd};
1189 
1190       TT.pid = xpopen_both((char *[]){get_archiver(), 0}, pipefd);
1191       close(TT.fd);
1192       TT.fd = pipefd[0];
1193     }
1194     do {
1195       TT.warn = 1;
1196       dirtree_flagread(dl->data,
1197         DIRTREE_SYMFOLLOW*FLAG(h)|DIRTREE_BREADTH*(FLAG(sort)|FLAG(s)),
1198         add_to_tar);
1199     } while (TT.incl != (dl = dl->next));
1200 
1201     writeall(TT.fd, toybuf, 1024);
1202     close(TT.fd);
1203   }
1204   if (TT.pid) {
1205     TT.pid = xpclose_both(TT.pid, 0);
1206     if (TT.pid) toys.exitval = TT.pid;
1207   }
1208   if (toys.exitval) error_msg("had errors");
1209 
1210   if (CFG_TOYBOX_FREE) {
1211     llist_traverse(TT.excl, llist_free_double);
1212     llist_traverse(TT.incl, llist_free_double);
1213     while(TT.hlc) free(TT.hlx[--TT.hlc].arg);
1214     free(TT.hlx);
1215     free(TT.cwd);
1216     close(TT.fd);
1217   }
1218 }
1219