]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
chase: replace path_prefix_root_cwd() with chaseat_prefix_root()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 17 Apr 2023 15:09:54 +0000 (00:09 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 Apr 2023 18:38:59 +0000 (03:38 +0900)
The function path_prefix_root_cwd() was introduced for prefixing the
result from chaseat() with root, but
- it is named slightly generic,
- the logic is different from what chase() does.

This makes the name more explanative and specific for the result of the
chaseat(), and make the logic consistent with chase().

Fixes https://github.com/systemd/systemd/pull/27199#issuecomment-1511387731.

Follow-up for #27199.

src/basic/chase.c
src/basic/chase.h
src/basic/os-util.c
src/basic/path-util.c
src/basic/path-util.h
src/shared/find-esp.c
src/test/test-chase.c
src/test/test-path-util.c

index 96b59e7845ff8d90236e18037b304bde69f0d71f..373252b64526f28c5f2e8acc2befc8569347022b 100644 (file)
@@ -583,6 +583,37 @@ int chase(const char *path, const char *root, ChaseFlags flags, char **ret_path,
         return r;
 }
 
+int chaseat_prefix_root(const char *path, const char *root, char **ret) {
+        char *q;
+        int r;
+
+        assert(path);
+        assert(ret);
+
+        /* This is mostly for prefixing the result of chaseat(). */
+
+        if (!path_is_absolute(path)) {
+                _cleanup_free_ char *root_abs = NULL;
+
+                /* If the dir_fd points to the root directory, chaseat() always returns an absolute path. */
+                assert(!empty_or_root(root));
+
+                r = path_make_absolute_cwd(root, &root_abs);
+                if (r < 0)
+                        return r;
+
+                root = path_simplify(root_abs);
+
+                q = path_join(root, path + (path[0] == '.' && IN_SET(path[1], '/', '\0')));
+        } else
+                q = strdup(path);
+        if (!q)
+                return -ENOMEM;
+
+        *ret = q;
+        return 0;
+}
+
 int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, int open_flags, char **ret_path) {
         _cleanup_close_ int path_fd = -EBADF;
         _cleanup_free_ char *p = NULL, *fname = NULL;
index 40121f7d70c7483082b9d51e9e2c01b0db42d7c3..f37e8368227c75b7abd4df86373f1282b8d7f572 100644 (file)
@@ -42,6 +42,8 @@ bool unsafe_transition(const struct stat *a, const struct stat *b);
 
 int chase(const char *path_with_prefix, const char *root, ChaseFlags chase_flags, char **ret_path, int *ret_fd);
 
+int chaseat_prefix_root(const char *path, const char *root, char **ret);
+
 int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, int open_flags, char **ret_path);
 int chase_and_opendir(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir);
 int chase_and_stat(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat);
