From 1d13f648d0fade38194db74b4f82ca68c8a26856 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 13 May 2015 17:42:10 +0200 Subject: 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. --- src/test/test-path-util.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/test/test-path-util.c') diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index e5b9c28bc0..09f0f2f89e 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -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; } -- cgit v1.2.3-54-g00ecf