tar: small fixes:
authorDenis Vlasenko <vda.linux@googlemail.com>
Fri, 24 Nov 2006 14:59:45 +0000 (14:59 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Fri, 24 Nov 2006 14:59:45 +0000 (14:59 -0000)
* size-optimize mapping code
* kill double close

archival/tar.c
libbb/procps.c

index 51d34ea..be661cc 100644 (file)
@@ -150,7 +150,9 @@ static HardLinkInfo *findHardLinkInfo(HardLinkInfo * hlInfo, struct stat *statbu
 
 /* Put an octal string into the specified buffer.
  * The number is zero padded and possibly null terminated.
- * Returns TRUE if successful. - DISABLED (no caller ever checked)  */
+ * Returns TRUE if successful. - DISABLED (no caller ever checked) */
+/* FIXME: we leave field untouched if value doesn't fit. */
+/* This is not good - what will happen at untar time?? */
 static void putOctal(char *cp, int len, long long value)
 {
        int tempLength;
@@ -205,9 +207,7 @@ static int writeTarHeader(struct TarBallInfo *tbInfo,
        putOctal(header.mode, sizeof(header.mode), statbuf->st_mode & 07777);
        putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
        putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
-       if (sizeof(header.size) != sizeof("00000000000"))
-               BUG_tar_header_size();
-       strcpy(header.size, "00000000000"); /* Regular file size is handled later */
+       memset(header.size, '0', sizeof(header.size)-1); /* Regular file size is handled later */
        putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
        strcpy(header.magic, "ustar  ");
 
@@ -401,7 +401,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
                        /* tar will be corrupted. So bail out. */
                        /* NB: GNU tar 1.16 warns and pads with zeroes */
                        /* or even seeks back and updates header */
-                       bb_error_msg_and_die("short read from %s", fileName);
+                       bb_error_msg_and_die("short read from %s, aborting", fileName);
                }
                /* Check that file did not grow in between? */
                /* if (safe_read(inputFileFd,1) == 1) warn but continue? */
@@ -461,8 +461,7 @@ static int writeTarFile(const int tar_fd, const int verboseFlag,
                        dup2(gzipDataPipe[0], 0);
                        close(gzipDataPipe[1]);
 
-                       if (tbInfo.tarFd != 1)
-                               dup2(tbInfo.tarFd, 1);
+                       dup2(tbInfo.tarFd, 1);
 
                        close(gzipStatusPipe[0]);
                        fcntl(gzipStatusPipe[1], F_SETFD, FD_CLOEXEC);  /* close on exec shows success */
@@ -831,7 +830,7 @@ int tar_main(int argc, char **argv)
                        flags = O_RDONLY;
                }
 
-               if ((tar_filename[0] == '-') && (tar_filename[1] == '\0')) {
+               if (tar_filename[0] == '-' && !tar_filename[1]) {
                        tar_handle->src_fd = fileno(tar_stream);
                        tar_handle->seek = seek_by_read;
                } else {
@@ -860,23 +859,24 @@ int tar_main(int argc, char **argv)
                writeTarFile(tar_handle->src_fd, verboseFlag, opt & TAR_OPT_DEREFERENCE,
                                tar_handle->accept,
                        tar_handle->reject, zipMode);
-       } else {
-               while (get_header_ptr(tar_handle) == EXIT_SUCCESS)
-                       /* nothing */;
-
-               /* Check that every file that should have been extracted was */
-               while (tar_handle->accept) {
-                       if (!find_list_entry(tar_handle->reject, tar_handle->accept->data)
-                        && !find_list_entry(tar_handle->passed, tar_handle->accept->data)
-                       ) {
-                               bb_error_msg_and_die("%s: not found in archive",
-                                       tar_handle->accept->data);
-                       }
-                       tar_handle->accept = tar_handle->accept->link;
-               }
+               /* NB: writeTarFile() closes tar_handle->src_fd */
+               return EXIT_SUCCESS;
        }
 
-       if (ENABLE_FEATURE_CLEAN_UP && tar_handle->src_fd != STDIN_FILENO)
+       while (get_header_ptr(tar_handle) == EXIT_SUCCESS)
+               /* nothing */;
+
+       /* Check that every file that should have been extracted was */
+       while (tar_handle->accept) {
+               if (!find_list_entry(tar_handle->reject, tar_handle->accept->data)
+                && !find_list_entry(tar_handle->passed, tar_handle->accept->data)
+               ) {
+                       bb_error_msg_and_die("%s: not found in archive",
+                               tar_handle->accept->data);
+               }
+               tar_handle->accept = tar_handle->accept->link;
+       }
+       if (ENABLE_FEATURE_CLEAN_UP /* && tar_handle->src_fd != STDIN_FILENO */)
                close(tar_handle->src_fd);
 
        return EXIT_SUCCESS;
index 52203ee..2581d03 100644 (file)
@@ -35,6 +35,7 @@ void clear_username_cache(void)
        clear_cache(&groupname);
 }
 
+#if 0 /* more generic, but we don't need that yet */
 /* Returns -N-1 if not found. */
 /* cp->cache[N] is allocated and must be filled in this case */
 static int get_cached(cache_t *cp, unsigned id)
@@ -48,25 +49,28 @@ static int get_cached(cache_t *cp, unsigned id)
        cp->cache[i++].id = id;
        return -i;
 }
+#endif
+
+typedef char* ug_func(char *name, long uid, int bufsize);
+static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
+{
+       int i;
+       for (i = 0; i < cp->size; i++)
+               if (cp->cache[i].id == id)
+                       return cp->cache[i].name;
+       i = cp->size++;
+       cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
+       cp->cache[i].id = id;
+       fp(cp->cache[i].name, id, sizeof(cp->cache[i].name));
+       return cp->cache[i].name;
+}
 const char* get_cached_username(uid_t uid)
 {
-       int i = get_cached(&username, uid);
-       if (i < 0) {
-               i = -i - 1;
-               bb_getpwuid(username.cache[i].name, uid,
-                       sizeof(username.cache[i].name));
-       }
-       return username.cache[i].name;
+       return get_cached(&username, uid, bb_getpwuid);
 }
-const char* get_cached_groupname(uid_t uid)
+const char* get_cached_groupname(gid_t gid)
 {
-       int i = get_cached(&groupname, uid);
-       if (i < 0) {
-               i = -i - 1;
-               bb_getgrgid(groupname.cache[i].name, uid,
-                       sizeof(groupname.cache[i].name));
-       }
-       return username.cache[i].name;
+       return get_cached(&groupname, gid, bb_getgrgid);
 }