Fix #define syntax
[oweals/busybox.git] / archival / tar.c
index 88a815de65bdd4aa63de6c7ca50b3db9ca2543fb..48d6ce22e81741d2e80ea12dbc2e2b54857dfd2e 100644 (file)
@@ -302,7 +302,7 @@ static inline int writeTarHeader(struct TarBallInfo *tbInfo,
        if ((size =
                 full_write(tbInfo->tarFd, (char *) &header,
                                        sizeof(struct TarHeader))) < 0) {
-               error_msg(io_error, real_name, strerror(errno));
+               error_msg(io_error, real_name);
                return (FALSE);
        }
        /* Pad the header up to the tar block size */
@@ -426,7 +426,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
 
                /* open the file we want to archive, and make sure all is well */
                if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
-                       error_msg("%s: Cannot open: %s", fileName, strerror(errno));
+                       perror_msg("%s: Cannot open", fileName);
                        return (FALSE);
                }
 
@@ -434,13 +434,13 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
                while ((size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0) {
                        if (full_write(tbInfo->tarFd, buffer, size) != size) {
                                /* Output file seems to have a problem */
-                               error_msg(io_error, fileName, strerror(errno));
+                               error_msg(io_error, fileName);
                                return (FALSE);
                        }
                        readSize += size;
                }
                if (size == -1) {
-                       error_msg(io_error, fileName, strerror(errno));
+                       error_msg(io_error, fileName);
                        return (FALSE);
                }
                /* Pad the file up to the tar block size */
@@ -474,7 +474,7 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
        }
 
        /* Open the tar file for writing.  */
