From 7851cfbcac23fc8c8e167c3691a1f63fd277fa1a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 31 May 2018 23:39:31 +0900 Subject: path-util: introduce path_simplify() The function is similar to path_kill_slashes() but also removes initial './', trailing '/.', and '/./' in the path. When the second argument of path_simplify() is false, then it behaves as the same as path_kill_slashes(). Hence, this also replaces path_kill_slashes() with path_simplify(). --- src/test/test-path-util.c | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'src/test') diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index e7a247027..80bca48e0 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -26,6 +26,16 @@ assert_se(path_equal(b, a) == !result); \ } +static void test_path_simplify(const char *in, const char *out, const char *out_dot) { + char *p; + + p = strdupa(in); + assert_se(streq(path_simplify(p, false), out)); + + p = strdupa(in); + assert_se(streq(path_simplify(p, true), out_dot)); +} + static void test_path(void) { _cleanup_close_ int fd = -1; @@ -69,15 +79,28 @@ static void test_path(void) { assert_se(fd >= 0); assert_se(fd_is_mount_point(fd, "/", 0) > 0); - { - char p1[] = "aaa/bbb////ccc"; - char p2[] = "//aaa/.////ccc"; - char p3[] = "/./"; - - assert_se(path_equal(path_kill_slashes(p1), "aaa/bbb/ccc")); - assert_se(path_equal(path_kill_slashes(p2), "/aaa/./ccc")); - assert_se(path_equal(path_kill_slashes(p3), "/./")); - } + test_path_simplify("aaa/bbb////ccc", "aaa/bbb/ccc", "aaa/bbb/ccc"); + test_path_simplify("//aaa/.////ccc", "/aaa/./ccc", "/aaa/ccc"); + test_path_simplify("///", "/", "/"); + test_path_simplify("///.//", "/.", "/"); + test_path_simplify("///.//.///", "/./.", "/"); + test_path_simplify("////.././///../.", "/.././../.", "/../.."); + test_path_simplify(".", ".", ""); + test_path_simplify("./", ".", ""); + test_path_simplify(".///.//./.", "./././.", ""); + test_path_simplify(".///.//././/", "./././.", ""); + test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.", + "/./aaa/././.bbb/../c./d.dd/..eeee/.", + "/aaa/.bbb/../c./d.dd/..eeee"); + test_path_simplify("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", + "/./aaa/././.bbb/../c./d.dd/..eeee/..", + "/aaa/.bbb/../c./d.dd/..eeee/.."); + test_path_simplify(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", + "././aaa/././.bbb/../c./d.dd/..eeee/..", + "aaa/.bbb/../c./d.dd/..eeee/.."); + test_path_simplify("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..", + ".././aaa/././.bbb/../c./d.dd/..eeee/..", + "../aaa/.bbb/../c./d.dd/..eeee/.."); assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo")); assert_se(PATH_IN_SET("/bin", "/bin")); -- cgit v1.2.3