From 0b2cfcb8fdea9673f3c2e0940f1b16d5825e16ea Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 29 Oct 2019 10:59:46 -0500 Subject: Let "find -L -type -l" find dangling symlinks. --- lib/dirtree.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'lib/dirtree.c') diff --git a/lib/dirtree.c b/lib/dirtree.c index df38b257..beaafd59 100644 --- a/lib/dirtree.c +++ b/lib/dirtree.c @@ -32,12 +32,18 @@ struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags) int len = 0, linklen = 0, statless = 0; if (name) { - // open code this because haven't got node to call dirtree_parentfd() on yet - int fd = parent ? parent->dirfd : AT_FDCWD; - - if (fstatat(fd, name, &st,AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW))) { - if (flags&DIRTREE_STATLESS) statless++; - else goto error; + // open code fd = because haven't got node to call dirtree_parentfd() on yet + int fd = parent ? parent->dirfd : AT_FDCWD, + sym = AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW); + + // stat dangling symlinks + if (fstatat(fd, name, &st, sym)) { + if (errno != ENOENT + || (!sym && fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW))) + { + if (flags&DIRTREE_STATLESS) statless++; + else goto error; + } } if (S_ISLNK(st.st_mode)) { if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error; -- cgit v1.2.3