tar.c (13247B)
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 #ifdef __GLIBC__ 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 = reallocarray(dirtimes, dirtimeslen + 1, sizeof(*dirtimes)); 82 dirtimes[dirtimeslen].name = strdup(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(const char *path, struct stat *st, void *data, struct recursor *r) 372 { 373 archive(path); 374 if (vflag) 375 puts(path); 376 377 if (S_ISDIR(st->st_mode)) 378 recurse(path, 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; j++) 404 if (fields[i].f[j] == ' ') 405 fields[i].f[j] = '\0'; 406 } 407 408 static void 409 chktar(struct header *h) 410 { 411 char tmp[8], *err, *p = (char *)h; 412 const char *reason; 413 long s1, s2, i; 414 415 if (h->prefix[0] == '\0' && h->name[0] == '\0') { 416 reason = "empty filename"; 417 goto bad; 418 } 419 if (h->magic[0] && strncmp("ustar", h->magic, 5)) { 420 reason = "not ustar format"; 421 goto bad; 422 } 423 memcpy(tmp, h->chksum, sizeof(tmp)); 424 for (i = 0; i < sizeof(tmp); i++) 425 if (tmp[i] == ' ') 426 tmp[i] = '\0'; 427 s1 = strtol(tmp, &err, 8); 428 if (s1 < 0 || *err != '\0') { 429 reason = "invalid checksum"; 430 goto bad; 431 } 432 memset(h->chksum, ' ', sizeof(h->chksum)); 433 for (i = 0, s2 = 0; i < sizeof(*h); i++) 434 s2 += (unsigned char)p[i]; 435 if (s1 != s2) { 436 reason = "incorrect checksum"; 437 goto bad; 438 } 439 memcpy(h->chksum, tmp, sizeof(h->chksum)); 440 return; 441 bad: 442 eprintf("malformed tar archive: %s\n", reason); 443 } 444 445 static void 446 xt(int argc, char *argv[], int mode) 447 { 448 char b[BLKSIZ], fname[256 + 1], *p; 449 struct timespec times[2]; 450 struct header *h = (struct header *)b; 451 struct dirtime *dirtime; 452 long size; 453 int i, n; 454 int (*fn)(char *, ssize_t, char[BLKSIZ]) = (mode == 'x') ? unarchive : print; 455 456 while (eread(tarfd, b, BLKSIZ) > 0 && h->name[0]) { 457 chktar(h); 458 sanitize(h), n = 0; 459 460 /* small dance around non-null terminated fields */ 461 if (h->prefix[0]) 462 n = snprintf(fname, sizeof(fname), "%.*s/", 463 (int)sizeof(h->prefix), h->prefix); 464 snprintf(fname + n, sizeof(fname) - n, "%.*s", 465 (int)sizeof(h->name), h->name); 466 467 if ((size = strtol(h->size, &p, 8)) < 0 || *p != '\0') 468 eprintf("strtol %s: invalid number\n", h->size); 469 470 if (argc) { 471 /* only extract the given files */ 472 for (i = 0; i < argc; i++) 473 if (!strcmp(argv[i], fname)) 474 break; 475 if (i == argc) { 476 skipblk(size); 477 continue; 478 } 479 } 480 481 /* ignore global pax header craziness */ 482 if (h->type == 'g' || h->type == 'x') { 483 skipblk(size); 484 continue; 485 } 486 487 fn(fname, size, b); 488 if (vflag && mode != 't') 489 puts(fname); 490 } 491 492 if (mode == 'x' && !mflag) { 493 while ((dirtime = popdirtime())) { 494 times[0].tv_sec = times[1].tv_sec = dirtime->mtime; 495 times[0].tv_nsec = times[1].tv_nsec = 0; 496 if (utimensat(AT_FDCWD, dirtime->name, times, 0) < 0) 497 eprintf("utimensat %s:", fname); 498 free(dirtime->name); 499 } 500 free(dirtimes); 501 dirtimes = NULL; 502 } 503 } 504 505 static void 506 usage(void) 507 { 508 eprintf("usage: %s [-C dir] [-J | -Z | -a | -j | -z] -x [-m | -t] " 509 "[-f file] [file ...]\n" 510 " %s [-C dir] [-J | -Z | -a | -j | -z] [-h] -c path ... " 511 "[-f file]\n", argv0, argv0); 512 } 513 514 int 515 main(int argc, char *argv[]) 516 { 517 struct recursor r = { .fn = c, .hist = NULL, .depth = 0, .maxdepth = 0, 518 .follow = 'P', .flags = DIRFIRST }; 519 struct stat st; 520 char *file = NULL, *dir = ".", mode = '\0'; 521 int fd; 522 523 ARGBEGIN { 524 case 'x': 525 case 'c': 526 case 't': 527 mode = ARGC(); 528 break; 529 case 'C': 530 dir = EARGF(usage()); 531 break; 532 case 'f': 533 file = EARGF(usage()); 534 break; 535 case 'm': 536 mflag = 1; 537 break; 538 case 'J': 539 case 'Z': 540 case 'a': 541 case 'j': 542 case 'z': 543 filtermode = ARGC(); 544 filtertool = filtertools[filtermode]; 545 break; 546 case 'h': 547 r.follow = 'L'; 548 break; 549 case 'v': 550 vflag = 1; 551 break; 552 default: 553 usage(); 554 } ARGEND 555 556 if (!mode) 557 usage(); 558 if (mode == 'c') 559 if (!argc) 560 usage(); 561 562 switch (mode) { 563 case 'c': 564 tarfd = 1; 565 if (file && *file != '-') { 566 tarfd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0644); 567 if (tarfd < 0) 568 eprintf("open %s:", file); 569 if (lstat(file, &st) < 0) 570 eprintf("lstat %s:", file); 571 tarinode = st.st_ino; 572 tardev = st.st_dev; 573 } 574 575 if (filtertool) 576 tarfd = comp(tarfd, filtertool, "-cf"); 577 578 if (chdir(dir) < 0) 579 eprintf("chdir %s:", dir); 580 for (; *argv; argc--, argv++) 581 recurse(*argv, NULL, &r); 582 break; 583 case 't': 584 case 'x': 585 tarfd = 0; 586 if (file && *file != '-') { 587 tarfd = open(file, O_RDONLY); 588 if (tarfd < 0) 589 eprintf("open %s:", file); 590 } 591 592 if (filtertool) { 593 fd = tarfd; 594 tarfd = decomp(tarfd, filtertool, "-cd"); 595 close(fd); 596 } 597 598 if (chdir(dir) < 0) 599 eprintf("chdir %s:", dir); 600 xt(argc, argv, mode); 601 break; 602 } 603 604 return recurse_status; 605 }