basic/stat-util: add null_or_empty_path_with_root()

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2022-03-29 15:07:05 +02:00
parent 9825181143
commit 48542eac39
3 changed files with 39 additions and 6 deletions
+10 -5
View File
@@ -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);
}