ENOENT is ignored, but other errors are reported.
---
 lib/dirtree.c     | 2 +-
 tests/find.test   | 4 ++++
 toys/posix/find.c | 6 ++++--
 3 files changed, 9 insertions(+), 3 deletions(-)
From 703fe2ad975e91268d445540c3108490ae5eaa1b Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Tue, 6 Aug 2019 13:21:21 -0700
Subject: [PATCH] find: fix dangling symlink behavior.

ENOENT is ignored, but other errors are reported.
---
 lib/dirtree.c     | 2 +-
 tests/find.test   | 4 ++++
 toys/posix/find.c | 6 ++++--
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/lib/dirtree.c b/lib/dirtree.c
index c5cf654a..e44df66c 100644
--- a/lib/dirtree.c
+++ b/lib/dirtree.c
@@ -36,7 +36,7 @@ struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags)
     int fd = parent ? parent->dirfd : AT_FDCWD;
 
     if (fstatat(fd, name, &st,AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW))) {
-      if (flags&DIRTREE_STATLESS) statless++;
+      if ((flags&DIRTREE_STATLESS) && errno == ENOENT) statless++;
       else goto error;
     }
     if (S_ISLNK(st.st_mode)) {
diff --git a/tests/find.test b/tests/find.test
index ab84fb07..cbca8e76 100755
--- a/tests/find.test
+++ b/tests/find.test
@@ -104,6 +104,10 @@ 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" "" ""
 
+ln -s does-not-exist dir/dangler
+ln -s looper dir/looper
+testing "-L dangling link" "LANG=C find -L dir -name file 2>&1 | sed s/\'//g" \
+  "dir/file\nfind: dir/looper: Too many levels of symbolic links\n" "" ""
 
 testing "-false" "find dir -false" "" "" ""
 testing "-true" "find dir/file -true" "dir/file\n" "" ""
diff --git a/toys/posix/find.c b/toys/posix/find.c
index 5fc9b0ce..782cc3bc 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -211,7 +211,8 @@ static int do_find(struct dirtree *new)
   struct double_list *argdata = TT.argdata;
   char *s, **ss;
 
-  recurse = DIRTREE_COMEAGAIN|(DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L));
+  recurse = DIRTREE_STATLESS|DIRTREE_COMEAGAIN|
+    (DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L));
 
   // skip . and .. below topdir, handle -xdev and -depth
   if (new) {
@@ -669,7 +670,8 @@ void find_main(void)
 
   // Loop through paths
   for (i = 0; i < len; i++)
-    dirtree_flagread(ss[i], DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L)),
+    dirtree_flagread(ss[i],
+      DIRTREE_STATLESS|(DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L))),
       do_find);
 
   execdir(0, 1);
-- 
2.22.0.770.g0f2c4a37fd-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to