path-util: introduce path_simplify()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 31 May 2018 14:39:31 +0000 (23:39 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 3 Jun 2018 14:39:26 +0000 (23:39 +0900)
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().

32 files changed:
src/basic/cgroup-util.c
src/basic/fileio.c
src/basic/mount-util.c
src/basic/path-util.c
src/basic/path-util.h
src/basic/unit-name.c
src/cgls/cgls.c
src/cgtop/cgtop.c
src/core/automount.c
src/core/cgroup.c
src/core/dbus-execute.c
src/core/dbus-path.c
src/core/dbus-socket.c
src/core/load-fragment.c
src/core/manager.c
src/core/mount.c
src/core/path.c
src/core/service.c
src/core/socket.c
src/core/swap.c
src/core/unit.c
src/delta/delta.c
src/fstab-generator/fstab-generator.c
src/libsystemd/sd-device/sd-device.c
src/mount/mount-tool.c
src/portable/portable.c
src/shared/conf-parser.c
src/shared/install.c
src/shared/machine-image.c
src/sysusers/sysusers.c
src/test/test-path-util.c
src/tmpfiles/tmpfiles.c

index 1b0221e..ca31dee 100644 (file)
@@ -628,7 +628,7 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch
                 if (!t)
                         return -ENOMEM;
 
-                *fs = path_kill_slashes(t);
+                *fs = path_simplify(t, false);
                 return 0;
         }
 
@@ -645,7 +645,7 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch
         if (r < 0)
                 return r;
 
-        path_kill_slashes(*fs);
+        path_simplify(*fs, false);
         return 0;
 }
 
@@ -1250,7 +1250,7 @@ int cg_split_spec(const char *spec, char **controller, char **path) {
                         if (!t)
                                 return -ENOMEM;
 
-                        *path = path_kill_slashes(t);
+                        *path = path_simplify(t, false);
                 }
 
                 if (controller)
@@ -1302,7 +1302,7 @@ int cg_split_spec(const char *spec, char **controller, char **path) {
                         return -EINVAL;
                 }
 
-                path_kill_slashes(u);
+                path_simplify(u, false);
         }
 
         if (controller)
@@ -1333,7 +1333,7 @@ int cg_mangle_path(const char *path, char **result) {
                 if (!t)
                         return -ENOMEM;
 
-                *result = path_kill_slashes(t);
+                *result = path_simplify(t, false);
                 return 0;
         }
 
index 6dd00e0..c12b4ac 100644 (file)
@@ -1234,7 +1234,7 @@ int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
 
         strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn), "XXXXXX");
 
-        *ret = path_kill_slashes(t);
+        *ret = path_simplify(t, false);
         return 0;
 }
 
@@ -1275,7 +1275,7 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
 
         *x = 0;
 
-        *ret = path_kill_slashes(t);
+        *ret = path_simplify(t, false);
         return 0;
 }
 
@@ -1315,7 +1315,7 @@ int tempfn_random_child(const char *p, const char *extra, char **ret) {
 
         *x = 0;
 
-        *ret = path_kill_slashes(t);
+        *ret = path_simplify(t, false);
         return 0;
 }
 
