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, "&(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))
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|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
529 // Do pending directory utimes(), NULL to flush all.
dirflush(char * name,int isdir)530 static int dirflush(char *name, int isdir)
531 {
532 char *s = 0, *ss;
533
534 // Barf if name not in TT.cwd
535 if (name) {
536 if (!(ss = s = xabspath(name, isdir ? ABS_LAST : 0))) {
537 error_msg("'%s' bad symlink", name);
538
539 return 1;
540 }
541 if (TT.cwd[1] && (!strstart(&ss, TT.cwd) || (*ss && *ss!='/'))) {
542 error_msg("'%s' %s not under '%s'", name, s, TT.cwd);
543 free(s);
544
545 return 1;
546 }
547
548 // --restrict means first entry extracted is what everything must be under
549 if (FLAG(restrict)) {
550 free(TT.cwd);
551 TT.cwd = xstrdup(s);
552 toys.optflags ^= FLAG_restrict;
553 }
554 // use resolved name so trailing / is stripped
555 if (isdir) unlink(s);
556 }
557
558 // Set deferred utimes() for directories this file isn't under.
559 // (Files must be depth-first ordered in tarball for this to matter.)
560 while (TT.dirs) {
561
562 // If next file is under (or equal to) this dir, keep waiting
563 if (name && strstart(&ss, ss = s) && (!*ss || *ss=='/')) break;
564
565 wsettime(TT.dirs->str+sizeof(long long), *(long long *)TT.dirs->str);
566 free(llist_pop(&TT.dirs));
567 }
568 free(s);
569
570 // name was under TT.cwd
571 return 0;
572 }
573
574 // write data to file
sendfile_sparse(int fd)575 static void sendfile_sparse(int fd)
576 {
577 long long len, used = 0, sent;
578 int i = 0, j;
579
580 do {
581 if (TT.sparselen) {
582 // Seek past holes or fill output with zeroes.
583 if (-1 == lseek(fd, len = TT.sparse[i*2], SEEK_SET)) {
584 sent = 0;
585 while (len) {
586 // first/last 512 bytes used, rest left zeroes
587 j = (len>3072) ? 3072 : len;
588 if (j != writeall(fd, toybuf+512, j)) goto error;
589 len -= j;
590 }
591 } else {
592 sent = len;
593 if (!(len = TT.sparse[i*2+1]) && ftruncate(fd, sent+len))
594 perror_msg("ftruncate");
595 }
596 if (len+used>TT.hdr.size) error_exit("sparse overflow");
597 } else len = TT.hdr.size;
598
599 len -= sendfile_len(TT.fd, fd, len, &sent);
600 used += sent;
601 if (len) {
602 error:
603 if (fd!=1) perror_msg(0);
604 skippy(TT.hdr.size-used);
605
606 break;
607 }
608 } while (++i<TT.sparselen);
609
610 close(fd);
611 }
612
extract_to_disk(char * name)613 static void extract_to_disk(char *name)
614 {
615 int ala = TT.hdr.mode;
616
617 if (dirflush(name, S_ISDIR(ala))) {
618 if (S_ISREG(ala) && !TT.hdr.link_target) skippy(TT.hdr.size);
619
620 return;
621 }
622
623 // create path before file if necessary
624 if (strrchr(name, '/') && mkpath(name) && errno!=EEXIST)
625 return perror_msg(":%s: can't mkdir", name);
626
627 // remove old file, if exists
628 if (!FLAG(k) && !S_ISDIR(ala) && rmdir(name) && errno!=ENOENT && unlink(name))
629 return perror_msg("can't remove: %s", name);
630
631 if (S_ISREG(ala)) {
632 // hardlink?
633 if (TT.hdr.link_target) {
634 if (link(TT.hdr.link_target, name))
635 return perror_msg("can't link '%s' -> '%s'", name, TT.hdr.link_target);
636 // write contents
637 } else {
638 int fd = WARN_ONLY|O_WRONLY|O_CREAT|(FLAG(overwrite) ? O_TRUNC : O_EXCL);
639
640 if ((fd = xcreate(name, fd, ala&07777)) != -1) sendfile_sparse(fd);
641 else return skippy(TT.hdr.size);
642 }
643 } else if (S_ISDIR(ala)) {
644 if ((mkdir(name, 0700) == -1) && errno != EEXIST)
645 return perror_msg("%s: can't create", name);
646 } else if (S_ISLNK(ala)) {
647 if (symlink(TT.hdr.link_target, name))
648 return perror_msg("can't link '%s' -> '%s'", name, TT.hdr.link_target);
649 } else if (mknod(name, ala, TT.hdr.device))
650 return perror_msg("can't create '%s'", name);
651
652 // Set ownership
653 if (!FLAG(o) && !geteuid()) {
654 int u = TT.hdr.uid, g = TT.hdr.gid;
655
656 if (TT.owner) TT.hdr.uid = TT.ouid;
657 else if (!FLAG(numeric_owner) && *TT.hdr.uname) {
658 struct passwd *pw = bufgetpwnamuid(TT.hdr.uname, 0);
659 if (pw) TT.hdr.uid = pw->pw_uid;
660 }
661
662 if (TT.group) TT.hdr.gid = TT.ggid;
663 else if (!FLAG(numeric_owner) && *TT.hdr.uname) {
664 struct group *gr = bufgetgrnamgid(TT.hdr.gname, 0);
665 if (gr) TT.hdr.gid = gr->gr_gid;
666 }
667
668 if (lchown(name, u, g)) perror_msg("chown %d:%d '%s'", u, g, name);;
669 }
670
671 if (!S_ISLNK(ala)) chmod(name, FLAG(p) ? ala : ala&0777);
672
673 // Apply mtime.
674 if (!FLAG(m)) {
675 if (S_ISDIR(ala)) {
676 struct string_list *sl;
677
678 // Writing files into a directory changes directory timestamps, so
679 // defer mtime updates until contents written.
680
681 sl = xmalloc(sizeof(struct string_list)+sizeof(long long)+strlen(name)+1);
682 *(long long *)sl->str = TT.hdr.mtime;
683 strcpy(sl->str+sizeof(long long), name);
684 sl->next = TT.dirs;
685 TT.dirs = sl;
686 } else wsettime(name, TT.hdr.mtime);
687 }
688 }
689
unpack_tar(char * first)690 static void unpack_tar(char *first)
691 {
692 struct double_list *walk, *delete;
693 struct tar_hdr tar;
694 int i, sefd = -1, and = 0;
695 unsigned maj, min;
696 char *s, *name;
697
698 for (;;) {
699 if (first) {
700 memcpy(&tar, first, i = 512);
701 first = 0;
702 } else {
703 // align to next block and read it
704 if (TT.hdr.size%512) skippy(512-TT.hdr.size%512);
705 i = readall(TT.fd, &tar, 512);
706 }
707
708 if (i && i!=512) error_exit("short header");
709
710 // Two consecutive empty headers ends tar even if there's more data
711 if (!i || !*tar.name) {
712 if (!i || and++) return;
713 TT.hdr.size = 0;
714 continue;
715 }
716 // ensure null temination even of pathological packets
717 tar.padd[0] = and = 0;
718
719 // Is this a valid TAR header?
720 if (!is_tar_header(&tar)) error_exit("bad header");
721 TT.hdr.size = OTOI(tar.size);
722
723 // If this header isn't writing something to the filesystem
724 if ((tar.type<'0' || tar.type>'7') && tar.type!='S'
725 && (*tar.magic && tar.type))
726 {
727 // Skip to next record if unknown type or payload > 1 megabyte
728 if (!strchr("KLx", tar.type) || TT.hdr.size>1<<20) skippy(TT.hdr.size);
729 // Read link or long name
730 else if (tar.type != 'x')
731 alloread(tar.type=='K'?&TT.hdr.link_target:&TT.hdr.name, TT.hdr.size);
732 // Loop through 'x' payload records in "LEN NAME=VALUE\n" format
733 else {
734 char *p, *pp, *buf = 0;
735 unsigned i, len, n;
736
737 alloread(&buf, TT.hdr.size);
738 for (p = buf; (p-buf)<TT.hdr.size; p += len) {
739 i = TT.hdr.size-(p-buf);
740 if (1!=sscanf(p, "%u %n", &len, &n) || len<n+4 || len>i || n>i) {
741 error_msg("bad header");
742 break;
743 }
744 p[len-1] = 0;
745 pp = p+n;
746 // Ignore "RHT." prefix, if any.
747 strstart(&pp, "RHT.");
748 if ((FLAG(selinux) && !(FLAG(t)|FLAG(O)))
749 && strstart(&pp, "security.selinux="))
750 {
751 i = strlen(pp);
752 sefd = xopen("/proc/self/attr/fscreate", O_WRONLY|WARN_ONLY);
753 if (sefd==-1 || i!=write(sefd, pp, i))
754 perror_msg("setfscreatecon %s", pp);
755 } else if (strstart(&pp, "path=")) {
756 free(TT.hdr.name);
757 TT.hdr.name = xstrdup(pp);
758 break;
759 }
760 }
761 free(buf);
762 }
763
764 continue;
765 }
766
767 // Handle sparse file type
768 TT.sparselen = 0;
769 if (tar.type == 'S') {
770 char sparse[512];
771 int max = 8;
772
773 // Load 4 pairs of offset/len from S block, plus 21 pairs from each
774 // continuation block, list says where to seek/write sparse file contents
775 s = 386+(char *)&tar;
776 *sparse = i = 0;
777
778 for (;;) {
779 if (!(TT.sparselen&511))
780 TT.sparse = xrealloc(TT.sparse, (TT.sparselen+512)*sizeof(long long));
781
782 // If out of data in block check continue flag, stop or load next block
783 if (++i>max || !*s) {
784 if (!(*sparse ? sparse[504] : ((char *)&tar)[482])) break;
785 xreadall(TT.fd, s = sparse, 512);
786 max = 41;
787 i = 0;
788 }
789 // Load next entry
790 TT.sparse[TT.sparselen++] = otoi(s, 12);
791 s += 12;
792 }
793
794 // Odd number of entries (from corrupted tar) would be dropped here
795 TT.sparselen /= 2;
796 if (TT.sparselen)
797 TT.hdr.ssize = TT.sparse[2*TT.sparselen-1]+TT.sparse[2*TT.sparselen-2];
798 } else TT.hdr.ssize = TT.hdr.size;
799
800 // At this point, we have something to output. Convert metadata.
801 TT.hdr.mode = OTOI(tar.mode)&0xfff;
802 if (tar.type == 'S' || !tar.type || !*tar.magic) TT.hdr.mode |= 0x8000;
803 else TT.hdr.mode |= (char []){8,8,10,2,6,4,1,8}[tar.type-'0']<<12;
804 TT.hdr.uid = OTOI(tar.uid);
805 TT.hdr.gid = OTOI(tar.gid);
806 TT.hdr.mtime = OTOI(tar.mtime);
807 maj = OTOI(tar.major);
808 min = OTOI(tar.minor);
809 TT.hdr.device = dev_makedev(maj, min);
810 TT.hdr.uname = xstrndup(TT.owner ? : tar.uname, sizeof(tar.uname));
811 TT.hdr.gname = xstrndup(TT.group ? : tar.gname, sizeof(tar.gname));
812
813 if (TT.owner) TT.hdr.uid = TT.ouid;
814 else if (!FLAG(numeric_owner)) {
815 struct passwd *pw = bufgetpwnamuid(TT.hdr.uname, 0);
816 if (pw && (TT.owner || !FLAG(numeric_owner))) TT.hdr.uid = pw->pw_uid;
817 }
818
819 if (TT.group) TT.hdr.gid = TT.ggid;
820 else if (!FLAG(numeric_owner)) {
821 struct group *gr = bufgetgrnamgid(TT.hdr.gname, 0);
822 if (gr) TT.hdr.gid = gr->gr_gid;
823 }
824
825 if (!TT.hdr.link_target && *tar.link)
826 TT.hdr.link_target = xstrndup(tar.link, sizeof(tar.link));
827 if (!TT.hdr.name) {
828 // Glue prefix and name fields together with / if necessary
829 i = (tar.type=='S') ? 0 : strnlen(tar.prefix, sizeof(tar.prefix));
830 TT.hdr.name = xmprintf("%.*s%s%.*s", i, tar.prefix,
831 (i && tar.prefix[i-1] != '/') ? "/" : "",
832 (int)sizeof(tar.name), tar.name);
833 }
834
835 // Old broken tar recorded dir as "file with trailing slash"
836 if (S_ISREG(TT.hdr.mode) && (s = strend(TT.hdr.name, "/"))) {
837 *s = 0;
838 TT.hdr.mode = (TT.hdr.mode & ~S_IFMT) | S_IFDIR;
839 }
840
841 // Non-regular files don't have contents stored in archive.
842 if ((TT.hdr.link_target && *TT.hdr.link_target)
843 || (tar.type && !S_ISREG(TT.hdr.mode)))
844 TT.hdr.size = 0;
845
846 // Files are seen even if excluded, so check them here.
847 // TT.seen points to first seen entry in TT.incl, or NULL if none yet.
848
849 if ((delete = filter(TT.incl, TT.hdr.name)) && TT.incl != TT.seen) {
850 if (!TT.seen) TT.seen = delete;
851
852 // Move seen entry to end of list.
853 if (TT.incl == delete) TT.incl = TT.incl->next;
854 else for (walk = TT.incl; walk != TT.seen; walk = walk->next) {
855 if (walk == delete) {
856 dlist_pop(&walk);
857 dlist_add_nomalloc(&TT.incl, delete);
858 }
859 }
860 }
861
862 // Skip excluded files, filtering on the untransformed name.
863 if (filter(TT.excl, name = TT.hdr.name) || (TT.incl && !delete)) {
864 skippy(TT.hdr.size);
865 goto done;
866 }
867
868 // We accept --show-transformed but always do, so it's a NOP.
869 name = TT.hdr.name;
870 if (xform(&name, 'r')) {
871 free(TT.hdr.name);
872 TT.hdr.name = name;
873 }
874 if ((i = "\0hs"[stridx("12", tar.type)+1])) xform(&TT.hdr.link_target, i);
875
876 for (i = 0; i<TT.strip; i++) {
877 char *s = strchr(name, '/');
878
879 if (s && s[1]) name = s+1;
880 else {
881 if (S_ISDIR(TT.hdr.mode)) *name = 0;
882 break;
883 }
884 }
885
886 if (!*name) skippy(TT.hdr.size);
887 else if (FLAG(t)) {
888 if (FLAG(v)) {
889 struct tm *lc = localtime(TT.mtime ? &TT.mtt : &TT.hdr.mtime);
890 char perm[12], gname[12];
891
892 mode_to_string(TT.hdr.mode, perm);
893 printf("%s", perm);
894 sprintf(perm, "%u", TT.hdr.uid);
895 sprintf(gname, "%u", TT.hdr.gid);
896 printf(" %s/%s ", *TT.hdr.uname ? TT.hdr.uname : perm,
897 *TT.hdr.gname ? TT.hdr.gname : gname);
898 if (tar.type=='3' || tar.type=='4') printf("%u,%u", maj, min);
899 else printf("%9lld", TT.hdr.ssize);
900 sprintf(perm, ":%02d", lc->tm_sec);
901 printf(" %d-%02d-%02d %02d:%02d%s ", 1900+lc->tm_year, 1+lc->tm_mon,
902 lc->tm_mday, lc->tm_hour, lc->tm_min, FLAG(full_time) ? perm : "");
903 }
904 printf("%s", name);
905 if (TT.hdr.link_target)
906 printf(" %s %s", tar.type=='2' ? "->" : "link to", TT.hdr.link_target);
907 xputc('\n');
908 skippy(TT.hdr.size);
909 } else {
910 if (FLAG(v)) printf("%s\n", name);
911 if (FLAG(O)) sendfile_sparse(1);
912 else if (FLAG(to_command)) {
913 if (S_ISREG(TT.hdr.mode)) {
914 int fd, pid;
915
916 xsetenv("TAR_FILETYPE", "f");
917 xsetenv(xmprintf("TAR_MODE=%o", TT.hdr.mode), 0);
918 xsetenv(xmprintf("TAR_SIZE=%lld", TT.hdr.ssize), 0);
919 xsetenv("TAR_FILENAME", name);
920 xsetenv("TAR_UNAME", TT.hdr.uname);
921 xsetenv("TAR_GNAME", TT.hdr.gname);
922 xsetenv(xmprintf("TAR_MTIME=%llo", (long long)TT.hdr.mtime), 0);
923 xsetenv(xmprintf("TAR_UID=%o", TT.hdr.uid), 0);
924 xsetenv(xmprintf("TAR_GID=%o", TT.hdr.gid), 0);
925
926 pid = xpopen((char *[]){"sh", "-c", TT.to_command, NULL}, &fd, 0);
927 // TODO: short write exits tar here, other skips data.
928 sendfile_sparse(fd);
929 fd = xpclose_both(pid, 0);
930 if (fd) error_msg("%d: Child returned %d", pid, fd);
931 }
932 } else extract_to_disk(name);
933 }
934
935 done:
936 if (sefd != -1) {
937 // zero length write resets fscreate context to default
938 (void)write(sefd, 0, 0);
939 close(sefd);
940 sefd = -1;
941 }
942 free(TT.hdr.name);
943 free(TT.hdr.link_target);
944 free(TT.hdr.uname);
945 free(TT.hdr.gname);
946 TT.hdr.name = TT.hdr.link_target = 0;
947 }
948 }
949
950 // Add copy of filename (minus trailing \n and /) to dlist **
trim2list(void * list,char * pline)951 static void trim2list(void *list, char *pline)
952 {
953 char *n = xstrdup(pline);
954 int i = strlen(n);
955
956 dlist_add(list, n);
957 if (i && n[i-1]=='\n') i--;
958 while (i>1 && n[i-1] == '/') i--;
959 n[i] = 0;
960 }
961
962 // do_lines callback, selects TT.incl or TT.excl based on call order
do_XT(char ** pline,long len)963 static void do_XT(char **pline, long len)
964 {
965 if (pline) trim2list(TT.X ? &TT.excl : &TT.incl, *pline);
966 }
967
get_archiver()968 static char *get_archiver()
969 {
970 return TT.I ? : FLAG(z) ? "gzip" : FLAG(j) ? "bzip2" : "xz";
971 }
972
tar_main(void)973 void tar_main(void)
974 {
975 char *s, **xfsed, **args = toys.optargs;
976 int len = 0, ii;
977
978 // Needed when extracting to command
979 signal(SIGPIPE, SIG_IGN);
980
981 // Get possible early errors out of the way
982 if (!geteuid()) toys.optflags |= FLAG_p;
983 if (TT.owner) {
984 if (!(s = strchr(TT.owner, ':'))) TT.ouid = xgetuid(TT.owner);
985 else {
986 TT.owner = xstrndup(TT.owner, s++-TT.owner);
987 TT.ouid = atolx_range(s, 0, INT_MAX);
988 }
989 }
990 if (TT.group) {
991 if (!(s = strchr(TT.group, ':'))) TT.ggid = xgetgid(TT.group);
992 else {
993 TT.group = xstrndup(TT.group, s++-TT.group);
994 TT.ggid = atolx_range(s, 0, INT_MAX);
995 }
996 }
997 if (TT.mtime) xparsedate(TT.mtime, &TT.mtt, (void *)&s, 1);
998
999 // TODO: collect filter types here and annotate saved include/exclude?
1000
1001 // Collect file list.
1002 for (; TT.exclude; TT.exclude = TT.exclude->next)
1003 trim2list(&TT.excl, TT.exclude->arg);
1004 for (;TT.X; TT.X = TT.X->next) do_lines(xopenro(TT.X->arg), '\n', do_XT);
1005 for (args = toys.optargs; *args; args++) trim2list(&TT.incl, *args);
1006 // -T is always --verbatim-files-from: no quote removal or -arg handling
1007 for (;TT.T; TT.T = TT.T->next)
1008 do_lines(xopenro(TT.T->arg), '\n'*!FLAG(null), do_XT);
1009
1010 // If include file list empty, don't create empty archive
1011 if (FLAG(c)) {
1012 if (!TT.incl) error_exit("empty archive");
1013 TT.fd = 1;
1014 }
1015
1016 if (TT.xform) {
1017 struct arg_list *al;
1018
1019 for (ii = 0, al = TT.xform; al; al = al->next) ii++;
1020 xfsed = xmalloc((ii+2)*2*sizeof(char *));
1021 xfsed[0] = "sed";
1022 xfsed[1] = "--tarxform";
1023 for (ii = 2, al = TT.xform; al; al = al->next) {
1024 xfsed[ii++] = "-e";
1025 xfsed[ii++] = al->arg;
1026 }
1027 xfsed[ii] = 0;
1028 TT.xfpipe[0] = TT.xfpipe[1] = -1;
1029 xpopen_both(xfsed, TT.xfpipe);
1030 free(xfsed);
1031 }
1032
1033 // nommu reentry for nonseekable input skips this, parent did it for us
1034 if (toys.stacktop) {
1035 if (TT.f && strcmp(TT.f, "-"))
1036 TT.fd = xcreate(TT.f, TT.fd*(O_WRONLY|O_CREAT|O_TRUNC), 0666);
1037 // Get destination directory
1038 if (TT.C) xchdir(TT.C);
1039 }
1040
1041 // Get destination directory
1042 TT.cwd = xabspath(s = xgetcwd(), ABS_PATH);
1043 free(s);
1044
1045 // Remember archive inode so we don't overwrite it or add it to itself
1046 {
1047 struct stat st;
1048
1049 if (!fstat(TT.fd, &st)) {
1050 TT.archive_di.ino = st.st_ino;
1051 TT.archive_di.dev = st.st_dev;
1052 }
1053 }
1054
1055 // Are we reading?
1056 if (FLAG(x)||FLAG(t)) {
1057 char *hdr = 0;
1058
1059 // autodetect compression type when not specified
1060 if (!(FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J))) {
1061 len = xread(TT.fd, hdr = toybuf+sizeof(toybuf)-512, 512);
1062 if (len!=512 || !is_tar_header(hdr)) {
1063 // detect gzip and bzip signatures
1064 if (SWAP_BE16(*(short *)hdr)==0x1f8b) toys.optflags |= FLAG_z;
1065 else if (!smemcmp(hdr, "BZh", 3)) toys.optflags |= FLAG_j;
1066 else if (peek_be(hdr, 7) == 0xfd377a585a0000ULL) toys.optflags |= FLAG_J;
1067 else error_exit("Not tar");
1068
1069 // if we can seek back we don't need to loop and copy data
1070 if (!lseek(TT.fd, -len, SEEK_CUR)) hdr = 0;
1071 }
1072 }
1073
1074 if (FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J)) {
1075 int pipefd[2] = {hdr ? -1 : TT.fd, -1}, i, pid;
1076 struct string_list *zcat = FLAG(I) ? 0 : find_in_path(getenv("PATH"),
1077 FLAG(z) ? "zcat" : FLAG(j) ? "bzcat" : "xzcat");
1078
1079 // Toybox provides more decompressors than compressors, so try them first
1080 TT.pid = xpopen_both(zcat ? (char *[]){zcat->str, 0} :
1081 (char *[]){get_archiver(), "-d", 0}, pipefd);
1082 if (CFG_TOYBOX_FREE) llist_traverse(zcat, free);
1083
1084 if (!hdr) {
1085 // If we could seek, child gzip inherited fd and we read its output
1086 close(TT.fd);
1087 TT.fd = pipefd[1];
1088
1089 } else {
1090
1091 // If we autodetected type but then couldn't lseek to put the data back
1092 // we have to loop reading data from TT.fd and pass it to gzip ourselves
1093 // (starting with the block of data we read to autodetect).
1094
1095 // dirty trick: move gzip input pipe to stdin so child closes spare copy
1096 dup2(pipefd[0], 0);
1097 if (pipefd[0]) close(pipefd[0]);
1098
1099 // Fork a copy of ourselves to handle extraction (reads from zip output
1100 // pipe, writes to stdout).
1101 pipefd[0] = pipefd[1];
1102 pipefd[1] = 1;
1103 pid = xpopen_both(0, pipefd);
1104 close(pipefd[1]);
1105
1106 // loop writing collated data to zip proc
1107 xwrite(0, hdr, len);
1108 for (;;) {
1109 if ((i = read(TT.fd, toybuf, sizeof(toybuf)))<1) {
1110 close(0);
1111 xwaitpid(pid);
1112 return;
1113 }
1114 xwrite(0, toybuf, i);
1115 }
1116 }
1117 }
1118
1119 unpack_tar(hdr);
1120 dirflush(0, 0);
1121 // Shut up archiver about inability to write all trailing NULs to pipe buf
1122 while (0<read(TT.fd, toybuf, sizeof(toybuf)));
1123
1124 // Each time a TT.incl entry is seen it's moved to the end of the list,
1125 // with TT.seen pointing to first seen list entry. Anything between
1126 // TT.incl and TT.seen wasn't encountered in archive..
1127 if (TT.seen != TT.incl) {
1128 if (!TT.seen) TT.seen = TT.incl;
1129 while (TT.incl != TT.seen) {
1130 error_msg("'%s' not in archive", TT.incl->data);
1131 TT.incl = TT.incl->next;
1132 }
1133 }
1134
1135 // are we writing? (Don't have to test flag here, one of 3 must be set)
1136 } else {
1137 struct double_list *dl = TT.incl;
1138
1139 // autodetect compression type based on -f name. (Use > to avoid.)
1140 if (TT.f && !FLAG(j) && !FLAG(z) && !FLAG(I) && !FLAG(J)) {
1141 char *tbz[] = {".tbz", ".tbz2", ".tar.bz", ".tar.bz2"};
1142 if (strend(TT.f, ".tgz") || strend(TT.f, ".tar.gz"))
1143 toys.optflags |= FLAG_z;
1144 if (strend(TT.f, ".txz") || strend(TT.f, ".tar.xz"))
1145 toys.optflags |= FLAG_J;
1146 else for (len = 0; len<ARRAY_LEN(tbz); len++)
1147 if (strend(TT.f, tbz[len])) toys.optflags |= FLAG_j;
1148 }
1149
1150 if (FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J)) {
1151 int pipefd[2] = {-1, TT.fd};
1152
1153 TT.pid = xpopen_both((char *[]){get_archiver(), 0}, pipefd);
1154 close(TT.fd);
1155 TT.fd = pipefd[0];
1156 }
1157 do {
1158 TT.warn = 1;
1159 dirtree_flagread(dl->data,
1160 DIRTREE_SYMFOLLOW*FLAG(h)|DIRTREE_BREADTH*(FLAG(sort)|FLAG(s)),
1161 add_to_tar);
1162 } while (TT.incl != (dl = dl->next));
1163
1164 writeall(TT.fd, toybuf, 1024);
1165 close(TT.fd);
1166 }
1167 if (TT.pid) {
1168 TT.pid = xpclose_both(TT.pid, 0);
1169 if (TT.pid) toys.exitval = TT.pid;
1170 }
1171 if (toys.exitval) error_msg("had errors");
1172
1173 if (CFG_TOYBOX_FREE) {
1174 llist_traverse(TT.excl, llist_free_double);
1175 llist_traverse(TT.incl, llist_free_double);
1176 while(TT.hlc) free(TT.hlx[--TT.hlc].arg);
1177 free(TT.hlx);
1178 free(TT.cwd);
1179 close(TT.fd);
1180 }
1181 }
1182