udhcpc: fix a problem with binary-encoded options #2
[oweals/busybox.git] / archival / tar.c
index cf972c24c6a3c38dae08fd614a9782718631fdf5..f46f7bb7a6453a0a29e35f8ef8e7c2479fc4869a 100644 (file)
@@ -60,8 +60,8 @@
 
 #if !ENABLE_FEATURE_SEAMLESS_GZ && !ENABLE_FEATURE_SEAMLESS_BZ2
 /* Do not pass gzip flag to writeTarFile() */
-#define writeTarFile(tar_fd, verboseFlag, dereferenceFlag, include, exclude, gzip) \
-       writeTarFile(tar_fd, verboseFlag, dereferenceFlag, include, exclude)
+#define writeTarFile(tar_fd, verboseFlag, recurseFlags, include, exclude, gzip) \
+       writeTarFile(tar_fd, verboseFlag, recurseFlags, include, exclude)
 #endif
 
 
@@ -333,13 +333,13 @@ static int writeTarHeader(struct TarBallInfo *tbInfo,
                 && (filesize <= 0x3fffffffffffffffffffffffLL)
 #endif
                ) {
-                       /* GNU tar uses "base-256 encoding" for very large numbers.
-                        * Encoding is binary, with highest bit always set as a marker
-                        * and sign in next-highest bit:
-                        * 80 00 .. 00 - zero
-                        * bf ff .. ff - largest positive number
-                        * ff ff .. ff - minus 1
-                        * c0 00 .. 00 - smallest negative number
+                       /* GNU tar uses "base-256 encoding" for very large numbers.
+                        * Encoding is binary, with highest bit always set as a marker
+                        * and sign in next-highest bit:
+                        * 80 00 .. 00 - zero
+                        * bf ff .. ff - largest positive number
+                        * ff ff .. ff - minus 1
+                        * c0 00 .. 00 - smallest negative number
                         */
                        char *p8 = header.size + sizeof(header.size);
                        do {
@@ -598,7 +598,7 @@ static void NOINLINE vfork_compressor(int tar_fd, int gzip)
 
 /* gcc 4.2.1 inlines it, making code bigger */
 static NOINLINE int writeTarFile(int tar_fd, int verboseFlag,
-       int dereferenceFlag, const llist_t *include,
+       int recurseFlags, const llist_t *include,
        const llist_t *exclude, int gzip)
 {
        int errorFlag = FALSE;
@@ -621,8 +621,7 @@ static NOINLINE int writeTarFile(int tar_fd, int verboseFlag,
 
        /* Read the directory/files and iterate over them one at a time */
        while (include) {
-               if (!recursive_action(include->data, ACTION_RECURSE |
-                               (dereferenceFlag ? ACTION_FOLLOWLINKS : 0),
+               if (!recursive_action(include->data, recurseFlags,
                                writeFileToTarball, writeFileToTarball, &tbInfo, 0)
                ) {
                        errorFlag = TRUE;
@@ -662,7 +661,7 @@ static NOINLINE int writeTarFile(int tar_fd, int verboseFlag,
 }
 #else
 int writeTarFile(int tar_fd, int verboseFlag,
-       int dereferenceFlag, const llist_t *include,
+       int recurseFlags, const llist_t *include,
        const llist_t *exclude, int gzip);
 #endif /* FEATURE_TAR_CREATE */
 
@@ -694,6 +693,7 @@ static llist_t *append_file_list_to_list(llist_t *list)
 //usage:       "-[" IF_FEATURE_TAR_CREATE("c") "xt"
 //usage:       IF_FEATURE_SEAMLESS_Z("Z")
 //usage:       IF_FEATURE_SEAMLESS_GZ("z")
+//usage:       IF_FEATURE_SEAMLESS_XZ("J")
 //usage:       IF_FEATURE_SEAMLESS_BZ2("j")
 //usage:       IF_FEATURE_SEAMLESS_LZMA("a")
 //usage:       IF_FEATURE_TAR_CREATE("h")
@@ -720,6 +720,9 @@ static llist_t *append_file_list_to_list(llist_t *list)
 //usage:       IF_FEATURE_SEAMLESS_GZ(
 //usage:     "\n       z       (De)compress using gzip"
 //usage:       )
+//usage:       IF_FEATURE_SEAMLESS_XZ(
+//usage:     "\n       J       (De)compress using xz"
+//usage:       )
 //usage:       IF_FEATURE_SEAMLESS_BZ2(
 //usage:     "\n       j       (De)compress using bzip2"
 //usage:       )
@@ -749,6 +752,7 @@ static llist_t *append_file_list_to_list(llist_t *list)
 //     o       no-same-owner
 //     p       same-permissions
 //     k       keep-old
+//     no-recursion
 //     numeric-owner
 //     no-same-permissions
 //     overwrite
@@ -765,9 +769,11 @@ enum {
        IF_FEATURE_TAR_FROM(     OPTBIT_INCLUDE_FROM,)
        IF_FEATURE_TAR_FROM(     OPTBIT_EXCLUDE_FROM,)
        IF_FEATURE_SEAMLESS_GZ(  OPTBIT_GZIP        ,)
-       IF_FEATURE_SEAMLESS_Z(   OPTBIT_COMPRESS    ,) // 16th bit
+       IF_FEATURE_SEAMLESS_XZ(  OPTBIT_XZ          ,) // 16th bit
+       IF_FEATURE_SEAMLESS_Z(   OPTBIT_COMPRESS    ,)
        IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,)
 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
+       OPTBIT_NORECURSION,
        IF_FEATURE_TAR_TO_COMMAND(OPTBIT_2COMMAND   ,)
        OPTBIT_NUMERIC_OWNER,
        OPTBIT_NOPRESERVE_PERM,
@@ -789,14 +795,16 @@ enum {
        OPT_INCLUDE_FROM = IF_FEATURE_TAR_FROM(     (1 << OPTBIT_INCLUDE_FROM)) + 0, // T
        OPT_EXCLUDE_FROM = IF_FEATURE_TAR_FROM(     (1 << OPTBIT_EXCLUDE_FROM)) + 0, // X
        OPT_GZIP         = IF_FEATURE_SEAMLESS_GZ(  (1 << OPTBIT_GZIP        )) + 0, // z
+       OPT_XZ           = IF_FEATURE_SEAMLESS_XZ(  (1 << OPTBIT_XZ          )) + 0, // J
        OPT_COMPRESS     = IF_FEATURE_SEAMLESS_Z(   (1 << OPTBIT_COMPRESS    )) + 0, // Z
        OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m
+       OPT_NORECURSION     = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NORECURSION    )) + 0, // no-recursion
        OPT_2COMMAND        = IF_FEATURE_TAR_TO_COMMAND(  (1 << OPTBIT_2COMMAND       )) + 0, // to-command
        OPT_NUMERIC_OWNER   = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER  )) + 0, // numeric-owner
        OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
        OPT_OVERWRITE       = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE      )) + 0, // overwrite
 
-       OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_COMPRESS),
+       OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_COMPRESS),
 };
 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
 static const char tar_longopts[] ALIGN1 =
