diff options
| author | Rob Landley <rob@landley.net> | 2005-10-16 03:54:49 +0000 |
|---|---|---|
| committer | Rob Landley <rob@landley.net> | 2005-10-16 03:54:49 +0000 |
| commit | 60fe7bfa41208da10348f0945ce3b3082e6edff6 (patch) | |
| tree | 2d41a2e57b7168e48cf96486f8e042ac03d02412 | |
| parent | 7547a6e2f6787f213a5d3e07ff5218aed4ee4b8e (diff) | |
| download | busybox-w32-60fe7bfa41208da10348f0945ce3b3082e6edff6.tar.gz busybox-w32-60fe7bfa41208da10348f0945ce3b3082e6edff6.tar.bz2 busybox-w32-60fe7bfa41208da10348f0945ce3b3082e6edff6.zip | |
Whitespace and curly bracket cleanup (our tabstop is 4 in busybox),
and switch more stuff from CONFIG to ENABLE.
| -rw-r--r-- | archival/tar.c | 334 |
1 files changed, 146 insertions, 188 deletions
diff --git a/archival/tar.c b/archival/tar.c index 4363d355b..4f02e9006 100644 --- a/archival/tar.c +++ b/archival/tar.c | |||
| @@ -21,20 +21,7 @@ | |||
| 21 | * Copyright (C) 1995 Bruce Perens | 21 | * Copyright (C) 1995 Bruce Perens |
| 22 | * This is free software under the GNU General Public License. | 22 | * This is free software under the GNU General Public License. |
| 23 | * | 23 | * |
| 24 | * This program is free software; you can redistribute it and/or modify | 24 | * Licensed under GPL v2 (or later), see file LICENSE in this tarball. |
| 25 | * it under the terms of the GNU General Public License as published by | ||
| 26 | * the Free Software Foundation; either version 2 of the License, or | ||
| 27 | * (at your option) any later version. | ||
| 28 | * | ||
| 29 | * This program is distributed in the hope that it will be useful, | ||
| 30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 32 | * General Public License for more details. | ||
| 33 | * | ||
| 34 | * You should have received a copy of the GNU General Public License | ||
| 35 | * along with this program; if not, write to the Free Software | ||
| 36 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 37 | * | ||
| 38 | */ | 25 | */ |
| 39 | 26 | ||
| 40 | #include <fcntl.h> | 27 | #include <fcntl.h> |
| @@ -94,22 +81,22 @@ typedef struct TarHeader TarHeader; | |||
| 94 | typedef struct HardLinkInfo HardLinkInfo; | 81 | typedef struct HardLinkInfo HardLinkInfo; |
| 95 | struct HardLinkInfo { | 82 | struct HardLinkInfo { |
| 96 | HardLinkInfo *next; /* Next entry in list */ | 83 | HardLinkInfo *next; /* Next entry in list */ |
| 97 | dev_t dev; /* Device number */ | 84 | dev_t dev; /* Device number */ |
| 98 | ino_t ino; /* Inode number */ | 85 | ino_t ino; /* Inode number */ |
| 99 | short linkCount; /* (Hard) Link Count */ | 86 | short linkCount; /* (Hard) Link Count */ |
| 100 | char name[1]; /* Start of filename (must be last) */ | 87 | char name[1]; /* Start of filename (must be last) */ |
| 101 | }; | 88 | }; |
| 102 | 89 | ||
| 103 | /* Some info to be carried along when creating a new tarball */ | 90 | /* Some info to be carried along when creating a new tarball */ |
| 104 | struct TarBallInfo { | 91 | struct TarBallInfo { |
| 105 | char *fileName; /* File name of the tarball */ | 92 | char *fileName; /* File name of the tarball */ |
| 106 | int tarFd; /* Open-for-write file descriptor | 93 | int tarFd; /* Open-for-write file descriptor |
| 107 | for the tarball */ | 94 | for the tarball */ |
| 108 | struct stat statBuf; /* Stat info for the tarball, letting | 95 | struct stat statBuf; /* Stat info for the tarball, letting |
| 109 | us know the inode and device that the | 96 | us know the inode and device that the |
| 110 | tarball lives, so we can avoid trying | 97 | tarball lives, so we can avoid trying |
| 111 | to include the tarball into itself */ | 98 | to include the tarball into itself */ |
| 112 | int verboseFlag; /* Whether to print extra stuff or not */ | 99 | int verboseFlag; /* Whether to print extra stuff or not */ |
| 113 | const llist_t *excludeList; /* List of files to not include */ | 100 | const llist_t *excludeList; /* List of files to not include */ |
| 114 | HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */ | 101 | HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */ |
| 115 | HardLinkInfo *hlInfo; /* Hard Link Info for the current file */ | 102 | HardLinkInfo *hlInfo; /* Hard Link Info for the current file */ |
| @@ -213,9 +200,7 @@ static int putOctal(char *cp, int len, long value) | |||
| 213 | 200 | ||
| 214 | /* Write out a tar header for the specified file/directory/whatever */ | 201 | /* Write out a tar header for the specified file/directory/whatever */ |
| 215 | static inline int writeTarHeader(struct TarBallInfo *tbInfo, | 202 | static inline int writeTarHeader(struct TarBallInfo *tbInfo, |
| 216 | const char *header_name, | 203 | const char *header_name, const char *real_name, struct stat *statbuf) |
| 217 | const char *real_name, | ||
| 218 | struct stat *statbuf) | ||
| 219 | { | 204 | { |
| 220 | long chksum = 0; | 205 | long chksum = 0; |
| 221 | struct TarHeader header; | 206 | struct TarHeader header; |
| @@ -393,11 +378,10 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, | |||
| 393 | if (header_name[0] == '\0') | 378 | if (header_name[0] == '\0') |
| 394 | return TRUE; | 379 | return TRUE; |
| 395 | 380 | ||
| 396 | # ifdef CONFIG_FEATURE_TAR_FROM | 381 | if (ENABLE_FEATURE_TAR_FROM && |
| 397 | if (exclude_file(tbInfo->excludeList, header_name)) { | 382 | exclude_file(tbInfo->excludeList, header_name)) { |
| 398 | return SKIP; | 383 | return SKIP; |
| 399 | } | 384 | } |
| 400 | # endif /* CONFIG_FEATURE_TAR_FROM */ | ||
| 401 | 385 | ||
| 402 | if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) { | 386 | if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) { |
| 403 | return (FALSE); | 387 | return (FALSE); |
| @@ -419,9 +403,9 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, | |||
| 419 | readSize = bb_copyfd_eof(inputFileFd, tbInfo->tarFd); | 403 | readSize = bb_copyfd_eof(inputFileFd, tbInfo->tarFd); |
| 420 | 404 | ||
| 421 | /* Pad the file up to the tar block size */ | 405 | /* Pad the file up to the tar block size */ |
| 422 | for (; (readSize % TAR_BLOCK_SIZE) != 0; readSize++) { | 406 | for (; (readSize % TAR_BLOCK_SIZE) != 0; readSize++) |
| 423 | write(tbInfo->tarFd, "\0", 1); | 407 | write(tbInfo->tarFd, "\0", 1); |
| 424 | } | 408 | |
| 425 | close(inputFileFd); | 409 | close(inputFileFd); |
| 426 | } | 410 | } |
| 427 | 411 | ||
| @@ -456,9 +440,8 @@ static inline int writeTarFile(const int tar_fd, const int verboseFlag, | |||
| 456 | char *zip_exec = (gzip == 1) ? "gzip" : "bzip2"; | 440 | char *zip_exec = (gzip == 1) ? "gzip" : "bzip2"; |
| 457 | 441 | ||
| 458 | 442 | ||
| 459 | if (pipe(gzipDataPipe) < 0 || pipe(gzipStatusPipe) < 0) { | 443 | if (pipe(gzipDataPipe) < 0 || pipe(gzipStatusPipe) < 0) |
| 460 | bb_perror_msg_and_die("Failed to create pipe"); | 444 | bb_perror_msg_and_die("create pipe"); |
| 461 | } | ||
| 462 | 445 | ||
| 463 | signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */ | 446 | signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */ |
| 464 | 447 | ||
| @@ -504,26 +487,23 @@ static inline int writeTarFile(const int tar_fd, const int verboseFlag, | |||
| 504 | close(gzipStatusPipe[0]); | 487 | close(gzipStatusPipe[0]); |
| 505 | 488 | ||
| 506 | tbInfo.tarFd = gzipDataPipe[1]; | 489 | tbInfo.tarFd = gzipDataPipe[1]; |
| 507 | } else { | 490 | } else bb_perror_msg_and_die("vfork gzip"); |
| 508 | bb_perror_msg_and_die("Failed to vfork gzip process"); | ||
| 509 | } | ||
| 510 | } | 491 | } |
| 511 | 492 | ||
| 512 | tbInfo.excludeList = exclude; | 493 | tbInfo.excludeList = exclude; |
| 513 | 494 | ||
| 514 | /* Read the directory/files and iterate over them one at a time */ | 495 | /* Read the directory/files and iterate over them one at a time */ |
| 515 | while (include) { | 496 | while (include) { |
| 516 | if (!recursive_action(include->data, TRUE, dereferenceFlag, FALSE, | 497 | if (!recursive_action(include->data, TRUE, dereferenceFlag, |
| 517 | writeFileToTarball, writeFileToTarball, | 498 | FALSE, writeFileToTarball, writeFileToTarball, &tbInfo)) |
| 518 | (void *) &tbInfo)) { | 499 | { |
| 519 | errorFlag = TRUE; | 500 | errorFlag = TRUE; |
| 520 | } | 501 | } |
| 521 | include = include->link; | 502 | include = include->link; |
| 522 | } | 503 | } |
| 523 | /* Write two empty blocks to the end of the archive */ | 504 | /* Write two empty blocks to the end of the archive */ |
| 524 | for (size = 0; size < (2 * TAR_BLOCK_SIZE); size++) { | 505 | for (size = 0; size < (2 * TAR_BLOCK_SIZE); size++) |
| 525 | write(tbInfo.tarFd, "\0", 1); | 506 | write(tbInfo.tarFd, "\0", 1); |
| 526 | } | ||
| 527 | 507 | ||
| 528 | /* To be pedantically correct, we would check if the tarball | 508 | /* To be pedantically correct, we would check if the tarball |
| 529 | * is smaller than 20 tar blocks, and pad it if it was smaller, | 509 | * is smaller than 20 tar blocks, and pad it if it was smaller, |
| @@ -531,19 +511,23 @@ static inline int writeTarFile(const int tar_fd, const int verboseFlag, | |||
| 531 | * so is considered a waste of space */ | 511 | * so is considered a waste of space */ |
| 532 | 512 | ||
| 533 | /* Hang up the tools, close up shop, head home */ | 513 | /* Hang up the tools, close up shop, head home */ |
| 534 | close(tbInfo.tarFd); | 514 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 515 | close(tbInfo.tarFd); | ||
| 516 | freeHardLinkInfo(&tbInfo.hlInfoHead); | ||
| 517 | } | ||
| 518 | |||
| 535 | if (errorFlag) | 519 | if (errorFlag) |
| 536 | bb_error_msg("Error exit delayed from previous errors"); | 520 | bb_error_msg("Error exit delayed from previous errors"); |
| 537 | 521 | ||
| 538 | freeHardLinkInfo(&tbInfo.hlInfoHead); | 522 | if (gzipPid && waitpid(gzipPid, NULL, 0)==-1) |
| 539 | 523 | bb_error_msg("Couldnt wait"); | |
| 540 | if (gzipPid) { | ||
| 541 | if (waitpid(gzipPid, NULL, 0) == -1) | ||
| 542 | printf("Couldnt wait ?"); | ||
| 543 | } | ||
| 544 | 524 | ||
| 545 | return !errorFlag; | 525 | return !errorFlag; |
| 546 | } | 526 | } |
| 527 | #else | ||
| 528 | int writeTarFile(const int tar_fd, const int verboseFlag, | ||
| 529 | const unsigned long dereferenceFlag, const llist_t *include, | ||
| 530 | const llist_t *exclude, const int gzip); | ||
| 547 | #endif /* tar_create */ | 531 | #endif /* tar_create */ |
| 548 | 532 | ||
| 549 | #ifdef CONFIG_FEATURE_TAR_FROM | 533 | #ifdef CONFIG_FEATURE_TAR_FROM |
| @@ -555,18 +539,19 @@ static llist_t *append_file_list_to_list(llist_t *list) | |||
| 555 | char *line; | 539 | char *line; |
| 556 | llist_t *newlist = NULL; | 540 | llist_t *newlist = NULL; |
| 557 | 541 | ||
| 558 | while(cur) { | 542 | while (cur) { |
| 559 | src_stream = bb_xfopen(cur->data, "r"); | 543 | src_stream = bb_xfopen(cur->data, "r"); |
| 560 | tmp = cur; | 544 | tmp = cur; |
| 561 | cur = cur->link; | 545 | cur = cur->link; |
| 562 | free(tmp); | 546 | free(tmp); |
| 563 | while((line = bb_get_chomped_line_from_file(src_stream)) != NULL) { | 547 | while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) |
| 564 | newlist = llist_add_to(newlist, line); | 548 | newlist = llist_add_to(newlist, line); |
| 565 | } | 549 | fclose(src_stream); |
| 566 | fclose(src_stream); | ||
| 567 | } | 550 | } |
| 568 | return newlist; | 551 | return newlist; |
| 569 | } | 552 | } |
| 553 | #else | ||
| 554 | #define append_file_list_to_list(x) 0 | ||
| 570 | #endif | 555 | #endif |
| 571 | 556 | ||
| 572 | #ifdef CONFIG_FEATURE_TAR_COMPRESS | 557 | #ifdef CONFIG_FEATURE_TAR_COMPRESS |
| @@ -576,7 +561,9 @@ static char get_header_tar_Z(archive_handle_t *archive_handle) | |||
| 576 | archive_handle->seek = seek_by_char; | 561 | archive_handle->seek = seek_by_char; |
| 577 | 562 | ||
| 578 | /* do the decompression, and cleanup */ | 563 | /* do the decompression, and cleanup */ |
| 579 | if ((bb_xread_char(archive_handle->src_fd) != 0x1f) || (bb_xread_char(archive_handle->src_fd) != 0x9d)) { | 564 | if (bb_xread_char(archive_handle->src_fd) != 0x1f || |
| 565 | bb_xread_char(archive_handle->src_fd) != 0x9d) | ||
| 566 | { | ||
| 580 | bb_error_msg_and_die("Invalid magic"); | 567 | bb_error_msg_and_die("Invalid magic"); |
| 581 | } | 568 | } |
| 582 | 569 | ||
| @@ -587,58 +574,59 @@ static char get_header_tar_Z(archive_handle_t *archive_handle) | |||
| 587 | /* Can only do one file at a time */ | 574 | /* Can only do one file at a time */ |
| 588 | return(EXIT_FAILURE); | 575 | return(EXIT_FAILURE); |
| 589 | } | 576 | } |
| 577 | #else | ||
| 578 | #define get_header_tar_Z 0 | ||
| 590 | #endif | 579 | #endif |
| 591 | 580 | ||
| 592 | #define CTX_TEST (1 << 0) | 581 | #define CTX_TEST (1 << 0) |
| 593 | #define CTX_EXTRACT (1 << 1) | 582 | #define CTX_EXTRACT (1 << 1) |
| 594 | #define TAR_OPT_BASEDIR (1 << 2) | 583 | #define TAR_OPT_BASEDIR (1 << 2) |
| 595 | #define TAR_OPT_TARNAME (1 << 3) | 584 | #define TAR_OPT_TARNAME (1 << 3) |
| 596 | #define TAR_OPT_2STDOUT (1 << 4) | 585 | #define TAR_OPT_2STDOUT (1 << 4) |
| 597 | #define TAR_OPT_P (1 << 5) | 586 | #define TAR_OPT_P (1 << 5) |
| 598 | #define TAR_OPT_VERBOSE (1 << 6) | 587 | #define TAR_OPT_VERBOSE (1 << 6) |
| 599 | #define TAR_OPT_KEEP_OLD (1 << 7) | 588 | #define TAR_OPT_KEEP_OLD (1 << 7) |
| 600 | 589 | ||
| 590 | # define CTX_CREATE (1 << 8) | ||
| 591 | # define TAR_OPT_DEREFERNCE (1 << 9) | ||
| 601 | #ifdef CONFIG_FEATURE_TAR_CREATE | 592 | #ifdef CONFIG_FEATURE_TAR_CREATE |
| 602 | # define CTX_CREATE (1 << 8) | 593 | # define TAR_OPT_STR_CREATE "ch" |
| 603 | # define TAR_OPT_DEREFERNCE (1 << 9) | ||
| 604 | # define TAR_OPT_STR_CREATE "ch" | ||
| 605 | # define TAR_OPT_FLAG_CREATE 2 | 594 | # define TAR_OPT_FLAG_CREATE 2 |
| 606 | #else | 595 | #else |
| 607 | # define CTX_CREATE 0 | 596 | # define TAR_OPT_STR_CREATE "" |
| 608 | # define TAR_OPT_STR_CREATE "" | ||
| 609 | # define TAR_OPT_FLAG_CREATE 0 | 597 | # define TAR_OPT_FLAG_CREATE 0 |
| 610 | #endif | 598 | #endif |
| 611 | 599 | ||
| 600 | # define TAR_OPT_BZIP2 (1 << (8 + TAR_OPT_FLAG_CREATE)) | ||
| 612 | #ifdef CONFIG_FEATURE_TAR_BZIP2 | 601 | #ifdef CONFIG_FEATURE_TAR_BZIP2 |
| 613 | # define TAR_OPT_BZIP2 (1 << (8 + TAR_OPT_FLAG_CREATE)) | 602 | # define TAR_OPT_STR_BZIP2 "j" |
| 614 | # define TAR_OPT_STR_BZIP2 "j" | 603 | # define TAR_OPT_FLAG_BZIP2 1 |
| 615 | # define TAR_OPT_FLAG_BZIP2 1 | ||
| 616 | #else | 604 | #else |
| 617 | # define TAR_OPT_STR_BZIP2 "" | 605 | # define TAR_OPT_STR_BZIP2 "" |
| 618 | # define TAR_OPT_FLAG_BZIP2 0 | 606 | # define TAR_OPT_FLAG_BZIP2 0 |
| 619 | #endif | 607 | #endif |
| 620 | 608 | ||
| 621 | #ifdef CONFIG_FEATURE_TAR_FROM | ||
| 622 | # define TAR_OPT_INCLUDE_FROM (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2)) | 609 | # define TAR_OPT_INCLUDE_FROM (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2)) |
| 623 | # define TAR_OPT_EXCLUDE_FROM (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + 1)) | 610 | # define TAR_OPT_EXCLUDE_FROM (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + 1)) |
| 624 | # define TAR_OPT_STR_FROM "T:X:" | 611 | #ifdef CONFIG_FEATURE_TAR_FROM |
| 625 | # define TAR_OPT_FLAG_FROM 2 | 612 | # define TAR_OPT_STR_FROM "T:X:" |
| 613 | # define TAR_OPT_FLAG_FROM 2 | ||
| 626 | #else | 614 | #else |
| 627 | # define TAR_OPT_STR_FROM "" | 615 | # define TAR_OPT_STR_FROM "" |
| 628 | # define TAR_OPT_FLAG_FROM 0 | 616 | # define TAR_OPT_FLAG_FROM 0 |
| 629 | #endif | 617 | #endif |
| 630 | 618 | ||
| 619 | # define TAR_OPT_GZIP (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + TAR_OPT_FLAG_FROM)) | ||
| 631 | #ifdef CONFIG_FEATURE_TAR_GZIP | 620 | #ifdef CONFIG_FEATURE_TAR_GZIP |
| 632 | # define TAR_OPT_GZIP (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + TAR_OPT_FLAG_FROM)) | 621 | # define TAR_OPT_STR_GZIP "z" |
| 633 | # define TAR_OPT_STR_GZIP "z" | 622 | # define TAR_OPT_FLAG_GZIP 1 |
| 634 | # define TAR_OPT_FLAG_GZIP 1 | ||
| 635 | #else | 623 | #else |
| 636 | # define TAR_OPT_STR_GZIP "" | 624 | # define TAR_OPT_STR_GZIP "" |
| 637 | # define TAR_OPT_FLAG_GZIP 0 | 625 | # define TAR_OPT_FLAG_GZIP 0 |
| 638 | #endif | 626 | #endif |
| 639 | 627 | ||
| 628 | # define TAR_OPT_UNCOMPRESS (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + TAR_OPT_FLAG_FROM + TAR_OPT_FLAG_GZIP)) | ||
| 640 | #ifdef CONFIG_FEATURE_TAR_COMPRESS | 629 | #ifdef CONFIG_FEATURE_TAR_COMPRESS |
| 641 | # define TAR_OPT_UNCOMPRESS (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + TAR_OPT_FLAG_FROM + TAR_OPT_FLAG_GZIP)) | ||
| 642 | # define TAR_OPT_STR_COMPRESS "Z" | 630 | # define TAR_OPT_STR_COMPRESS "Z" |
| 643 | #else | 631 | #else |
| 644 | # define TAR_OPT_STR_COMPRESS "" | 632 | # define TAR_OPT_STR_COMPRESS "" |
| @@ -653,34 +641,36 @@ static const char tar_options[]="txC:f:Opvk" \ | |||
| 653 | 641 | ||
| 654 | #ifdef CONFIG_FEATURE_TAR_LONG_OPTIONS | 642 | #ifdef CONFIG_FEATURE_TAR_LONG_OPTIONS |
| 655 | static const struct option tar_long_options[] = { | 643 | static const struct option tar_long_options[] = { |
| 656 | { "list", 0, NULL, 't' }, | 644 | { "list", 0, NULL, 't' }, |
| 657 | { "extract", 0, NULL, 'x' }, | 645 | { "extract", 0, NULL, 'x' }, |
| 658 | { "directory", 1, NULL, 'C' }, | 646 | { "directory", 1, NULL, 'C' }, |
| 659 | { "file", 1, NULL, 'f' }, | 647 | { "file", 1, NULL, 'f' }, |
| 660 | { "to-stdout", 0, NULL, 'O' }, | 648 | { "to-stdout", 0, NULL, 'O' }, |
| 661 | { "same-permissions", 0, NULL, 'p' }, | 649 | { "same-permissions", 0, NULL, 'p' }, |
| 662 | { "verbose", 0, NULL, 'v' }, | 650 | { "verbose", 0, NULL, 'v' }, |
| 663 | { "keep-old", 0, NULL, 'k' }, | 651 | { "keep-old", 0, NULL, 'k' }, |
| 664 | # ifdef CONFIG_FEATURE_TAR_CREATE | 652 | # ifdef CONFIG_FEATURE_TAR_CREATE |
| 665 | { "create", 0, NULL, 'c' }, | 653 | { "create", 0, NULL, 'c' }, |
| 666 | { "dereference", 0, NULL, 'h' }, | 654 | { "dereference", 0, NULL, 'h' }, |
| 667 | # endif | 655 | # endif |
| 668 | # ifdef CONFIG_FEATURE_TAR_BZIP2 | 656 | # ifdef CONFIG_FEATURE_TAR_BZIP2 |
| 669 | { "bzip2", 0, NULL, 'j' }, | 657 | { "bzip2", 0, NULL, 'j' }, |
| 670 | # endif | 658 | # endif |
| 671 | # ifdef CONFIG_FEATURE_TAR_FROM | 659 | # ifdef CONFIG_FEATURE_TAR_FROM |
| 672 | { "files-from", 1, NULL, 'T' }, | 660 | { "files-from", 1, NULL, 'T' }, |
| 673 | { "exclude-from", 1, NULL, 'X' }, | 661 | { "exclude-from", 1, NULL, 'X' }, |
| 674 | { "exclude", 1, NULL, '\n' }, | 662 | { "exclude", 1, NULL, '\n' }, |
| 675 | # endif | 663 | # endif |
| 676 | # ifdef CONFIG_FEATURE_TAR_GZIP | 664 | # ifdef CONFIG_FEATURE_TAR_GZIP |
| 677 | { "gzip", 0, NULL, 'z' }, | 665 | { "gzip", 0, NULL, 'z' }, |
| 678 | # endif | 666 | # endif |
| 679 | # ifdef CONFIG_FEATURE_TAR_COMPRESS | 667 | # ifdef CONFIG_FEATURE_TAR_COMPRESS |
| 680 | { "compress", 0, NULL, 'Z' }, | 668 | { "compress", 0, NULL, 'Z' }, |
| 681 | # endif | 669 | # endif |
| 682 | { 0, 0, 0, 0 } | 670 | { 0, 0, 0, 0 } |
| 683 | }; | 671 | }; |
| 672 | #else | ||
| 673 | #define tar_long_options 0 | ||
| 684 | #endif | 674 | #endif |
| 685 | 675 | ||
| 686 | int tar_main(int argc, char **argv) | 676 | int tar_main(int argc, char **argv) |
| @@ -690,24 +680,18 @@ int tar_main(int argc, char **argv) | |||
| 690 | char *base_dir = NULL; | 680 | char *base_dir = NULL; |
| 691 | const char *tar_filename = "-"; | 681 | const char *tar_filename = "-"; |
| 692 | unsigned long opt; | 682 | unsigned long opt; |
| 693 | |||
| 694 | #if defined(CONFIG_FEATURE_TAR_FROM) | ||
| 695 | llist_t *excludes = NULL; | 683 | llist_t *excludes = NULL; |
| 696 | #endif | ||
| 697 | 684 | ||
| 698 | /* Initialise default values */ | 685 | /* Initialise default values */ |
| 699 | tar_handle = init_handle(); | 686 | tar_handle = init_handle(); |
| 700 | tar_handle->flags = ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL; | 687 | tar_handle->flags = ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL; |
| 701 | 688 | ||
| 702 | /* Prepend '-' to the first argument if required */ | 689 | /* Prepend '-' to the first argument if required */ |
| 703 | #ifdef CONFIG_FEATURE_TAR_CREATE | 690 | bb_opt_complementally = ENABLE_FEATURE_TAR_CREATE ? |
| 704 | bb_opt_complementally = "--:-1:X::T::\n::c:t:x:?:c--tx:t--cx:x--ct"; | 691 | "--:-1:X::T::\n::c:t:x:?:c--tx:t--cx:x--ct" : |
| 705 | #else | 692 | "--:-1:X::T::\n::t:x:?:t--x:x--t"; |
| 706 | bb_opt_complementally = "--:-1:X::T::\n::t:x:?:t--x:x--t"; | 693 | if (ENABLE_FEATURE_TAR_LONG_OPTIONS) |
| 707 | #endif | 694 | bb_applet_long_options = tar_long_options; |
| 708 | #ifdef CONFIG_FEATURE_TAR_LONG_OPTIONS | ||
| 709 | bb_applet_long_options = tar_long_options; | ||
| 710 | #endif | ||
| 711 | opt = bb_getopt_ulflags(argc, argv, tar_options, | 695 | opt = bb_getopt_ulflags(argc, argv, tar_options, |
| 712 | &base_dir, /* Change to dir <optarg> */ | 696 | &base_dir, /* Change to dir <optarg> */ |
| 713 | &tar_filename /* archive filename */ | 697 | &tar_filename /* archive filename */ |
| @@ -717,62 +701,52 @@ int tar_main(int argc, char **argv) | |||
| 717 | &excludes | 701 | &excludes |
| 718 | #endif | 702 | #endif |
| 719 | ); | 703 | ); |
| 720 | 704 | ||
| 721 | if(opt & CTX_TEST) { | 705 | if (opt & CTX_TEST) { |
| 722 | if ((tar_handle->action_header == header_list) || | 706 | if ((tar_handle->action_header == header_list) || |
| 723 | (tar_handle->action_header == header_verbose_list)) { | 707 | (tar_handle->action_header == header_verbose_list)) |
| 724 | tar_handle->action_header = header_verbose_list; | 708 | { |
| 725 | } else { | 709 | tar_handle->action_header = header_verbose_list; |
| 726 | tar_handle->action_header = header_list; | 710 | } else tar_handle->action_header = header_list; |
| 727 | } | ||
| 728 | } | ||
| 729 | if(opt & CTX_EXTRACT) { | ||
| 730 | if (tar_handle->action_data != data_extract_to_stdout) | ||
| 731 | tar_handle->action_data = data_extract_all; | ||
| 732 | } | 711 | } |
| 733 | if(opt & TAR_OPT_2STDOUT) { | 712 | if((opt & CTX_EXTRACT) && tar_handle->action_data != data_extract_to_stdout) |
| 734 | /* To stdout */ | 713 | tar_handle->action_data = data_extract_all; |
| 714 | |||
| 715 | if (opt & TAR_OPT_2STDOUT) | ||
| 735 | tar_handle->action_data = data_extract_to_stdout; | 716 | tar_handle->action_data = data_extract_to_stdout; |
| 736 | } | 717 | |
| 737 | if(opt & TAR_OPT_VERBOSE) { | 718 | if (opt & TAR_OPT_VERBOSE) { |
| 738 | if ((tar_handle->action_header == header_list) || | 719 | if ((tar_handle->action_header == header_list) || |
| 739 | (tar_handle->action_header == header_verbose_list)) | 720 | (tar_handle->action_header == header_verbose_list)) |
| 740 | { | 721 | { |
| 741 | tar_handle->action_header = header_verbose_list; | 722 | tar_handle->action_header = header_verbose_list; |
| 742 | } else { | 723 | } else |
| 743 | tar_handle->action_header = header_list; | 724 | tar_handle->action_header = header_list; |
| 744 | } | ||
| 745 | } | 725 | } |
| 746 | if (opt & TAR_OPT_KEEP_OLD) { | 726 | if (opt & TAR_OPT_KEEP_OLD) |
| 747 | tar_handle->flags &= ~ARCHIVE_EXTRACT_UNCONDITIONAL; | 727 | tar_handle->flags &= ~ARCHIVE_EXTRACT_UNCONDITIONAL; |
| 748 | } | ||
| 749 | 728 | ||
| 750 | #ifdef CONFIG_FEATURE_TAR_GZIP | 729 | |
| 751 | if(opt & TAR_OPT_GZIP) { | 730 | if (ENABLE_FEATURE_TAR_GZIP && (opt & TAR_OPT_GZIP)) |
| 752 | get_header_ptr = get_header_tar_gz; | 731 | get_header_ptr = get_header_tar_gz; |
| 753 | } | 732 | |
| 754 | #endif | 733 | if (ENABLE_FEATURE_TAR_BZIP2 && (opt & TAR_OPT_BZIP2)) |
| 755 | #ifdef CONFIG_FEATURE_TAR_BZIP2 | ||
| 756 | if(opt & TAR_OPT_BZIP2) { | ||
| 757 | get_header_ptr = get_header_tar_bz2; | 734 | get_header_ptr = get_header_tar_bz2; |
| 758 | } | 735 | |
| 759 | #endif | 736 | if (ENABLE_FEATURE_TAR_COMPRESS && (opt & TAR_OPT_UNCOMPRESS)) |
| 760 | #ifdef CONFIG_FEATURE_TAR_COMPRESS | ||
| 761 | if(opt & TAR_OPT_UNCOMPRESS) { | ||
| 762 | get_header_ptr = get_header_tar_Z; | 737 | get_header_ptr = get_header_tar_Z; |
| 738 | |||
| 739 | if (ENABLE_FEATURE_TAR_FROM) { | ||
| 740 | tar_handle->reject = append_file_list_to_list(tar_handle->reject); | ||
| 741 | /* Append excludes to reject */ | ||
| 742 | while (excludes) { | ||
| 743 | llist_t *temp = excludes->link; | ||
| 744 | excludes->link = tar_handle->reject; | ||
| 745 | tar_handle->reject = excludes; | ||
| 746 | excludes = temp; | ||
| 747 | } | ||
| 748 | tar_handle->accept = append_file_list_to_list(tar_handle->accept); | ||
| 763 | } | 749 | } |
| 764 | #endif | ||
| 765 | #ifdef CONFIG_FEATURE_TAR_FROM | ||
| 766 | tar_handle->reject = append_file_list_to_list(tar_handle->reject); | ||
| 767 | /* Append excludes to reject */ | ||
| 768 | while(excludes) { | ||
| 769 | llist_t *temp = excludes->link; | ||
| 770 | excludes->link = tar_handle->reject; | ||
| 771 | tar_handle->reject = excludes; | ||
| 772 | excludes = temp; | ||
| 773 | } | ||
| 774 | tar_handle->accept = append_file_list_to_list(tar_handle->accept); | ||
| 775 | #endif | ||
| 776 | 750 | ||
| 777 | /* Check if we are reading from stdin */ | 751 | /* Check if we are reading from stdin */ |
| 778 | if (argv[optind] && *argv[optind] == '-') { | 752 | if (argv[optind] && *argv[optind] == '-') { |
| @@ -784,34 +758,30 @@ int tar_main(int argc, char **argv) | |||
| 784 | /* TODO: This is the same as in ar, separate function ? */ | 758 | /* TODO: This is the same as in ar, separate function ? */ |
| 785 | while (optind < argc) { | 759 | while (optind < argc) { |
| 786 | char *filename_ptr = last_char_is(argv[optind], '/'); | 760 | char *filename_ptr = last_char_is(argv[optind], '/'); |
| 787 | if (filename_ptr > argv[optind]) { | 761 | if (filename_ptr > argv[optind]) |
| 788 | *filename_ptr = '\0'; | 762 | *filename_ptr = '\0'; |
| 789 | } | 763 | |
| 790 | tar_handle->accept = llist_add_to(tar_handle->accept, argv[optind]); | 764 | tar_handle->accept = llist_add_to(tar_handle->accept, argv[optind]); |
| 791 | optind++; | 765 | optind++; |
| 792 | } | 766 | } |
| 793 | 767 | ||
| 794 | if ((tar_handle->accept) || (tar_handle->reject)) { | 768 | if ((tar_handle->accept) || (tar_handle->reject)) |
| 795 | tar_handle->filter = filter_accept_reject_list; | 769 | tar_handle->filter = filter_accept_reject_list; |
| 796 | } | ||
| 797 | 770 | ||
| 798 | /* Open the tar file */ | 771 | /* Open the tar file */ |
| 799 | { | 772 | { |
| 800 | FILE *tar_stream; | 773 | FILE *tar_stream; |
| 801 | int flags; | 774 | int flags; |
| 802 | 775 | ||
| 803 | #ifdef CONFIG_FEATURE_TAR_CREATE | 776 | if (ENABLE_FEATURE_TAR_CREATE && (opt & CTX_CREATE)) { |
| 804 | if (opt & CTX_CREATE) { | ||
| 805 | /* Make sure there is at least one file to tar up. */ | 777 | /* Make sure there is at least one file to tar up. */ |
| 806 | if (tar_handle->accept == NULL) { | 778 | if (tar_handle->accept == NULL) |
| 807 | bb_error_msg_and_die("Cowardly refusing to create an empty archive"); | 779 | bb_error_msg_and_die("empty archive"); |
| 808 | } | 780 | |
| 809 | tar_stream = stdout; | 781 | tar_stream = stdout; |
| 810 | flags = O_WRONLY | O_CREAT | O_EXCL; | 782 | flags = O_WRONLY | O_CREAT | O_EXCL; |
| 811 | unlink(tar_filename); | 783 | unlink(tar_filename); |
| 812 | } else | 784 | } else { |
| 813 | #endif | ||
| 814 | { | ||
| 815 | tar_stream = stdin; | 785 | tar_stream = stdin; |
| 816 | flags = O_RDONLY; | 786 | flags = O_RDONLY; |
| 817 | } | 787 | } |
| @@ -824,54 +794,42 @@ int tar_main(int argc, char **argv) | |||
| 824 | } | 794 | } |
| 825 | } | 795 | } |
| 826 | 796 | ||
| 827 | if ((base_dir) && (chdir(base_dir))) { | 797 | if ((base_dir) && (chdir(base_dir))) |
| 828 | bb_perror_msg_and_die("Couldnt chdir to %s", base_dir); | 798 | bb_perror_msg_and_die("Couldnt chdir to %s", base_dir); |
| 829 | } | ||
| 830 | 799 | ||
| 831 | #ifdef CONFIG_FEATURE_TAR_CREATE | ||
| 832 | /* create an archive */ | 800 | /* create an archive */ |
| 833 | if (opt & CTX_CREATE) { | 801 | if (ENABLE_FEATURE_TAR_CREATE && (opt & CTX_CREATE)) { |
| 834 | int verboseFlag = FALSE; | 802 | int verboseFlag = FALSE; |
| 835 | int zipMode = 0; | 803 | int zipMode = 0; |
| 836 | 804 | ||
| 837 | # ifdef CONFIG_FEATURE_TAR_GZIP | 805 | if (ENABLE_FEATURE_TAR_GZIP && get_header_ptr == get_header_tar_gz) |
| 838 | if (get_header_ptr == get_header_tar_gz) { | 806 | zipMode = 1; |
| 839 | zipMode = 1; | 807 | if (ENABLE_FEATURE_TAR_BZIP2 && get_header_ptr == get_header_tar_bz2) |
| 840 | } | 808 | zipMode = 2; |
| 841 | # endif /* CONFIG_FEATURE_TAR_GZIP */ | ||
| 842 | # ifdef CONFIG_FEATURE_TAR_BZIP2 | ||
| 843 | if (get_header_ptr == get_header_tar_bz2) { | ||
| 844 | zipMode = 2; | ||
| 845 | } | ||
| 846 | # endif /* CONFIG_FEATURE_TAR_BZIP2 */ | ||
| 847 | 809 | ||
| 848 | if ((tar_handle->action_header == header_list) || | 810 | if ((tar_handle->action_header == header_list) || |
| 849 | (tar_handle->action_header == header_verbose_list)) { | 811 | (tar_handle->action_header == header_verbose_list)) |
| 812 | { | ||
| 850 | verboseFlag = TRUE; | 813 | verboseFlag = TRUE; |
| 851 | } | 814 | } |
| 852 | writeTarFile(tar_handle->src_fd, verboseFlag, opt & TAR_OPT_DEREFERNCE, tar_handle->accept, | 815 | writeTarFile(tar_handle->src_fd, verboseFlag, opt & TAR_OPT_DEREFERNCE, tar_handle->accept, |
| 853 | tar_handle->reject, zipMode); | 816 | tar_handle->reject, zipMode); |
| 854 | } else | 817 | } else { |
| 855 | #endif /* CONFIG_FEATURE_TAR_CREATE */ | ||
| 856 | { | ||
| 857 | while (get_header_ptr(tar_handle) == EXIT_SUCCESS); | 818 | while (get_header_ptr(tar_handle) == EXIT_SUCCESS); |
| 858 | 819 | ||
| 859 | /* Ckeck that every file that should have been extracted was */ | 820 | /* Check that every file that should have been extracted was */ |
| 860 | while (tar_handle->accept) { | 821 | while (tar_handle->accept) { |
| 861 | if (find_list_entry(tar_handle->reject, tar_handle->accept->data) == NULL) { | 822 | if (!find_list_entry(tar_handle->reject, tar_handle->accept->data) |
| 862 | if (find_list_entry(tar_handle->passed, tar_handle->accept->data) == NULL) { | 823 | && !find_list_entry(tar_handle->passed, tar_handle->accept->data)) |
| 863 | bb_error_msg_and_die("%s: Not found in archive", tar_handle->accept->data); | 824 | { |
| 864 | } | 825 | bb_error_msg_and_die("%s: Not found in archive", tar_handle->accept->data); |
| 865 | } | 826 | } |
| 866 | tar_handle->accept = tar_handle->accept->link; | 827 | tar_handle->accept = tar_handle->accept->link; |
| 867 | } | 828 | } |
| 868 | } | 829 | } |
| 869 | 830 | ||
| 870 | #ifdef CONFIG_FEATURE_CLEAN_UP | 831 | if (ENABLE_FEATURE_CLEAN_UP && tar_handle->src_fd != STDIN_FILENO) |
| 871 | if (tar_handle->src_fd != STDIN_FILENO) { | ||
| 872 | close(tar_handle->src_fd); | 832 | close(tar_handle->src_fd); |
| 873 | } | ||
| 874 | #endif /* CONFIG_FEATURE_CLEAN_UP */ | ||
| 875 | 833 | ||
| 876 | return(EXIT_SUCCESS); | 834 | return(EXIT_SUCCESS); |
| 877 | } | 835 | } |
