]> cygwin.com Git - cygwin-apps/setup.git/commitdiff
Handle tar type flag 'K' (GNU long link extension)
authorJon Turney <jon.turney@dronecode.org.uk>
Thu, 10 Feb 2022 15:38:56 +0000 (15:38 +0000)
committerJon Turney <jon.turney@dronecode.org.uk>
Mon, 14 Feb 2022 19:45:22 +0000 (19:45 +0000)
Handle tar type flag'K' (GNU long link extension)
Also report unexpected filename in headers with long name extension type
Also clean up some cruft in archive_tar.cc

Currently, the only existing package which contains type flag 'K'
entries is f21-backgrounds-extras.

This also now correctly handles tar archive header linknames of exactly
100 characters (even when not preceeded by a 'K' type header), where the
linkname field is not null terminated as it should be, rather than
allowing the 'ustar\0' magic immediately following it to be interpreted
as part of the linkname.

archive_tar.cc
archive_tar.h

index 5b2a771909683ac56dab53208ad1602c882531cf..63269a61c2c6e153f771f7b01e7648e5b38d04e4 100644 (file)
@@ -21,9 +21,7 @@
 #include <sys/fcntl.h>
 #include <errno.h>
 
-//#include "zlib/zlib.h"
 #include "io_stream.h"
-//#include "compress.h"
 #include "win32.h"
 #include "archive.h"
 #include "archive_tar.h"
 #include "LogFile.h"
 #include "filemanip.h"
 
-#if 0
-#undef _WIN32
-#include "bzlib.h"
-
-#define SYMLINK_COOKIE "!<symlink>"
-#endif
 static int err;
-
 static char buf[512];
 
 int _tar_verbose = 0;
@@ -154,13 +145,26 @@ archive_tar::next_file_name ()
   if (n == 0)
     return std::string();
 
-  if (!state.have_longname && state.tar_header.typeflag != 'L')
+  if (!state.have_longname)
     {
       memcpy (state.filename, state.tar_header.name, 100);
       state.filename[100] = 0;
     }
-  else if (state.have_longname)
-    state.have_longname = 0;
+
+  if (!state.have_longlink)
+    {
+      memcpy (state.linkname, state.tar_header.linkname, 100);
+      state.linkname[100] = 0;
+    }
+
+  /* typeflag for any 'real' file consumes the longname/longlink state from
+     previous blocks */
+  if ((state.tar_header.typeflag != 'K') &&
+      (state.tar_header.typeflag != 'L'))
+    {
+      state.have_longname = 0;
+      state.have_longlink = 0;
+    }
 
   sscanf (state.tar_header.size, "%zo", &state.file_length);
   state.file_offset = 0;
@@ -171,9 +175,13 @@ archive_tar::next_file_name ()
 
   switch (state.tar_header.typeflag)
     {
+    case 'K':                  /* GNU tar long link extension */
     case 'L':                  /* GNU tar long name extension */
-      /* we read the 'file' into the long filename, then call back into here
-       * to find out if the actual file is a real file, or a special file..
+      if (strcmp(state.tar_header.name, "././@LongLink") != 0)
+        LogBabblePrintf("tar: unexpected filename %s in file type %c header\n", state.tar_header.name, state.tar_header.typeflag);
+
+      /* we read the 'file' into the long filename/linkname, then recursively
+       * call ourselves to handle the following block.
        */
       if (state.file_length > CYG_PATH_MAX)
        {
@@ -187,7 +195,18 @@ archive_tar::next_file_name ()
          skip_file ();
          return next_file_name ();
        }
-      c = state.filename;
+
+      if (state.tar_header.typeflag == 'L')
+        {
+          c = state.filename;
+          state.have_longname = 1;
+        }
+      else
+        {
+          c = state.linkname;
+          state.have_longlink = 1;
+        }
+
       /* FIXME: this should be a single read() call */
       while (state.file_length > state.file_offset)
        {
@@ -195,17 +214,16 @@ archive_tar::next_file_name ()
            state.file_length - state.file_offset >
            512 ? 512 : state.file_length - state.file_offset;
          if (state.parent->read (buf, 512) < 512)
-           // FIXME: What's up with the "0"? It's probably a mistake, and
-           // should be "". It used to be written as 0, and was subject to a
-           // bizarre implicit conversion by the unwise String(int)
-           // constructor.
-           return "0";
+            {
+              LogPlainPrintf( "error: error reading long name\n");
+              return "";
+            }
          memcpy (c, buf, need);
          c += need;
          state.file_offset += need;
        }
       *c = 0;
-      state.have_longname = 1;
+
       return next_file_name ();
 
     case '3':                  /* char */
@@ -297,7 +315,7 @@ archive_tar::linktarget ()
   /* TODO: consider .. path traversal issues */
   if (next_file_type () == ARCHIVE_FILE_SYMLINK ||
       next_file_type () == ARCHIVE_FILE_HARDLINK)
-    return state.tar_header.linkname;
+    return state.linkname;
   return std::string();
 }
 
index cc0a46c2c11d2311a2bb7ad2464bc0921a782da6..3adc05606831285635afe8fa1284f054d4920ab4 100644 (file)
@@ -66,12 +66,14 @@ public:
   int lasterr;
   int eocf;
   char have_longname;
+  bool have_longlink;
   /* where in the current file are we? */
   size_t file_offset;
   size_t file_length;
   int header_read;
   tar_header_type tar_header;
   char filename[CYG_PATH_MAX + 512];
+  char linkname[CYG_PATH_MAX + 512];
   tar_map_result_type *tar_map_result;
 };
 
This page took 0.038726 seconds and 5 git commands to generate.