On Fri, Sep 13, 2019 at 12:08 PM Rob Landley <[email protected]> wrote:
>
> On 9/12/19 6:20 PM, enh via Toybox wrote:
> > This doesn't fix xvf, but I've run out of time for now and wanted to
> > show what I'd found so far.
>
> Ah sorry, I just hadn't gotten around to it yet. (New job, terrible cold, and 
> I
> was working on -newerXY and shell...)
>
> I'll take a look,

i've found the issue in the meantime. literally the last place i
looked (for obvious reasons when you see the fix).

new patch attached...

> By the way, do you have a test tarball with xattrs in it? I want to add xattr
> support, but devuan seem to doesn't use xattrs anywhere I can spot. (I didn't
> install selinux support.)

no, but i'll make one. (i need to write a test for this tar fix too.)

> Rob
From d0a951d8ebcf61b94861034921e7c719bd8a6d47 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 x/t for sparse extension headers.

If we have a hole at the end, we need to ftruncate the file.

Bug: 140403724
---
 tests/tar.test   |  2 ++
 toys/posix/tar.c | 12 ++++++++----
 2 files changed, 10 insertions(+), 4 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..72ec5d33 100644
--- a/toys/posix/tar.c
+++ b/toys/posix/tar.c
@@ -438,7 +438,6 @@ static void sendfile_sparse(int fd)
 
   do {
     if (TT.sparselen) {
-      if (!TT.sparse[i*2+1]) continue;
       // Seek past holes or fill output with zeroes.
       if (-1 == lseek(fd, len = TT.sparse[i*2], SEEK_SET)) {
         sent = 0;
@@ -464,6 +463,8 @@ error:
     }
   } while (++i<TT.sparselen);
 
+  if (ftruncate(fd, xlseek(fd, 0, SEEK_CUR))) perror_msg("ftruncate");
+
   close(fd);
 }
 
@@ -551,6 +552,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 +578,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 +624,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 +723,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 +741,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