Add wget -P support, finishing off bug #1176
[oweals/busybox.git] / tar.c
diff --git a/tar.c b/tar.c
index 0cb638a7f1eea8982a4d1ebebc4ce9effae6e440..e68194ff79ef65d12589682c9a8b5c5f7e32bb4f 100644 (file)
--- a/tar.c
+++ b/tar.c
@@ -151,6 +151,7 @@ extern int tar_main(int argc, char **argv)
        char** excludeList=NULL;
        char** extractList=NULL;
        const char *tarName="-";
+       const char *cwd=NULL;
 #if defined BB_FEATURE_TAR_EXCLUDE
        int excludeListSize=0;
        FILE *fileList;
@@ -181,9 +182,9 @@ extern int tar_main(int argc, char **argv)
 
        while (
 #ifndef BB_FEATURE_TAR_EXCLUDE
-                       (opt = getopt(argc, argv, "cxtzvOf:"))
+                       (opt = getopt(argc, argv, "cxtzvOf:pC:"))
 #else
-                       (opt = getopt_long(argc, argv, "cxtzvOf:X:", longopts, NULL))
+                       (opt = getopt_long(argc, argv, "cxtzvOf:X:pC:", longopts, NULL))
 #endif
                        > 0) {
                switch (opt) {
@@ -238,7 +239,16 @@ extern int tar_main(int argc, char **argv)
                                fclose(fileList);
                                break;
 #endif
-                               default:
+                       case 'p':
+                               break;
+                       case 'C':
+                               cwd = xgetcwd((char *)cwd);
+                               if (chdir(optarg)) {
+                                       printf("cd: %s: %s\n", optarg, strerror(errno));
+                                       return EXIT_FAILURE;
+                               }
+                               break;
+                       default:
                                        show_usage();
                }
        }
@@ -290,6 +300,8 @@ extern int tar_main(int argc, char **argv)
 #endif                 
        }
 
+       if (cwd)
+               chdir(cwd);
        if (status == TRUE)
                return EXIT_SUCCESS;
        else
@@ -326,7 +338,9 @@ tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
        if (extractFlag==TRUE && tostdoutFlag==FALSE) {
                /* Create the path to the file, just in case it isn't there...
                 * This should not screw up path permissions or anything. */
-               create_path(header->name, 0777);
+               char *dir = dirname (header->name);
+               make_directory (dir, -1, FILEUTILS_RECUR);
+               free (dir);
                if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, 
                                                header->mode & ~S_IFMT)) < 0) {
                        error_msg(io_error, header->name, strerror(errno)); 
@@ -385,8 +399,7 @@ tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
        if (extractFlag==FALSE || tostdoutFlag==TRUE)
                return( TRUE);
 
-       if (create_path(header->name, header->mode) != TRUE) {
-               perror_msg("%s: Cannot mkdir", header->name); 
+       if (make_directory(header->name, header->mode, FILEUTILS_RECUR) < 0) {
                return( FALSE);
        }
        /* make the final component, just in case it was
@@ -576,10 +589,6 @@ static int readTarFile(int tarFd, int extractFlag, int listFlag,
        TarHeader rawHeader;
        TarInfo header;
 
-       /* Set the umask for this process so it doesn't 
-        * screw up permission setting for us later. */
-       umask(0);
-
        /* Read the tar file, and iterate over it one file at a time */
        while ( (status = full_read(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
 
@@ -1109,10 +1118,6 @@ static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
        if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
                error_msg_and_die(io_error, tarName, strerror(errno)); 
 
-       /* Set the umask for this process so it doesn't 
-        * screw up permission setting for us later. */
-       umask(0);
-
        /* Read the directory/files and iterate over them one at a time */
        while (*argv != NULL) {
                if (recursive_action(*argv++, TRUE, FALSE, FALSE,