util: add generic calls for prefixing a root directory to a path
So far a number of utilities implemented their own calls for this, unify them in prefix_root() and prefix_roota(). The former uses heap memory, the latter allocates from the stack via alloca(). Port over most users of a --root= logic.
This commit is contained in:
parent
8b44a3d22c
commit
1d13f648d0
7 changed files with 109 additions and 23 deletions
|
@ -294,6 +294,34 @@ static void test_path_startswith(void) {
|
|||
assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
|
||||
}
|
||||
|
||||
static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
const char *t;
|
||||
|
||||
assert_se(s = prefix_root(r, p));
|
||||
assert_se(streq_ptr(s, expected));
|
||||
|
||||
t = prefix_roota(r, p);
|
||||
assert_se(t);
|
||||
assert_se(streq_ptr(t, expected));
|
||||
}
|
||||
|
||||
static void test_prefix_root(void) {
|
||||
test_prefix_root_one("/", "/foo", "/foo");
|
||||
test_prefix_root_one(NULL, "/foo", "/foo");
|
||||
test_prefix_root_one("", "/foo", "/foo");
|
||||
test_prefix_root_one("///", "/foo", "/foo");
|
||||
test_prefix_root_one("/", "////foo", "/foo");
|
||||
test_prefix_root_one(NULL, "////foo", "/foo");
|
||||
|
||||
test_prefix_root_one("/foo", "/bar", "/foo/bar");
|
||||
test_prefix_root_one("/foo", "bar", "/foo/bar");
|
||||
test_prefix_root_one("foo", "bar", "foo/bar");
|
||||
test_prefix_root_one("/foo/", "/bar", "/foo/bar");
|
||||
test_prefix_root_one("/foo/", "//bar", "/foo/bar");
|
||||
test_prefix_root_one("/foo///", "//bar", "/foo/bar");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
test_path();
|
||||
test_find_binary(argv[0], true);
|
||||
|
@ -304,6 +332,7 @@ int main(int argc, char **argv) {
|
|||
test_make_relative();
|
||||
test_strv_resolve();
|
||||
test_path_startswith();
|
||||
test_prefix_root();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue