1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2003-2008 Tim Kientzle 5 * All rights reserved. 6 */ 7 8 #include "bsdtar_platform.h" 9 10 #ifdef HAVE_SYS_PARAM_H 11 #include <sys/param.h> 12 #endif 13 #ifdef HAVE_SYS_STAT_H 14 #include <sys/stat.h> 15 #endif 16 #ifdef HAVE_COPYFILE_H 17 #include <copyfile.h> 18 #endif 19 #ifdef HAVE_ERRNO_H 20 #include <errno.h> 21 #endif 22 #ifdef HAVE_FCNTL_H 23 #include <fcntl.h> 24 #endif 25 #ifdef HAVE_LANGINFO_H 26 #include <langinfo.h> 27 #endif 28 #ifdef HAVE_LIMITS_H 29 #include <limits.h> 30 #endif 31 #ifdef HAVE_LOCALE_H 32 #include <locale.h> 33 #endif 34 #ifdef HAVE_PATHS_H 35 #include <paths.h> 36 #endif 37 #ifdef HAVE_SIGNAL_H 38 #include <signal.h> 39 #endif 40 #include <stdio.h> 41 #ifdef HAVE_STDLIB_H 42 #include <stdlib.h> 43 #endif 44 #ifdef HAVE_STRING_H 45 #include <string.h> 46 #endif 47 #ifdef HAVE_TIME_H 48 #include <time.h> 49 #endif 50 #ifdef HAVE_UNISTD_H 51 #include <unistd.h> 52 #endif 53 54 #include "bsdtar.h" 55 #include "err.h" 56 57 #if ARCHIVE_VERSION_NUMBER < 4000000 && !defined(_PATH_DEFTAPE) 58 // Libarchive 4.0 and later will NOT define _PATH_DEFTAPE 59 // but will honor it if it's set in the build. 60 // Until then, we'll continue to set it by default on certain platforms: 61 #if defined(__linux) 62 #define _PATH_DEFTAPE "/dev/st0" 63 #elif defined(_WIN32) && !defined(__CYGWIN__) 64 #define _PATH_DEFTAPE "\\\\.\\tape0" 65 #elif !defined(__APPLE__) 66 #define _PATH_DEFTAPE "/dev/tape" 67 #endif 68 #endif 69 70 #define _PATH_STDIO "-" 71 72 #ifdef __MINGW32__ 73 int _CRT_glob = 0; /* Disable broken CRT globbing. */ 74 #endif 75 76 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1)) 77 static volatile int siginfo_occurred; 78 79 static void 80 siginfo_handler(int sig) 81 { 82 (void)sig; /* UNUSED */ 83 siginfo_occurred = 1; 84 } 85 86 int 87 need_report(void) 88 { 89 int r = siginfo_occurred; 90 siginfo_occurred = 0; 91 return (r); 92 } 93 #else 94 int 95 need_report(void) 96 { 97 return (0); 98 } 99 #endif 100 101 static __LA_NORETURN void long_help(void); 102 static void only_mode(struct bsdtar *, const char *opt, 103 const char *valid); 104 static void set_mode(struct bsdtar *, char opt); 105 static __LA_NORETURN void version(void); 106 107 /* A basic set of security flags to request from libarchive. */ 108 #define SECURITY \ 109 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \ 110 | ARCHIVE_EXTRACT_SECURE_NODOTDOT) 111 112 static char const * const vcs_files[] = { 113 /* CVS */ 114 "CVS", ".cvsignore", 115 /* RCS */ 116 "RCS", 117 /* SCCS */ 118 "SCCS", 119 /* SVN */ 120 ".svn", 121 /* git */ 122 ".git", ".gitignore", ".gitattributes", ".gitmodules", 123 /* Arch */ 124 ".arch-ids", "{arch}", "=RELEASE-ID", "=meta-update", "=update", 125 /* Bazaar */ 126 ".bzr", ".bzrignore", ".bzrtags", 127 /* Mercurial */ 128 ".hg", ".hgignore", ".hgtags", 129 /* darcs */ 130 "_darcs", 131 NULL 132 }; 133 134 int 135 main(int argc, char **argv) 136 { 137 struct bsdtar *bsdtar, bsdtar_storage; 138 int opt, t; 139 char compression, compression2; 140 const char *compression_name, *compression2_name; 141 const char *compress_program; 142 char *tptr, *uptr; 143 char possible_help_request; 144 char buff[16]; 145 long l; 146 147 /* 148 * Use a pointer for consistency, but stack-allocated storage 149 * for ease of cleanup. 150 */ 151 bsdtar = &bsdtar_storage; 152 memset(bsdtar, 0, sizeof(*bsdtar)); 153 bsdtar->fd = -1; /* Mark as "unused" */ 154 bsdtar->gid = -1; 155 bsdtar->uid = -1; 156 bsdtar->flags = 0; 157 compression = compression2 = '\0'; 158 compression_name = compression2_name = NULL; 159 compress_program = NULL; 160 161 #if defined(HAVE_SIGACTION) 162 { /* Set up signal handling. */ 163 struct sigaction sa; 164 sa.sa_handler = siginfo_handler; 165 sigemptyset(&sa.sa_mask); 166 sa.sa_flags = 0; 167 #ifdef SIGINFO 168 if (sigaction(SIGINFO, &sa, NULL)) 169 lafe_errc(1, errno, "sigaction(SIGINFO) failed"); 170 #endif 171 #ifdef SIGUSR1 172 /* ... and treat SIGUSR1 the same way as SIGINFO. */ 173 if (sigaction(SIGUSR1, &sa, NULL)) 174 lafe_errc(1, errno, "sigaction(SIGUSR1) failed"); 175 #endif 176 #ifdef SIGPIPE 177 /* Ignore SIGPIPE signals. */ 178 sa.sa_handler = SIG_IGN; 179 sigaction(SIGPIPE, &sa, NULL); 180 #endif 181 } 182 #endif 183 184 /* Set lafe_progname before calling lafe_warnc. */ 185 lafe_setprogname(*argv, "bsdtar"); 186 187 #if HAVE_SETLOCALE 188 if (setlocale(LC_ALL, "") == NULL) 189 lafe_warnc(0, "Failed to set default locale"); 190 #endif 191 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER) 192 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 193 #endif 194 possible_help_request = 0; 195 196 /* Look up uid of current user for future reference */ 197 bsdtar->user_uid = geteuid(); 198 199 /* Default: open tape drive. */ 200 bsdtar->filename = getenv("TAPE"); 201 #if defined(_PATH_DEFTAPE) 202 if (bsdtar->filename == NULL) { 203 #if defined(_WIN32) && !defined(__CYGWIN__) 204 int tapeExists = !_access(_PATH_DEFTAPE, 0); 205 #else 206 int tapeExists = !access(_PATH_DEFTAPE, F_OK); 207 #endif 208 if (tapeExists) { 209 bsdtar->filename = _PATH_DEFTAPE; 210 } 211 } 212 #endif 213 if (bsdtar->filename == NULL) { 214 bsdtar->filename = _PATH_STDIO; 215 } 216 217 /* Default block size settings. */ 218 bsdtar->bytes_per_block = DEFAULT_BYTES_PER_BLOCK; 219 /* Allow library to default this unless user specifies -b. */ 220 bsdtar->bytes_in_last_block = -1; 221 222 /* Default: preserve mod time on extract */ 223 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME; 224 225 /* Default: Perform basic security checks. */ 226 bsdtar->extract_flags |= SECURITY; 227 228 /* Default: Extract atomically if possible */ 229 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SAFE_WRITES; 230 231 #ifndef _WIN32 232 /* On POSIX systems, assume --same-owner and -p when run by 233 * the root user. This doesn't make any sense on Windows. */ 234 if (bsdtar->user_uid == 0) { 235 /* --same-owner */ 236 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER; 237 /* -p */ 238 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM; 239 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL; 240 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR; 241 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; 242 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA; 243 } 244 #endif 245 246 /* 247 * Enable Mac OS "copyfile()" extension by default. 248 * This has no effect on other platforms. 249 */ 250 bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE; 251 #ifdef COPYFILE_DISABLE_VAR 252 if (getenv(COPYFILE_DISABLE_VAR)) 253 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE; 254 #endif 255 #if defined(__APPLE__) 256 /* 257 * On Mac OS ACLs are archived with copyfile() (--mac-metadata) 258 * Translation to NFSv4 ACLs has to be requested explicitly with --acls 259 */ 260 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_ACL; 261 #endif 262 263 bsdtar->matching = archive_match_new(); 264 if (bsdtar->matching == NULL) 265 lafe_errc(1, errno, "Out of memory"); 266 bsdtar->cset = cset_new(); 267 if (bsdtar->cset == NULL) 268 lafe_errc(1, errno, "Out of memory"); 269 270 bsdtar->argv = argv; 271 bsdtar->argc = argc; 272 273 /* 274 * Comments following each option indicate where that option 275 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's 276 * no such comment, then I don't know of anyone else who 277 * implements that option. 278 */ 279 while ((opt = bsdtar_getopt(bsdtar)) != -1) { 280 switch (opt) { 281 case 'a': /* GNU tar */ 282 bsdtar->flags |= OPTFLAG_AUTO_COMPRESS; 283 break; 284 case OPTION_ACLS: /* GNU tar */ 285 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL; 286 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_ACL; 287 bsdtar->flags |= OPTFLAG_ACLS; 288 break; 289 case 'B': /* GNU tar */ 290 /* libarchive doesn't need this; just ignore it. */ 291 break; 292 case 'b': /* SUSv2 */ 293 tptr = NULL; 294 l = strtol(bsdtar->argument, &tptr, 10); 295 if (l <= 0 || l > 8192L || 296 *(bsdtar->argument) == '\0' || tptr == NULL || 297 *tptr != '\0') { 298 lafe_errc(1, 0, "Invalid or out of range " 299 "(1..8192) argument to -b"); 300 } 301 bsdtar->bytes_per_block = 512 * (int)l; 302 /* Explicit -b forces last block size. */ 303 bsdtar->bytes_in_last_block = bsdtar->bytes_per_block; 304 break; 305 case OPTION_B64ENCODE: 306 if (compression2 != '\0') 307 lafe_errc(1, 0, 308 "Can't specify both --uuencode and " 309 "--b64encode"); 310 compression2 = opt; 311 compression2_name = "b64encode"; 312 break; 313 case 'C': /* GNU tar */ 314 if (strlen(bsdtar->argument) == 0) 315 lafe_errc(1, 0, 316 "Meaningless option: -C ''"); 317 318 set_chdir(bsdtar, bsdtar->argument); 319 break; 320 case 'c': /* SUSv2 */ 321 set_mode(bsdtar, opt); 322 break; 323 case OPTION_CHECK_LINKS: /* GNU tar */ 324 bsdtar->flags |= OPTFLAG_WARN_LINKS; 325 break; 326 case OPTION_CHROOT: /* NetBSD */ 327 bsdtar->flags |= OPTFLAG_CHROOT; 328 break; 329 case OPTION_CLEAR_NOCHANGE_FFLAGS: 330 bsdtar->extract_flags |= 331 ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS; 332 break; 333 case OPTION_EXCLUDE: /* GNU tar */ 334 if (archive_match_exclude_pattern( 335 bsdtar->matching, bsdtar->argument) != ARCHIVE_OK) 336 lafe_errc(1, 0, 337 "Couldn't exclude %s\n", bsdtar->argument); 338 break; 339 case OPTION_EXCLUDE_VCS: /* GNU tar */ 340 for(t=0; vcs_files[t]; t++) { 341 if (archive_match_exclude_pattern( 342 bsdtar->matching, 343 vcs_files[t]) != ARCHIVE_OK) 344 lafe_errc(1, 0, "Couldn't " 345 "exclude %s\n", vcs_files[t]); 346 } 347 break; 348 case OPTION_FFLAGS: 349 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; 350 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_FFLAGS; 351 bsdtar->flags |= OPTFLAG_FFLAGS; 352 break; 353 case OPTION_FORMAT: /* GNU tar, others */ 354 cset_set_format(bsdtar->cset, bsdtar->argument); 355 break; 356 case 'f': /* SUSv2 */ 357 bsdtar->filename = bsdtar->argument; 358 break; 359 case OPTION_GID: /* cpio */ 360 tptr = NULL; 361 l = strtol(bsdtar->argument, &tptr, 10); 362 if (l < 0 || l >= INT_MAX || *(bsdtar->argument) == '\0' || 363 tptr == NULL || *tptr != '\0') { 364 lafe_errc(1, 0, "Invalid argument to --gid"); 365 } 366 bsdtar->gid = (int)l; 367 break; 368 case OPTION_GNAME: /* cpio */ 369 bsdtar->gname = bsdtar->argument; 370 break; 371 case OPTION_GROUP: /* GNU tar */ 372 tptr = NULL; 373 374 uptr = strchr(bsdtar->argument, ':'); 375 if (uptr != NULL) { 376 if (uptr[1] == '\0') { 377 lafe_errc(1, 0, "Invalid argument to --group (missing id after :)"); 378 } 379 uptr[0] = 0; 380 uptr++; 381 l = strtol(uptr, &tptr, 10); 382 if (l < 0 || l >= INT_MAX || *uptr == '\0' || 383 tptr == NULL || *tptr != '\0') { 384 lafe_errc(1, 0, "Invalid argument to --group (%s is not a number)", uptr); 385 } else { 386 bsdtar->gid = (int)l; 387 } 388 bsdtar->gname = bsdtar->argument; 389 } else { 390 l = strtol(bsdtar->argument, &tptr, 10); 391 if (l < 0 || l >= INT_MAX || *(bsdtar->argument) == '\0' || 392 tptr == NULL || *tptr != '\0') { 393 bsdtar->gname = bsdtar->argument; 394 } else { 395 bsdtar->gid = (int)l; 396 bsdtar->gname = ""; 397 } 398 } 399 break; 400 case OPTION_GRZIP: 401 if (compression != '\0') 402 lafe_errc(1, 0, 403 "Can't specify both -%c and -%c", opt, 404 compression); 405 compression = opt; 406 compression_name = "grzip"; 407 break; 408 case 'H': /* BSD convention */ 409 bsdtar->symlink_mode = 'H'; 410 break; 411 case 'h': /* Linux Standards Base, gtar; synonym for -L */ 412 bsdtar->symlink_mode = 'L'; 413 /* Hack: -h by itself is the "help" command. */ 414 possible_help_request = 1; 415 break; 416 case OPTION_HELP: /* GNU tar, others */ 417 long_help(); 418 /* NOTREACHED*/ 419 case OPTION_HFS_COMPRESSION: /* Mac OS X v10.6 or later */ 420 bsdtar->extract_flags |= 421 ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED; 422 break; 423 case OPTION_IGNORE_ZEROS: 424 bsdtar->flags |= OPTFLAG_IGNORE_ZEROS; 425 break; 426 case 'I': /* GNU tar */ 427 /* 428 * TODO: Allow 'names' to come from an archive, 429 * not just a text file. Design a good UI for 430 * allowing names and mode/owner to be read 431 * from an archive, with contents coming from 432 * disk. This can be used to "refresh" an 433 * archive or to design archives with special 434 * permissions without having to create those 435 * permissions on disk. 436 */ 437 bsdtar->names_from_file = bsdtar->argument; 438 break; 439 case OPTION_INCLUDE: 440 /* 441 * No one else has the @archive extension, so 442 * no one else needs this to filter entries 443 * when transforming archives. 444 */ 445 if (archive_match_include_pattern(bsdtar->matching, 446 bsdtar->argument) != ARCHIVE_OK) 447 lafe_errc(1, 0, 448 "Failed to add %s to inclusion list", 449 bsdtar->argument); 450 break; 451 case 'j': /* GNU tar */ 452 if (compression != '\0') 453 lafe_errc(1, 0, 454 "Can't specify both -%c and -%c", opt, 455 compression); 456 compression = opt; 457 compression_name = "bzip2"; 458 break; 459 case 'J': /* GNU tar 1.21 and later */ 460 if (compression != '\0') 461 lafe_errc(1, 0, 462 "Can't specify both -%c and -%c", opt, 463 compression); 464 compression = opt; 465 compression_name = "xz"; 466 break; 467 case 'k': /* GNU tar */ 468 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE; 469 break; 470 case OPTION_KEEP_NEWER_FILES: /* GNU tar */ 471 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; 472 break; 473 case 'L': /* BSD convention */ 474 bsdtar->symlink_mode = 'L'; 475 break; 476 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */ 477 /* GNU tar 1.13 used -l for --one-file-system */ 478 bsdtar->flags |= OPTFLAG_WARN_LINKS; 479 break; 480 case OPTION_LRZIP: 481 case OPTION_LZ4: 482 case OPTION_LZIP: /* GNU tar beginning with 1.23 */ 483 case OPTION_LZMA: /* GNU tar beginning with 1.20 */ 484 case OPTION_LZOP: /* GNU tar beginning with 1.21 */ 485 case OPTION_ZSTD: 486 if (compression != '\0') 487 lafe_errc(1, 0, 488 "Can't specify both -%c and -%c", opt, 489 compression); 490 compression = opt; 491 switch (opt) { 492 case OPTION_LRZIP: compression_name = "lrzip"; break; 493 case OPTION_LZ4: compression_name = "lz4"; break; 494 case OPTION_LZIP: compression_name = "lzip"; break; 495 case OPTION_LZMA: compression_name = "lzma"; break; 496 case OPTION_LZOP: compression_name = "lzop"; break; 497 case OPTION_ZSTD: compression_name = "zstd"; break; 498 } 499 break; 500 case 'm': /* SUSv2 */ 501 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME; 502 break; 503 case OPTION_MAC_METADATA: /* Mac OS X */ 504 bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE; 505 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA; 506 bsdtar->flags |= OPTFLAG_MAC_METADATA; 507 break; 508 case 'n': /* GNU tar */ 509 bsdtar->flags |= OPTFLAG_NO_SUBDIRS; 510 break; 511 /* 512 * Selecting files by time: 513 * --newer-?time='date' Only files newer than 'date' 514 * --newer-?time-than='file' Only files newer than time 515 * on specified file (useful for incremental backups) 516 */ 517 case OPTION_NEWER_CTIME: /* GNU tar */ 518 if (archive_match_include_date(bsdtar->matching, 519 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER, 520 bsdtar->argument) != ARCHIVE_OK) 521 lafe_errc(1, 0, "Error : %s", 522 archive_error_string(bsdtar->matching)); 523 break; 524 case OPTION_NEWER_CTIME_THAN: 525 if (archive_match_include_file_time(bsdtar->matching, 526 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER, 527 bsdtar->argument) != ARCHIVE_OK) 528 lafe_errc(1, 0, "Error : %s", 529 archive_error_string(bsdtar->matching)); 530 break; 531 case OPTION_NEWER_MTIME: /* GNU tar */ 532 if (archive_match_include_date(bsdtar->matching, 533 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER, 534 bsdtar->argument) != ARCHIVE_OK) 535 lafe_errc(1, 0, "Error : %s", 536 archive_error_string(bsdtar->matching)); 537 break; 538 case OPTION_NEWER_MTIME_THAN: 539 if (archive_match_include_file_time(bsdtar->matching, 540 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER, 541 bsdtar->argument) != ARCHIVE_OK) 542 lafe_errc(1, 0, "Error : %s", 543 archive_error_string(bsdtar->matching)); 544 break; 545 case OPTION_NODUMP: /* star */ 546 bsdtar->readdisk_flags |= ARCHIVE_READDISK_HONOR_NODUMP; 547 break; 548 case OPTION_NOPRESERVE_HFS_COMPRESSION: 549 /* Mac OS X v10.6 or later */ 550 bsdtar->extract_flags |= 551 ARCHIVE_EXTRACT_NO_HFS_COMPRESSION; 552 break; 553 case OPTION_NO_ACLS: /* GNU tar */ 554 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL; 555 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_ACL; 556 bsdtar->flags |= OPTFLAG_NO_ACLS; 557 break; 558 case OPTION_NO_FFLAGS: 559 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS; 560 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_FFLAGS; 561 bsdtar->flags |= OPTFLAG_NO_FFLAGS; 562 break; 563 case OPTION_NO_MAC_METADATA: /* Mac OS X */ 564 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE; 565 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA; 566 bsdtar->flags |= OPTFLAG_NO_MAC_METADATA; 567 break; 568 case OPTION_NO_READ_SPARSE: 569 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_SPARSE; 570 bsdtar->flags |= OPTFLAG_NO_READ_SPARSE; 571 break; 572 case OPTION_NO_SAFE_WRITES: 573 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_SAFE_WRITES; 574 break; 575 case OPTION_NO_SAME_OWNER: /* GNU tar */ 576 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; 577 break; 578 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */ 579 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM; 580 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL; 581 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR; 582 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS; 583 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA; 584 break; 585 case OPTION_NO_XATTRS: /* GNU tar */ 586 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR; 587 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_XATTR; 588 bsdtar->flags |= OPTFLAG_NO_XATTRS; 589 break; 590 case OPTION_NULL: /* GNU tar */ 591 bsdtar->flags |= OPTFLAG_NULL; 592 break; 593 case OPTION_NUMERIC_OWNER: /* GNU tar */ 594 bsdtar->uname = ""; 595 bsdtar->gname = ""; 596 bsdtar->flags |= OPTFLAG_NUMERIC_OWNER; 597 break; 598 case 'O': /* GNU tar */ 599 bsdtar->flags |= OPTFLAG_STDOUT; 600 break; 601 case 'o': /* SUSv2 and GNU conflict here, but not fatally */ 602 bsdtar->flags |= OPTFLAG_O; 603 break; 604 /* 605 * Selecting files by time: 606 * --older-?time='date' Only files older than 'date' 607 * --older-?time-than='file' Only files older than time 608 * on specified file 609 */ 610 case OPTION_OLDER_CTIME: 611 if (archive_match_include_date(bsdtar->matching, 612 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER, 613 bsdtar->argument) != ARCHIVE_OK) 614 lafe_errc(1, 0, "Error : %s", 615 archive_error_string(bsdtar->matching)); 616 break; 617 case OPTION_OLDER_CTIME_THAN: 618 if (archive_match_include_file_time(bsdtar->matching, 619 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER, 620 bsdtar->argument) != ARCHIVE_OK) 621 lafe_errc(1, 0, "Error : %s", 622 archive_error_string(bsdtar->matching)); 623 break; 624 case OPTION_OLDER_MTIME: 625 if (archive_match_include_date(bsdtar->matching, 626 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER, 627 bsdtar->argument) != ARCHIVE_OK) 628 lafe_errc(1, 0, "Error : %s", 629 archive_error_string(bsdtar->matching)); 630 break; 631 case OPTION_OLDER_MTIME_THAN: 632 if (archive_match_include_file_time(bsdtar->matching, 633 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER, 634 bsdtar->argument) != ARCHIVE_OK) 635 lafe_errc(1, 0, "Error : %s", 636 archive_error_string(bsdtar->matching)); 637 break; 638 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */ 639 bsdtar->readdisk_flags |= 640 ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS; 641 break; 642 case OPTION_OPTIONS: 643 if (bsdtar->option_options != NULL) { 644 lafe_warnc(0, 645 "Ignoring previous option '%s', separate multiple options with commas", 646 bsdtar->option_options); 647 } 648 bsdtar->option_options = bsdtar->argument; 649 break; 650 case OPTION_OWNER: /* GNU tar */ 651 tptr = NULL; 652 653 uptr = strchr(bsdtar->argument, ':'); 654 if (uptr != NULL) { 655 if (uptr[1] == 0) { 656 lafe_errc(1, 0, "Invalid argument to --owner (missing id after :)"); 657 } 658 uptr[0] = 0; 659 uptr++; 660 l = strtol(uptr, &tptr, 10); 661 if (l < 0 || l >= INT_MAX || *uptr == '\0' || 662 tptr == NULL || *tptr != '\0') { 663 lafe_errc(1, 0, "Invalid argument to --owner (%s is not a number)", uptr); 664 } else { 665 bsdtar->uid = (int)l; 666 } 667 bsdtar->uname = bsdtar->argument; 668 } else { 669 l = strtol(bsdtar->argument, &tptr, 10); 670 if (l < 0 || l >= INT_MAX || *(bsdtar->argument) == '\0' || 671 tptr == NULL || *tptr != '\0') { 672 bsdtar->uname = bsdtar->argument; 673 } else { 674 bsdtar->uid = (int)l; 675 bsdtar->uname = ""; 676 } 677 } 678 break; 679 #if 0 680 /* 681 * The common BSD -P option is not necessary, since 682 * our default is to archive symlinks, not follow 683 * them. This is convenient, as -P conflicts with GNU 684 * tar anyway. 685 */ 686 case 'P': /* BSD convention */ 687 /* Default behavior, no option necessary. */ 688 break; 689 #endif 690 case 'P': /* GNU tar */ 691 bsdtar->extract_flags &= ~SECURITY; 692 bsdtar->flags |= OPTFLAG_ABSOLUTE_PATHS; 693 break; 694 case 'p': /* GNU tar, star */ 695 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM; 696 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL; 697 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR; 698 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; 699 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA; 700 break; 701 case OPTION_PASSPHRASE: 702 bsdtar->passphrase = bsdtar->argument; 703 break; 704 case OPTION_POSIX: /* GNU tar */ 705 cset_set_format(bsdtar->cset, "pax"); 706 break; 707 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */ 708 bsdtar->flags |= OPTFLAG_FAST_READ; 709 break; 710 case 'r': /* SUSv2 */ 711 set_mode(bsdtar, opt); 712 break; 713 case OPTION_READ_SPARSE: 714 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_SPARSE; 715 bsdtar->flags |= OPTFLAG_READ_SPARSE; 716 break; 717 case 'S': /* NetBSD pax-as-tar */ 718 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE; 719 break; 720 case 's': /* NetBSD pax-as-tar */ 721 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H) || defined(HAVE_PCRE2POSIX_H) 722 add_substitution(bsdtar, bsdtar->argument); 723 #else 724 lafe_warnc(0, 725 "-s is not supported by this version of bsdtar"); 726 usage(); 727 #endif 728 break; 729 case OPTION_SAFE_WRITES: 730 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SAFE_WRITES; 731 break; 732 case OPTION_SAME_OWNER: /* GNU tar */ 733 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER; 734 break; 735 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */ 736 tptr = NULL; 737 l = strtol(bsdtar->argument, &tptr, 10); 738 if (l < 0 || l > 100000L || *(bsdtar->argument) == '\0' || 739 tptr == NULL || *tptr != '\0') { 740 lafe_errc(1, 0, "Invalid argument to " 741 "--strip-components"); 742 } 743 bsdtar->strip_components = (int)l; 744 break; 745 case 'T': /* GNU tar */ 746 bsdtar->names_from_file = bsdtar->argument; 747 break; 748 case 't': /* SUSv2 */ 749 set_mode(bsdtar, opt); 750 bsdtar->verbose++; 751 break; 752 case OPTION_TOTALS: /* GNU tar */ 753 bsdtar->flags |= OPTFLAG_TOTALS; 754 break; 755 case 'U': /* GNU tar */ 756 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK; 757 bsdtar->flags |= OPTFLAG_UNLINK_FIRST; 758 break; 759 case 'u': /* SUSv2 */ 760 set_mode(bsdtar, opt); 761 break; 762 case OPTION_UID: /* cpio */ 763 tptr = NULL; 764 l = strtol(bsdtar->argument, &tptr, 10); 765 if (l < 0 || l >= INT_MAX || *(bsdtar->argument) == '\0' || 766 tptr == NULL || *tptr != '\0') { 767 lafe_errc(1, 0, "Invalid argument to --uid"); 768 } 769 bsdtar->uid = (int)l; 770 break; 771 case OPTION_UNAME: /* cpio */ 772 bsdtar->uname = bsdtar->argument; 773 break; 774 case OPTION_UUENCODE: 775 if (compression2 != '\0') 776 lafe_errc(1, 0, 777 "Can't specify both --uuencode and " 778 "--b64encode"); 779 compression2 = opt; 780 compression2_name = "uuencode"; 781 break; 782 case 'v': /* SUSv2 */ 783 bsdtar->verbose++; 784 break; 785 case OPTION_VERSION: /* GNU convention */ 786 version(); 787 /* NOTREACHED */ 788 #if 0 789 /* 790 * The -W longopt feature is handled inside of 791 * bsdtar_getopt(), so -W is not available here. 792 */ 793 case 'W': /* Obscure GNU convention. */ 794 break; 795 #endif 796 case 'w': /* SUSv2 */ 797 bsdtar->flags |= OPTFLAG_INTERACTIVE; 798 break; 799 case 'X': /* GNU tar */ 800 if (archive_match_exclude_pattern_from_file( 801 bsdtar->matching, bsdtar->argument, 0) 802 != ARCHIVE_OK) 803 lafe_errc(1, 0, "Error : %s", 804 archive_error_string(bsdtar->matching)); 805 break; 806 case 'x': /* SUSv2 */ 807 set_mode(bsdtar, opt); 808 break; 809 case OPTION_XATTRS: /* GNU tar */ 810 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR; 811 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_XATTR; 812 bsdtar->flags |= OPTFLAG_XATTRS; 813 break; 814 case 'y': /* FreeBSD version of GNU tar */ 815 if (compression != '\0') 816 lafe_errc(1, 0, 817 "Can't specify both -%c and -%c", opt, 818 compression); 819 compression = opt; 820 compression_name = "bzip2"; 821 break; 822 case 'Z': /* GNU tar */ 823 if (compression != '\0') 824 lafe_errc(1, 0, 825 "Can't specify both -%c and -%c", opt, 826 compression); 827 compression = opt; 828 compression_name = "compress"; 829 break; 830 case 'z': /* GNU tar, star, many others */ 831 if (compression != '\0') 832 lafe_errc(1, 0, 833 "Can't specify both -%c and -%c", opt, 834 compression); 835 compression = opt; 836 compression_name = "gzip"; 837 break; 838 case OPTION_USE_COMPRESS_PROGRAM: 839 compress_program = bsdtar->argument; 840 break; 841 default: 842 usage(); 843 } 844 } 845 846 /* 847 * Sanity-check options. 848 */ 849 850 /* If no "real" mode was specified, treat -h as --help. */ 851 if ((bsdtar->mode == '\0') && possible_help_request) { 852 long_help(); 853 } 854 855 /* Otherwise, a mode is required. */ 856 if (bsdtar->mode == '\0') 857 lafe_errc(1, 0, 858 "Must specify one of -c, -r, -t, -u, -x"); 859 860 /* Check boolean options only permitted in certain modes. */ 861 if (bsdtar->flags & OPTFLAG_AUTO_COMPRESS) { 862 only_mode(bsdtar, "-a", "cx"); 863 if (bsdtar->mode == 'x') { 864 bsdtar->flags &= ~OPTFLAG_AUTO_COMPRESS; 865 lafe_warnc(0, 866 "Ignoring option -a in mode -x"); 867 } 868 } 869 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) 870 only_mode(bsdtar, "--one-file-system", "cru"); 871 if (bsdtar->flags & OPTFLAG_FAST_READ) 872 only_mode(bsdtar, "--fast-read", "xt"); 873 if (bsdtar->extract_flags & ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED) 874 only_mode(bsdtar, "--hfsCompression", "x"); 875 if (bsdtar->extract_flags & ARCHIVE_EXTRACT_NO_HFS_COMPRESSION) 876 only_mode(bsdtar, "--nopreserveHFSCompression", "x"); 877 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_HONOR_NODUMP) 878 only_mode(bsdtar, "--nodump", "cru"); 879 if (bsdtar->flags & OPTFLAG_ACLS) 880 only_mode(bsdtar, "--acls", "crux"); 881 if (bsdtar->flags & OPTFLAG_NO_ACLS) 882 only_mode(bsdtar, "--no-acls", "crux"); 883 if (bsdtar->flags & OPTFLAG_XATTRS) 884 only_mode(bsdtar, "--xattrs", "crux"); 885 if (bsdtar->flags & OPTFLAG_NO_XATTRS) 886 only_mode(bsdtar, "--no-xattrs", "crux"); 887 if (bsdtar->flags & OPTFLAG_FFLAGS) 888 only_mode(bsdtar, "--fflags", "crux"); 889 if (bsdtar->flags & OPTFLAG_NO_FFLAGS) 890 only_mode(bsdtar, "--no-fflags", "crux"); 891 if (bsdtar->flags & OPTFLAG_MAC_METADATA) 892 only_mode(bsdtar, "--mac-metadata", "crux"); 893 if (bsdtar->flags & OPTFLAG_NO_MAC_METADATA) 894 only_mode(bsdtar, "--no-mac-metadata", "crux"); 895 if (bsdtar->flags & OPTFLAG_O) { 896 switch (bsdtar->mode) { 897 case 'c': 898 /* 899 * In GNU tar, -o means "old format." The 900 * "ustar" format is the closest thing 901 * supported by libarchive. 902 */ 903 cset_set_format(bsdtar->cset, "ustar"); 904 /* TODO: bsdtar->create_format = "v7"; */ 905 break; 906 case 'x': 907 /* POSIX-compatible behavior. */ 908 bsdtar->flags |= OPTFLAG_NO_OWNER; 909 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; 910 break; 911 default: 912 only_mode(bsdtar, "-o", "xc"); 913 break; 914 } 915 } 916 if (bsdtar->flags & OPTFLAG_STDOUT) 917 only_mode(bsdtar, "-O", "xt"); 918 if (bsdtar->flags & OPTFLAG_UNLINK_FIRST) 919 only_mode(bsdtar, "-U", "x"); 920 if (bsdtar->flags & OPTFLAG_WARN_LINKS) 921 only_mode(bsdtar, "--check-links", "cr"); 922 923 if ((bsdtar->flags & OPTFLAG_AUTO_COMPRESS) && 924 cset_auto_compress(bsdtar->cset, bsdtar->filename)) { 925 /* Ignore specified compressions if auto-compress works. */ 926 compression = '\0'; 927 compression2 = '\0'; 928 } 929 /* Check other parameters only permitted in certain modes. */ 930 if (compress_program != NULL) { 931 only_mode(bsdtar, "--use-compress-program", "cxt"); 932 cset_add_filter_program(bsdtar->cset, compress_program); 933 /* Ignore specified compressions. */ 934 compression = '\0'; 935 compression2 = '\0'; 936 } 937 if (compression != '\0') { 938 switch (compression) { 939 case 'J': case 'j': case 'y': case 'Z': case 'z': 940 strcpy(buff, "-?"); 941 buff[1] = compression; 942 break; 943 default: 944 strcpy(buff, "--"); 945 strcat(buff, compression_name); 946 break; 947 } 948 only_mode(bsdtar, buff, "cxt"); 949 cset_add_filter(bsdtar->cset, compression_name); 950 } 951 if (compression2 != '\0') { 952 strcpy(buff, "--"); 953 strcat(buff, compression2_name); 954 only_mode(bsdtar, buff, "cxt"); 955 cset_add_filter(bsdtar->cset, compression2_name); 956 } 957 if (cset_get_format(bsdtar->cset) != NULL) 958 only_mode(bsdtar, "--format", "cru"); 959 if (bsdtar->symlink_mode != '\0') { 960 strcpy(buff, "-?"); 961 buff[1] = bsdtar->symlink_mode; 962 only_mode(bsdtar, buff, "cru"); 963 } 964 965 /* 966 * When creating an archive from a directory tree, the directory 967 * walking code will already avoid entering directories when 968 * recursive inclusion of directory content is disabled, therefore 969 * changing the matching behavior has no effect for creation modes. 970 * It is relevant for extraction or listing. 971 */ 972 archive_match_set_inclusion_recursion(bsdtar->matching, 973 !(bsdtar->flags & OPTFLAG_NO_SUBDIRS)); 974 975 /* Filename "-" implies stdio. */ 976 if (strcmp(bsdtar->filename, "-") == 0) 977 bsdtar->filename = NULL; 978 979 switch(bsdtar->mode) { 980 case 'c': 981 tar_mode_c(bsdtar); 982 break; 983 case 'r': 984 tar_mode_r(bsdtar); 985 break; 986 case 't': 987 tar_mode_t(bsdtar); 988 break; 989 case 'u': 990 tar_mode_u(bsdtar); 991 break; 992 case 'x': 993 tar_mode_x(bsdtar); 994 break; 995 } 996 997 archive_match_free(bsdtar->matching); 998 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H) || defined(HAVE_PCRE2POSIX_H) 999 cleanup_substitution(bsdtar); 1000 #endif 1001 cset_free(bsdtar->cset); 1002 passphrase_free(bsdtar->ppbuff); 1003 1004 if (bsdtar->return_value != 0) 1005 lafe_warnc(0, 1006 "Error exit delayed from previous errors."); 1007 return (bsdtar->return_value); 1008 } 1009 1010 static void 1011 set_mode(struct bsdtar *bsdtar, char opt) 1012 { 1013 if (bsdtar->mode != '\0' && bsdtar->mode != opt) 1014 lafe_errc(1, 0, 1015 "Can't specify both -%c and -%c", opt, bsdtar->mode); 1016 bsdtar->mode = opt; 1017 } 1018 1019 /* 1020 * Verify that the mode is correct. 1021 */ 1022 static void 1023 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes) 1024 { 1025 if (strchr(valid_modes, bsdtar->mode) == NULL) 1026 lafe_errc(1, 0, 1027 "Option %s is not permitted in mode -%c", 1028 opt, bsdtar->mode); 1029 } 1030 1031 1032 void 1033 usage(void) 1034 { 1035 const char *p; 1036 1037 p = lafe_getprogname(); 1038 1039 fprintf(stderr, "Usage:\n"); 1040 fprintf(stderr, " List: %s -tf <archive-filename>\n", p); 1041 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p); 1042 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p); 1043 fprintf(stderr, " Help: %s --help\n", p); 1044 exit(1); 1045 } 1046 1047 static void 1048 version(void) 1049 { 1050 printf("bsdtar %s - %s \n", 1051 BSDTAR_VERSION_STRING, 1052 archive_version_details()); 1053 exit(0); 1054 } 1055 1056 static const char *long_help_msg = 1057 "First option must be a mode specifier:\n" 1058 " -c Create -r Add/Replace -t List -u Update -x Extract\n" 1059 "Common Options:\n" 1060 " -b # Use # 512-byte records per I/O block\n" 1061 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n" 1062 " -v Verbose\n" 1063 " -w Interactive\n" 1064 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n" 1065 " <file>, <dir> add these items to archive\n" 1066 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n" 1067 " --format {ustar|pax|cpio|shar} Select archive format\n" 1068 " --exclude <pattern> Skip files that match pattern\n" 1069 " -C <dir> Change to <dir> before processing remaining files\n" 1070 " @<archive> Add entries from <archive> to output\n" 1071 "List: %p -t [options] [<patterns>]\n" 1072 " <patterns> If specified, list only entries that match\n" 1073 "Extract: %p -x [options] [<patterns>]\n" 1074 " <patterns> If specified, extract only entries that match\n" 1075 " -k Keep (don't overwrite) existing files\n" 1076 " -m Don't restore modification times\n" 1077 " -O Write entries to stdout, don't restore to disk\n" 1078 " -p Restore permissions (including ACLs, owner, file flags)\n"; 1079 1080 1081 /* 1082 * Note that the word 'bsdtar' will always appear in the first line 1083 * of output. 1084 * 1085 * In particular, /bin/sh scripts that need to test for the presence 1086 * of bsdtar can use the following template: 1087 * 1088 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \ 1089 * echo bsdtar; else echo not bsdtar; fi 1090 */ 1091 static void 1092 long_help(void) 1093 { 1094 const char *prog; 1095 const char *p; 1096 1097 prog = lafe_getprogname(); 1098 1099 fflush(stderr); 1100 1101 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : ""; 1102 printf("%s%s: manipulate archive files\n", prog, p); 1103 1104 for (p = long_help_msg; *p != '\0'; p++) { 1105 if (*p == '%') { 1106 if (p[1] == 'p') { 1107 fputs(prog, stdout); 1108 p++; 1109 } else 1110 putchar('%'); 1111 } else 1112 putchar(*p); 1113 } 1114 version(); 1115 } 1116