[systemd-commits] 2 commits - src/shared

Lennart Poettering lennart at kemper.freedesktop.org
Fri May 16 09:49:17 PDT 2014


 src/shared/path-lookup.c |   12 +++++-
 src/shared/path-util.c   |   90 +++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/path-util.h   |    1 
 3 files changed, 101 insertions(+), 2 deletions(-)

New commits:
commit 66379f841ec6129e4c2c3a2bbc39a1f54000ed2f
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date:   Sat May 3 11:52:13 2014 +0300

    path-lookup: don't hardcode .config
    
    If XDG_CONFIG_HOME is set, then we should respect that.

diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c
index c6b4ba1..e072fd6 100644
--- a/src/shared/path-lookup.c
+++ b/src/shared/path-lookup.c
@@ -125,6 +125,8 @@ static char** user_dirs(
                         goto fail;
 
         } else if (home) {
+                _cleanup_free_ char *data_home_parent = NULL;
+
                 if (asprintf(&data_home, "%s/.local/share/systemd/user", home) < 0)
                         goto fail;
 
@@ -135,8 +137,14 @@ static char** user_dirs(
                  * then filter out this link, if it is actually is
                  * one. */
 
-                mkdir_parents_label(data_home, 0777);
-                (void) symlink("../../../.config/systemd/user", data_home);
+                if (path_get_parent(data_home, &data_home_parent) >= 0) {
+                        _cleanup_free_ char *config_home_relative = NULL;
+
+                        if (path_make_relative(data_home_parent, config_home, &config_home_relative) >= 0) {
+                                mkdir_parents_label(data_home, 0777);
+                                (void) symlink(config_home_relative, data_home);
+                        }
+                }
         }
 
         e = getenv("XDG_DATA_DIRS");

commit 7cb9c51ce81818c200f27de4db4a4076cbe4265b
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date:   Sat May 3 11:52:12 2014 +0300

    path-util: add path_make_relative()
    
    In user_dirs() in path-lookup.c, I want to replace this:
            symlink("../../../.config/systemd/user", data_home);
    with
            symlink(config_home, data_home);
    to avoid hardcoding .config when XDG_CONFIG_HOME is set.
    
    The problem is that config_home is an absolute path, and it's better
    to make the symlink relative. path_make_relative() is an utility
    function that converts an absolute path into a relative one.

diff --git a/src/shared/path-util.c b/src/shared/path-util.c
index 8bf9a3c..2f38c10 100644
--- a/src/shared/path-util.c
+++ b/src/shared/path-util.c
@@ -132,6 +132,96 @@ char *path_make_absolute_cwd(const char *p) {
         return path_make_absolute(p, cwd);
 }
 
+int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
+        char *r, *p;
+        unsigned n_parents;
+        size_t to_path_len;
+
+        assert(from_dir);
+        assert(to_path);
+        assert(_r);
+
+        /* Strips the common part, and adds ".." elements as necessary. */
+
+        if (!path_is_absolute(from_dir))
+                return -EINVAL;
+
+        if (!path_is_absolute(to_path))
+                return -EINVAL;
+
+        /* Skip the common part. */
+        for (;;) {
+                size_t a;
+                size_t b;
+
+                from_dir += strspn(from_dir, "/");
+                to_path += strspn(to_path, "/");
+
+                if (!*from_dir) {
+                        if (!*to_path)
+                                /* from_dir equals to_path. */
+                                r = strdup(".");
+                        else
+                                /* from_dir is a parent directory of to_path. */
+                                r = strdup(to_path);
+
+                        if (!r)
+                                return -ENOMEM;
+
+                        *_r = r;
+                        return 0;
+                }
+
+                if (!*to_path)
+                        break;
+
+                a = strcspn(from_dir, "/");
+                b = strcspn(to_path, "/");
+
+                if (a != b)
+                        break;
+
+                if (memcmp(from_dir, to_path, a) != 0)
+                        break;
+
+                from_dir += a;
+                to_path += b;
+        }
+
+        /* If we're here, then "from_dir" has one or more elements that need to
+         * be replaced with "..". */
+
+        /* Count the number of necessary ".." elements. */
+        for (n_parents = 0;;) {
+                from_dir += strspn(from_dir, "/");
+
+                if (!*from_dir)
+                        break;
+
+                from_dir += strcspn(from_dir, "/");
+                n_parents++;
+        }
+
+        to_path_len = strlen(to_path);
+
+        r = malloc(n_parents * 3 + to_path_len);
+        if (!r)
+                return -ENOMEM;
+
+        for (p = r; n_parents > 0; n_parents--, p += 3)
+                memcpy(p, "../", 3);
+
+        if (to_path_len > 0)
+                memcpy(p, to_path, to_path_len);
+        else
+                /* "to_path" is a parent directory of "from_dir". Let's remove
+                 * the redundant slash from the end of the result. */
+                *(p - 1) = 0;
+
+        *_r = r;
+        return 0;
+}
+
 char **path_strv_make_absolute_cwd(char **l) {
         char **s;
 
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
index fdf1f6b..6882d78 100644
--- a/src/shared/path-util.h
+++ b/src/shared/path-util.h
@@ -41,6 +41,7 @@ int path_get_parent(const char *path, char **parent);
 bool path_is_absolute(const char *p) _pure_;
 char* path_make_absolute(const char *p, const char *prefix);
 char* path_make_absolute_cwd(const char *p);
+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_;
 bool path_equal(const char *a, const char *b) _pure_;



More information about the systemd-commits mailing list