@@ -829,12 +837,16 @@ static const char tar_longopts[] ALIGN1 =
 # if ENABLE_FEATURE_SEAMLESS_GZ
        "gzip\0"                No_argument       "z"
 # endif
+# if ENABLE_FEATURE_SEAMLESS_XZ
+       "xz\0"                  No_argument       "J"
+# endif
 # if ENABLE_FEATURE_SEAMLESS_Z
        "compress\0"            No_argument       "Z"
 # endif
 # if ENABLE_FEATURE_TAR_NOPRESERVE_TIME
        "touch\0"               No_argument       "m"
 # endif
+       "no-recursion\0"        No_argument       "\xfa"
 # if ENABLE_FEATURE_TAR_TO_COMMAND
        "to-command\0"          Required_argument "\xfb"
 # endif
@@ -921,6 +933,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
                IF_FEATURE_SEAMLESS_LZMA("a"   )
                IF_FEATURE_TAR_FROM(     "T:X:")
                IF_FEATURE_SEAMLESS_GZ(  "z"   )
+               IF_FEATURE_SEAMLESS_XZ(  "J"   )
                IF_FEATURE_SEAMLESS_Z(   "Z"   )
                IF_FEATURE_TAR_NOPRESERVE_TIME("m")
                , &base_dir // -C dir
@@ -1050,7 +1063,9 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
                        zipMode = 2;
 #endif
                /* NB: writeTarFile() closes tar_handle->src_fd */
-               return writeTarFile(tar_handle->src_fd, verboseFlag, opt & OPT_DEREFERENCE,
+               return writeTarFile(tar_handle->src_fd, verboseFlag,
+                               (opt & OPT_DEREFERENCE ? ACTION_FOLLOWLINKS : 0)
+                               | (opt & OPT_NORECURSION ? 0 : ACTION_RECURSE),
                                tar_handle->accept,
                                tar_handle->reject, zipMode);
        }
@@ -1071,6 +1086,9 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
                if (opt & OPT_LZMA)
                        USE_FOR_MMU(xformer = unpack_lzma_stream;)
                        USE_FOR_NOMMU(xformer_prog = "unlzma";)
+               if (opt & OPT_XZ)
+                       USE_FOR_MMU(xformer = unpack_xz_stream;)
+                       USE_FOR_NOMMU(xformer_prog = "unxz";)
 
                open_transformer_with_sig(tar_handle->src_fd, xformer, xformer_prog);
                /* Can't lseek over pipes */