index 2774e48..68a83eb 100644 (file)
@@ -413,7 +413,7 @@ int bind_remount_recursive_with_mountinfo(const char *prefix, bool ro, char **bl
         if (!cleaned)
                 return -ENOMEM;
 
-        path_kill_slashes(cleaned);
+        path_simplify(cleaned, false);
 
         done = set_new(&path_hash_ops);
         if (!done)
index 4f42881..769417c 100644 (file)
@@ -159,7 +159,7 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
                         if (!r)
                                 return -ENOMEM;
 
-                        path_kill_slashes(r);
+                        path_simplify(r, false);
 
                         *_r = r;
                         return 0;
@@ -214,7 +214,7 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
                 p = mempcpy(p, "../", 3);
 
         strcpy(p, to_path);
-        path_kill_slashes(r);
+        path_simplify(r, false);
 
         *_r = r;
         return 0;
@@ -235,7 +235,7 @@ int path_strv_make_absolute_cwd(char **l) {
                 if (r < 0)
                         return r;
 
-                path_kill_slashes(t);
+                path_simplify(t, false);
                 free_and_replace(*s, t);
         }
 
@@ -335,17 +335,30 @@ char **path_strv_resolve_uniq(char **l, const char *root) {
         return strv_uniq(l);
 }
 
-char *path_kill_slashes(char *path) {
+char *path_simplify(char *path, bool kill_dots) {
         char *f, *t;
-        bool slash = false;
+        bool slash = false, ignore_slash = false, absolute;
 
-        /* Removes redundant inner and trailing slashes. Modifies the
-         * passed string in-place.
+        assert(path);
+
+        /* Removes redundant inner and trailing slashes. Also removes unnecessary dots
+         * if kill_dots is true. Modifies the passed string in-place.
          *
-         * ///foo///bar/ becomes /foo/bar
+         * ///foo//./bar/.   becomes /foo/./bar/.  (if kill_dots is false)
+         * ///foo//./bar/.   becomes /foo/bar      (if kill_dots is true)
+         * .//./foo//./bar/. becomes ./foo/bar     (if kill_dots is false)
+         * .//./foo//./bar/. becomes foo/bar       (if kill_dots is true)
          */
 
-        for (f = path, t = path; *f; f++) {
+        absolute = path_is_absolute(path);
+
+        f = path;
+        if (kill_dots && *f == '.' && IN_SET(f[1], 0, '/')) {
+                ignore_slash = true;
+                f++;
+        }
+
+        for (t = path; *f; f++) {
 
                 if (*f == '/') {
                         slash = true;
@@ -353,17 +366,21 @@ char *path_kill_slashes(char *path) {
                 }
 
                 if (slash) {
+                        if (kill_dots && *f == '.' && IN_SET(f[1], 0, '/'))
+                                continue;
+
                         slash = false;
-                        *(t++) = '/';
+                        if (ignore_slash)
+                                ignore_slash = false;
+                        else
+                                *(t++) = '/';
                 }
 
                 *(t++) = *f;
         }
 
-        /* Special rule, if we are talking of the root directory, a
-        trailing slash is good */
-
-        if (t == path && slash)
+        /* Special rule, if we are talking of the root directory, a trailing slash is good */
+        if (absolute && t == path)
                 *(t++) = '/';
 
         *t = 0;
@@ -530,7 +547,7 @@ int find_binary(const char *name, char **ret) {
                         /* Found it! */
 
                         if (ret) {
-                                *ret = path_kill_slashes(j);
+                                *ret = path_simplify(j, false);
                                 j = NULL;
                         }
 
@@ -684,12 +701,11 @@ int parse_path_argument_and_warn(const char *path, bool suppress_root, char **ar
         if (r < 0)
                 return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
 
-        path_kill_slashes(p);
+        path_simplify(p, false);
         if (suppress_root && empty_or_root(p))
                 p = mfree(p);
 
-        free(*arg);
-        *arg = p;
+        free_and_replace(*arg, p);
 
         return 0;
 }
index 134e1eb..fd6dc1e 100644 (file)
@@ -50,12 +50,12 @@ 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_make_relative(const char *from_dir, const char *to_path, char **_r);
-char* path_kill_slashes(char *path);
 char* path_startswith(const char *path, const char *prefix) _pure_;
 int path_compare(const char *a, const char *b) _pure_;
 bool path_equal(const char *a, const char *b) _pure_;
 bool path_equal_or_files_same(const char *a, const char *b, int flags);
 char* path_join(const char *root, const char *path, const char *rest);
+char* path_simplify(char *path, bool kill_dots);
 
 static inline bool path_equal_ptr(const char *a, const char *b) {
         return !!a == !!b && (!a || path_equal(a, b));
@@ -101,11 +101,11 @@ int mkfs_exists(const char *fstype);
  * the tree, to root. Also returns "" (and not "/"!) for the root
  * directory. Excludes the specified directory itself */
 #define PATH_FOREACH_PREFIX(prefix, path) \
-        for (char *_slash = ({ path_kill_slashes(strcpy(prefix, path)); streq(prefix, "/") ? NULL : strrchr(prefix, '/'); }); _slash && ((*_slash = 0), true); _slash = strrchr((prefix), '/'))
+        for (char *_slash = ({ path_simplify(strcpy(prefix, path), false); streq(prefix, "/") ? NULL : strrchr(prefix, '/'); }); _slash && ((*_slash = 0), true); _slash = strrchr((prefix), '/'))
 
 /* Same as PATH_FOREACH_PREFIX but also includes the specified path itself */
 #define PATH_FOREACH_PREFIX_MORE(prefix, path) \
-        for (char *_slash = ({ path_kill_slashes(strcpy(prefix, path)); if (streq(prefix, "/")) prefix[0] = 0; strrchr(prefix, 0); }); _slash && ((*_slash = 0), true); _slash = strrchr((prefix), '/'))
+        for (char *_slash = ({ path_simplify(strcpy(prefix, path), false); if (streq(prefix, "/")) prefix[0] = 0; strrchr(prefix, 0); }); _slash && ((*_slash = 0), true); _slash = strrchr((prefix), '/'))
 
 char *prefix_root(const char *root, const char *path);
 
index 4790790..4fa0066 100644 (file)
@@ -383,7 +383,7 @@ int unit_name_path_escape(const char *f, char **ret) {
         if (!p)
                 return -ENOMEM;
 
-        path_kill_slashes(p);
+        path_simplify(p, false);
 
         if (empty_or_root(p))
                 s = strdup("-");
index 7c7ea18..4ad84be 100644 (file)
@@ -241,7 +241,7 @@ int main(int argc, char *argv[]) {
                                                 goto finish;
                                         }
 
-                                        path_kill_slashes(j);
+                                        path_simplify(j, false);
                                         path = j;
                                 } else
                                         path = root;
index 401c086..b2b6a25 100644 (file)
@@ -470,7 +470,7 @@ static int refresh_one(
                 if (!p)
                         return -ENOMEM;
 
-                path_kill_slashes(p);
+                path_simplify(p, false);
 
                 r = refresh_one(controller, p, a, b, iteration, depth + 1, &child);
                 if (r < 0)
index c49b523..e7ef6e7 100644 (file)
@@ -199,7 +199,7 @@ static int automount_set_where(Automount *a) {
         if (r < 0)
                 return r;
 
-        path_kill_slashes(a->where);
+        path_simplify(a->where, false);
         return 1;
 }
 
index 2577832..b3f8600 100644 (file)
@@ -1519,7 +1519,7 @@ static int unit_attach_pid_to_cgroup_via_bus(Unit *u, pid_t pid, const char *suf
                 return -EINVAL;
 
         pp = strjoina("/", pp, suffix_path);
-        path_kill_slashes(pp);
+        path_simplify(pp, false);
 
         r = sd_bus_call_method(u->manager->system_bus,
                                "org.freedesktop.systemd1",
index ec1b62f..8c752ce 100644 (file)
@@ -935,7 +935,7 @@ int bus_set_transient_exec_command(
 
                         c->flags = b ? EXEC_COMMAND_IGNORE_FAILURE : 0;
 
-                        path_kill_slashes(c->path);
+                        path_simplify(c->path, false);
                         exec_command_append_list(exec_command, c);
                 }
 
@@ -2051,7 +2051,7 @@ int bus_exec_context_set_transient_property(
                         if (!path_is_absolute(i + offset))
                                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid %s", name);
 
-                        path_kill_slashes(i + offset);
+                        path_simplify(i + offset, false);
                 }
 
                 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
index fec67d5..7bd0aca 100644 (file)
@@ -108,13 +108,14 @@ static int bus_path_set_transient_property(
                                 if (!k)
                                         return -ENOMEM;
 
+                                path_simplify(k, false);
+
                                 s = new0(PathSpec, 1);
                                 if (!s)
                                         return -ENOMEM;
 
                                 s->unit = u;
-                                s->path = path_kill_slashes(k);
-                                k = NULL;
+                                s->path = TAKE_PTR(k);
                                 s->type = t;
                                 s->inotify_fd = -1;
 
index 88e180a..aee8143 100644 (file)
@@ -365,7 +365,7 @@ static int bus_socket_set_transient_property(
 
                         if (p->type != SOCKET_SOCKET) {
                                 p->path = strdup(a);
-                                path_kill_slashes(p->path);
+                                path_simplify(p->path, false);
 
                         } else if (streq(t, "Netlink")) {
                                 r = socket_address_parse_netlink(&p->address, a);
index dd4c755..8e692bf 100644 (file)
@@ -325,7 +325,7 @@ int config_parse_unit_path_strv_printf(
                         return 0;
                 }
 
-                path_kill_slashes(k);
+                path_simplify(k, false);
 
                 r = strv_push(x, k);
                 if (r < 0)
@@ -376,7 +376,7 @@ int config_parse_socket_listen(const char *unit,
                         return 0;
                 }
 
-                path_kill_slashes(p->path);
+                path_simplify(p->path, false);
 
         } else if (streq(lvalue, "ListenNetlink")) {
                 _cleanup_free_ char  *k = NULL;
@@ -673,7 +673,7 @@ int config_parse_exec(
                         n[nlen] = NULL;
                 }
 
-                path_kill_slashes(path);
+                path_simplify(path, false);
 
                 while (!isempty(p)) {
                         _cleanup_free_ char *word = NULL, *resolved = NULL;
@@ -1624,7 +1624,7 @@ int config_parse_path_spec(const char *unit,
                 return 0;
         }
 
-        path_kill_slashes(k);
+        path_simplify(k, false);
 
         if (!path_is_absolute(k)) {
                 log_syntax(unit, LOG_ERR, filename, line, 0, "Path is not absolute, ignoring: %s", k);
@@ -2048,7 +2048,7 @@ int config_parse_working_directory(
                         return missing_ok ? 0 : -ENOEXEC;
                 }
 
-                path_kill_slashes(k);
+                path_simplify(k, false);
 
                 if (!utf8_is_valid(k)) {
                         log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
@@ -3871,7 +3871,7 @@ int config_parse_namespace_path_strv(
                         continue;
                 }
 
-                path_kill_slashes(resolved);
+                path_simplify(resolved, false);
 
                 joined = strjoin(ignore_enoent ? "-" : "",
                                  shall_prefix ? "+" : "",
@@ -3955,7 +3955,7 @@ int config_parse_temporary_filesystems(
                         continue;
                 }
 
-                path_kill_slashes(resolved);
+                path_simplify(resolved, false);
 
                 r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, path, w);
                 if (r == -ENOMEM)
@@ -4036,7 +4036,7 @@ int config_parse_bind_paths(
                         return 0;
                 }
 
-                path_kill_slashes(s);
+                path_simplify(s, false);
 
                 /* Optionally, the destination is specified. */
                 if (p && p[-1] == ':') {
@@ -4068,7 +4068,7 @@ int config_parse_bind_paths(
                                 return 0;
                         }
 
-                        d = path_kill_slashes(dresolved);
+                        d = path_simplify(dresolved, false);
 
                         /* Optionally, there's also a short option string specified */
                         if (p && p[-1] == ':') {
@@ -4203,7 +4203,7 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
                 if (c++ >= FOLLOW_MAX)
                         return -ELOOP;
 
-                path_kill_slashes(*filename);
+                path_simplify(*filename, false);
 
                 /* Add the file name we are currently looking at to
                  * the names of this unit, but only if it is a valid
index b4f197c..b5742f1 100644 (file)
@@ -3867,7 +3867,7 @@ Set *manager_get_units_requiring_mounts_for(Manager *m, const char *path) {
         assert(path);
 
         strcpy(p, path);
-        path_kill_slashes(p);
+        path_simplify(p, false);
 
         return hashmap_get(m->units_requiring_mounts_for, streq(p, "/") ? "" : p);
 }
index d5463b7..d9bc4ab 100644 (file)
@@ -554,7 +554,7 @@ static int mount_add_extras(Mount *m) {
                         return r;
         }
 
-        path_kill_slashes(m->where);
+        path_simplify(m->where, false);
 
         if (!u->description) {
                 r = unit_set_description(u, m->where);
index c0c0f07..e0b9238 100644 (file)
@@ -67,7 +67,7 @@ int path_spec_watch(PathSpec *s, sd_event_io_handler_t handler) {
 
         (void) sd_event_source_set_description(s->event_source, "path");
 
-        /* This function assumes the path was passed through path_kill_slashes()! */
+        /* This function assumes the path was passed through path_simplify()! */
         assert(!strstr(s->path, "//"));
 
         for (slash = strchr(s->path, '/'); ; slash = strchr(slash+1, '/')) {
index 1af3032..068fd67 100644 (file)
@@ -2887,7 +2887,7 @@ static int service_demand_pid_file(Service *s) {
                 return -ENOMEM;
         }
 
-        path_kill_slashes(ps->path);
+        path_simplify(ps->path, false);
 
         /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
          * keep their PID file open all the time. */
index 238c942..0ef355a 100644 (file)
@@ -1393,7 +1393,7 @@ static int usbffs_dispatch_eps(SocketPort *p) {
                 if (!ep)
                         return -ENOMEM;
 
-                path_kill_slashes(ep);
+                path_simplify(ep, false);
 
                 r = usbffs_address_create(ep);
                 if (r < 0)
index e75c9f2..95534bd 100644 (file)
@@ -307,7 +307,7 @@ static int swap_load(Unit *u) {
                                 return -ENOMEM;
                 }
 
-                path_kill_slashes(s->what);
+                path_simplify(s->what, false);
 
                 if (!UNIT(s)->description) {
                         r = unit_set_description(u, s->what);
index 2567365..fd0d8da 100644 (file)
@@ -4551,7 +4551,7 @@ int unit_require_mounts_for(Unit *u, const char *path, UnitDependencyMask mask)
         if (!p)
                 return -ENOMEM;
 
-        path = path_kill_slashes(p);
+        path = path_simplify(p, false);
 
         if (!path_is_normalized(path))
                 return -EPERM;
index aab1dbf..074ef42 100644 (file)
@@ -655,7 +655,7 @@ int main(int argc, char *argv[]) {
                 int i;
 
                 for (i = optind; i < argc; i++) {
-                        path_kill_slashes(argv[i]);
+                        path_simplify(argv[i], false);
 
                         k = process_suffix_chop(argv[i]);
                         if (k < 0)
index 5c7e1df..daa8f5c 100644 (file)
@@ -543,7 +543,7 @@ static int parse_fstab(bool initrd) {
                         return log_oom();
 
                 if (is_path(where)) {
-                        path_kill_slashes(where);
+                        path_simplify(where, false);
 
                         /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
                          * mount units, but causes problems since it historically worked to have symlinks in e.g.
index 7726cdb..0e92fcb 100644 (file)
@@ -178,7 +178,7 @@ int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
                                 return log_oom();
 
                         free_and_replace(syspath, new_syspath);
-                        path_kill_slashes(syspath);
+                        path_simplify(syspath, false);
                 }
 
                 if (path_startswith(syspath,  "/sys/devices/")) {
index 0298930..5e22a43 100644 (file)
@@ -354,7 +354,7 @@ static int parse_argv(int argc, char *argv[]) {
                         if (!arg_mount_what)
                                 return log_oom();
 
-                        path_kill_slashes(arg_mount_what);
+                        path_simplify(arg_mount_what, false);
 
                         if (!path_is_absolute(arg_mount_what)) {
                                 log_error("Only absolute path is supported: %s", arg_mount_what);
@@ -372,7 +372,7 @@ static int parse_argv(int argc, char *argv[]) {
                                 if (!arg_mount_where)
                                         return log_oom();
 
-                                path_kill_slashes(arg_mount_where);
+                                path_simplify(arg_mount_where, false);
 
                                 if (!path_is_absolute(arg_mount_where)) {
                                         log_error("Only absolute path is supported: %s", arg_mount_where);
@@ -992,7 +992,7 @@ static int action_umount(
                         if (!p)
                                 return log_oom();
 
-                        path_kill_slashes(p);
+                        path_simplify(p, false);
 
                         r = stop_mounts(bus, p);
                         if (r < 0)
index aaabe9f..eac0a4d 100644 (file)
@@ -630,14 +630,14 @@ static int portable_changes_add(
         if (!p)
                 return -ENOMEM;
 
-        path_kill_slashes(p);
+        path_simplify(p, false);
 
         if (source) {
                 s = strdup(source);
                 if (!s)
                         return -ENOMEM;
 
-                path_kill_slashes(s);
+                path_simplify(s, false);
         }
 
         c[(*n_changes)++] = (PortableChange) {
index c74c83b..f10366c 100644 (file)
@@ -740,7 +740,7 @@ int config_parse_path(
         if (!n)
                 return log_oom();
 
-        path_kill_slashes(n);
+        path_simplify(n, false);
 
 finalize:
         free(*s);
index ecda662..46e07eb 100644 (file)
@@ -309,9 +309,9 @@ int unit_file_changes_add(
         if (!p || (source && !s))
                 return -ENOMEM;
 
-        path_kill_slashes(p);
+        path_simplify(p, false);
         if (s)
-                path_kill_slashes(s);
+                path_simplify(s, false);
 
         c[*n_changes] = (UnitFileChange) { type, p, s };
         p = s = NULL;
@@ -516,7 +516,7 @@ static int mark_symlink_for_removal(
         if (!n)
                 return -ENOMEM;
 
-        path_kill_slashes(n);
+        path_simplify(n, false);
 
         r = set_consume(*remove_symlinks_to, n);
         if (r == -EEXIST)
@@ -598,7 +598,7 @@ static int remove_marked_symlinks_fd(
                         p = path_make_absolute(de->d_name, path);
                         if (!p)
                                 return -ENOMEM;
-                        path_kill_slashes(p);
+                        path_simplify(p, false);
 
                         q = readlink_malloc(p, &dest);
                         if (q == -ENOENT)
index e67f1f0..404dd2a 100644 (file)
@@ -170,7 +170,7 @@ static int image_new(
         if (!i->path)
                 return -ENOMEM;
 
-        path_kill_slashes(i->path);
+        path_simplify(i->path, false);
 
         *ret = TAKE_PTR(i);
 
index c14cfd4..7caa97e 100644 (file)
@@ -1599,8 +1599,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                 if (resolved_id) {
                         if (path_is_absolute(resolved_id)) {
                                 i->uid_path = TAKE_PTR(resolved_id);
-
-                                path_kill_slashes(i->uid_path);
+                                path_simplify(i->uid_path, false);
                         } else {
                                 _cleanup_free_ char *uid = NULL, *gid = NULL;
                                 if (split_pair(resolved_id, ":", &uid, &gid) == 0) {
@@ -1651,8 +1650,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                 if (resolved_id) {
                         if (path_is_absolute(resolved_id)) {
                                 i->gid_path = TAKE_PTR(resolved_id);
-
-                                path_kill_slashes(i->gid_path);
+                                path_simplify(i->gid_path, false);
                         } else {
                                 r = parse_gid(resolved_id, &i->gid);
                                 if (r < 0)
index c65b0b0..d963d1c 100644 (file)
                 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"));
index ae39d72..6bfa44f 100644 (file)
@@ -431,7 +431,7 @@ static void load_unix_sockets(void) {
                         goto fail;
                 }
 
-                path_kill_slashes(s);
+                path_simplify(s, false);
 
                 r = set_consume(unix_sockets, s);
                 if (r < 0 && r != -EEXIST) {
@@ -2289,7 +2289,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
                         return -EBADMSG;
                 }
 
-                path_kill_slashes(i.argument);
+                path_simplify(i.argument, false);
                 break;
 
         case CREATE_CHAR_DEVICE:
@@ -2362,7 +2362,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
                 return -EBADMSG;
         }
 
-        path_kill_slashes(i.path);
+        path_simplify(i.path, false);
 
         if (!should_include_path(i.path))
                 return 0;