tar.c (13325B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <sys/stat.h> 3 #include <sys/time.h> 4 #include <sys/types.h> 5 #ifndef major 6 #include <sys/sysmacros.h> 7 #endif 8 9 #include <errno.h> 10 #include <fcntl.h> 11 #include <grp.h> 12 #include <libgen.h> 13 #include <pwd.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 #include <unistd.h> 18 19 #include "fs.h" 20 #include "util.h" 21 22 #define BLKSIZ 512 23 24 enum Type { 25 REG = '0', 26 AREG = '\0', 27 HARDLINK = '1', 28 SYMLINK = '2', 29 CHARDEV = '3', 30 BLOCKDEV = '4', 31 DIRECTORY = '5', 32 FIFO = '6', 33 RESERVED = '7' 34 }; 35 36 struct header { 37 char name[100]; 38 char mode[8]; 39 char uid[8]; 40 char gid[8]; 41 char size[12]; 42 char mtime[12]; 43 char chksum[8]; 44 char type; 45 char linkname[100]; 46 char magic[6]; 47 char version[2]; 48 char uname[32]; 49 char gname[32]; 50 char major[8]; 51 char minor[8]; 52 char prefix[155]; 53 }; 54 55 static struct dirtime { 56 char *name; 57 time_t mtime; 58 } *dirtimes; 59 60 static size_t dirtimeslen; 61 62 static int tarfd; 63 static ino_t tarinode; 64 static dev_t tardev; 65 66 static int mflag, vflag; 67 static int filtermode; 68 static const char *filtertool; 69 70 static const char *filtertools[] = { 71 ['J'] = "xz", 72 ['Z'] = "compress", 73 ['a'] = "lzma", 74 ['j'] = "bzip2", 75 ['z'] = "gzip", 76 }; 77 78 static void 79 pushdirtime(char *name, time_t mtime) 80 { 81 dirtimes = ereallocarray(dirtimes, dirtimeslen + 1, sizeof(*dirtimes)); 82 dirtimes[dirtimeslen].name = estrdup(name); 83 dirtimes[dirtimeslen].mtime = mtime; 84 dirtimeslen++; 85 } 86 87 static struct dirtime * 88 popdirtime(void) 89 { 90 if (dirtimeslen) { 91 dirtimeslen--; 92 return &dirtimes[dirtimeslen]; 93 } 94 return NULL; 95 } 96 97 static int 98 comp(int fd, const char *tool, const char *flags) 99 { 100 int fds[2]; 101 102 if (pipe(fds) < 0) 103 eprintf("pipe:"); 104 105 switch (fork()) { 106 case -1: 107 eprintf("fork:"); 108 case 0: 109 dup2(fd, 1); 110 dup2(fds[0], 0); 111 close(fds[0]); 112 close(fds[1]); 113 114 execlp(tool, tool, flags, NULL); 115 weprintf("execlp %s:", tool); 116 _exit(1); 117 } 118 close(fds[0]); 119 return fds[1]; 120 } 121 122 static int 123 decomp(int fd, const char *tool, const char *flags) 124 { 125 int fds[2]; 126 127 if (pipe(fds) < 0) 128 eprintf("pipe:"); 129 130 switch (fork()) { 131 case -1: 132 eprintf("fork:"); 133 case 0: 134 dup2(fd, 0); 135 dup2(fds[1], 1); 136 close(fds[0]); 137 close(fds[1]); 138 139 execlp(tool, tool, flags, NULL); 140 weprintf("execlp %s:", tool); 141 _exit(1); 142 } 143 close(fds[1]); 144 return fds[0]; 145 } 146 147 static ssize_t 148 eread(int fd, void *buf, size_t n) 149 { 150 ssize_t r; 151 152 again: 153 r = read(fd, buf, n); 154 if (r < 0) { 155 if (errno == EINTR) 156 goto again; 157 eprintf("read:"); 158 } 159 return r; 160 } 161 162 static ssize_t 163 ewrite(int fd, const void *buf, size_t n) 164 { 165 ssize_t r; 166 167 if ((r = write(fd, buf, n)) != n) 168 eprintf("write:"); 169 return r; 170 } 171 172 static void 173 putoctal(char *dst, unsigned num, int size) 174 { 175 if (snprintf(dst, size, "%.*o", size - 1, num) >= size) 176 eprintf("snprintf: input number too large\n"); 177 } 178 179 static int 180 archive(const char *path) 181 { 182 char b[BLKSIZ]; 183 struct group *gr; 184 struct header *h; 185 struct passwd *pw; 186 struct stat st; 187 size_t chksum, i; 188 ssize_t l, r; 189 int fd = -1; 190 191 if (lstat(path, &st) < 0) { 192 weprintf("lstat %s:", path); 193 return 0; 194 } else if (st.st_ino == tarinode && st.st_dev == tardev) { 195 weprintf("ignoring %s\n", path); 196 return 0; 197 } 198 199 pw = getpwuid(st.st_uid); 200 gr = getgrgid(st.st_gid); 201 202 h = (struct header *)b; 203 memset(b, 0, sizeof(b)); 204 estrlcpy(h->name, path, sizeof(h->name)); 205 putoctal(h->mode, (unsigned)st.st_mode & 0777, sizeof(h->mode)); 206 putoctal(h->uid, (unsigned)st.st_uid, sizeof(h->uid)); 207 putoctal(h->gid, (unsigned)st.st_gid, sizeof(h->gid)); 208 putoctal(h->size, 0, sizeof(h->size)); 209 putoctal(h->mtime, (unsigned)st.st_mtime, sizeof(h->mtime)); 210 memcpy( h->magic, "ustar", sizeof(h->magic)); 211 memcpy( h->version, "00", sizeof(h->version)); 212 estrlcpy(h->uname, pw ? pw->pw_name : "", sizeof(h->uname)); 213 estrlcpy(h->gname, gr ? gr->gr_name : "", sizeof(h->gname)); 214 215 if (S_ISREG(st.st_mode)) { 216 h->type = REG; 217 putoctal(h->size, (unsigned)st.st_size, sizeof(h->size)); 218 fd = open(path, O_RDONLY); 219 if (fd < 0) 220 eprintf("open %s:", path); 221 } else if (S_ISDIR(st.st_mode)) { 222 h->type = DIRECTORY; 223 } else if (S_ISLNK(st.st_mode)) { 224 h->type = SYMLINK; 225 if ((r = readlink(path, h->linkname, sizeof(h->linkname) - 1)) < 0) 226 eprintf("readlink %s:", path); 227 h->linkname[r] = '\0'; 228 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 229 h->type = S_ISCHR(st.st_mode) ? CHARDEV : BLOCKDEV; 230 putoctal(h->major, (unsigned)major(st.st_dev), sizeof(h->major)); 231 putoctal(h->minor, (unsigned)minor(st.st_dev), sizeof(h->minor)); 232 } else if (S_ISFIFO(st.st_mode)) { 233 h->type = FIFO; 234 } 235 236 memset(h->chksum, ' ', sizeof(h->chksum)); 237 for (i = 0, chksum = 0; i < sizeof(*h); i++) 238 chksum += (unsigned char)b[i]; 239 putoctal(h->chksum, chksum, sizeof(h->chksum)); 240 ewrite(tarfd, b, BLKSIZ); 241 242 if (fd != -1) { 243 while ((l = eread(fd, b, BLKSIZ)) > 0) { 244 if (l < BLKSIZ) 245 memset(b + l, 0, BLKSIZ - l); 246 ewrite(tarfd, b, BLKSIZ); 247 } 248 close(fd); 249 } 250 251 return 0; 252 } 253 254 static int 255 unarchive(char *fname, ssize_t l, char b[BLKSIZ]) 256 { 257 char lname[101], *tmp, *p; 258 long mode, major, minor, type, mtime, uid, gid; 259 struct header *h = (struct header *)b; 260 int fd = -1; 261 struct timespec times[2]; 262 263 if (!mflag && ((mtime = strtol(h->mtime, &p, 8)) < 0 || *p != '\0')) 264 eprintf("strtol %s: invalid number\n", h->mtime); 265 if (remove(fname) < 0 && errno != ENOENT) 266 weprintf("remove %s:", fname); 267 268 tmp = estrdup(fname); 269 mkdirp(dirname(tmp), 0777, 0777); 270 free(tmp); 271 272 switch (h->type) { 273 case REG: 274 case AREG: 275 case RESERVED: 276 if ((mode = strtol(h->mode, &p, 8)) < 0 || *p != '\0') 277 eprintf("strtol %s: invalid number\n", h->mode); 278 fd = open(fname, O_WRONLY | O_TRUNC | O_CREAT, 0600); 279 if (fd < 0) 280 eprintf("open %s:", fname); 281 break; 282 case HARDLINK: 283 case SYMLINK: 284 snprintf(lname, sizeof(lname), "%.*s", (int)sizeof(h->linkname), 285 h->linkname); 286 if (((h->type == HARDLINK) ? link : symlink)(lname, fname) < 0) 287 eprintf("%s %s -> %s:", 288 (h->type == HARDLINK) ? "link" : "symlink", 289 fname, lname); 290 break; 291 case DIRECTORY: 292 if ((mode = strtol(h->mode, &p, 8)) < 0 || *p != '\0') 293 eprintf("strtol %s: invalid number\n", h->mode); 294 if (mkdir(fname, (mode_t)mode) < 0 && errno != EEXIST) 295 eprintf("mkdir %s:", fname); 296 pushdirtime(fname, mtime); 297 break; 298 case CHARDEV: 299 case BLOCKDEV: 300 if ((mode = strtol(h->mode, &p, 8)) < 0 || *p != '\0') 301 eprintf("strtol %s: invalid number\n", h->mode); 302 if ((major = strtol(h->major, &p, 8)) < 0 || *p != '\0') 303 eprintf("strtol %s: invalid number\n", h->major); 304 if ((minor = strtol(h->minor, &p, 8)) < 0 || *p != '\0') 305 eprintf("strtol %s: invalid number\n", h->minor); 306 type = (h->type == CHARDEV) ? S_IFCHR : S_IFBLK; 307 if (mknod(fname, type | mode, makedev(major, minor)) < 0) 308 eprintf("mknod %s:", fname); 309 break; 310 case FIFO: 311 if ((mode = strtol(h->mode, &p, 8)) < 0 || *p != '\0') 312 eprintf("strtol %s: invalid number\n", h->mode); 313 if (mknod(fname, S_IFIFO | mode, 0) < 0) 314 eprintf("mknod %s:", fname); 315 break; 316 default: 317 eprintf("unsupported tar-filetype %c\n", h->type); 318 } 319 320 if ((uid = strtol(h->uid, &p, 8)) < 0 || *p != '\0') 321 eprintf("strtol %s: invalid number\n", h->uid); 322 if ((gid = strtol(h->gid, &p, 8)) < 0 || *p != '\0') 323 eprintf("strtol %s: invalid number\n", h->gid); 324 325 if (fd != -1) { 326 for (; l > 0; l -= BLKSIZ) 327 if (eread(tarfd, b, BLKSIZ) > 0) 328 ewrite(fd, b, MIN(l, BLKSIZ)); 329 close(fd); 330 } 331 332 if (h->type == HARDLINK) 333 return 0; 334 335 times[0].tv_sec = times[1].tv_sec = mtime; 336 times[0].tv_nsec = times[1].tv_nsec = 0; 337 if (!mflag && utimensat(AT_FDCWD, fname, times, AT_SYMLINK_NOFOLLOW) < 0) 338 weprintf("utimensat %s:", fname); 339 if (h->type == SYMLINK) { 340 if (!getuid() && lchown(fname, uid, gid)) 341 weprintf("lchown %s:", fname); 342 } else { 343 if (!getuid() && chown(fname, uid, gid)) 344 weprintf("chown %s:", fname); 345 if (chmod(fname, mode) < 0) 346 eprintf("fchmod %s:", fname); 347 } 348 349 return 0; 350 } 351 352 static void 353 skipblk(ssize_t l) 354 { 355 char b[BLKSIZ]; 356 357 for (; l > 0; l -= BLKSIZ) 358 if (!eread(tarfd, b, BLKSIZ)) 359 break; 360 } 361 362 static int 363 print(char *fname, ssize_t l, char b[BLKSIZ]) 364 { 365 puts(fname); 366 skipblk(l); 367 return 0; 368 } 369 370 static void 371 c(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r) 372 { 373 archive(r->path); 374 if (vflag) 375 puts(r->path); 376 377 if (S_ISDIR(st->st_mode)) 378 recurse(dirfd, name, NULL, r); 379 } 380 381 static void 382 sanitize(struct header *h) 383 { 384 size_t i, j; 385 struct { 386 char *f; 387 size_t l; 388 } fields[] = { 389 { h->mode, sizeof(h->mode) }, 390 { h->uid, sizeof(h->uid) }, 391 { h->gid, sizeof(h->gid) }, 392 { h->size, sizeof(h->size) }, 393 { h->mtime, sizeof(h->mtime) }, 394 { h->chksum, sizeof(h->chksum) }, 395 { h->major, sizeof(h->major) }, 396 { h->minor, sizeof(h->minor) } 397 }; 398 399 /* Numeric fields can be terminated with spaces instead of 400 * NULs as per the ustar specification. Patch all of them to 401 * use NULs so we can perform string operations on them. */ 402 for (i = 0; i < LEN(fields); i++){ 403 for (j = 0; j < fields[i].l && fields[i].f[j] == ' '; j++); 404 for (; j < fields[i].l; j++) 405 if (fields[i].f[j] == ' ') 406 fields[i].f[j] = '\0'; 407 } 408 } 409 410 static void 411 chktar(struct header *h) 412 { 413 char tmp[8], *err, *p = (char *)h; 414 const char *reason; 415 long s1, s2, i; 416 417 if (h->prefix[0] == '\0' && h->name[0] == '\0') { 418 reason = "empty filename"; 419 goto bad; 420 } 421 if (h->magic[0] && strncmp("ustar", h->magic, 5)) { 422 reason = "not ustar format"; 423 goto bad; 424 } 425 memcpy(tmp, h->chksum, sizeof(tmp)); 426 for (i = 0; i < sizeof(tmp) && tmp[i] == ' '; i++); 427 for (; i < sizeof(tmp); i++) 428 if (tmp[i] == ' ') 429 tmp[i] = '\0'; 430 s1 = strtol(tmp, &err, 8); 431 if (s1 < 0 || *err != '\0') { 432 reason = "invalid checksum"; 433 goto bad; 434 } 435 memset(h->chksum, ' ', sizeof(h->chksum)); 436 for (i = 0, s2 = 0; i < sizeof(*h); i++) 437 s2 += (unsigned char)p[i]; 438 if (s1 != s2) { 439 reason = "incorrect checksum"; 440 goto bad; 441 } 442 memcpy(h->chksum, tmp, sizeof(h->chksum)); 443 return; 444 bad: 445 eprintf("malformed tar archive: %s\n", reason); 446 } 447 448 static void 449 xt(int argc, char *argv[], int mode) 450 { 451 char b[BLKSIZ], fname[256 + 1], *p; 452 struct timespec times[2]; 453 struct header *h = (struct header *)b; 454 struct dirtime *dirtime; 455 long size; 456 int i, n; 457 int (*fn)(char *, ssize_t, char[BLKSIZ]) = (mode == 'x') ? unarchive : print; 458 459 while (eread(tarfd, b, BLKSIZ) > 0 && h->name[0]) { 460 chktar(h); 461 sanitize(h), n = 0; 462 463 /* small dance around non-null terminated fields */ 464 if (h->prefix[0]) 465 n = snprintf(fname, sizeof(fname), "%.*s/", 466 (int)sizeof(h->prefix), h->prefix); 467 snprintf(fname + n, sizeof(fname) - n, "%.*s", 468 (int)sizeof(h->name), h->name); 469 470 if ((size = strtol(h->size, &p, 8)) < 0 || *p != '\0') 471 eprintf("strtol %s: invalid number\n", h->size); 472 473 if (argc) { 474 /* only extract the given files */ 475 for (i = 0; i < argc; i++) 476 if (!strcmp(argv[i], fname)) 477 break; 478 if (i == argc) { 479 skipblk(size); 480 continue; 481 } 482 } 483 484 /* ignore global pax header craziness */ 485 if (h->type == 'g' || h->type == 'x') { 486 skipblk(size); 487 continue; 488 } 489 490 fn(fname, size, b); 491 if (vflag && mode != 't') 492 puts(fname); 493 } 494 495 if (mode == 'x' && !mflag) { 496 while ((dirtime = popdirtime())) { 497 times[0].tv_sec = times[1].tv_sec = dirtime->mtime; 498 times[0].tv_nsec = times[1].tv_nsec = 0; 499 if (utimensat(AT_FDCWD, dirtime->name, times, 0) < 0) 500 eprintf("utimensat %s:", fname); 501 free(dirtime->name); 502 } 503 free(dirtimes); 504 dirtimes = NULL; 505 } 506 } 507 508 static void 509 usage(void) 510 { 511 eprintf("usage: %s [-C dir] [-J | -Z | -a | -j | -z] -x [-m | -t] " 512 "[-f file] [file ...]\n" 513 " %s [-C dir] [-J | -Z | -a | -j | -z] [-h] -c path ... " 514 "[-f file]\n", argv0, argv0); 515 } 516 517 int 518 main(int argc, char *argv[]) 519 { 520 struct recursor r = { .fn = c, .follow = 'P', .flags = DIRFIRST }; 521 struct stat st; 522 char *file = NULL, *dir = ".", mode = '\0'; 523 int fd; 524 525 ARGBEGIN { 526 case 'x': 527 case 'c': 528 case 't': 529 mode = ARGC(); 530 break; 531 case 'C': 532 dir = EARGF(usage()); 533 break; 534 case 'f': 535 file = EARGF(usage()); 536 break; 537 case 'm': 538 mflag = 1; 539 break; 540 case 'J': 541 case 'Z': 542 case 'a': 543 case 'j': 544 case 'z': 545 filtermode = ARGC(); 546 filtertool = filtertools[filtermode]; 547 break; 548 case 'h': 549 r.follow = 'L'; 550 break; 551 case 'v': 552 vflag = 1; 553 break; 554 default: 555 usage(); 556 } ARGEND 557 558 if (!mode) 559 usage(); 560 if (mode == 'c') 561 if (!argc) 562 usage(); 563 564 switch (mode) { 565 case 'c': 566 tarfd = 1; 567 if (file && *file != '-') { 568 tarfd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0644); 569 if (tarfd < 0) 570 eprintf("open %s:", file); 571 if (lstat(file, &st) < 0) 572 eprintf("lstat %s:", file); 573 tarinode = st.st_ino; 574 tardev = st.st_dev; 575 } 576 577 if (filtertool) 578 tarfd = comp(tarfd, filtertool, "-cf"); 579 580 if (chdir(dir) < 0) 581 eprintf("chdir %s:", dir); 582 for (; *argv; argc--, argv++) 583 recurse(AT_FDCWD, *argv, NULL, &r); 584 break; 585 case 't': 586 case 'x': 587 tarfd = 0; 588 if (file && *file != '-') { 589 tarfd = open(file, O_RDONLY); 590 if (tarfd < 0) 591 eprintf("open %s:", file); 592 } 593 594 if (filtertool) { 595 fd = tarfd; 596 tarfd = decomp(tarfd, filtertool, "-cd"); 597 close(fd); 598 } 599 600 if (chdir(dir) < 0) 601 eprintf("chdir %s:", dir); 602 xt(argc, argv, mode); 603 break; 604 } 605 606 return recurse_status; 607 }