index dd8faf23765ce9f193a5eb72082873752aed0512..5d06e20871cb3858135ca3e43c9e744f3369acd7 100644 (file)
@@ -155,7 +155,7 @@ int open_os_release(const char *root, char **ret_path, int *ret_fd) {
                 return r;
 
         if (ret_path) {
-                r = path_prefix_root_cwd(p, root, ret_path);
+                r = chaseat_prefix_root(p, root, ret_path);
                 if (r < 0)
                         return r;
         }
@@ -292,7 +292,7 @@ int open_extension_release(
                 return r;
 
         if (ret_path) {
-                r = path_prefix_root_cwd(p, root, ret_path);
+                r = chaseat_prefix_root(p, root, ret_path);
                 if (r < 0)
                         return r;
         }
index fa2e26789f39cbe1a63701dc094998a9340601e2..0b0f0da7602675b5e33797c09dc1f403413442a7 100644 (file)
@@ -100,34 +100,6 @@ int path_make_absolute_cwd(const char *p, char **ret) {
         return 0;
 }
 
-int path_prefix_root_cwd(const char *p, const char *root, char **ret) {
-        _cleanup_free_ char *root_abs = NULL;
-        char *c;
-        int r;
-
-        assert(p);
-        assert(ret);
-
-        /* Unlike path_make_absolute(), this always prefixes root path if specified.
-         * The root path is always simplified, but the provided path will not.
-         * This is useful for prefixing the result of chaseat(). */
-
-        if (empty_or_root(root))
-                return path_make_absolute_cwd(p, ret);
-
-        r = path_make_absolute_cwd(root, &root_abs);
-        if (r < 0)
-                return r;
-
-        path_simplify(root_abs);
-        c = path_join(root_abs, p);
-        if (!c)
-                return -ENOMEM;
-
-        *ret = c;
-        return 0;
-}
-
 int path_make_relative(const char *from, const char *to, char **ret) {
         _cleanup_free_ char *result = NULL;
         unsigned n_parents;
index a0af9de6742c5ca3bab7d360a2a81020ec3864c9..7843599816e69972bc283daf7e074df654adbe0d 100644 (file)
@@ -60,7 +60,6 @@ int path_split_and_make_absolute(const char *p, char ***ret);
 char* path_make_absolute(const char *p, const char *prefix);
 int safe_getcwd(char **ret);
 int path_make_absolute_cwd(const char *p, char **ret);
-int path_prefix_root_cwd(const char *p, const char *root, char **ret);
 int path_make_relative(const char *from, const char *to, char **ret);
 int path_make_relative_parent(const char *from_child, const char *to, char **ret);
 char *path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) _pure_;
index 6a0002a2bdfa07f8b990f43b70db915cc02cb6f3..c4cf50851716a4cef3f2ba374368597718397ec2 100644 (file)
@@ -540,7 +540,7 @@ int find_esp_and_warn(
                 return r;
 
         if (ret_path) {
-                r = path_prefix_root_cwd(p, root, ret_path);
+                r = chaseat_prefix_root(p, root, ret_path);
                 if (r < 0)
                         return r;
         }
@@ -859,7 +859,7 @@ int find_xbootldr_and_warn(
                 return r;
 
         if (ret_path) {
-                r = path_prefix_root_cwd(p, root, ret_path);
+                r = chaseat_prefix_root(p, root, ret_path);
                 if (r < 0)
                         return r;
         }
index c5fc08ca25a0e961c239b689cc6f2d8e2bb9f704..558f4109e3f5395572b94886c7933f06dfe909f9 100644 (file)
@@ -648,4 +648,41 @@ static int intro(void) {
         return EXIT_SUCCESS;
 }
 
+TEST(chaseat_prefix_root) {
+        _cleanup_free_ char *cwd = NULL, *ret = NULL, *expected = NULL;
+
+        assert_se(safe_getcwd(&cwd) >= 0);
+
+        assert_se(chaseat_prefix_root("/hoge", NULL, &ret) >= 0);
+        assert_se(streq(ret, "/hoge"));
+
+        ret = mfree(ret);
+
+        assert_se(chaseat_prefix_root("/hoge", "a/b/c", &ret) >= 0);
+        assert_se(streq(ret, "/hoge"));
+
+        ret = mfree(ret);
+
+        assert_se(chaseat_prefix_root("hoge", "/a/b//./c///", &ret) >= 0);
+        assert_se(streq(ret, "/a/b/c/hoge"));
+
+        ret = mfree(ret);
+
+        assert_se(chaseat_prefix_root("hoge", "a/b//./c///", &ret) >= 0);
+        assert_se(expected = path_join(cwd, "a/b/c/hoge"));
+        assert_se(streq(ret, expected));
+
+        ret = mfree(ret);
+        expected = mfree(expected);
+
+        assert_se(chaseat_prefix_root("./hoge/aaa/../././b", "/a/b//./c///", &ret) >= 0);
+        assert_se(streq(ret, "/a/b/c/hoge/aaa/../././b"));
+
+        ret = mfree(ret);
+
+        assert_se(chaseat_prefix_root("./hoge/aaa/../././b", "a/b//./c///", &ret) >= 0);
+        assert_se(expected = path_join(cwd, "a/b/c/hoge/aaa/../././b"));
+        assert_se(streq(ret, expected));
+}
+
 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
index 22e8f3481afbf33f1e64c6bdb5b99eafc81f6440..e40ffea4d53e72726e70390c11421229c6981c3d 100644 (file)
@@ -494,45 +494,6 @@ TEST(fsck_exists) {
         assert_se(fsck_exists_for_fstype("/../bin/") == 0);
 }
 
-TEST(path_prefix_root_cwd) {
-        _cleanup_free_ char *cwd = NULL, *ret = NULL, *expected = NULL;
-
-        assert_se(safe_getcwd(&cwd) >= 0);
-
-        assert_se(path_prefix_root_cwd("hoge", NULL, &ret) >= 0);
-        assert_se(expected = path_join(cwd, "hoge"));
-        assert_se(streq(ret, expected));
-
-        ret = mfree(ret);
-        expected = mfree(expected);
-
-        assert_se(path_prefix_root_cwd("/hoge", NULL, &ret) >= 0);
-        assert_se(streq(ret, "/hoge"));
-
-        ret = mfree(ret);
-
-        assert_se(path_prefix_root_cwd("hoge", "/a/b//./c///", &ret) >= 0);
-        assert_se(streq(ret, "/a/b/c/hoge"));
-
-        ret = mfree(ret);
-
-        assert_se(path_prefix_root_cwd("hoge", "a/b//./c///", &ret) >= 0);
-        assert_se(expected = path_join(cwd, "a/b/c/hoge"));
-        assert_se(streq(ret, expected));
-
-        ret = mfree(ret);
-        expected = mfree(expected);
-
-        assert_se(path_prefix_root_cwd("/../hoge/aaa/../././b", "/a/b//./c///", &ret) >= 0);
-        assert_se(streq(ret, "/a/b/c/../hoge/aaa/../././b"));
-
-        ret = mfree(ret);
-
-        assert_se(path_prefix_root_cwd("/../hoge/aaa/../././b", "a/b//./c///", &ret) >= 0);
-        assert_se(expected = path_join(cwd, "a/b/c/../hoge/aaa/../././b"));
-        assert_se(streq(ret, expected));
-}
-
 static void test_path_make_relative_one(const char *from, const char *to, const char *expected) {
         _cleanup_free_ char *z = NULL;
         int r;