]> Savannah Git Hosting - gnulib.git/commitdiff
canonicalize: omit second readlink when not needed
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 17 Dec 2020 09:13:40 +0000 (01:13 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 17 Dec 2020 09:39:46 +0000 (01:39 -0800)
* lib/canonicalize.c (canonicalize_filename_mode_stk):
Omit second readlink when (can_exist != CAN_MISSING
&& startlen != 0 && !logical).  Simplify.

ChangeLog
lib/canonicalize.c

index 58c7c16d7a193726ed7d4564981c96194902a34a..fccedabe31e74101b6a37207027b6a97c36d8859 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2020-12-17  Paul Eggert  <eggert@cs.ucla.edu>
 
+       canonicalize: omit second readlink when not needed
+       * lib/canonicalize.c (canonicalize_filename_mode_stk):
+       Omit second readlink when (can_exist != CAN_MISSING
+       && startlen != 0 && !logical).  Simplify.
+
        canonicalize: remove arbitrary 8192-byte limit
        Remove canonicalize.c’s arbitrary 8192-byte limit on platforms
        like GNU Hurd that do not define the PATH_MAX macro, and similarly
index 04fae10a42b4ab72473c0c997cb39b5efc406f84..bcfd52be26f995bece18433e12b8379768825c73 100644 (file)
@@ -360,31 +360,15 @@ canonicalize_filename_mode_stk (const char *name, canonicalize_mode_t can_mode,
                     dest++;
                 }
             }
-          else if (can_exist != CAN_MISSING
-                   && (startlen == 0
-                       ? stat (rname, &st) < 0
-                       : !logical && readlink (rname, &discard, 1) < 0))
-            {
-              switch (errno)
-                {
-                case EINVAL:
-                case EOVERFLOW: /* Possible with stat.  */
-                  /* RNAME exists and is not a symbolic link.  */
-                  break;
-
-                case ENOENT:
-                  /* RNAME does not exist.  */
-                  if (can_exist == CAN_EXISTING
+          else if (! (can_exist == CAN_MISSING
+                      || (startlen == 0
+                          ? stat (rname, &st) == 0 || errno == EOVERFLOW
+                          : ((logical && 0 <= readlink (rname, &discard, 1))
+                             || errno == EINVAL))
                       || (can_exist == CAN_ALL_BUT_LAST
-                          && end[strspn (end, SLASHES)]))
-                    goto error;
-                  break;
-
-                default:
-                  /* Some other problem with RNAME.  */
-                  goto error;
-                }
-            }
+                          && errno == ENOENT
+                          && !end[strspn (end, SLASHES)])))
+            goto error;
         }
     }
   if (dest > rname + prefix_len + 1 && ISSLASH (dest[-1]))