diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-03-24 15:18:20 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-03-27 20:12:44 +0100 |
commit | ce7eb6aa84b24d411ede7ed570faba78ee084493 (patch) | |
tree | b2c6fbcdbce5422f236910c44d8699e57d4ad1c9 /src/libsystemd/sd-path/sd-path.c | |
parent | Move path-lookup.c to src/basic (diff) | |
download | systemd-ce7eb6aa84b24d411ede7ed570faba78ee084493.tar.xz systemd-ce7eb6aa84b24d411ede7ed570faba78ee084493.zip |
sd-path: simplify implementation of sd_path_lookup*()
The two functions were duplicating a lot of functionality and more
importantly, they both had explicit lists of types which are search
paths. I want to add more types, and I don't want to have to remember
to add them to both lists.
Diffstat (limited to 'src/libsystemd/sd-path/sd-path.c')
-rw-r--r-- | src/libsystemd/sd-path/sd-path.c | 102 |
1 files changed, 42 insertions, 60 deletions
diff --git a/src/libsystemd/sd-path/sd-path.c b/src/libsystemd/sd-path/sd-path.c index eed8229f5b..8bdc2435a3 100644 --- a/src/libsystemd/sd-path/sd-path.c +++ b/src/libsystemd/sd-path/sd-path.c @@ -323,59 +323,55 @@ static int get_path(uint64_t type, char **buffer, const char **ret) { return -EOPNOTSUPP; } -_public_ int sd_path_lookup(uint64_t type, const char *suffix, char **path) { +static int get_path_alloc(uint64_t type, const char *suffix, char **path) { _cleanup_free_ char *buffer = NULL; + char *buffer2 = NULL; const char *ret; - char *cc; int r; - assert_return(path, -EINVAL); - - if (IN_SET(type, - SD_PATH_SEARCH_BINARIES, - SD_PATH_SEARCH_BINARIES_DEFAULT, - SD_PATH_SEARCH_LIBRARY_PRIVATE, - SD_PATH_SEARCH_LIBRARY_ARCH, - SD_PATH_SEARCH_SHARED, - SD_PATH_SEARCH_CONFIGURATION_FACTORY, - SD_PATH_SEARCH_STATE_FACTORY, - SD_PATH_SEARCH_CONFIGURATION)) { + assert(path); - _cleanup_strv_free_ char **l = NULL; - - r = sd_path_lookup_strv(type, suffix, &l); - if (r < 0) - return r; + r = get_path(type, &buffer, &ret); + if (r < 0) + return r; - buffer = strv_join(l, ":"); + if (suffix) { + suffix += strspn(suffix, "/"); + buffer2 = path_join(ret, suffix); + if (!buffer2) + return -ENOMEM; + } else if (!buffer) { + buffer = strdup(ret); if (!buffer) return -ENOMEM; - - *path = TAKE_PTR(buffer); - return 0; } - r = get_path(type, &buffer, &ret); - if (r < 0) + *path = buffer2 ?: TAKE_PTR(buffer); + return 0; +} + +_public_ int sd_path_lookup(uint64_t type, const char *suffix, char **path) { + int r; + + assert_return(path, -EINVAL); + + r = get_path_alloc(type, suffix, path); + if (r != -EOPNOTSUPP) return r; - if (!suffix) { - if (!buffer) { - buffer = strdup(ret); - if (!buffer) - return -ENOMEM; - } + /* Fall back to sd_path_lookup_strv */ + _cleanup_strv_free_ char **l = NULL; + char *buffer; - *path = TAKE_PTR(buffer); - return 0; - } + r = sd_path_lookup_strv(type, suffix, &l); + if (r < 0) + return r; - suffix += strspn(suffix, "/"); - cc = path_join(ret, suffix); - if (!cc) + buffer = strv_join(l, ":"); + if (!buffer) return -ENOMEM; - *path = TAKE_PTR(cc); + *path = buffer; return 0; } @@ -546,43 +542,29 @@ static int get_search(uint64_t type, char ***list) { } _public_ int sd_path_lookup_strv(uint64_t type, const char *suffix, char ***paths) { - char **i, **j; _cleanup_strv_free_ char **l = NULL, **n = NULL; int r; assert_return(paths, -EINVAL); - if (!IN_SET(type, - SD_PATH_SEARCH_BINARIES, - SD_PATH_SEARCH_BINARIES_DEFAULT, - SD_PATH_SEARCH_LIBRARY_PRIVATE, - SD_PATH_SEARCH_LIBRARY_ARCH, - SD_PATH_SEARCH_SHARED, - SD_PATH_SEARCH_CONFIGURATION_FACTORY, - SD_PATH_SEARCH_STATE_FACTORY, - SD_PATH_SEARCH_CONFIGURATION)) { - - char *p; + r = get_search(type, &l); + if (r == -EOPNOTSUPP) { + _cleanup_free_ char *t = NULL; - r = sd_path_lookup(type, suffix, &p); + r = get_path_alloc(type, suffix, &t); if (r < 0) return r; l = new(char*, 2); - if (!l) { - free(p); + if (!l) return -ENOMEM; - } - - l[0] = p; + l[0] = TAKE_PTR(t); l[1] = NULL; *paths = TAKE_PTR(l); return 0; - } - r = get_search(type, &l); - if (r < 0) + } else if (r < 0) return r; if (!suffix) { @@ -594,7 +576,7 @@ _public_ int sd_path_lookup_strv(uint64_t type, const char *suffix, char ***path if (!n) return -ENOMEM; - j = n; + char **i, **j = n; STRV_FOREACH(i, l) { *j = path_join(*i, suffix); if (!*j) @@ -602,8 +584,8 @@ _public_ int sd_path_lookup_strv(uint64_t type, const char *suffix, char ***path j++; } - *j = NULL; + *paths = TAKE_PTR(n); return 0; } |