1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-19 22:50:17 +03:00

stat-util: ignore hidden_or_backup_file when checking if dir is empty

Commit https://github.com/systemd/systemd/commit/a068aceafbf
changed dir_is_emtpy_at to use FOREACH_DIRENT_IN_BUFFER instead of
FOREACH_DIRENT, but used dot_or_dotdot which just checks if the name
is literally '.' or '..' which is not enough, previous behaviour was
to ignore all hidden files, so restore that and add a test case.

Fixes https://github.com/systemd/systemd/issues/23220
This commit is contained in:
Luca Boccassi 2022-04-30 17:47:56 +01:00 committed by Yu Watanabe
parent dfd672f84b
commit f470cb6d13
2 changed files with 8 additions and 2 deletions

View File

@ -106,7 +106,7 @@ int dir_is_empty_at(int dir_fd, const char *path) {
msan_unpoison(&buffer, n);
FOREACH_DIRENT_IN_BUFFER(de, &buffer.de, n)
if (!dot_or_dot_dot(de->d_name))
if (!hidden_or_backup_file(de->d_name))
return 0;
return 1;

View File

@ -151,7 +151,7 @@ TEST(fd_is_ns) {
TEST(dir_is_empty) {
_cleanup_(rm_rf_physical_and_freep) char *empty_dir = NULL;
_cleanup_free_ char *j = NULL, *jj = NULL;
_cleanup_free_ char *j = NULL, *jj = NULL, *jjj = NULL;
assert_se(dir_is_empty_at(AT_FDCWD, "/proc") == 0);
assert_se(dir_is_empty_at(AT_FDCWD, "/icertainlydontexistdoi") == -ENOENT);
@ -169,11 +169,17 @@ TEST(dir_is_empty) {
assert_se(jj);
assert_se(touch(jj) >= 0);
jjj = path_join(empty_dir, ".qqq");
assert_se(jjj);
assert_se(touch(jjj) >= 0);
assert_se(dir_is_empty_at(AT_FDCWD, empty_dir) == 0);
assert_se(unlink(j) >= 0);
assert_se(dir_is_empty_at(AT_FDCWD, empty_dir) == 0);
assert_se(unlink(jj) >= 0);
assert_se(dir_is_empty_at(AT_FDCWD, empty_dir) > 0);
assert_se(unlink(jjj) >= 0);
assert_se(dir_is_empty_at(AT_FDCWD, empty_dir) > 0);
}
static int intro(void) {