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