-       if (tarName == NULL) {
+       if (tarName == NULL || (tarName[0] == '-' && tarName[1] == '\0')) {
                tbInfo.tarFd = fileno(stdout);
                tbInfo.verboseFlag = verboseFlag ? 2 : 0;
        } else {
@@ -483,7 +483,7 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
        }
 
        if (tbInfo.tarFd < 0) {
-               perror_msg("Error opening '%s'", tarName);
+               perror_msg("%s: Cannot open", tarName);
                freeHardLinkInfo(&tbInfo.hlInfoHead);
                return (FALSE);
        }
@@ -491,7 +491,7 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
        /* Store the stat info for the tarball's file, so
         * can avoid including the tarball into itself....  */
        if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
-               error_msg_and_die(io_error, tarName, strerror(errno));
+               error_msg_and_die(io_error, tarName);
 
 #ifdef CONFIG_FEATURE_TAR_GZIP
        if (gzip) {
@@ -586,14 +586,10 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
 static const llist_t *append_file_list_to_list(const char *filename, const llist_t *list)
 {
        FILE *src_stream = xfopen(filename, "r");
-       while(1) {
-               char *line = get_line_from_file(src_stream);
-               if (line == NULL) {
-                       break;
-               }
+       char *line;
+       while((line = get_line_from_file(src_stream)) != NULL) {
                chomp(line);
                list = add_to_list(list, line);
-               free(line);
        }
        fclose(src_stream);
 
@@ -609,19 +605,26 @@ int tar_main(int argc, char **argv)
        archive_handle_t *tar_handle;
        int opt;
        char *base_dir = NULL;
+       char *tar_filename = "-";
 
 #ifdef CONFIG_FEATURE_TAR_CREATE
-       char *src_filename = NULL;
        unsigned char tar_create = FALSE;
 #endif
 
+       /* Prepend '-' to the first argument if required */
+       if (argv[1][0] != '-') {
+               char *tmp = xmalloc(strlen(argv[1]) + 2);
+               tmp[0] = '-';
+               strcpy(tmp + 1, argv[1]);
+               argv[1] = tmp;
+       }
+
        if (argc < 2) {
                show_usage();
        }
 
        /* Initialise default values */
        tar_handle = init_handle();
-       tar_handle->src_fd = fileno(stdin);
        tar_handle->flags = ARCHIVE_CREATE_LEADING_DIRS;
 
        while ((opt = getopt(argc, argv, "ctxT:X:C:f:Opvz")) != -1) {
@@ -659,10 +662,7 @@ int tar_main(int argc, char **argv)
                        base_dir = optarg;
                        break;
                case 'f':               /* archive filename */
-#ifdef CONFIG_FEATURE_TAR_CREATE
-                       src_filename = optarg;
-#endif
-                       tar_handle->src_fd = xopen(optarg, O_RDONLY);
+                       tar_filename = optarg;
                        break;
                case 'O':               /* To stdout */
                        tar_handle->action_data = data_extract_to_stdout;
@@ -694,29 +694,32 @@ int tar_main(int argc, char **argv)
                }
        }
 
-       if (*argv[optind] == '-') {
+       /* Check if we are reading from stdin */
+       if ((argv[optind]) && (*argv[optind] == '-')) {
+               /* Default is to read from stdin, so just skip to next arg */
                optind++;
        }
 
        /* Setup an array of filenames to work with */
        /* TODO: This is the same as in ar, seperate function ? */
        while (optind < argc) {
+#if 0
                char absolute_path[PATH_MAX];
-
                realpath(argv[optind], absolute_path);
                tar_handle->accept = add_to_list(tar_handle->accept, absolute_path);
-               optind++;
-#ifdef CONFIG_FEATURE_TAR_EXCLUDE
-               if (tar_handle->reject) {
-                       tar_handle->filter = filter_accept_reject_list;
-               } else
 #endif
-                       tar_handle->filter = filter_accept_list;
-               }
+               tar_handle->accept = add_to_list(tar_handle->accept, argv[optind]);
+               optind++;
+
+       }
+
+       if ((tar_handle->accept) || (tar_handle->reject)) {
+               tar_handle->filter = filter_accept_reject_list;
+       }
 
        if ((base_dir) && (chdir(base_dir))) {
                perror_msg_and_die("Couldnt chdir");
-               }
+       }
 
 #ifdef CONFIG_FEATURE_TAR_CREATE
        /* create an archive */
@@ -728,26 +731,45 @@ int tar_main(int argc, char **argv)
                if (get_header_ptr == get_header_tar_gz) {
                        gzipFlag = TRUE;
                }
-# endif
+# endif /* CONFIG_FEATURE_TAR_GZIP */
+
                if (tar_handle->action_header == header_verbose_list) {
                        verboseFlag = TRUE;
-       }
-               writeTarFile(src_filename, verboseFlag, tar_handle->accept,
+               }
+               writeTarFile(tar_filename, verboseFlag, tar_handle->accept,
                        tar_handle->reject, gzipFlag);
        } else 
-#endif
+#endif /* CONFIG_FEATURE_TAR_CREATE */
+       {
+               if ((tar_filename[0] == '-') && (tar_filename[1] == '\0')) {
+                       tar_handle->src_fd = fileno(stdin);
+               } else {
+                       tar_handle->src_fd = xopen(tar_filename, O_RDONLY);
+               }
 #ifdef CONFIG_FEATURE_TAR_GZIP
                if (get_header_ptr == get_header_tar_gz) {
                        get_header_tar_gz(tar_handle);
                } else
-#endif
+#endif /* CONFIG_FEATURE_TAR_CREATE */
+
                        while (get_header_tar(tar_handle) == EXIT_SUCCESS);
 
+               /* Ckeck that every file that should have been extracted was */
+               while (tar_handle->accept) {
+                       if (find_list_entry(tar_handle->reject, tar_handle->accept->data) == NULL) {
+                               if (find_list_entry(tar_handle->passed, tar_handle->accept->data) == NULL) {
+                                       error_msg_and_die("%s: Not found in archive\n", tar_handle->accept->data);
+                               }
+                       }
+                       tar_handle->accept = tar_handle->accept->link;
+               }
+       }
+
 #ifdef CONFIG_FEATURE_CLEAN_UP
        if (tar_handle->src_fd != fileno(stdin)) {
                close(tar_handle->src_fd);
        }
-#endif
+#endif /* CONFIG_FEATURE_CLEAN_UP */
 
        return(EXIT_SUCCESS);
 }