Updates from both Vladimir and Larry
[oweals/busybox.git] / tar.c
diff --git a/tar.c b/tar.c
index 7cfad72b513de3a9c8d9dc31ea5e4fb87ed21222..282656fe766b6ece79e7f28fef775294144fff58 100644 (file)
--- a/tar.c
+++ b/tar.c
 #include <stdlib.h>
 #include <unistd.h>
 #include "busybox.h"
-#define BB_DECLARE_EXTERN
-#define bb_need_io_error
-#define bb_need_name_longer_than_foo
-#include "messages.c"
 
 /* Tar file constants  */
 #ifndef MAJOR
@@ -386,6 +382,7 @@ tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
 static int
 tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
 {
+       int result;
 
        if (extractFlag==FALSE || tostdoutFlag==TRUE)
                return( TRUE);
@@ -397,12 +394,15 @@ tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
        /* make the final component, just in case it was
         * omitted by create_path() (which will skip the
         * directory if it doesn't have a terminating '/') */
-       if (mkdir(header->name, header->mode) < 0 && errno != EEXIST) {
+       result = mkdir(header->name, header->mode);
+       /* Don't fix permissions on pre-existing directories */
+       if (result == 0) {
+               fixUpPermissions(header);
+       } else if (result < 0 && errno != EEXIST) {
                perror_msg("%s", header->name);
                return FALSE;
        }
 
-       fixUpPermissions(header);
        return( TRUE);
 }
 
@@ -490,7 +490,7 @@ readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
                static int alreadyWarned=FALSE;
 
                while (*(header->name) == '/')
-                       ++*(header->name);
+                       header->name++;
 
                if (alreadyWarned == FALSE) {
                        error_msg("Removing leading '/' from member names");
@@ -599,7 +599,7 @@ static int readTarFile(int tarFd, int extractFlag, int listFlag,
                        }
                }
                if ( *(header.name) == '\0' )
-                               goto endgame;
+                       continue;
                header.tarFd = tarFd;
 
                /* Skip funky extra GNU headers that precede long files */
@@ -710,7 +710,7 @@ static int readTarFile(int tarFd, int extractFlag, int listFlag,
                        case REGTYPE0:
                                /* If the name ends in a '/' then assume it is
                                 * supposed to be a directory, and fall through */
-                               if (header.name[strlen(header.name)-1] != '/') {
+                               if (!last_char_is(header.name,'/')) {
                                        if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
                                                errorFlag=TRUE;
                                        break;
@@ -925,16 +925,12 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
                header.typeflag = LNKTYPE;
                strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
        } else if (S_ISLNK(statbuf->st_mode)) {
-               int link_size=0;
-               char buffer[BUFSIZ];
-               header.typeflag  = SYMTYPE;
-               link_size = readlink(real_name, buffer, sizeof(buffer) - 1);
-               if ( link_size < 0) {
-                       perror_msg("Error reading symlink '%s'", header.name);
+               char *lpath = xreadlink(real_name);
+               if (!lpath) /* Already printed err msg inside xreadlink() */
                        return ( FALSE);
-               }
-               buffer[link_size] = '\0';
-               strncpy(header.linkname, buffer, sizeof(header.linkname)); 
+               header.typeflag  = SYMTYPE;
+               strncpy(header.linkname, lpath, sizeof(header.linkname)); 
+               free(lpath);
        } else if (S_ISDIR(statbuf->st_mode)) {
                header.typeflag  = DIRTYPE;
                strncat(header.name, "/", sizeof(header.name));