chase: replace path_prefix_root_cwd() with chaseat_prefix_root()

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.
This commit is contained in:
Yu Watanabe
2023-04-19 03:38:59 +09:00
parent 8d3c49b168
commit 60e761d8f3
8 changed files with 74 additions and 72 deletions
+37
View 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);
-39
View 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;