diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2001-10-13 05:03:29 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2001-10-13 05:03:29 +0000 |
commit | 0e766183ee0d9108c4f803a5e66372fc9c2c119d (patch) | |
tree | d29958f6c596dfd76fd69e97164378fa9dbe550c | |
parent | f86bbfaddd5803a173d51e41f18af9310f7a612c (diff) | |
download | busybox-w32-0e766183ee0d9108c4f803a5e66372fc9c2c119d.tar.gz busybox-w32-0e766183ee0d9108c4f803a5e66372fc9c2c119d.tar.bz2 busybox-w32-0e766183ee0d9108c4f803a5e66372fc9c2c119d.zip |
Superficial changes to make the exclude/include code easier to understand
-rw-r--r-- | archival/tar.c | 56 | ||||
-rw-r--r-- | tar.c | 56 |
2 files changed, 68 insertions, 44 deletions
diff --git a/archival/tar.c b/archival/tar.c index ea0247e56..bb66d40cc 100644 --- a/archival/tar.c +++ b/archival/tar.c | |||
@@ -513,25 +513,30 @@ void append_file_to_list(char *filename, char ***name_list, int *num_of_entries) | |||
513 | if (line_ptr) { | 513 | if (line_ptr) { |
514 | *line_ptr = '\0'; | 514 | *line_ptr = '\0'; |
515 | } | 515 | } |
516 | *name_list = realloc(*name_list, sizeof(char *) * (*num_of_entries + 1)); | 516 | *name_list = realloc(*name_list, sizeof(char *) * (*num_of_entries + 2)); |
517 | (*name_list)[*num_of_entries] = xstrdup(line); | 517 | (*name_list)[*num_of_entries] = xstrdup(line); |
518 | (*num_of_entries)++; | 518 | (*num_of_entries)++; |
519 | free(line); | 519 | free(line); |
520 | } | 520 | } |
521 | fclose(src_stream); | 521 | fclose(src_stream); |
522 | (*name_list)[*num_of_entries] = NULL; | ||
522 | } | 523 | } |
523 | 524 | ||
524 | #ifdef BB_FEATURE_TAR_EXCLUDE | 525 | #ifdef BB_FEATURE_TAR_EXCLUDE |
525 | char **merge_list(char **include_list, char **exclude_list) | 526 | /* |
527 | * Create a list of names that are in the include list AND NOT in the exclude lists | ||
528 | */ | ||
529 | char **list_and_not_list(char **include_list, char **exclude_list) | ||
526 | { | 530 | { |
527 | char **new_include_list = NULL; | 531 | char **new_include_list = NULL; |
528 | int new_include_count = 0; | 532 | int new_include_count = 0; |
529 | int include_count = 0; | 533 | int include_count = 0; |
530 | int exclude_count; | 534 | int exclude_count; |
531 | 535 | ||
532 | if (include_list == NULL) | 536 | if (include_list == NULL) { |
533 | return exclude_list; | 537 | return(NULL); |
534 | 538 | } | |
539 | |||
535 | while (include_list[include_count] != NULL) { | 540 | while (include_list[include_count] != NULL) { |
536 | int found = FALSE; | 541 | int found = FALSE; |
537 | exclude_count = 0; | 542 | exclude_count = 0; |
@@ -547,12 +552,12 @@ char **merge_list(char **include_list, char **exclude_list) | |||
547 | new_include_list = realloc(new_include_list, sizeof(char *) * (include_count + 2)); | 552 | new_include_list = realloc(new_include_list, sizeof(char *) * (include_count + 2)); |
548 | new_include_list[new_include_count] = include_list[include_count]; | 553 | new_include_list[new_include_count] = include_list[include_count]; |
549 | new_include_count++; | 554 | new_include_count++; |
550 | new_include_list[new_include_count] = NULL; | ||
551 | } else { | 555 | } else { |
552 | free(include_list[include_count]); | 556 | free(include_list[include_count]); |
553 | } | 557 | } |
554 | include_count++; | 558 | include_count++; |
555 | } | 559 | } |
560 | new_include_list[new_include_count] = NULL; | ||
556 | return(new_include_list); | 561 | return(new_include_list); |
557 | } | 562 | } |
558 | #endif | 563 | #endif |
@@ -572,12 +577,13 @@ int tar_main(int argc, char **argv) | |||
572 | 577 | ||
573 | #ifdef BB_FEATURE_TAR_EXCLUDE | 578 | #ifdef BB_FEATURE_TAR_EXCLUDE |
574 | char **exclude_list = NULL; | 579 | char **exclude_list = NULL; |
575 | int exclude_count = 0; | 580 | int exclude_list_count = 0; |
576 | #endif | 581 | #endif |
577 | 582 | ||
578 | FILE *src_stream = NULL; | 583 | FILE *src_stream = NULL; |
579 | FILE *uncompressed_stream = NULL; | 584 | FILE *uncompressed_stream = NULL; |
580 | char **include_list = NULL; | 585 | char **include_list = NULL; |
586 | char **the_real_list = NULL; | ||
581 | char *src_filename = NULL; | 587 | char *src_filename = NULL; |
582 | char *dst_prefix = NULL; | 588 | char *dst_prefix = NULL; |
583 | char *file_list_name = NULL; | 589 | char *file_list_name = NULL; |
@@ -585,7 +591,7 @@ int tar_main(int argc, char **argv) | |||
585 | unsigned short untar_funct = 0; | 591 | unsigned short untar_funct = 0; |
586 | unsigned short untar_funct_required = 0; | 592 | unsigned short untar_funct_required = 0; |
587 | unsigned short extract_function = 0; | 593 | unsigned short extract_function = 0; |
588 | int include_count = 0; | 594 | int include_list_count = 0; |
589 | int gunzip_pid; | 595 | int gunzip_pid; |
590 | int gz_fd = 0; | 596 | int gz_fd = 0; |
591 | 597 | ||
@@ -621,13 +627,13 @@ int tar_main(int argc, char **argv) | |||
621 | /* Exclude or Include files listed in <filename>*/ | 627 | /* Exclude or Include files listed in <filename>*/ |
622 | #ifdef BB_FEATURE_TAR_EXCLUDE | 628 | #ifdef BB_FEATURE_TAR_EXCLUDE |
623 | case 'X': | 629 | case 'X': |
624 | append_file_to_list(optarg, &exclude_list, &exclude_count); | 630 | append_file_to_list(optarg, &exclude_list, &exclude_list_count); |
625 | exclude_list[exclude_count] = NULL; | 631 | exclude_list[exclude_list_count] = NULL; |
626 | break; | 632 | break; |
627 | #endif | 633 | #endif |
628 | case 'T': | 634 | case 'T': |
629 | // by default a list is an include list | 635 | // by default a list is an include list |
630 | append_file_to_list(optarg, &include_list, &include_count); | 636 | append_file_to_list(optarg, &include_list, &include_list_count); |
631 | break; | 637 | break; |
632 | 638 | ||
633 | case 'C': // Change to dir <optarg> | 639 | case 'C': // Change to dir <optarg> |
@@ -677,23 +683,29 @@ int tar_main(int argc, char **argv) | |||
677 | 683 | ||
678 | /* Setup an array of filenames to work with */ | 684 | /* Setup an array of filenames to work with */ |
679 | while (optind < argc) { | 685 | while (optind < argc) { |
680 | include_list = realloc(include_list, sizeof(char *) * (include_count + 2)); | 686 | include_list = realloc(include_list, sizeof(char *) * (include_list_count + 2)); |
681 | include_list[include_count] = xstrdup(argv[optind]); | 687 | include_list[include_list_count] = xstrdup(argv[optind]); |
682 | include_count++; | 688 | include_list_count++; |
683 | optind++; | 689 | optind++; |
684 | include_list[include_count] = NULL; | 690 | include_list[include_list_count] = NULL; |
685 | } | 691 | } |
686 | 692 | ||
693 | /* By default the include list is the list we act on */ | ||
694 | the_real_list = include_list; | ||
695 | |||
687 | #ifdef BB_FEATURE_TAR_EXCLUDE | 696 | #ifdef BB_FEATURE_TAR_EXCLUDE |
688 | /* Remove excluded files from the include list */ | ||
689 | if (exclude_list != NULL) { | 697 | if (exclude_list != NULL) { |
690 | /* If both an exclude and include file list were present then | ||
691 | * it's an exclude from include list only, if not it's really an | ||
692 | * exclude list (and a poor choice of variable names) */ | ||
693 | if (include_list == NULL) { | 698 | if (include_list == NULL) { |
699 | /* the_real_list is an exclude list */ | ||
694 | extract_function |= extract_exclude_list; | 700 | extract_function |= extract_exclude_list; |
701 | the_real_list = exclude_list; | ||
702 | } else { | ||
703 | /* Both an exclude and include file list was present, | ||
704 | * its an exclude from include list only. | ||
705 | * Remove excluded files from the include list | ||
706 | */ | ||
707 | the_real_list = list_and_not_list(include_list, exclude_list); | ||
695 | } | 708 | } |
696 | include_list = merge_list(include_list, exclude_list); | ||
697 | } | 709 | } |
698 | #endif | 710 | #endif |
699 | 711 | ||
@@ -717,7 +729,7 @@ int tar_main(int argc, char **argv) | |||
717 | uncompressed_stream = src_stream; | 729 | uncompressed_stream = src_stream; |
718 | 730 | ||
719 | /* extract or list archive */ | 731 | /* extract or list archive */ |
720 | unarchive(uncompressed_stream, stdout, &get_header_tar, extract_function, dst_prefix, include_list); | 732 | unarchive(uncompressed_stream, stdout, &get_header_tar, extract_function, dst_prefix, the_real_list); |
721 | fclose(uncompressed_stream); | 733 | fclose(uncompressed_stream); |
722 | } | 734 | } |
723 | #ifdef BB_FEATURE_TAR_CREATE | 735 | #ifdef BB_FEATURE_TAR_CREATE |
@@ -733,7 +745,7 @@ int tar_main(int argc, char **argv) | |||
733 | if (extract_function & extract_verbose_list) { | 745 | if (extract_function & extract_verbose_list) { |
734 | verboseFlag = TRUE; | 746 | verboseFlag = TRUE; |
735 | } | 747 | } |
736 | writeTarFile(src_filename, verboseFlag, &argv[argc - 1], include_list); | 748 | writeTarFile(src_filename, verboseFlag, &argv[argc - 1], the_real_list); |
737 | } | 749 | } |
738 | #endif // BB_FEATURE_TAR_CREATE | 750 | #endif // BB_FEATURE_TAR_CREATE |
739 | 751 | ||
@@ -513,25 +513,30 @@ void append_file_to_list(char *filename, char ***name_list, int *num_of_entries) | |||
513 | if (line_ptr) { | 513 | if (line_ptr) { |
514 | *line_ptr = '\0'; | 514 | *line_ptr = '\0'; |
515 | } | 515 | } |
516 | *name_list = realloc(*name_list, sizeof(char *) * (*num_of_entries + 1)); | 516 | *name_list = realloc(*name_list, sizeof(char *) * (*num_of_entries + 2)); |
517 | (*name_list)[*num_of_entries] = xstrdup(line); | 517 | (*name_list)[*num_of_entries] = xstrdup(line); |
518 | (*num_of_entries)++; | 518 | (*num_of_entries)++; |
519 | free(line); | 519 | free(line); |
520 | } | 520 | } |
521 | fclose(src_stream); | 521 | fclose(src_stream); |
522 | (*name_list)[*num_of_entries] = NULL; | ||
522 | } | 523 | } |
523 | 524 | ||
524 | #ifdef BB_FEATURE_TAR_EXCLUDE | 525 | #ifdef BB_FEATURE_TAR_EXCLUDE |
525 | char **merge_list(char **include_list, char **exclude_list) | 526 | /* |
527 | * Create a list of names that are in the include list AND NOT in the exclude lists | ||
528 | */ | ||
529 | char **list_and_not_list(char **include_list, char **exclude_list) | ||
526 | { | 530 | { |
527 | char **new_include_list = NULL; | 531 | char **new_include_list = NULL; |
528 | int new_include_count = 0; | 532 | int new_include_count = 0; |
529 | int include_count = 0; | 533 | int include_count = 0; |
530 | int exclude_count; | 534 | int exclude_count; |
531 | 535 | ||
532 | if (include_list == NULL) | 536 | if (include_list == NULL) { |
533 | return exclude_list; | 537 | return(NULL); |
534 | 538 | } | |
539 | |||
535 | while (include_list[include_count] != NULL) { | 540 | while (include_list[include_count] != NULL) { |
536 | int found = FALSE; | 541 | int found = FALSE; |
537 | exclude_count = 0; | 542 | exclude_count = 0; |
@@ -547,12 +552,12 @@ char **merge_list(char **include_list, char **exclude_list) | |||
547 | new_include_list = realloc(new_include_list, sizeof(char *) * (include_count + 2)); | 552 | new_include_list = realloc(new_include_list, sizeof(char *) * (include_count + 2)); |
548 | new_include_list[new_include_count] = include_list[include_count]; | 553 | new_include_list[new_include_count] = include_list[include_count]; |
549 | new_include_count++; | 554 | new_include_count++; |
550 | new_include_list[new_include_count] = NULL; | ||
551 | } else { | 555 | } else { |
552 | free(include_list[include_count]); | 556 | free(include_list[include_count]); |
553 | } | 557 | } |
554 | include_count++; | 558 | include_count++; |
555 | } | 559 | } |
560 | new_include_list[new_include_count] = NULL; | ||
556 | return(new_include_list); | 561 | return(new_include_list); |
557 | } | 562 | } |
558 | #endif | 563 | #endif |
@@ -572,12 +577,13 @@ int tar_main(int argc, char **argv) | |||
572 | 577 | ||
573 | #ifdef BB_FEATURE_TAR_EXCLUDE | 578 | #ifdef BB_FEATURE_TAR_EXCLUDE |
574 | char **exclude_list = NULL; | 579 | char **exclude_list = NULL; |
575 | int exclude_count = 0; | 580 | int exclude_list_count = 0; |
576 | #endif | 581 | #endif |
577 | 582 | ||
578 | FILE *src_stream = NULL; | 583 | FILE *src_stream = NULL; |
579 | FILE *uncompressed_stream = NULL; | 584 | FILE *uncompressed_stream = NULL; |
580 | char **include_list = NULL; | 585 | char **include_list = NULL; |
586 | char **the_real_list = NULL; | ||
581 | char *src_filename = NULL; | 587 | char *src_filename = NULL; |
582 | char *dst_prefix = NULL; | 588 | char *dst_prefix = NULL; |
583 | char *file_list_name = NULL; | 589 | char *file_list_name = NULL; |
@@ -585,7 +591,7 @@ int tar_main(int argc, char **argv) | |||
585 | unsigned short untar_funct = 0; | 591 | unsigned short untar_funct = 0; |
586 | unsigned short untar_funct_required = 0; | 592 | unsigned short untar_funct_required = 0; |
587 | unsigned short extract_function = 0; | 593 | unsigned short extract_function = 0; |
588 | int include_count = 0; | 594 | int include_list_count = 0; |
589 | int gunzip_pid; | 595 | int gunzip_pid; |
590 | int gz_fd = 0; | 596 | int gz_fd = 0; |
591 | 597 | ||
@@ -621,13 +627,13 @@ int tar_main(int argc, char **argv) | |||
621 | /* Exclude or Include files listed in <filename>*/ | 627 | /* Exclude or Include files listed in <filename>*/ |
622 | #ifdef BB_FEATURE_TAR_EXCLUDE | 628 | #ifdef BB_FEATURE_TAR_EXCLUDE |
623 | case 'X': | 629 | case 'X': |
624 | append_file_to_list(optarg, &exclude_list, &exclude_count); | 630 | append_file_to_list(optarg, &exclude_list, &exclude_list_count); |
625 | exclude_list[exclude_count] = NULL; | 631 | exclude_list[exclude_list_count] = NULL; |
626 | break; | 632 | break; |
627 | #endif | 633 | #endif |
628 | case 'T': | 634 | case 'T': |
629 | // by default a list is an include list | 635 | // by default a list is an include list |
630 | append_file_to_list(optarg, &include_list, &include_count); | 636 | append_file_to_list(optarg, &include_list, &include_list_count); |
631 | break; | 637 | break; |
632 | 638 | ||
633 | case 'C': // Change to dir <optarg> | 639 | case 'C': // Change to dir <optarg> |
@@ -677,23 +683,29 @@ int tar_main(int argc, char **argv) | |||
677 | 683 | ||
678 | /* Setup an array of filenames to work with */ | 684 | /* Setup an array of filenames to work with */ |
679 | while (optind < argc) { | 685 | while (optind < argc) { |
680 | include_list = realloc(include_list, sizeof(char *) * (include_count + 2)); | 686 | include_list = realloc(include_list, sizeof(char *) * (include_list_count + 2)); |
681 | include_list[include_count] = xstrdup(argv[optind]); | 687 | include_list[include_list_count] = xstrdup(argv[optind]); |
682 | include_count++; | 688 | include_list_count++; |
683 | optind++; | 689 | optind++; |
684 | include_list[include_count] = NULL; | 690 | include_list[include_list_count] = NULL; |
685 | } | 691 | } |
686 | 692 | ||
693 | /* By default the include list is the list we act on */ | ||
694 | the_real_list = include_list; | ||
695 | |||
687 | #ifdef BB_FEATURE_TAR_EXCLUDE | 696 | #ifdef BB_FEATURE_TAR_EXCLUDE |
688 | /* Remove excluded files from the include list */ | ||
689 | if (exclude_list != NULL) { | 697 | if (exclude_list != NULL) { |
690 | /* If both an exclude and include file list were present then | ||
691 | * it's an exclude from include list only, if not it's really an | ||
692 | * exclude list (and a poor choice of variable names) */ | ||
693 | if (include_list == NULL) { | 698 | if (include_list == NULL) { |
699 | /* the_real_list is an exclude list */ | ||
694 | extract_function |= extract_exclude_list; | 700 | extract_function |= extract_exclude_list; |
701 | the_real_list = exclude_list; | ||
702 | } else { | ||
703 | /* Both an exclude and include file list was present, | ||
704 | * its an exclude from include list only. | ||
705 | * Remove excluded files from the include list | ||
706 | */ | ||
707 | the_real_list = list_and_not_list(include_list, exclude_list); | ||
695 | } | 708 | } |
696 | include_list = merge_list(include_list, exclude_list); | ||
697 | } | 709 | } |
698 | #endif | 710 | #endif |
699 | 711 | ||
@@ -717,7 +729,7 @@ int tar_main(int argc, char **argv) | |||
717 | uncompressed_stream = src_stream; | 729 | uncompressed_stream = src_stream; |
718 | 730 | ||
719 | /* extract or list archive */ | 731 | /* extract or list archive */ |
720 | unarchive(uncompressed_stream, stdout, &get_header_tar, extract_function, dst_prefix, include_list); | 732 | unarchive(uncompressed_stream, stdout, &get_header_tar, extract_function, dst_prefix, the_real_list); |
721 | fclose(uncompressed_stream); | 733 | fclose(uncompressed_stream); |
722 | } | 734 | } |
723 | #ifdef BB_FEATURE_TAR_CREATE | 735 | #ifdef BB_FEATURE_TAR_CREATE |
@@ -733,7 +745,7 @@ int tar_main(int argc, char **argv) | |||
733 | if (extract_function & extract_verbose_list) { | 745 | if (extract_function & extract_verbose_list) { |
734 | verboseFlag = TRUE; | 746 | verboseFlag = TRUE; |
735 | } | 747 | } |
736 | writeTarFile(src_filename, verboseFlag, &argv[argc - 1], include_list); | 748 | writeTarFile(src_filename, verboseFlag, &argv[argc - 1], the_real_list); |
737 | } | 749 | } |
738 | #endif // BB_FEATURE_TAR_CREATE | 750 | #endif // BB_FEATURE_TAR_CREATE |
739 | 751 | ||