This doesn't fix xvf, but I've run out of time for now and wanted to
show what I'd found so far.

Bug: 140403724
---
 tests/tar.test   | 2 ++
 toys/posix/tar.c | 9 ++++++---
 2 files changed, 8 insertions(+), 3 deletions(-)
From ef8c8e4284163e291712be936d562307e0ac28b1 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Thu, 12 Sep 2019 16:18:10 -0700
Subject: [PATCH] tar: fix tvf for sparse extension headers.

This doesn't fix xvf, but I've run out of time for now and wanted to
show what I'd found so far.

Bug: 140403724
---
 tests/tar.test   | 2 ++
 toys/posix/tar.c | 9 ++++++---
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/tests/tar.test b/tests/tar.test
index afa35e94..aacf2466 100644
--- a/tests/tar.test
+++ b/tests/tar.test
@@ -231,6 +231,8 @@ tar c --sparse fweep > fweep.tar
 FWEEP=$(du fweep)
 rm fweep
 testing "sparse extract" "tar xf fweep.tar && du fweep" "$FWEEP\n" "" ""
+testing "sparse tvf" "tar tvf fweep.tar | grep -wq 13172736 && echo right size"\
+  "right size\n" "" ""
 rm fweep fweep.tar
 
 if false
diff --git a/toys/posix/tar.c b/toys/posix/tar.c
index f4f7a928..8ba4b3da 100644
--- a/toys/posix/tar.c
+++ b/toys/posix/tar.c
@@ -551,6 +551,7 @@ static void unpack_tar(char *first)
   struct tar_hdr tar;
   int i, and = 0;
   unsigned maj, min;
+  long long realsize;
   char *s;
 
   for (;;) {
@@ -576,7 +577,7 @@ static void unpack_tar(char *first)
 
     // Is this a valid TAR header?
     if (!is_tar_header(&tar)) error_exit("bad header");
-    TT.hdr.size = OTOI(tar.size);
+    TT.hdr.size = realsize = OTOI(tar.size);
 
     // If this header isn't writing something to the filesystem
     if ((tar.type<'0' || tar.type>'7') && tar.type!='S'
@@ -622,6 +623,8 @@ static void unpack_tar(char *first)
       s = 386+(char *)&tar;
       *sparse = i = 0;
 
+      realsize = OTOI(483+(char *)&tar)*512;
+
       for (;;) {
         if (!(TT.sparselen&511))
           TT.sparse = xrealloc(TT.sparse, (TT.sparselen+512)*sizeof(long long));
@@ -719,7 +722,7 @@ static void unpack_tar(char *first)
         printf(" %s/%s ", *TT.hdr.uname ? TT.hdr.uname : perm,
           *TT.hdr.gname ? TT.hdr.gname : gname);
         if (tar.type=='3' || tar.type=='4') printf("%u,%u", maj, min);
-        else printf("%9lld", (long long)TT.hdr.size);
+        else printf("%9lld", realsize);
         sprintf(perm, ":%02d", lc->tm_sec);
         printf("  %d-%02d-%02d %02d:%02d%s ", 1900+lc->tm_year, 1+lc->tm_mon,
           lc->tm_mday, lc->tm_hour, lc->tm_min, FLAG(full_time) ? perm : "");
@@ -737,7 +740,7 @@ static void unpack_tar(char *first)
 
           xsetenv("TAR_FILETYPE", "f");
           xsetenv(xmprintf("TAR_MODE=%o", TT.hdr.mode), 0);
-          xsetenv(xmprintf("TAR_SIZE=%lld", TT.hdr.size), 0);
+          xsetenv(xmprintf("TAR_SIZE=%lld", realsize), 0);
           xsetenv("TAR_FILENAME", TT.hdr.name);
           xsetenv("TAR_UNAME", TT.hdr.uname);
           xsetenv("TAR_GNAME", TT.hdr.gname);
-- 
2.23.0.237.gc6a4ce50a0-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to