diff options
author | Lennart Poettering <lennart@poettering.net> | 2022-05-04 10:53:00 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2022-05-04 13:29:14 +0200 |
commit | db55bbf29b8d0268884348bf3270b8b2a2db3b31 (patch) | |
tree | f17d57f6f02880a87ac4fb067768b797a0fe1d7b /src/basic/stat-util.c | |
parent | 080b8c2ace2dcc4ee3faf5b0ee95f5117aa27dab (diff) | |
download | systemd-db55bbf29b8d0268884348bf3270b8b2a2db3b31.tar.gz |
stat-util: fix dir_is_empty() with hidden/backup files
This is a follow-up for f470cb6d13558fc06131dc677d54a089a0b07359 which in
turn is a follow-up for a068aceafbffcba85398cce636c25d659265087a.
The latter started to honour hidden files when deciding whether a
directory is empty. The former reverted to the old behaviour to fix
issue #23220.
It introduced a bug though: when a directory contains a larger number of
hidden entries the getdents64() buffer will not suffice to read them,
since we just allocate three entries for it (which is definitely enough
if we just ignore the . + .. entries, but not ig we ignore more).
I think it's a bit confusing that dir_is_empty() can return true even if
rmdir() on the dir would return ENOTEMPTY. Hence, let's rework the
function to make it optional whether hidden files are ignored or not.
After all, I looking at the users of this function I am pretty sure in
more cases we want to honour hidden files.
Diffstat (limited to 'src/basic/stat-util.c')
-rw-r--r-- | src/basic/stat-util.c | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c index a7812714aa..64c2f80f3c 100644 --- a/src/basic/stat-util.c +++ b/src/basic/stat-util.c @@ -71,13 +71,10 @@ int is_device_node(const char *path) { return !!(S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode)); } -int dir_is_empty_at(int dir_fd, const char *path) { +int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup) { _cleanup_close_ int fd = -1; - /* Allocate space for at least 3 full dirents, since every dir has at least two entries ("." + - * ".."), and only once we have seen if there's a third we know whether the dir is empty or not. */ - DEFINE_DIRENT_BUFFER(buffer, 3); - struct dirent *de; - ssize_t n; + struct dirent *buf; + size_t m; if (path) { assert(dir_fd >= 0 || dir_fd == AT_FDCWD); @@ -99,15 +96,30 @@ int dir_is_empty_at(int dir_fd, const char *path) { return fd; } - n = getdents64(fd, &buffer, sizeof(buffer)); - if (n < 0) - return -errno; + /* Allocate space for at least 3 full dirents, since every dir has at least two entries ("." + + * ".."), and only once we have seen if there's a third we know whether the dir is empty or not. If + * 'ignore_hidden_or_backup' is true we'll allocate a bit more, since we might skip over a bunch of + * entries that we end up ignoring. */ + m = (ignore_hidden_or_backup ? 16 : 3) * DIRENT_SIZE_MAX; + buf = alloca(m); + + for (;;) { + struct dirent *de; + ssize_t n; + + n = getdents64(fd, buf, m); + if (n < 0) + return -errno; + if (n == 0) + break; - msan_unpoison(&buffer, n); + assert((size_t) n <= m); + msan_unpoison(buf, n); - FOREACH_DIRENT_IN_BUFFER(de, &buffer.de, n) - if (!hidden_or_backup_file(de->d_name)) - return 0; + FOREACH_DIRENT_IN_BUFFER(de, buf, n) + if (!(ignore_hidden_or_backup ? hidden_or_backup_file(de->d_name) : dot_or_dot_dot(de->d_name))) + return 0; + } return 1; } |