Subject: Re: press for cygwin
From: Warren Young <[email-masked]>
To: Cygwin-L <[email-masked]>
Date: Fri, 07 Sep 2001 13:26:59 -0600

Warren Young wrote:
> 
> Done.  See cygwin-patches.

Well, junk.  `pears that cygwin-patches is by invitation only.  So the
patches are inlined below.

This patch adds "*.cwp" ("CygWin Package") file name recognition to
setup.exe.  .tar.gz and .tar.bz2 recognition appears to still work. 
Also, .cwp files can be either bzip2 or gzip files, as the program now
does magic number checking.  This also applies to .tar.gz and .tar.bz2
-- if the file is named .tar.gz and is not actually a gzip file, or
whatever, the program will refuse to even try to open it.

The choose.cc patch is below, and the tar.cc patch follows in the next
message.

Change log entry:

2001-09-05  Warren Young  <[email-masked]>
        * choose.cc (find_tar_ext): Add *.cgw extension recognition

        * tar.cc (tar_open): Now calls decomp_factory() to create gzbz
        instance by examining the file name given.

        * tar.cc (decomp_factory): new function

--- cinstall/choose.cc   Wed Sep  5 18:38:57 2001
+++ cinstall/choose.cc.new       Wed Sep  5 18:36:06 2001
@@ -1208,6 +1208,29 @@ base (const char *s)
 int
 find_tar_ext (const char *path)
 {
+#if 1
+  char temp_path[_MAX_PATH];
+  strncpy(temp_path, path, sizeof(temp_path));
+  temp_path[sizeof(temp_path) - 1] = '\0';
+
+  char* p = strrchr(temp_path, '.');
+  if (!p)
+    return 0;
+
+  if (strcmp(p, ".cwp") == 0)
+    return p - temp_path;
+  else if ((strcmp(p, ".gz") == 0) || (strcmp(p, ".bz2") == 0))
+    {
+      // found .gz or .bz2, make sure ".tar" is before that.
+      *p = '\0';
+      p = strrchr(temp_path, '.');
+      if (p && (strcmp(p, ".tar") == 0))
+        return p - temp_path;
+      return 0;
+    }
+  else
+    return 0;
+#else
   char *p = strchr (path, '\0') - 7;
   if (p <= path)
     return 0;
@@ -1220,6 +1243,7 @@ find_tar_ext (const char *path)
     return 0;

   return p - path;
+#endif
 }

 /* Parse a filename into package, version, and extension components. */

--- cinstall/tar.cc      Wed Sep  5 18:38:58 2001
+++ cinstall/tar.cc.new  Wed Sep  5 17:58:36 2001
@@ -151,6 +151,40 @@ xstrdup (char *c)
   return r;
 }

+static gzbz*
+decomp_factory(const char* pathname)
+{
+#if 1
+  HANDLE h = CreateFile(pathname, GENERIC_READ, 0, 0, OPEN_EXISTING,
+                  FILE_ATTRIBUTE_NORMAL, 0);
+  char ac[3];
+  DWORD n = sizeof(ac);
+
+  if (!h)
+    return 0;
+
+  if (!ReadFile(h, ac, sizeof(ac), &n, 0))
+    {
+         CloseHandle(h);
+      return 0;
+       }
+  CloseHandle(h);
+
+  if (memcmp(ac, "\037\213", 2) == 0)
+    return new gz (pathname);
+  else if (memcmp(ac, "BZh", 3) == 0)
+    return new bz (pathname);
+  else
+    return 0;
+#else
+       if (strstr(pathname, "bz2"))
+               return new bz(pathname);
+       else
+               return new gz(pathname);
+#endif
+}
+
+
 int
 tar_open (const char *pathname)
 {
@@ -163,10 +197,14 @@ tar_open (const char *pathname)
     return 1;
   _tar_file_size = size;

-  if (strstr (pathname, ".bz2"))
-    z = new bz (pathname);
-  else
-    z = new gz (pathname);
+  z = decomp_factory(pathname);
+  if (!z)
+    {
+         fprintf (stderr, "error: could not figure out compression type
"
+                       "for file '%s'\n", pathname);
+         return 1;
+       }
+
   if (sizeof (tar_header) != 512)
     {
       /* drastic, but important */

-- 
= Warren -- ICBM Address: 36.8274040 N, 108.0204086 W, alt. 1714m

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/