mirror of
https://github.com/systemd/systemd.git
synced 2025-03-19 22:50:17 +03:00
basic/stat-util: add null_or_empty_path_with_root()
This commit is contained in:
parent
9825181143
commit
48542eac39
@ -127,17 +127,22 @@ bool null_or_empty(struct stat *st) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int null_or_empty_path(const char *fn) {
|
||||
int null_or_empty_path_with_root(const char *fn, const char *root) {
|
||||
struct stat st;
|
||||
int r;
|
||||
|
||||
assert(fn);
|
||||
|
||||
/* If we have the path, let's do an easy text comparison first. */
|
||||
if (path_equal(fn, "/dev/null"))
|
||||
/* A symlink to /dev/null or an empty file?
|
||||
* When looking under root_dir, we can't expect /dev/ to be mounted,
|
||||
* so let's see if the path is a (possibly dangling) symlink to /dev/null. */
|
||||
|
||||
if (path_equal_ptr(path_startswith(fn, root ?: "/"), "dev/null"))
|
||||
return true;
|
||||
|
||||
if (stat(fn, &st) < 0)
|
||||
return -errno;
|
||||
r = chase_symlinks_and_stat(fn, root, CHASE_PREFIX_ROOT, NULL, &st, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return null_or_empty(&st);
|
||||
}
|
||||
|
@ -31,9 +31,13 @@ static inline int dir_is_populated(const char *path) {
|
||||
}
|
||||
|
||||
bool null_or_empty(struct stat *st) _pure_;
|
||||
int null_or_empty_path(const char *fn);
|
||||
int null_or_empty_path_with_root(const char *fn, const char *root);
|
||||
int null_or_empty_fd(int fd);
|
||||
|
||||
static inline int null_or_empty_path(const char *fn) {
|
||||
return null_or_empty_path_with_root(fn, NULL);
|
||||
}
|
||||
|
||||
int path_is_read_only_fs(const char *path);
|
||||
|
||||
int files_same(const char *filea, const char *fileb, int flags);
|
||||
|
@ -18,6 +18,30 @@
|
||||
#include "tests.h"
|
||||
#include "tmpfile-util.h"
|
||||
|
||||
TEST(null_or_empty_path) {
|
||||
assert_se(null_or_empty_path("/dev/null") == 1);
|
||||
assert_se(null_or_empty_path("/dev/tty") == 1); /* We assume that any character device is "empty", bleh. */
|
||||
assert_se(null_or_empty_path("../../../../../../../../../../../../../../../../../../../../dev/null") == 1);
|
||||
assert_se(null_or_empty_path("/proc/self/exe") == 0);
|
||||
assert_se(null_or_empty_path("/nosuchfileordir") == -ENOENT);
|
||||
}
|
||||
|
||||
TEST(null_or_empty_path_with_root) {
|
||||
assert_se(null_or_empty_path_with_root("/dev/null", NULL) == 1);
|
||||
assert_se(null_or_empty_path_with_root("/dev/null", "/") == 1);
|
||||
assert_se(null_or_empty_path_with_root("/dev/null", "/.././../") == 1);
|
||||
assert_se(null_or_empty_path_with_root("/dev/null", "/.././..") == 1);
|
||||
assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", NULL) == 1);
|
||||
assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", "/") == 1);
|
||||
assert_se(null_or_empty_path_with_root("/proc/self/exe", NULL) == 0);
|
||||
assert_se(null_or_empty_path_with_root("/proc/self/exe", "/") == 0);
|
||||
assert_se(null_or_empty_path_with_root("/nosuchfileordir", NULL) == -ENOENT);
|
||||
assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././../") == -ENOENT);
|
||||
assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././..") == -ENOENT);
|
||||
assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar") == 1);
|
||||
assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar/") == 1);
|
||||
}
|
||||
|
||||
TEST(files_same) {
|
||||
_cleanup_close_ int fd = -1;
|
||||
char name[] = "/tmp/test-files_same.XXXXXX";
|
||||
|
Loading…
x
Reference in New Issue
Block a user