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 /* Default: Extract atomically if possible */ 244 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SAFE_WRITES; 245 246 #ifndef _WIN32 247 /* On POSIX systems, assume --same-owner and -p when run by 248 * the root user. This doesn't make any sense on Windows. */ 249 if (bsdtar->user_uid == 0) { 250 /* --same-owner */ 251 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER; 252 /* -p */ 253 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM; 254 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL; 255 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR; 256 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; 257 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA; 258 } 259 #endif 260 261 /* 262 * Enable Mac OS "copyfile()" extension by default. 263 * This has no effect on other platforms. 264 */ 265 bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE; 266 #ifdef COPYFILE_DISABLE_VAR 267 if (getenv(COPYFILE_DISABLE_VAR)) 268 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE; 269 #endif 270 #if defined(__APPLE__) 271 /* 272 * On Mac OS ACLs are archived with copyfile() (--mac-metadata) 273 * Translation to NFSv4 ACLs has to be requested explicitly with --acls 274 */ 275 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_ACL; 276 #endif 277 278 bsdtar->matching = archive_match_new(); 279 if (bsdtar->matching == NULL) 280 lafe_errc(1, errno, "Out of memory"); 281 bsdtar->cset = cset_new(); 282 if (bsdtar->cset == NULL) 283 lafe_errc(1, errno, "Out of memory"); 284 285 bsdtar->argv = argv; 286 bsdtar->argc = argc; 287 288 /* 289 * Comments following each option indicate where that option 290 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's 291 * no such comment, then I don't know of anyone else who 292 * implements that option. 293 */ 294 while ((opt = bsdtar_getopt(bsdtar)) != -1) { 295 switch (opt) { 296 case 'a': /* GNU tar */ 297 bsdtar->flags |= OPTFLAG_AUTO_COMPRESS; 298 break; 299 case OPTION_ACLS: /* GNU tar */ 300 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL; 301 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_ACL; 302 bsdtar->flags |= OPTFLAG_ACLS; 303 break; 304 case 'B': /* GNU tar */ 305 /* libarchive doesn't need this; just ignore it. */ 306 break; 307 case 'b': /* SUSv2 */ 308 tptr = NULL; 309 l = strtol(bsdtar->argument, &tptr, 10); 310 if (l <= 0 || l > 8192L || 311 *(bsdtar->argument) == '\0' || tptr == NULL || 312 *tptr != '\0') { 313 lafe_errc(1, 0, "Invalid or out of range " 314 "(1..8192) argument to -b"); 315 } 316 bsdtar->bytes_per_block = 512 * (int)l; 317 /* Explicit -b forces last block size. */ 318 bsdtar->bytes_in_last_block = bsdtar->bytes_per_block; 319 break; 320 case OPTION_B64ENCODE: 321 if (compression2 != '\0') 322 lafe_errc(1, 0, 323 "Can't specify both --uuencode and " 324 "--b64encode"); 325 compression2 = opt; 326 compression2_name = "b64encode"; 327 break; 328 case 'C': /* GNU tar */ 329 if (strlen(bsdtar->argument) == 0) 330 lafe_errc(1, 0, 331 "Meaningless option: -C ''"); 332 333 set_chdir(bsdtar, bsdtar->argument); 334 break; 335 case 'c': /* SUSv2 */ 336 set_mode(bsdtar, opt); 337 break; 338 case OPTION_CHECK_LINKS: /* GNU tar */ 339 bsdtar->flags |= OPTFLAG_WARN_LINKS; 340 break; 341 case OPTION_CHROOT: /* NetBSD */ 342 bsdtar->flags |= OPTFLAG_CHROOT; 343 break; 344 case OPTION_CLEAR_NOCHANGE_FFLAGS: 345 bsdtar->extract_flags |= 346 ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS; 347 break; 348 case OPTION_EXCLUDE: /* GNU tar */ 349 if (archive_match_exclude_pattern( 350 bsdtar->matching, bsdtar->argument) != ARCHIVE_OK) 351 lafe_errc(1, 0, 352 "Couldn't exclude %s\n", bsdtar->argument); 353 break; 354 case OPTION_EXCLUDE_VCS: /* GNU tar */ 355 for(t=0; vcs_files[t]; t++) { 356 if (archive_match_exclude_pattern( 357 bsdtar->matching, 358 vcs_files[t]) != ARCHIVE_OK) 359 lafe_errc(1, 0, "Couldn't " 360 "exclude %s\n", vcs_files[t]); 361 } 362 break; 363 case OPTION_FFLAGS: 364 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; 365 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_FFLAGS; 366 bsdtar->flags |= OPTFLAG_FFLAGS; 367 break; 368 case OPTION_FORMAT: /* GNU tar, others */ 369 cset_set_format(bsdtar->cset, bsdtar->argument); 370 break; 371 case 'f': /* SUSv2 */ 372 bsdtar->filename = bsdtar->argument; 373 break; 374 case OPTION_GID: /* cpio */ 375 tptr = NULL; 376 l = strtol(bsdtar->argument, &tptr, 10); 377 if (l < 0 || l >= INT_MAX || *(bsdtar->argument) == '\0' || 378 tptr == NULL || *tptr != '\0') { 379 lafe_errc(1, 0, "Invalid argument to --gid"); 380 } 381 bsdtar->gid = (int)l; 382 break; 383 case OPTION_GNAME: /* cpio */ 384 bsdtar->gname = bsdtar->argument; 385 break; 386 case OPTION_GROUP: /* GNU tar */ 387 tptr = NULL; 388 389 uptr = strchr(bsdtar->argument, ':'); 390 if (uptr != NULL) { 391 if (uptr[1] == '\0') { 392 lafe_errc(1, 0, "Invalid argument to --group (missing id after :)"); 393 } 394 uptr[0] = 0; 395 uptr++; 396 l = strtol(uptr, &tptr, 10); 397 if (l < 0 || l >= INT_MAX || *uptr == '\0' || 398 tptr == NULL || *tptr != '\0') { 399 lafe_errc(1, 0, "Invalid argument to --group (%s is not a number)", uptr); 400 } else { 401 bsdtar->gid = (int)l; 402 } 403 bsdtar->gname = bsdtar->argument; 404 } else { 405 l = strtol(bsdtar->argument, &tptr, 10); 406 if (l < 0 || l >= INT_MAX || *(bsdtar->argument) == '\0' || 407 tptr == NULL || *tptr != '\0') { 408 bsdtar->gname = bsdtar->argument; 409 } else { 410 bsdtar->gid = (int)l; 411 bsdtar->gname = ""; 412 } 413 } 414 break; 415 case OPTION_GRZIP: 416 if (compression != '\0') 417 lafe_errc(1, 0, 418 "Can't specify both -%c and -%c", opt, 419 compression); 420 compression = opt; 421 compression_name = "grzip"; 422 break; 423 case 'H': /* BSD convention */ 424 bsdtar->symlink_mode = 'H'; 425 break; 426 case 'h': /* Linux Standards Base, gtar; synonym for -L */ 427 bsdtar->symlink_mode = 'L'; 428 /* Hack: -h by itself is the "help" command. */ 429 possible_help_request = 1; 430 break; 431 case OPTION_HELP: /* GNU tar, others */ 432 long_help(); 433 /* NOTREACHED*/ 434 case OPTION_HFS_COMPRESSION: /* Mac OS X v10.6 or later */ 435 bsdtar->extract_flags |= 436 ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED; 437 break; 438 case OPTION_IGNORE_ZEROS: 439 bsdtar->flags |= OPTFLAG_IGNORE_ZEROS; 440 break; 441 case 'I': /* GNU tar */ 442 /* 443 * TODO: Allow 'names' to come from an archive, 444 * not just a text file. Design a good UI for 445 * allowing names and mode/owner to be read 446 * from an archive, with contents coming from 447 * disk. This can be used to "refresh" an 448 * archive or to design archives with special 449 * permissions without having to create those 450 * permissions on disk. 451 */ 452 bsdtar->names_from_file = bsdtar->argument; 453 break; 454 case OPTION_INCLUDE: 455 /* 456 * No one else has the @archive extension, so 457 * no one else needs this to filter entries 458 * when transforming archives. 459 */ 460 if (archive_match_include_pattern(bsdtar->matching, 461 bsdtar->argument) != ARCHIVE_OK) 462 lafe_errc(1, 0, 463 "Failed to add %s to inclusion list", 464 bsdtar->argument); 465 break; 466 case 'j': /* GNU tar */ 467 if (compression != '\0') 468 lafe_errc(1, 0, 469 "Can't specify both -%c and -%c", opt, 470 compression); 471 compression = opt; 472 compression_name = "bzip2"; 473 break; 474 case 'J': /* GNU tar 1.21 and later */ 475 if (compression != '\0') 476 lafe_errc(1, 0, 477 "Can't specify both -%c and -%c", opt, 478 compression); 479 compression = opt; 480 compression_name = "xz"; 481 break; 482 case 'k': /* GNU tar */ 483 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE; 484 break; 485 case OPTION_KEEP_NEWER_FILES: /* GNU tar */ 486 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; 487 break; 488 case 'L': /* BSD convention */ 489 bsdtar->symlink_mode = 'L'; 490 break; 491 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */ 492 /* GNU tar 1.13 used -l for --one-file-system */ 493 bsdtar->flags |= OPTFLAG_WARN_LINKS; 494 break; 495 case OPTION_LRZIP: 496 case OPTION_LZ4: 497 case OPTION_LZIP: /* GNU tar beginning with 1.23 */ 498 case OPTION_LZMA: /* GNU tar beginning with 1.20 */ 499 case OPTION_LZOP: /* GNU tar beginning with 1.21 */ 500 case OPTION_ZSTD: 501 if (compression != '\0') 502 lafe_errc(1, 0, 503 "Can't specify both -%c and -%c", opt, 504 compression); 505 compression = opt; 506 switch (opt) { 507 case OPTION_LRZIP: compression_name = "lrzip"; break; 508 case OPTION_LZ4: compression_name = "lz4"; break; 509 case OPTION_LZIP: compression_name = "lzip"; break; 510 case OPTION_LZMA: compression_name = "lzma"; break; 511 case OPTION_LZOP: compression_name = "lzop"; break; 512 case OPTION_ZSTD: compression_name = "zstd"; break; 513 } 514 break; 515 case 'm': /* SUSv2 */ 516 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME; 517 break; 518 case OPTION_MAC_METADATA: /* Mac OS X */ 519 bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE; 520 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA; 521 bsdtar->flags |= OPTFLAG_MAC_METADATA; 522 break; 523 case 'n': /* GNU tar */ 524 bsdtar->flags |= OPTFLAG_NO_SUBDIRS; 525 break; 526 /* 527 * Selecting files by time: 528 * --newer-?time='date' Only files newer than 'date' 529 * --newer-?time-than='file' Only files newer than time 530 * on specified file (useful for incremental backups) 531 */ 532 case OPTION_NEWER_CTIME: /* GNU tar */ 533 if (archive_match_include_date(bsdtar->matching, 534 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER, 535 bsdtar->argument) != ARCHIVE_OK) 536 lafe_errc(1, 0, "Error : %s", 537 archive_error_string(bsdtar->matching)); 538 break; 539 case OPTION_NEWER_CTIME_THAN: 540 if (archive_match_include_file_time(bsdtar->matching, 541 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER, 542 bsdtar->argument) != ARCHIVE_OK) 543 lafe_errc(1, 0, "Error : %s", 544 archive_error_string(bsdtar->matching)); 545 break; 546 case OPTION_NEWER_MTIME: /* GNU tar */ 547 if (archive_match_include_date(bsdtar->matching, 548 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER, 549 bsdtar->argument) != ARCHIVE_OK) 550 lafe_errc(1, 0, "Error : %s", 551 archive_error_string(bsdtar->matching)); 552 break; 553 case OPTION_NEWER_MTIME_THAN: 554 if (archive_match_include_file_time(bsdtar->matching, 555 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER, 556 bsdtar->argument) != ARCHIVE_OK) 557 lafe_errc(1, 0, "Error : %s", 558 archive_error_string(bsdtar->matching)); 559 break; 560 case OPTION_NODUMP: /* star */ 561 bsdtar->readdisk_flags |= ARCHIVE_READDISK_HONOR_NODUMP; 562 break; 563 case OPTION_NOPRESERVE_HFS_COMPRESSION: 564 /* Mac OS X v10.6 or later */ 565 bsdtar->extract_flags |= 566 ARCHIVE_EXTRACT_NO_HFS_COMPRESSION; 567 break; 568 case OPTION_NO_ACLS: /* GNU tar */ 569 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL; 570 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_ACL; 571 bsdtar->flags |= OPTFLAG_NO_ACLS; 572 break; 573 case OPTION_NO_FFLAGS: 574 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS; 575 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_FFLAGS; 576 bsdtar->flags |= OPTFLAG_NO_FFLAGS; 577 break; 578 case OPTION_NO_MAC_METADATA: /* Mac OS X */ 579 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE; 580 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA; 581 bsdtar->flags |= OPTFLAG_NO_MAC_METADATA; 582 break; 583 case OPTION_NO_READ_SPARSE: 584 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_SPARSE; 585 bsdtar->flags |= OPTFLAG_NO_READ_SPARSE; 586 break; 587 case OPTION_NO_SAFE_WRITES: 588 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_SAFE_WRITES; 589 break; 590 case OPTION_NO_SAME_OWNER: /* GNU tar */ 591 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; 592 break; 593 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */ 594 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM; 595 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL; 596 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR; 597 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS; 598 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA; 599 break; 600 case OPTION_NO_XATTRS: /* GNU tar */ 601 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR; 602 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_XATTR; 603 bsdtar->flags |= OPTFLAG_NO_XATTRS; 604 break; 605 case OPTION_NULL: /* GNU tar */ 606 bsdtar->flags |= OPTFLAG_NULL; 607 break; 608 case OPTION_NUMERIC_OWNER: /* GNU tar */ 609 bsdtar->uname = ""; 610 bsdtar->gname = ""; 611 bsdtar->flags |= OPTFLAG_NUMERIC_OWNER; 612 break; 613 case 'O': /* GNU tar */ 614 bsdtar->flags |= OPTFLAG_STDOUT; 615 break; 616 case 'o': /* SUSv2 and GNU conflict here, but not fatally */ 617 bsdtar->flags |= OPTFLAG_O; 618 break; 619 /* 620 * Selecting files by time: 621 * --older-?time='date' Only files older than 'date' 622 * --older-?time-than='file' Only files older than time 623 * on specified file 624 */ 625 case OPTION_OLDER_CTIME: 626 if (archive_match_include_date(bsdtar->matching, 627 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER, 628 bsdtar->argument) != ARCHIVE_OK) 629 lafe_errc(1, 0, "Error : %s", 630 archive_error_string(bsdtar->matching)); 631 break; 632 case OPTION_OLDER_CTIME_THAN: 633 if (archive_match_include_file_time(bsdtar->matching, 634 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER, 635 bsdtar->argument) != ARCHIVE_OK) 636 lafe_errc(1, 0, "Error : %s", 637 archive_error_string(bsdtar->matching)); 638 break; 639 case OPTION_OLDER_MTIME: 640 if (archive_match_include_date(bsdtar->matching, 641 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER, 642 bsdtar->argument) != ARCHIVE_OK) 643 lafe_errc(1, 0, "Error : %s", 644 archive_error_string(bsdtar->matching)); 645 break; 646 case OPTION_OLDER_MTIME_THAN: 647 if (archive_match_include_file_time(bsdtar->matching, 648 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER, 649 bsdtar->argument) != ARCHIVE_OK) 650 lafe_errc(1, 0, "Error : %s", 651 archive_error_string(bsdtar->matching)); 652 break; 653 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */ 654 bsdtar->readdisk_flags |= 655 ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS; 656 break; 657 case OPTION_OPTIONS: 658 if (bsdtar->option_options != NULL) { 659 lafe_warnc(0, 660 "Ignoring previous option '%s', separate multiple options with commas", 661 bsdtar->option_options); 662 } 663 bsdtar->option_options = bsdtar->argument; 664 break; 665 case OPTION_OWNER: /* GNU tar */ 666 tptr = NULL; 667 668 uptr = strchr(bsdtar->argument, ':'); 669 if (uptr != NULL) { 670 if (uptr[1] == 0) { 671 lafe_errc(1, 0, "Invalid argument to --owner (missing id after :)"); 672 } 673 uptr[0] = 0; 674 uptr++; 675 l = strtol(uptr, &tptr, 10); 676 if (l < 0 || l >= INT_MAX || *uptr == '\0' || 677 tptr == NULL || *tptr != '\0') { 678 lafe_errc(1, 0, "Invalid argument to --owner (%s is not a number)", uptr); 679 } else { 680 bsdtar->uid = (int)l; 681 } 682 bsdtar->uname = bsdtar->argument; 683 } else { 684 l = strtol(bsdtar->argument, &tptr, 10); 685 if (l < 0 || l >= INT_MAX || *(bsdtar->argument) == '\0' || 686 tptr == NULL || *tptr != '\0') { 687 bsdtar->uname = bsdtar->argument; 688 } else { 689 bsdtar->uid = (int)l; 690 bsdtar->uname = ""; 691 } 692 } 693 break; 694 #if 0 695 /* 696 * The common BSD -P option is not necessary, since 697 * our default is to archive symlinks, not follow 698 * them. This is convenient, as -P conflicts with GNU 699 * tar anyway. 700 */ 701 case 'P': /* BSD convention */ 702 /* Default behavior, no option necessary. */ 703 break; 704 #endif 705 case 'P': /* GNU tar */ 706 bsdtar->extract_flags &= ~SECURITY; 707 bsdtar->flags |= OPTFLAG_ABSOLUTE_PATHS; 708 break; 709 case 'p': /* GNU tar, star */ 710 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM; 711 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL; 712 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR; 713 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; 714 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA; 715 break; 716 case OPTION_PASSPHRASE: 717 bsdtar->passphrase = bsdtar->argument; 718 break; 719 case OPTION_POSIX: /* GNU tar */ 720 cset_set_format(bsdtar->cset, "pax"); 721 break; 722 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */ 723 bsdtar->flags |= OPTFLAG_FAST_READ; 724 break; 725 case 'r': /* SUSv2 */ 726 set_mode(bsdtar, opt); 727 break; 728 case OPTION_READ_SPARSE: 729 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_SPARSE; 730 bsdtar->flags |= OPTFLAG_READ_SPARSE; 731 break; 732 case 'S': /* NetBSD pax-as-tar */ 733 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE; 734 break; 735 case 's': /* NetBSD pax-as-tar */ 736 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H) || defined(HAVE_PCRE2POSIX_H) 737 add_substitution(bsdtar, bsdtar->argument); 738 #else 739 lafe_warnc(0, 740 "-s is not supported by this version of bsdtar"); 741 usage(); 742 #endif 743 break; 744 case OPTION_SAFE_WRITES: 745 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SAFE_WRITES; 746 break; 747 case OPTION_SAME_OWNER: /* GNU tar */ 748 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER; 749 break; 750 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */ 751 tptr = NULL; 752 l = strtol(bsdtar->argument, &tptr, 10); 753 if (l < 0 || l > 100000L || *(bsdtar->argument) == '\0' || 754 tptr == NULL || *tptr != '\0') { 755 lafe_errc(1, 0, "Invalid argument to " 756 "--strip-components"); 757 } 758 bsdtar->strip_components = (int)l; 759 break; 760 case 'T': /* GNU tar */ 761 bsdtar->names_from_file = bsdtar->argument; 762 break; 763 case 't': /* SUSv2 */ 764 set_mode(bsdtar, opt); 765 bsdtar->verbose++; 766 break; 767 case OPTION_TOTALS: /* GNU tar */ 768 bsdtar->flags |= OPTFLAG_TOTALS; 769 break; 770 case 'U': /* GNU tar */ 771 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK; 772 bsdtar->flags |= OPTFLAG_UNLINK_FIRST; 773 break; 774 case 'u': /* SUSv2 */ 775 set_mode(bsdtar, opt); 776 break; 777 case OPTION_UID: /* cpio */ 778 tptr = NULL; 779 l = strtol(bsdtar->argument, &tptr, 10); 780 if (l < 0 || l >= INT_MAX || *(bsdtar->argument) == '\0' || 781 tptr == NULL || *tptr != '\0') { 782 lafe_errc(1, 0, "Invalid argument to --uid"); 783 } 784 bsdtar->uid = (int)l; 785 break; 786 case OPTION_UNAME: /* cpio */ 787 bsdtar->uname = bsdtar->argument; 788 break; 789 case OPTION_UUENCODE: 790 if (compression2 != '\0') 791 lafe_errc(1, 0, 792 "Can't specify both --uuencode and " 793 "--b64encode"); 794 compression2 = opt; 795 compression2_name = "uuencode"; 796 break; 797 case 'v': /* SUSv2 */ 798 bsdtar->verbose++; 799 break; 800 case OPTION_VERSION: /* GNU convention */ 801 version(); 802 /* NOTREACHED */ 803 #if 0 804 /* 805 * The -W longopt feature is handled inside of 806 * bsdtar_getopt(), so -W is not available here. 807 */ 808 case 'W': /* Obscure GNU convention. */ 809 break; 810 #endif 811 case 'w': /* SUSv2 */ 812 bsdtar->flags |= OPTFLAG_INTERACTIVE; 813 break; 814 case 'X': /* GNU tar */ 815 if (archive_match_exclude_pattern_from_file( 816 bsdtar->matching, bsdtar->argument, 0) 817 != ARCHIVE_OK) 818 lafe_errc(1, 0, "Error : %s", 819 archive_error_string(bsdtar->matching)); 820 break; 821 case 'x': /* SUSv2 */ 822 set_mode(bsdtar, opt); 823 break; 824 case OPTION_XATTRS: /* GNU tar */ 825 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR; 826 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_XATTR; 827 bsdtar->flags |= OPTFLAG_XATTRS; 828 break; 829 case 'y': /* FreeBSD version of GNU tar */ 830 if (compression != '\0') 831 lafe_errc(1, 0, 832 "Can't specify both -%c and -%c", opt, 833 compression); 834 compression = opt; 835 compression_name = "bzip2"; 836 break; 837 case 'Z': /* GNU tar */ 838 if (compression != '\0') 839 lafe_errc(1, 0, 840 "Can't specify both -%c and -%c", opt, 841 compression); 842 compression = opt; 843 compression_name = "compress"; 844 break; 845 case 'z': /* GNU tar, star, many others */ 846 if (compression != '\0') 847 lafe_errc(1, 0, 848 "Can't specify both -%c and -%c", opt, 849 compression); 850 compression = opt; 851 compression_name = "gzip"; 852 break; 853 case OPTION_USE_COMPRESS_PROGRAM: 854 compress_program = bsdtar->argument; 855 break; 856 default: 857 usage(); 858 } 859 } 860 861 /* 862 * Sanity-check options. 863 */ 864 865 /* If no "real" mode was specified, treat -h as --help. */ 866 if ((bsdtar->mode == '\0') && possible_help_request) { 867 long_help(); 868 } 869 870 /* Otherwise, a mode is required. */ 871 if (bsdtar->mode == '\0') 872 lafe_errc(1, 0, 873 "Must specify one of -c, -r, -t, -u, -x"); 874 875 /* Check boolean options only permitted in certain modes. */ 876 if (bsdtar->flags & OPTFLAG_AUTO_COMPRESS) { 877 only_mode(bsdtar, "-a", "cx"); 878 if (bsdtar->mode == 'x') { 879 bsdtar->flags &= ~OPTFLAG_AUTO_COMPRESS; 880 lafe_warnc(0, 881 "Ignoring option -a in mode -x"); 882 } 883 } 884 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) 885 only_mode(bsdtar, "--one-file-system", "cru"); 886 if (bsdtar->flags & OPTFLAG_FAST_READ) 887 only_mode(bsdtar, "--fast-read", "xt"); 888 if (bsdtar->extract_flags & ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED) 889 only_mode(bsdtar, "--hfsCompression", "x"); 890 if (bsdtar->extract_flags & ARCHIVE_EXTRACT_NO_HFS_COMPRESSION) 891 only_mode(bsdtar, "--nopreserveHFSCompression", "x"); 892 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_HONOR_NODUMP) 893 only_mode(bsdtar, "--nodump", "cru"); 894 if (bsdtar->flags & OPTFLAG_ACLS) 895 only_mode(bsdtar, "--acls", "crux"); 896 if (bsdtar->flags & OPTFLAG_NO_ACLS) 897 only_mode(bsdtar, "--no-acls", "crux"); 898 if (bsdtar->flags & OPTFLAG_XATTRS) 899 only_mode(bsdtar, "--xattrs", "crux"); 900 if (bsdtar->flags & OPTFLAG_NO_XATTRS) 901 only_mode(bsdtar, "--no-xattrs", "crux"); 902 if (bsdtar->flags & OPTFLAG_FFLAGS) 903 only_mode(bsdtar, "--fflags", "crux"); 904 if (bsdtar->flags & OPTFLAG_NO_FFLAGS) 905 only_mode(bsdtar, "--no-fflags", "crux"); 906 if (bsdtar->flags & OPTFLAG_MAC_METADATA) 907 only_mode(bsdtar, "--mac-metadata", "crux"); 908 if (bsdtar->flags & OPTFLAG_NO_MAC_METADATA) 909 only_mode(bsdtar, "--no-mac-metadata", "crux"); 910 if (bsdtar->flags & OPTFLAG_O) { 911 switch (bsdtar->mode) { 912 case 'c': 913 /* 914 * In GNU tar, -o means "old format." The 915 * "ustar" format is the closest thing 916 * supported by libarchive. 917 */ 918 cset_set_format(bsdtar->cset, "ustar"); 919 /* TODO: bsdtar->create_format = "v7"; */ 920 break; 921 case 'x': 922 /* POSIX-compatible behavior. */ 923 bsdtar->flags |= OPTFLAG_NO_OWNER; 924 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; 925 break; 926 default: 927 only_mode(bsdtar, "-o", "xc"); 928 break; 929 } 930 } 931 if (bsdtar->flags & OPTFLAG_STDOUT) 932 only_mode(bsdtar, "-O", "xt"); 933 if (bsdtar->flags & OPTFLAG_UNLINK_FIRST) 934 only_mode(bsdtar, "-U", "x"); 935 if (bsdtar->flags & OPTFLAG_WARN_LINKS) 936 only_mode(bsdtar, "--check-links", "cr"); 937 938 if ((bsdtar->flags & OPTFLAG_AUTO_COMPRESS) && 939 cset_auto_compress(bsdtar->cset, bsdtar->filename)) { 940 /* Ignore specified compressions if auto-compress works. */ 941 compression = '\0'; 942 compression2 = '\0'; 943 } 944 /* Check other parameters only permitted in certain modes. */ 945 if (compress_program != NULL) { 946 only_mode(bsdtar, "--use-compress-program", "cxt"); 947 cset_add_filter_program(bsdtar->cset, compress_program); 948 /* Ignore specified compressions. */ 949 compression = '\0'; 950 compression2 = '\0'; 951 } 952 if (compression != '\0') { 953 switch (compression) { 954 case 'J': case 'j': case 'y': case 'Z': case 'z': 955 strcpy(buff, "-?"); 956 buff[1] = compression; 957 break; 958 default: 959 strcpy(buff, "--"); 960 strcat(buff, compression_name); 961 break; 962 } 963 only_mode(bsdtar, buff, "cxt"); 964 cset_add_filter(bsdtar->cset, compression_name); 965 } 966 if (compression2 != '\0') { 967 strcpy(buff, "--"); 968 strcat(buff, compression2_name); 969 only_mode(bsdtar, buff, "cxt"); 970 cset_add_filter(bsdtar->cset, compression2_name); 971 } 972 if (cset_get_format(bsdtar->cset) != NULL) 973 only_mode(bsdtar, "--format", "cru"); 974 if (bsdtar->symlink_mode != '\0') { 975 strcpy(buff, "-?"); 976 buff[1] = bsdtar->symlink_mode; 977 only_mode(bsdtar, buff, "cru"); 978 } 979 980 /* 981 * When creating an archive from a directory tree, the directory 982 * walking code will already avoid entering directories when 983 * recursive inclusion of directory content is disabled, therefore 984 * changing the matching behavior has no effect for creation modes. 985 * It is relevant for extraction or listing. 986 */ 987 archive_match_set_inclusion_recursion(bsdtar->matching, 988 !(bsdtar->flags & OPTFLAG_NO_SUBDIRS)); 989 990 /* Filename "-" implies stdio. */ 991 if (strcmp(bsdtar->filename, "-") == 0) 992 bsdtar->filename = NULL; 993 994 switch(bsdtar->mode) { 995 case 'c': 996 tar_mode_c(bsdtar); 997 break; 998 case 'r': 999 tar_mode_r(bsdtar); 1000 break; 1001 case 't': 1002 tar_mode_t(bsdtar); 1003 break; 1004 case 'u': 1005 tar_mode_u(bsdtar); 1006 break; 1007 case 'x': 1008 tar_mode_x(bsdtar); 1009 break; 1010 } 1011 1012 archive_match_free(bsdtar->matching); 1013 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H) || defined(HAVE_PCRE2POSIX_H) 1014 cleanup_substitution(bsdtar); 1015 #endif 1016 cset_free(bsdtar->cset); 1017 passphrase_free(bsdtar->ppbuff); 1018 1019 if (bsdtar->return_value != 0) 1020 lafe_warnc(0, 1021 "Error exit delayed from previous errors."); 1022 return (bsdtar->return_value); 1023 } 1024 1025 static void 1026 set_mode(struct bsdtar *bsdtar, char opt) 1027 { 1028 if (bsdtar->mode != '\0' && bsdtar->mode != opt) 1029 lafe_errc(1, 0, 1030 "Can't specify both -%c and -%c", opt, bsdtar->mode); 1031 bsdtar->mode = opt; 1032 } 1033 1034 /* 1035 * Verify that the mode is correct. 1036 */ 1037 static void 1038 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes) 1039 { 1040 if (strchr(valid_modes, bsdtar->mode) == NULL) 1041 lafe_errc(1, 0, 1042 "Option %s is not permitted in mode -%c", 1043 opt, bsdtar->mode); 1044 } 1045 1046 1047 void 1048 usage(void) 1049 { 1050 const char *p; 1051 1052 p = lafe_getprogname(); 1053 1054 fprintf(stderr, "Usage:\n"); 1055 fprintf(stderr, " List: %s -tf <archive-filename>\n", p); 1056 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p); 1057 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p); 1058 fprintf(stderr, " Help: %s --help\n", p); 1059 exit(1); 1060 } 1061 1062 static void 1063 version(void) 1064 { 1065 printf("bsdtar %s - %s \n", 1066 BSDTAR_VERSION_STRING, 1067 archive_version_details()); 1068 exit(0); 1069 } 1070 1071 static const char *long_help_msg = 1072 "First option must be a mode specifier:\n" 1073 " -c Create -r Add/Replace -t List -u Update -x Extract\n" 1074 "Common Options:\n" 1075 " -b # Use # 512-byte records per I/O block\n" 1076 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n" 1077 " -v Verbose\n" 1078 " -w Interactive\n" 1079 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n" 1080 " <file>, <dir> add these items to archive\n" 1081 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n" 1082 " --format {ustar|pax|cpio|shar} Select archive format\n" 1083 " --exclude <pattern> Skip files that match pattern\n" 1084 " -C <dir> Change to <dir> before processing remaining files\n" 1085 " @<archive> Add entries from <archive> to output\n" 1086 "List: %p -t [options] [<patterns>]\n" 1087 " <patterns> If specified, list only entries that match\n" 1088 "Extract: %p -x [options] [<patterns>]\n" 1089 " <patterns> If specified, extract only entries that match\n" 1090 " -k Keep (don't overwrite) existing files\n" 1091 " -m Don't restore modification times\n" 1092 " -O Write entries to stdout, don't restore to disk\n" 1093 " -p Restore permissions (including ACLs, owner, file flags)\n"; 1094 1095 1096 /* 1097 * Note that the word 'bsdtar' will always appear in the first line 1098 * of output. 1099 * 1100 * In particular, /bin/sh scripts that need to test for the presence 1101 * of bsdtar can use the following template: 1102 * 1103 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \ 1104 * echo bsdtar; else echo not bsdtar; fi 1105 */ 1106 static void 1107 long_help(void) 1108 { 1109 const char *prog; 1110 const char *p; 1111 1112 prog = lafe_getprogname(); 1113 1114 fflush(stderr); 1115 1116 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : ""; 1117 printf("%s%s: manipulate archive files\n", prog, p); 1118 1119 for (p = long_help_msg; *p != '\0'; p++) { 1120 if (*p == '%') { 1121 if (p[1] == 'p') { 1122 fputs(prog, stdout); 1123 p++; 1124 } else 1125 putchar('%'); 1126 } else 1127 putchar(*p); 1128 } 1129 version(); 1130 } 1131