X Tutup
Skip to content

Commit 48542ea

Browse files
committed
basic/stat-util: add null_or_empty_path_with_root()
1 parent 9825181 commit 48542ea

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/basic/stat-util.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,22 @@ bool null_or_empty(struct stat *st) {
127127
return false;
128128
}
129129

130-
int null_or_empty_path(const char *fn) {
130+
int null_or_empty_path_with_root(const char *fn, const char *root) {
131131
struct stat st;
132+
int r;
132133

133134
assert(fn);
134135

135-
/* If we have the path, let's do an easy text comparison first. */
136-
if (path_equal(fn, "/dev/null"))
136+
/* A symlink to /dev/null or an empty file?
137+
* When looking under root_dir, we can't expect /dev/ to be mounted,
138+
* so let's see if the path is a (possibly dangling) symlink to /dev/null. */
139+
140+
if (path_equal_ptr(path_startswith(fn, root ?: "/"), "dev/null"))
137141
return true;
138142

139-
if (stat(fn, &st) < 0)
140-
return -errno;
143+
r = chase_symlinks_and_stat(fn, root, CHASE_PREFIX_ROOT, NULL, &st, NULL);
144+
if (r < 0)
145+
return r;
141146

142147
return null_or_empty(&st);
143148
}

src/basic/stat-util.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ static inline int dir_is_populated(const char *path) {
3131
}
3232

3333
bool null_or_empty(struct stat *st) _pure_;
34-
int null_or_empty_path(const char *fn);
34+
int null_or_empty_path_with_root(const char *fn, const char *root);
3535
int null_or_empty_fd(int fd);
3636

37+
static inline int null_or_empty_path(const char *fn) {
38+
return null_or_empty_path_with_root(fn, NULL);
39+
}
40+
3741
int path_is_read_only_fs(const char *path);
3842

3943
int files_same(const char *filea, const char *fileb, int flags);

src/test/test-stat-util.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@
1818
#include "tests.h"
1919
#include "tmpfile-util.h"
2020

21+
TEST(null_or_empty_path) {
22+
assert_se(null_or_empty_path("/dev/null") == 1);
23+
assert_se(null_or_empty_path("/dev/tty") == 1); /* We assume that any character device is "empty", bleh. */
24+
assert_se(null_or_empty_path("../../../../../../../../../../../../../../../../../../../../dev/null") == 1);
25+
assert_se(null_or_empty_path("/proc/self/exe") == 0);
26+
assert_se(null_or_empty_path("/nosuchfileordir") == -ENOENT);
27+
}
28+
29+
TEST(null_or_empty_path_with_root) {
30+
assert_se(null_or_empty_path_with_root("/dev/null", NULL) == 1);
31+
assert_se(null_or_empty_path_with_root("/dev/null", "/") == 1);
32+
assert_se(null_or_empty_path_with_root("/dev/null", "/.././../") == 1);
33+
assert_se(null_or_empty_path_with_root("/dev/null", "/.././..") == 1);
34+
assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", NULL) == 1);
35+
assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", "/") == 1);
36+
assert_se(null_or_empty_path_with_root("/proc/self/exe", NULL) == 0);
37+
assert_se(null_or_empty_path_with_root("/proc/self/exe", "/") == 0);
38+
assert_se(null_or_empty_path_with_root("/nosuchfileordir", NULL) == -ENOENT);
39+
assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././../") == -ENOENT);
40+
assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././..") == -ENOENT);
41+
assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar") == 1);
42+
assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar/") == 1);
43+
}
44+
2145
TEST(files_same) {
2246
_cleanup_close_ int fd = -1;
2347
char name[] = "/tmp/test-files_same.XXXXXX";

0 commit comments

Comments
 (0)
X Tutup