Unlike ls, find does treat ENOENT specially. Add an extra test (and fix
the behavior) for the case of ENOENT for a path provided on the command
line --- unlike other ENOENT cases (typically dangling symlinks), ENOENT
for a command line argument should report an error.

Also remove obsolete `|sed` from the symlink loop test.
---
 tests/find.test   | 8 +++++++-
 toys/posix/find.c | 8 ++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
From 1939435f912c87eb25f6ef24681604c2b4eb98ac Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Wed, 28 Aug 2019 14:37:50 -0700
Subject: [PATCH] Fix find(1) after c26870dab3462c6176936384b090df6b9ba46dee.

Unlike ls, find does treat ENOENT specially. Add an extra test (and fix
the behavior) for the case of ENOENT for a path provided on the command
line --- unlike other ENOENT cases (typically dangling symlinks), ENOENT
for a command line argument should report an error.

Also remove obsolete `|sed` from the symlink loop test.
---
 tests/find.test   | 8 +++++++-
 toys/posix/find.c | 8 ++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/tests/find.test b/tests/find.test
index fea6a3fc..b3e2c4fa 100755
--- a/tests/find.test
+++ b/tests/find.test
@@ -102,18 +102,24 @@ testing "-printf" "find dir -name file -printf '%f %p %P %s'" \
   "file dir/file file 0" "" ""
 testing "-printf .N" "find dir -name file -printf %.2f" "fi" "" ""
 
+# No error message for a dangling link.
 ln -s does-not-exist dir/dangler
 testing "-L dangling symlink silent" \
   "LANG=C find -L dir -name file 2>&1" "dir/file\n" "" ""
 rm -f dir/dangler
 
+# An error for a symlink loop.
 ln -s looper dir/looper
 testing "-L symlink loop noisy" \
-  "LANG=C find -L dir -name file 2>err | sed s/\'//g ; grep -q dir/looper err || echo missing error" \
+  "LANG=C find -L dir -name file 2>err ; grep -q dir/looper err || echo missing error" \
   "dir/file\n" "" ""
 rm -f dir/looper err
 
 testing "-false" "find dir -false" "" "" ""
 testing "-true" "find dir/file -true" "dir/file\n" "" ""
 
+testing "missing root error" \
+  "LANG=C find -L dir/missing-root 2>err ; grep -q dir/missing-root err || echo missing error" \
+  "" "" ""
+
 rm -rf dir
diff --git a/toys/posix/find.c b/toys/posix/find.c
index 782cc3bc..58a6f88d 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -216,6 +216,14 @@ static int do_find(struct dirtree *new)
 
   // skip . and .. below topdir, handle -xdev and -depth
   if (new) {
+    // Handle stat failures first.
+    if (!new->st.st_blksize && !new->st.st_dev && !new->st.st_ino) {
+      if (!new->parent || errno != ENOENT) {
+        perror_msg("'%s'", s = dirtree_path(new, 0));
+        free(s);
+      }
+      return 0;
+    }
     if (new->parent) {
       if (!dirtree_notdotdot(new)) return 0;
       if (TT.xdev && new->st.st_dev != new->parent->st.st_dev) recurse = 0;
-- 
2.23.0.187.g17f5b7556c-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to