On 21/11/2026 00:09, Collin Funk wrote:
Pádraig Brady <[email protected]> writes:

On 20/11/2026 21:39, Collin Funk wrote:
I was looking at this one a bit yesterday (didn't have anything close to
a patch being finished, so don't worry about that), and wanted to see if
FTS_NOSTAT could be used to avoid stat'ing twice where O_PATH is
available. I guess it doesn't matter too much in practice. It is a
larger refactoring that can probably just be done later.

Oh good point. I'll have a look at that optimization.

NP. My thinking was to enable FTS_NOSTAT always, and then just handle
the stat in adjust_file_mode(). Perhaps it is just me, but I find that
conditionally using FTS_NOSTAT as src/chown-core.c does require a bit
more careful thought.

FTS_NOSTAT still does stat command-line arguments, though. I was
conflicted about whether it was worth handling the FTS_ROOTLEVEL case or
not, to avoid stat.

I've done the FTS_NOSTAT in the attached.
It's worth doing as it halves the stats,
and is very little code and mostly comments.
The tricky bit was to do with symlink handling.

Also, I didn't love calling it a bug given that O_PATH and AT_EMPTY_PATH
are Linux-isms, meaning that the bug, i.e., the second lookup, occurs
everywhere else. Maybe that can be mentioned in the NEWS entry, if it
can be done without making it too wordy?
Well I only did a quick search on FreeBSD and it seems
these are available since 13.1 (2022), but I have not tested there.
I will test there and make the NEWS less Linux specific.

True, I am probably just splitting hairs. 2022 is relatively new. The
'shred' races, e.g., were also because the program was written before
openat existed. I felt less bad about calling those bugs since we were a
bit later in that case. :)

Oh I see what you're saying.
Given this is quite an edge case problem,
and this avoidance is only possible with kernel/libc
features available relatively recently
(glibc >= 2.39 (2024) on Kernel >= 6.6 (2023), or FreeBSD >= 13.1 (2022))
then it could be classed as an improvement rather than a bug fix.

I'll clarify version details in NEWS anyway.

BTW I've just tested on FreeBSD 14.1 and all tests pass
(and the new code was used there).

cheers,
Padraig.
From 5d9c3c0b5ee245fea4da0a39ea464e895b7879fe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Fri, 20 Nov 2026 23:55:15 +0000
Subject: [PATCH] chmod: optimize the number of stat calls

Halve the number of stat calls (when not using --verbose)
by avoiding stat() within FTS traversal.

* src/chmod.c (process_file): Adjust for consistent symlink handling.
(main): Only do FTS stat() with --verbose.
* tests/chmod/thru-dangling.sh: Add a test case to ensure encountered
dangling symlinks are diagnosed with --dereference.
Suggested by Collin Funk.
---
 src/chmod.c                  | 22 +++++++++++++++++++---
 tests/chmod/thru-dangling.sh |  5 +++++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/src/chmod.c b/src/chmod.c
index 6aececb77..d1dbb9b5b 100644
--- a/src/chmod.c
+++ b/src/chmod.c
@@ -232,7 +232,11 @@ adjust_file_mode (int dirfd, char const *file, bool follow, bool ignore_missing,
                   int *changed_fd)
 {
   *changed_fd = -1;
-  if (ROOT_DEV_INO_CHECK (root_dev_ino, st))
+  /* FTS supplies full stat information for directories even with FTS_NOSTAT.
+     We do this separate --preseve-root check as it may be different with -H,-L
+     (where FTS follows a symlink to '/'), while -h would select the link
+     itself for the fstat below.  Check the directory so FTS skips it.  */
+  if (S_ISDIR (st->st_mode) && ROOT_DEV_INO_CHECK (root_dev_ino, st))
     return ROOT_PRESERVED;
 #if defined O_PATH && defined AT_EMPTY_PATH && HAVE_WORKING_O_NOFOLLOW
   int fd = openat (dirfd, file, O_PATH | O_CLOEXEC | (follow ? 0 : O_NOFOLLOW));
@@ -307,12 +311,18 @@ process_file (FTS *fts, FTSENT *ent)
   struct change_status ch = {0};
   ch.status = CH_NO_STAT;
   struct stat stat_buf;
-  bool ignore_missing = ignore_missing_fts_entry (ent);
+
+  /* Adjust so a symlink identified by readdir
+     or by FTS' stat() are handled consistently.  */
+  int fts_info = ent->fts_info;
+  if (fts_info == FTS_NSOK && S_ISLNK (file_stats->st_mode))
+    fts_info = FTS_SL;
+  bool ignore_missing = fts_info != FTS_SL && ignore_missing_fts_entry (ent);
 
   if (ignore_missing && ignorable_fts_error (ent))
     return true;
 
-  switch (ent->fts_info)
+  switch (fts_info)
     {
     case FTS_DP:
       return true;
@@ -766,6 +776,12 @@ main (int argc, char **argv)
     }
 
   bit_flags |= FTS_DEFER_STAT;
+#if defined O_PATH && defined AT_EMPTY_PATH && HAVE_WORKING_O_NOFOLLOW
+  /* fstat supplies the mode, except when -v reports a failure to open
+     or stat the file.  */
+  if (verbosity != V_high)
+    bit_flags |= FTS_NOSTAT;
+#endif
   ok = process_files (argv + optind, bit_flags);
 
   main_exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
diff --git a/tests/chmod/thru-dangling.sh b/tests/chmod/thru-dangling.sh
index 47ece88dd..15a7c3beb 100755
--- a/tests/chmod/thru-dangling.sh
+++ b/tests/chmod/thru-dangling.sh
@@ -28,4 +28,9 @@ chmod 644 dangle 2> out && fail=1
 echo "chmod: cannot operate on dangling symlink 'dangle'" > exp
 compare exp out || fail=1
 
+# A symlink identified by readdir must still get the dereference diagnostic.
+mkdir dir && mv dangle dir/ || framework_failure_
+returns_ 1 chmod -RH --dereference u+w dir 2>out || fail=1
+grep -F "cannot dereference 'dir/dangle'" out || fail=1
+
 Exit $fail
-- 
2.56.0

Reply via email to