]> Dogcows Code - chaz/tar/blobdiff - src/misc.c
Revamp tar_getcwd/normalize_filename stuff.
[chaz/tar] / src / misc.c
index b8ef6eabf1ae16e43b1fece6d948b7bf0bc65b76..5264952e95c0b3e8fc812b8f9b698878b2d5442b 100644 (file)
@@ -229,11 +229,12 @@ zap_slashes (char *name)
 }
 
 /* Normalize FILE_NAME by removing redundant slashes and "."
-   components, including redundant trailing slashes.  Leave ".."
-   alone, as it may be significant in the presence of symlinks and on
-   platforms where "/.." != "/".  Destructive version: modifies its
-   argument. */
-static void
+   components, including redundant trailing slashes.
+   Leave ".." alone, as it may be significant in the presence 
+   of symlinks and on platforms where "/.." != "/".
+
+   Destructive version: modifies its argument. */
+void
 normalize_filename_x (char *file_name)
 {
   char *name = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
@@ -267,11 +268,12 @@ normalize_filename_x (char *file_name)
 }
 
 /* Normalize NAME by removing redundant slashes and "." components,
-   including redundant trailing slashes.  Return a normalized
-   newly-allocated copy.  */
+   including redundant trailing slashes.
+
+   Return a normalized newly-allocated copy.  */
 
 char *
-normalize_filename (const char *name)
+normalize_filename (int cdidx, const char *name)
 {
   char *copy = NULL;
 
@@ -283,21 +285,20 @@ normalize_filename (const char *name)
          getcwd is slow, it might fail, and it does not necessarily
          return a canonical name even when it succeeds.  Perhaps we
          can use dev+ino pairs instead of names?  */
-      copy = tar_getcwd ();
-      if (copy)
-        {
-          size_t copylen = strlen (copy);
-          bool need_separator = ! (DOUBLE_SLASH_IS_DISTINCT_ROOT
-                                   && copylen == 2 && ISSLASH (copy[1]));
-          copy = xrealloc (copy, copylen + need_separator + strlen (name) + 1);
-          copy[copylen] = DIRECTORY_SEPARATOR;
-          strcpy (copy + copylen + need_separator, name);
-        }
-      else
-        WARN ((0, errno, _("Cannot get working directory")));
+      const char *cwd = tar_getcdpath (cdidx);
+      size_t copylen;
+      bool need_separator;
+      
+      copylen = strlen (cwd);
+      need_separator = ! (DOUBLE_SLASH_IS_DISTINCT_ROOT
+                         && copylen == 2 && ISSLASH (cwd[1]));
+      copy = xmalloc (copylen + need_separator + strlen (name) + 1);
+      strcpy (copy, cwd);
+      copy[copylen] = DIRECTORY_SEPARATOR;
+      strcpy (copy + copylen + need_separator, name);
     }
 
-  if (! copy)
+  if (!copy)
     copy = xstrdup (name);
   normalize_filename_x (copy);
   return copy;
@@ -831,7 +832,8 @@ struct wd
 {
   /* The directory's name.  */
   char const *name;
-
+  /* Current working directory; initialized by tar_getcwd */
+  char *cwd; 
   /* If nonzero, the file descriptor of the directory, or AT_FDCWD if
      the working directory.  If zero, the directory needs to be opened
      to be used.  */
@@ -886,6 +888,7 @@ chdir_arg (char const *dir)
       if (! wd_count)
        {
          wd[wd_count].name = ".";
+         wd[wd_count].cwd = xgetcwd ();
          wd[wd_count].fd = AT_FDCWD;
          wd_count++;
        }
@@ -903,6 +906,14 @@ chdir_arg (char const *dir)
     }
 
   wd[wd_count].name = dir;
+  if (IS_ABSOLUTE_FILE_NAME (wd[wd_count].name))
+    wd[wd_count].cwd = xstrdup (wd[wd_count].name);
+  else
+    {
+      namebuf_t nbuf = namebuf_create (wd[wd_count - 1].cwd);
+      namebuf_add_dir (nbuf, wd[wd_count].name);
+      wd[wd_count].cwd = namebuf_finish (nbuf);
+    }
   wd[wd_count].fd = 0;
   return wd_count++;
 }
@@ -976,19 +987,23 @@ chdir_do (int i)
     }
 }
 \f
-char *
-tar_getcwd (void)
+const char *
+tar_dirname (void)
 {
-  static char *cwd;
-  namebuf_t nbuf;
-  int i;
-
-  if (!cwd)
-    cwd = xgetcwd ();
-  nbuf = namebuf_create (cwd);
-  for (i = 1; i <= chdir_current; i++)
-    namebuf_add_dir (nbuf, wd[i].name);
-  return namebuf_finish (nbuf);
+  return wd[chdir_current].name;
+}
+
+const char *
+tar_getcdpath (int idx)
+{
+  if (!wd)
+    {
+      static char *cwd;
+      if (!cwd)
+       cwd = xgetcwd ();
+      return cwd;
+    }
+  return wd[idx].cwd;
 }
 \f
 void
This page took 0.02992 seconds and 4 git commands to generate.