]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: prefer issymlink to readlink with a small buffer
authorCollin Funk <collin.funk1@gmail.com>
Wed, 3 Sep 2025 05:25:46 +0000 (22:25 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Thu, 4 Sep 2025 01:31:08 +0000 (18:31 -0700)
* bootstrap.conf (gnulib_modules): Add issymlink and issymlinkat.
* src/copy.c: Include issymlink.h.
(copy_reg): Use issymlink instead of readlinkat.
* src/rmdir.c: Include issymlink.h.
(main): Use issymlink instead of readlink.
* src/tail.c: Include issymlink.h.
(recheck, any_symlinks): Use issymlink instead of readlink.
* src/test.c: Include issymlink.h.
(unary_operator): Use issymlink instead of readlink.

bootstrap.conf
src/copy.c
src/rmdir.c
src/tail.c
src/test.c

index 49fcf30f3263d2cd8e7fe48620bc58961529a5da..7d09752bc42d683f6becfdd67b634ac62ca1bcb6 100644 (file)
@@ -152,6 +152,8 @@ gnulib_modules="
   isapipe
   isatty
   isblank
+  issymlink
+  issymlinkat
   largefile
   lchmod
   ldtoastr
index 73ded685735b8c2bae71cbdfcf67090e4aba7b82..e89f376f2774219318a32eabf1a15efcc2a019de 100644 (file)
@@ -49,6 +49,7 @@
 #include "hashcode-file.h"
 #include "ignore-value.h"
 #include "ioblksize.h"
+#include "issymlink.h"
 #include "quote.h"
 #include "renameatu.h"
 #include "root-uid.h"
@@ -1444,7 +1445,7 @@ copy_reg (char const *src_name, char const *dst_name,
 
       /* When trying to copy through a dangling destination symlink,
          the above open fails with EEXIST.  If that happens, and
-         readlinkat shows that it is a symlink, then we
+         issymlinkat shows that it is a symlink, then we
          have a problem: trying to resolve this dangling symlink to
          a directory/destination-entry pair is fundamentally racy,
          so punt.  If x->open_dangling_dest_symlink is set (cp sets
@@ -1454,8 +1455,7 @@ copy_reg (char const *src_name, char const *dst_name,
          only when copying, i.e., not in move_mode.  */
       if (dest_desc < 0 && dest_errno == EEXIST && ! x->move_mode)
         {
-          char dummy[1];
-          if (0 <= readlinkat (dst_dirfd, dst_relname, dummy, sizeof dummy))
+          if (issymlinkat (dst_dirfd, dst_relname) == 1)
             {
               if (x->open_dangling_dest_symlink)
                 {
index fead0bb198da67139e5593cde143d588bbfa4213..9ca3f7ed15bc47be06be22dbd7f572ba0cdebd06 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/types.h>
 
 #include "system.h"
+#include "issymlink.h"
 #include "prog-fprintf.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
@@ -269,8 +270,7 @@ main (int argc, char **argv)
                       /* Ensure the last component was a symlink.  */
                       char *dir_arg = xstrdup (dir);
                       strip_trailing_slashes (dir);
-                      char linkbuf[1];
-                      if (0 <= readlink (dir, linkbuf, 1))
+                      if (issymlink (dir) == 1)
                         {
                           error (0, 0,
                                  _("failed to remove %s:"
index ce3a3285bee796bb7f36f1006eab7d044b4ed75e..753963937755dcf29c42c85b330aed6940bf0d73 100644 (file)
@@ -39,6 +39,7 @@
 #include "fcntl--.h"
 #include "iopoll.h"
 #include "isapipe.h"
+#include "issymlink.h"
 #include "posixver.h"
 #include "quote.h"
 #include "stat-size.h"
@@ -976,8 +977,7 @@ recheck (struct File_spec *f, bool blocking)
 
   affirm (valid_file_spec (f));
 
-  char linkbuf[1];
-  if (! disable_inotify && 0 <= readlink (f->name, linkbuf, 1))
+  if (! disable_inotify && issymlink (f->name) == 1)
     {
       /* Diagnose the edge case where a regular file is changed
          to a symlink.  We avoid inotify with symlinks since
@@ -1350,9 +1350,8 @@ any_non_remote_file (const struct File_spec *f, int n_files)
 static bool
 any_symlinks (const struct File_spec *f, int n_files)
 {
-  char linkbuf[1];
   for (int i = 0; i < n_files; i++)
-    if (0 <= readlink (f[i].name, linkbuf, 1))
+    if (issymlink (f[i].name) == 1)
       return true;
   return false;
 }
index 0c0785b9ca507fa4e81fdf16ede71f8a3eaa8abe..61bf8b741274093fb420316f9906f31cab9dc6ea 100644 (file)
@@ -41,6 +41,7 @@
 #include "system.h"
 #include "assure.h"
 #include "c-ctype.h"
+#include "issymlink.h"
 #include "quote.h"
 #include "stat-time.h"
 #include "strnumcmp.h"
@@ -467,8 +468,7 @@ unary_operator (void)
 
     case 'h':                  /* File is a symbolic link? */
       unary_advance ();
-      char linkbuf[1];
-      return 0 <= readlink (argv[pos - 1], linkbuf, 1);
+      return issymlink (argv[pos - 1]) == 1;
 
     case 'u':                  /* File is setuid? */
       unary_advance ();