diff options
author | Jim Meyering <meyering@redhat.com> | 2008-01-30 13:43:15 +0100 |
---|---|---|
committer | Jim Meyering <meyering@redhat.com> | 2008-01-30 13:51:38 +0100 |
commit | ed5c4e770a27862813c0182be8680abeb005d15b (patch) | |
tree | 9eff6d3bb6c974405a392116607daddde202b0ef /src/system.h | |
parent | 194ca7b3f9d7992c6c40804d8d15fda5e257604a (diff) | |
download | coreutils-ed5c4e770a27862813c0182be8680abeb005d15b.tar.xz |
Improve "rmdir --ignore-fail-on-non-empty"
* src/rmdir.c (remove_parents, main): With --ignore-fail-on-non-empty,
suppress a diagnostic also for other errno values, which can arise
with read-only media or when the parent directory has the immutable
attribute (set via chattr +i).
(errno_may_be_empty, ignorable_failure): New functions.
* src/remove.c (is_empty_dir): Move function to ...
* src/system.h (is_empty_dir): ...here, and make it inline.
Suggested by Josselin Mouette in <http://bugs.debian.org/363011>
via Bob Proulx.
* NEWS: Mention the improvement.
Diffstat (limited to 'src/system.h')
-rw-r--r-- | src/system.h | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/system.h b/src/system.h index 54c8a8b3d..b0b954510 100644 --- a/src/system.h +++ b/src/system.h @@ -384,6 +384,36 @@ readdir_ignoring_dot_and_dotdot (DIR *dirp) } } +/* Return true if DIR is determined to be an empty directory. */ +static inline bool +is_empty_dir (int fd_cwd, char const *dir) +{ + DIR *dirp; + struct dirent const *dp; + int saved_errno; + int fd = openat (fd_cwd, dir, + (O_RDONLY | O_DIRECTORY + | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK)); + + if (fd < 0) + return false; + + dirp = fdopendir (fd); + if (dirp == NULL) + { + close (fd); + return false; + } + + errno = 0; + dp = readdir_ignoring_dot_and_dotdot (dirp); + saved_errno = errno; + closedir (dirp); + if (dp != NULL) + return false; + return saved_errno == 0 ? true : false; +} + /* Factor out some of the common --help and --version processing code. */ /* These enum values cannot possibly conflict with the option values |