- patch from Denis Vlasenko to add and use bb_xopen3()
[oweals/busybox.git] / archival / tar.c
index 4f02e9006c369b8af6ab4e625765fdb16bfaba88..dcc2f163e4a695f6298f5da0e867048272764f43 100644 (file)
@@ -19,9 +19,8 @@
  *
  * Based in part on the tar implementation from busybox-0.28
  *  Copyright (C) 1995 Bruce Perens
- *  This is free software under the GNU General Public License.
  *
- * Licensed under GPL v2 (or later), see file LICENSE in this tarball.
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 #include <fcntl.h>
@@ -44,7 +43,7 @@
 
 /* Tar file constants  */
 # define TAR_MAGIC          "ustar"    /* ustar and a null */
-# define TAR_VERSION        "  "       /* Be compatable with GNU tar format */
+# define TAR_VERSION        "  "       /* Be compatible with GNU tar format */
 
 #define TAR_BLOCK_SIZE         512
 #define TAR_MAGIC_LEN          6
@@ -91,7 +90,7 @@ struct HardLinkInfo {
 struct TarBallInfo {
        char *fileName;                 /* File name of the tarball */
        int tarFd;                              /* Open-for-write file descriptor
-                                                          for the tarball */
+                                                          for the tarball */
        struct stat statBuf;    /* Stat info for the tarball, letting
                                                           us know the inode and device that the
                                                           tarball lives, so we can avoid trying
@@ -322,6 +321,8 @@ static inline int exclude_file(const llist_t *excluded_files, const char *file)
 
        return 0;
 }
+# else
+#define exclude_file(excluded_files, file) 0
 # endif
 
 static int writeFileToTarball(const char *fileName, struct stat *statbuf,
@@ -329,6 +330,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
 {
        struct TarBallInfo *tbInfo = (struct TarBallInfo *) userData;
        const char *header_name;
+       int inputFileFd = -1;
 
        /*
           ** Check to see if we are dealing with a hard link.
@@ -383,30 +385,32 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
                return SKIP;
        }
 
-       if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) {
-               return (FALSE);
-       }
-
-       /* Now, if the file is a regular file, copy it out to the tarball */
-       if ((tbInfo->hlInfo == NULL)
-               && (S_ISREG(statbuf->st_mode))) {
-               int inputFileFd;
-               ssize_t readSize = 0;
+       /* Is this a regular file? */
+       if ((tbInfo->hlInfo == NULL) && (S_ISREG(statbuf->st_mode))) {
 
                /* open the file we want to archive, and make sure all is well */
                if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
                        bb_perror_msg("%s: Cannot open", fileName);
                        return (FALSE);
                }
+       }
+
+       /* Add an entry to the tarball */
+       if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) {
+               return (FALSE);
+       }
+
+       /* If it was a regular file, write out the body */
+       if (inputFileFd >= 0 ) {
+               ssize_t readSize = 0;
 
                /* write the file to the archive */
                readSize = bb_copyfd_eof(inputFileFd, tbInfo->tarFd);
+               close(inputFileFd);
 
                /* Pad the file up to the tar block size */
                for (; (readSize % TAR_BLOCK_SIZE) != 0; readSize++)
                        write(tbInfo->tarFd, "\0", 1);
-
-               close(inputFileFd);
        }
 
        return (TRUE);
@@ -449,6 +453,7 @@ static inline int writeTarFile(const int tar_fd, const int verboseFlag,
                        /* Avoid vfork clobbering */
                        (void) &include;
                        (void) &errorFlag;
+                       (void) &zip_exec;
 # endif
 
                gzipPid = vfork();
@@ -461,9 +466,9 @@ static inline int writeTarFile(const int tar_fd, const int verboseFlag,
                                dup2(tbInfo.tarFd, 1);
 
                        close(gzipStatusPipe[0]);
-                       fcntl(gzipStatusPipe[1], F_SETFD, FD_CLOEXEC);  /* close on exec shows sucess */
+                       fcntl(gzipStatusPipe[1], F_SETFD, FD_CLOEXEC);  /* close on exec shows success */
 
-                       execlp(zip_exec, zip_exec, "-f", 0);
+                       execlp(zip_exec, zip_exec, "-f", NULL);
                        vfork_exec_errno = errno;
 
                        close(gzipStatusPipe[1]);
@@ -479,7 +484,7 @@ static inline int writeTarFile(const int tar_fd, const int verboseFlag,
 
                                if (n == 0 && vfork_exec_errno != 0) {
                                        errno = vfork_exec_errno;
-                                       bb_perror_msg_and_die("Could not exec %s",zip_exec);
+                                       bb_perror_msg_and_die("Could not exec %s", zip_exec);
                                } else if ((n < 0) && (errno == EAGAIN || errno == EINTR))
                                        continue;       /* try it again */
                                break;
@@ -510,11 +515,12 @@ static inline int writeTarFile(const int tar_fd, const int verboseFlag,
         * but that isn't necessary for GNU tar interoperability, and
         * so is considered a waste of space */
 
+       /* Close so the child process (if any) will exit */
+       close(tbInfo.tarFd);
+
        /* Hang up the tools, close up shop, head home */
-       if (ENABLE_FEATURE_CLEAN_UP) {
-               close(tbInfo.tarFd);
+       if (ENABLE_FEATURE_CLEAN_UP)
                freeHardLinkInfo(&tbInfo.hlInfoHead);
-       }
 
        if (errorFlag)
                bb_error_msg("Error exit delayed from previous errors");
@@ -562,7 +568,7 @@ static char get_header_tar_Z(archive_handle_t *archive_handle)
 
        /* do the decompression, and cleanup */
        if (bb_xread_char(archive_handle->src_fd) != 0x1f ||
-               bb_xread_char(archive_handle->src_fd) != 0x9d)
+               bb_xread_char(archive_handle->src_fd) != 0x9d)
        {
                bb_error_msg_and_die("Invalid magic");
        }
@@ -578,66 +584,86 @@ static char get_header_tar_Z(archive_handle_t *archive_handle)
 #define get_header_tar_Z       0
 #endif
 
-#define CTX_TEST                               (1 << 0)
-#define CTX_EXTRACT                            (1 << 1)
-#define TAR_OPT_BASEDIR                        (1 << 2)
-#define TAR_OPT_TARNAME                        (1 << 3)
-#define TAR_OPT_2STDOUT                        (1 << 4)
-#define TAR_OPT_P                              (1 << 5)
-#define TAR_OPT_VERBOSE                        (1 << 6)
-#define TAR_OPT_KEEP_OLD               (1 << 7)
-
-# define CTX_CREATE                            (1 << 8)
-# define TAR_OPT_DEREFERNCE            (1 << 9)
+#define CTX_TEST                          (1 << 0)
+#define CTX_EXTRACT                       (1 << 1)
+#define TAR_OPT_BASEDIR                   (1 << 2)
+#define TAR_OPT_TARNAME                   (1 << 3)
+#define TAR_OPT_2STDOUT                   (1 << 4)
+#define TAR_OPT_P                         (1 << 5)
+#define TAR_OPT_VERBOSE                   (1 << 6)
+#define TAR_OPT_KEEP_OLD                  (1 << 7)
+
+#define TAR_OPT_AFTER_START               8
+
+#define CTX_CREATE                        (1 << (TAR_OPT_AFTER_START))
+#define TAR_OPT_DEREFERNCE                (1 << (TAR_OPT_AFTER_START + 1))
 #ifdef CONFIG_FEATURE_TAR_CREATE
-# define TAR_OPT_STR_CREATE            "ch"
-# define TAR_OPT_FLAG_CREATE   2
+# define TAR_OPT_STR_CREATE               "ch"
+# define TAR_OPT_AFTER_CREATE             TAR_OPT_AFTER_START + 2
 #else
-# define TAR_OPT_STR_CREATE            ""
-# define TAR_OPT_FLAG_CREATE   0
+# define TAR_OPT_STR_CREATE               ""
+# define TAR_OPT_AFTER_CREATE             TAR_OPT_AFTER_START
 #endif
 
-# define TAR_OPT_BZIP2                 (1 << (8 + TAR_OPT_FLAG_CREATE))
+#define TAR_OPT_BZIP2                     (1 << (TAR_OPT_AFTER_CREATE))
 #ifdef CONFIG_FEATURE_TAR_BZIP2
-# define TAR_OPT_STR_BZIP2             "j"
-# define TAR_OPT_FLAG_BZIP2            1
+# define TAR_OPT_STR_BZIP2                "j"
+# define TAR_OPT_AFTER_BZIP2              TAR_OPT_AFTER_CREATE + 1
+#else
+# define TAR_OPT_STR_BZIP2                ""
+# define TAR_OPT_AFTER_BZIP2              TAR_OPT_AFTER_CREATE
+#endif
+
+#define TAR_OPT_LZMA                      (1 << (TAR_OPT_AFTER_BZIP2))
+#ifdef CONFIG_FEATURE_TAR_LZMA
+# define TAR_OPT_STR_LZMA                 "a"
+# define TAR_OPT_AFTER_LZMA               TAR_OPT_AFTER_BZIP2 + 1
 #else
-# define TAR_OPT_STR_BZIP2             ""
-# define TAR_OPT_FLAG_BZIP2            0
+# define TAR_OPT_STR_LZMA                 ""
+# define TAR_OPT_AFTER_LZMA               TAR_OPT_AFTER_BZIP2
 #endif
 
-# define TAR_OPT_INCLUDE_FROM  (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2))
-# define TAR_OPT_EXCLUDE_FROM  (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + 1))
+#define TAR_OPT_INCLUDE_FROM              (1 << (TAR_OPT_AFTER_LZMA))
+#define TAR_OPT_EXCLUDE_FROM              (1 << (TAR_OPT_AFTER_LZMA + 1))
 #ifdef CONFIG_FEATURE_TAR_FROM
-# define TAR_OPT_STR_FROM              "T:X:"
-# define TAR_OPT_FLAG_FROM             2
+# define TAR_OPT_STR_FROM                 "T:X:"
+# define TAR_OPT_AFTER_FROM               TAR_OPT_AFTER_LZMA + 2
 #else
-# define TAR_OPT_STR_FROM              ""
-# define TAR_OPT_FLAG_FROM             0
+# define TAR_OPT_STR_FROM                 ""
+# define TAR_OPT_AFTER_FROM               TAR_OPT_AFTER_LZMA
 #endif
 
-# define TAR_OPT_GZIP                  (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + TAR_OPT_FLAG_FROM))
+#define TAR_OPT_GZIP                      (1 << (TAR_OPT_AFTER_FROM))
 #ifdef CONFIG_FEATURE_TAR_GZIP
-# define TAR_OPT_STR_GZIP              "z"
-# define TAR_OPT_FLAG_GZIP             1
+# define TAR_OPT_STR_GZIP                 "z"
+# define TAR_OPT_AFTER_GZIP               TAR_OPT_AFTER_FROM + 1
 #else
-# define TAR_OPT_STR_GZIP              ""
-# define TAR_OPT_FLAG_GZIP             0
+# define TAR_OPT_STR_GZIP                 ""
+# define TAR_OPT_AFTER_GZIP               TAR_OPT_AFTER_FROM
 #endif
 
-# define TAR_OPT_UNCOMPRESS            (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + TAR_OPT_FLAG_FROM + TAR_OPT_FLAG_GZIP))
+#define TAR_OPT_UNCOMPRESS                (1 << (TAR_OPT_AFTER_GZIP))
 #ifdef CONFIG_FEATURE_TAR_COMPRESS
-# define TAR_OPT_STR_COMPRESS  "Z"
+# define TAR_OPT_STR_COMPRESS             "Z"
+# define TAR_OPT_AFTER_COMPRESS           TAR_OPT_AFTER_GZIP + 1
 #else
-# define TAR_OPT_STR_COMPRESS  ""
+# define TAR_OPT_STR_COMPRESS             ""
+# define TAR_OPT_AFTER_COMPRESS           TAR_OPT_AFTER_GZIP
 #endif
 
+#define TAR_OPT_NOPRESERVE_OWN            (1 << (TAR_OPT_AFTER_COMPRESS))
+#define TAR_OPT_NOPRESERVE_PERM           (1 << (TAR_OPT_AFTER_COMPRESS + 1))
+#define TAR_OPT_STR_NOPRESERVE            "\203\213"
+#define TAR_OPT_AFTER_NOPRESERVE          TAR_OPT_AFTER_COMPRESS + 2
+
 static const char tar_options[]="txC:f:Opvk" \
        TAR_OPT_STR_CREATE \
        TAR_OPT_STR_BZIP2 \
+       TAR_OPT_STR_LZMA \
        TAR_OPT_STR_FROM \
        TAR_OPT_STR_GZIP \
-       TAR_OPT_STR_COMPRESS;
+       TAR_OPT_STR_COMPRESS \
+       TAR_OPT_STR_NOPRESERVE;
 
 #ifdef CONFIG_FEATURE_TAR_LONG_OPTIONS
 static const struct option tar_long_options[] = {
@@ -649,6 +675,8 @@ static const struct option tar_long_options[] = {
        { "same-permissions",   0,      NULL,   'p' },
        { "verbose",                    0,      NULL,   'v' },
        { "keep-old",                   0,      NULL,   'k' },
+       { "no-same-owner",              0,      NULL,   '\203' },
+       { "no-same-permissions",0,      NULL,   '\213' },
 # ifdef CONFIG_FEATURE_TAR_CREATE
        { "create",                             0,      NULL,   'c' },
        { "dereference",                0,      NULL,   'h' },
@@ -656,6 +684,9 @@ static const struct option tar_long_options[] = {
 # ifdef CONFIG_FEATURE_TAR_BZIP2
        { "bzip2",                              0,      NULL,   'j' },
 # endif
+# ifdef CONFIG_FEATURE_TAR_LZMA
+       { "lzma",                               0,      NULL,   'a' },
+# endif
 # ifdef CONFIG_FEATURE_TAR_FROM
        { "files-from",                 1,      NULL,   'T' },
        { "exclude-from",               1,      NULL,   'X' },
@@ -667,7 +698,7 @@ static const struct option tar_long_options[] = {
 # ifdef CONFIG_FEATURE_TAR_COMPRESS
        { "compress",                   0,      NULL,   'Z' },
 # endif
-       { 0,                    0, 0, 0 }
+       { 0,                                    0, 0, 0 }
 };
 #else
 #define tar_long_options       0
@@ -681,15 +712,15 @@ int tar_main(int argc, char **argv)
        const char *tar_filename = "-";
        unsigned long opt;
        llist_t *excludes = NULL;
-       
+
        /* Initialise default values */
        tar_handle = init_handle();
        tar_handle->flags = ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
 
        /* Prepend '-' to the first argument if required */
        bb_opt_complementally = ENABLE_FEATURE_TAR_CREATE ?
-               "--:-1:X::T::\n::c:t:x:?:c--tx:t--cx:x--ct" :
-               "--:-1:X::T::\n::t:x:?:t--x:x--t";
+               "--:X::T::\n::c:t:x:?:c--tx:t--cx:x--ct" :
+               "--:X::T::\n::t:x:?:t--x:x--t";
        if (ENABLE_FEATURE_TAR_LONG_OPTIONS)
                bb_applet_long_options = tar_long_options;
        opt = bb_getopt_ulflags(argc, argv, tar_options,
@@ -711,7 +742,7 @@ int tar_main(int argc, char **argv)
        }
        if((opt & CTX_EXTRACT) && tar_handle->action_data != data_extract_to_stdout)
                tar_handle->action_data = data_extract_all;
-               
+
        if (opt & TAR_OPT_2STDOUT)
                tar_handle->action_data = data_extract_to_stdout;
 
@@ -726,6 +757,11 @@ int tar_main(int argc, char **argv)
        if (opt & TAR_OPT_KEEP_OLD)
                tar_handle->flags &= ~ARCHIVE_EXTRACT_UNCONDITIONAL;
 
+       if (opt & TAR_OPT_NOPRESERVE_OWN)
+               tar_handle->flags |= ARCHIVE_NOPRESERVE_OWN;
+
+       if (opt & TAR_OPT_NOPRESERVE_PERM)
+               tar_handle->flags |= ARCHIVE_NOPRESERVE_PERM;
 
        if (ENABLE_FEATURE_TAR_GZIP && (opt & TAR_OPT_GZIP))
                get_header_ptr = get_header_tar_gz;
@@ -733,6 +769,9 @@ int tar_main(int argc, char **argv)
        if (ENABLE_FEATURE_TAR_BZIP2 && (opt & TAR_OPT_BZIP2))
                get_header_ptr = get_header_tar_bz2;
 
+       if (ENABLE_FEATURE_TAR_LZMA && (opt & TAR_OPT_LZMA))
+               get_header_ptr = get_header_tar_lzma;
+
        if (ENABLE_FEATURE_TAR_COMPRESS && (opt & TAR_OPT_UNCOMPRESS))
                get_header_ptr = get_header_tar_Z;
 
@@ -794,8 +833,8 @@ int tar_main(int argc, char **argv)
                }
        }
 
-       if ((base_dir) && (chdir(base_dir)))
-               bb_perror_msg_and_die("Couldnt chdir to %s", base_dir);
+       if (base_dir)
+               bb_xchdir(base_dir);
 
        /* create an archive */
        if (ENABLE_FEATURE_TAR_CREATE && (opt & CTX_CREATE)) {
@@ -803,9 +842,9 @@ int tar_main(int argc, char **argv)
                int zipMode = 0;
 
                if (ENABLE_FEATURE_TAR_GZIP && get_header_ptr == get_header_tar_gz)
-                       zipMode = 1;
+                       zipMode = 1;
                if (ENABLE_FEATURE_TAR_BZIP2 && get_header_ptr == get_header_tar_bz2)
-                       zipMode = 2;
+                       zipMode = 2;
 
                if ((tar_handle->action_header == header_list) ||
                                (tar_handle->action_header == header_verbose_list))