Bernhard Fischer says: use xmalloc() instead of malloc()
[oweals/busybox.git] / archival / libunarchive / get_header_tar.c
index 013450a2aae44bcdc14ee600d2639f54a474740d..513909d5f83a7c81f7f1852a108039aeb30342f8 100644 (file)
@@ -13,9 +13,9 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  *
- *  FIXME: Better checking required in oldcompatability mode, 
- *  the file db.1.85.tar.gz from sleepycat.com has trailing garbage
- *  GNU tar can handle it, busybox tar reports invalid tar header. 
+ *  FIXME:
+ *    In privileged mode if uname and gname map to a uid and gid then use the
+ *    mapped value instead of the uid/gid values in tar header
  *
  *  References:
  *    GNU tar and star man pages,
@@ -62,6 +62,7 @@ extern char get_header_tar(archive_handle_t *archive_handle)
        } tar;
        long sum = 0;
        long i;
+       static int end = 0;
 
        /* Align header */
        data_align(archive_handle, 512);
@@ -74,8 +75,17 @@ extern char get_header_tar(archive_handle_t *archive_handle)
 
        /* If there is no filename its an empty header */
        if (tar.formated.name[0] == 0) {
+               if (end) {
+                       /* This is the second consecutive empty header! End of archive!
+                        * Read until the end to empty the pipe from gz or bz2
+                        */
+                       while (bb_full_read(archive_handle->src_fd, tar.raw, 512) == 512);
+                       return(EXIT_FAILURE);
+               }
+               end = 1;
                return(EXIT_SUCCESS);
        }
+       end = 0;
 
        /* Check header has valid magic, "ustar" is for the proper tar
         * 0's are for the old tar format
@@ -115,34 +125,38 @@ extern char get_header_tar(archive_handle_t *archive_handle)
                file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
        }
 
-       file_header->mode = strtol(tar.formated.mode, NULL, 8);
        file_header->uid = strtol(tar.formated.uid, NULL, 8);
        file_header->gid = strtol(tar.formated.gid, NULL, 8);
        file_header->size = strtol(tar.formated.size, NULL, 8);
        file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
-       file_header->link_name = (tar.formated.linkname[0] != '\0') ? 
+       file_header->link_name = (tar.formated.linkname[0] != '\0') ?
            bb_xstrdup(tar.formated.linkname) : NULL;
-       file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
-                                strtol(tar.formated.devminor, NULL, 8));
+       file_header->device = makedev(strtol(tar.formated.devmajor, NULL, 8),
+               strtol(tar.formated.devminor, NULL, 8));
+
+       /* Set bits 0-11 of the files mode */
+       file_header->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
 
-       /* Fix mode, used by the old format */
+       /* Set bits 12-15 of the files mode */
        switch (tar.formated.typeflag) {
        /* busybox identifies hard links as being regular files with 0 size and a link name */
        case '1':
-               file_header->mode &= (S_IFREG | 07777);
+               file_header->mode |= S_IFREG;
                break;
        case 'x':
        case 'g':
                bb_error_msg_and_die("pax is not tar");
                break;
-#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
+       case '7':
+               /* Reserved for high performance files, treat as normal file */
        case 0:
        case '0':
+#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
                if (last_char_is(file_header->name, '/')) {
                        file_header->mode |= S_IFDIR;
-               } else {
+               } else
+#endif
                        file_header->mode |= S_IFREG;
-               }
                break;
        case '2':
                file_header->mode |= S_IFLNK;
@@ -159,11 +173,6 @@ extern char get_header_tar(archive_handle_t *archive_handle)
        case '6':
                file_header->mode |= S_IFIFO;
                break;
-       case '7':
-               /* Reserved for high performance files, treat as normal file */
-               file_header->mode |= S_IFREG;
-               break;
-#endif
 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
        case 'L': {
                        longname = xmalloc(file_header->size + 1);
@@ -206,7 +215,7 @@ extern char get_header_tar(archive_handle_t *archive_handle)
                archive_handle->action_data(archive_handle);
                archive_handle->passed = llist_add_to(archive_handle->passed, file_header->name);
        } else {
-               data_skip(archive_handle);                      
+               data_skip(archive_handle);
        }
        archive_handle->offset += file_header->size;