return 0;
}
-static int empty_or_root_to_null(const char **path) {
- int r;
-
- assert(path);
-
- /* This nullifies the input path when the path is empty or points to "/". */
-
- if (empty_or_root(*path)) {
- *path = NULL;
- return 0;
- }
-
- r = path_is_root(*path);
- if (r < 0)
- return r;
- if (r > 0)
- *path = NULL;
-
- return 0;
-}
-
int chase(const char *path, const char *root, ChaseFlags flags, char **ret_path, int *ret_fd) {
_cleanup_free_ char *root_abs = NULL, *absolute = NULL, *p = NULL;
_cleanup_close_ int fd = -EBADF, pfd = -EBADF;
if (isempty(path))
return -EINVAL;
- r = empty_or_root_to_null(&root);
+ r = empty_or_root_harder_to_null(&root);
if (r < 0)
return r;
if (!path_is_absolute(path)) {
_cleanup_free_ char *root_abs = NULL;
- r = empty_or_root_to_null(&root);
+ r = empty_or_root_harder_to_null(&root);
if (r < 0 && r != -ENOENT)
return r;
if (!path_is_absolute(path))
return -EINVAL;
- r = empty_or_root_to_null(&root);
+ r = empty_or_root_harder_to_null(&root);
if (r < 0 && r != -ENOENT)
return r;
return isempty(path) ? "/" : path;
}
+int empty_or_root_harder_to_null(const char **path) {
+ int r;
+
+ assert(path);
+
+ /* This nullifies the input path when the path is empty or points to "/". */
+
+ if (empty_or_root(*path)) {
+ *path = NULL;
+ return 0;
+ }
+
+ r = path_is_root(*path);
+ if (r < 0)
+ return r;
+ if (r > 0)
+ *path = NULL;
+
+ return 0;
+}
+
bool path_strv_contains(char * const *l, const char *path) {
assert(path);
bool empty_or_root(const char *path) _pure_;
const char* empty_to_root(const char *path) _pure_;
+int empty_or_root_harder_to_null(const char **path);
bool path_strv_contains(char * const *l, const char *path);
bool prefixed_path_strv_contains(char * const *l, const char *path);
assert_se(!empty_or_root("//yy//"));
}
+TEST(empty_or_root_harder_to_null) {
+ const char *p;
+
+ p = NULL;
+ ASSERT_OK(empty_or_root_harder_to_null(&p));
+ ASSERT_NULL(p);
+
+ p = "/";
+ ASSERT_OK(empty_or_root_harder_to_null(&p));
+ ASSERT_NULL(p);
+
+ p = "////////";
+ ASSERT_OK(empty_or_root_harder_to_null(&p));
+ ASSERT_NULL(p);
+
+ p = "/../../././//";
+ ASSERT_OK(empty_or_root_harder_to_null(&p));
+ ASSERT_NULL(p);
+
+ p = "/usr";
+ ASSERT_OK(empty_or_root_harder_to_null(&p));
+ ASSERT_STREQ(p, "/usr");
+
+ p = "/usr/../../../";
+ ASSERT_OK(empty_or_root_harder_to_null(&p));
+ ASSERT_NULL(p);
+
+ p = "/usr/";
+ ASSERT_OK(empty_or_root_harder_to_null(&p));
+ ASSERT_STREQ(p, "/usr/");
+
+ p = "///././/../../usr////";
+ ASSERT_OK(empty_or_root_harder_to_null(&p));
+ ASSERT_STREQ(p, "///././/../../usr////");
+}
+
TEST(path_startswith_set) {
ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), "");
ASSERT_STREQ(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar");