diff options
author | Lennart Poettering <lennart@poettering.net> | 2015-10-26 17:30:56 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2015-10-27 13:25:55 +0100 |
commit | 5f311f8c0e51e2f13773823feb6a71f7c6f2838c (patch) | |
tree | 6edfc648016e7000715e11c1ede94fde32e88d82 /src | |
parent | e4e73a632524c382139034d4271f53b6089ab4cb (diff) |
util: remove path_get_parent(), in favour of dirname_malloc()
We don't need two functions that do essentialy the same, hence drop
path_get_parent(), and stick to dirname_malloc(), but move it to
path-util.[ch].
Diffstat (limited to 'src')
-rw-r--r-- | src/analyze/analyze-verify.c | 7 | ||||
-rw-r--r-- | src/basic/btrfs-util.c | 8 | ||||
-rw-r--r-- | src/basic/path-util.c | 75 | ||||
-rw-r--r-- | src/basic/path-util.h | 3 | ||||
-rw-r--r-- | src/basic/util.c | 18 | ||||
-rw-r--r-- | src/basic/util.h | 1 | ||||
-rw-r--r-- | src/core/automount.c | 7 | ||||
-rw-r--r-- | src/core/mount.c | 7 | ||||
-rw-r--r-- | src/core/socket.c | 6 | ||||
-rw-r--r-- | src/core/unit.c | 5 | ||||
-rw-r--r-- | src/import/pull-dkr.c | 8 | ||||
-rw-r--r-- | src/journal/catalog.c | 1 | ||||
-rw-r--r-- | src/shared/install.c | 7 | ||||
-rw-r--r-- | src/systemctl/systemctl.c | 3 | ||||
-rw-r--r-- | src/test/test-path-util.c | 14 |
15 files changed, 62 insertions, 108 deletions
diff --git a/src/analyze/analyze-verify.c b/src/analyze/analyze-verify.c index f4255f979e..2b6240232c 100644 --- a/src/analyze/analyze-verify.c +++ b/src/analyze/analyze-verify.c @@ -21,12 +21,13 @@ #include <stdlib.h> -#include "manager.h" +#include "analyze-verify.h" #include "bus-util.h" #include "log.h" -#include "strv.h" +#include "manager.h" #include "pager.h" -#include "analyze-verify.h" +#include "path-util.h" +#include "strv.h" static int generate_path(char **var, char **filenames) { char **filename; diff --git a/src/basic/btrfs-util.c b/src/basic/btrfs-util.c index f799f8dcc2..7b49a1a516 100644 --- a/src/basic/btrfs-util.c +++ b/src/basic/btrfs-util.c @@ -60,13 +60,13 @@ static int validate_subvolume_name(const char *name) { static int open_parent(const char *path, int flags) { _cleanup_free_ char *parent = NULL; - int r, fd; + int fd; assert(path); - r = path_get_parent(path, &parent); - if (r < 0) - return r; + parent = dirname_malloc(path); + if (!parent) + return -ENOMEM; fd = open(parent, flags); if (fd < 0) diff --git a/src/basic/path-util.c b/src/basic/path-util.c index 0015667ac0..7abb3a7b9e 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -27,6 +27,12 @@ #include <sys/statvfs.h> #include <unistd.h> +/* When we include libgen.h because we need dirname() we immediately + * undefine basename() since libgen.h defines it as a macro to the + * POSIX version which is really broken. We prefer GNU basename(). */ +#include <libgen.h> +#undef basename + #include "fd-util.h" #include "fileio.h" #include "log.h" @@ -46,47 +52,6 @@ bool is_path(const char *p) { return !!strchr(p, '/'); } -int path_get_parent(const char *path, char **_r) { - const char *e, *a = NULL, *b = NULL, *p; - char *r; - bool slash = false; - - assert(path); - assert(_r); - - if (!*path) - return -EINVAL; - - for (e = path; *e; e++) { - - if (!slash && *e == '/') { - a = b; - b = e; - slash = true; - } else if (slash && *e != '/') - slash = false; - } - - if (*(e-1) == '/') - p = a; - else - p = b; - - if (!p) - return -EINVAL; - - if (p == path) - r = strdup("/"); - else - r = strndup(path, p-path); - - if (!r) - return -ENOMEM; - - *_r = r; - return 0; -} - int path_split_and_make_absolute(const char *p, char ***ret) { char **l; int r; @@ -659,7 +624,6 @@ fallback_fstat: int path_is_mount_point(const char *t, int flags) { _cleanup_close_ int fd = -1; _cleanup_free_ char *canonical = NULL, *parent = NULL; - int r; assert(t); @@ -678,9 +642,9 @@ int path_is_mount_point(const char *t, int flags) { t = canonical; } - r = path_get_parent(t, &parent); - if (r < 0) - return r; + parent = dirname_malloc(t); + if (!parent) + return -ENOMEM; fd = openat(AT_FDCWD, parent, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_PATH); if (fd < 0) @@ -937,3 +901,24 @@ int parse_path_argument_and_warn(const char *path, bool suppress_root, char **ar *arg = p; return 0; } + +char* dirname_malloc(const char *path) { + char *d, *dir, *dir2; + + assert(path); + + d = strdup(path); + if (!d) + return NULL; + + dir = dirname(d); + assert(dir); + + if (dir == d) + return d; + + dir2 = strdup(dir); + free(d); + + return dir2; +} diff --git a/src/basic/path-util.h b/src/basic/path-util.h index 9d4522c8eb..708cdc1707 100644 --- a/src/basic/path-util.h +++ b/src/basic/path-util.h @@ -37,7 +37,6 @@ bool is_path(const char *p) _pure_; int path_split_and_make_absolute(const char *p, char ***ret); -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); int path_make_absolute_cwd(const char *p, char **ret); @@ -103,3 +102,5 @@ char *prefix_root(const char *root, const char *path); }) int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg); + +char* dirname_malloc(const char *path); diff --git a/src/basic/util.c b/src/basic/util.c index 5e3a363bb4..f403dca10e 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -398,24 +398,6 @@ int dir_is_empty(const char *path) { return 1; } -char* dirname_malloc(const char *path) { - char *d, *dir, *dir2; - - d = strdup(path); - if (!d) - return NULL; - dir = dirname(d); - assert(dir); - - if (dir != d) { - dir2 = strdup(dir); - free(d); - return dir2; - } - - return dir; -} - void rename_process(const char name[8]) { assert(name); diff --git a/src/basic/util.h b/src/basic/util.h index 6c3449a706..ec95c53130 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -163,7 +163,6 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path); bool is_device_path(const char *path); int dir_is_empty(const char *path); -char* dirname_malloc(const char *path); static inline int dir_is_populated(const char *path) { int r; diff --git a/src/core/automount.c b/src/core/automount.c index 2ee32312fb..df98711d96 100644 --- a/src/core/automount.c +++ b/src/core/automount.c @@ -141,13 +141,12 @@ static void automount_done(Unit *u) { static int automount_add_mount_links(Automount *a) { _cleanup_free_ char *parent = NULL; - int r; assert(a); - r = path_get_parent(a->where, &parent); - if (r < 0) - return r; + parent = dirname_malloc(a->where); + if (!parent) + return -ENOMEM; return unit_require_mounts_for(UNIT(a), parent); } diff --git a/src/core/mount.c b/src/core/mount.c index 59f56bdefa..9d8b55da58 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -252,9 +252,10 @@ static int mount_add_mount_links(Mount *m) { if (!path_equal(m->where, "/")) { /* Adds in links to other mount points that might lie further * up in the hierarchy */ - r = path_get_parent(m->where, &parent); - if (r < 0) - return r; + + parent = dirname_malloc(m->where); + if (!parent) + return -ENOMEM; r = unit_require_mounts_for(UNIT(m), parent); if (r < 0) diff --git a/src/core/socket.c b/src/core/socket.c index 49cef210dc..8ea7bd9af4 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -1169,9 +1169,9 @@ static int usbffs_dispatch_eps(SocketPort *p) { _cleanup_free_ char *path = NULL; int r, i, n, k; - r = path_get_parent(p->path, &path); - if (r < 0) - return r; + path = dirname_malloc(p->path); + if (!path) + return -ENOMEM; r = scandir(path, &ent, usbffs_select_ep, alphasort); if (r < 0) diff --git a/src/core/unit.c b/src/core/unit.c index 17c135cf3f..b44a2a5e2d 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -417,12 +417,11 @@ static void unit_remove_transient(Unit *u) { STRV_FOREACH(i, u->dropin_paths) { _cleanup_free_ char *p = NULL; - int r; (void) unlink(*i); - r = path_get_parent(*i, &p); - if (r >= 0) + p = dirname_malloc(*i); + if (p) (void) rmdir(p); } } diff --git a/src/import/pull-dkr.c b/src/import/pull-dkr.c index 1cd9e7b0bd..448dbafa9f 100644 --- a/src/import/pull-dkr.c +++ b/src/import/pull-dkr.c @@ -479,13 +479,13 @@ static int dkr_pull_make_local_copy(DkrPull *i, DkrPullVersion version) { if (!i->final_path) { i->final_path = strjoin(i->image_root, "/.dkr-", i->id, NULL); if (!i->final_path) - return log_oom(); + return -ENOMEM; } if (version == DKR_PULL_V2) { - r = path_get_parent(i->image_root, &p); - if (r < 0) - return r; + p = dirname_malloc(i->image_root); + if (!p) + return -ENOMEM; } r = pull_make_local_copy(i->final_path, p ?: i->image_root, i->local, i->force_local); diff --git a/src/journal/catalog.c b/src/journal/catalog.c index d5bc17b4b6..fe3975b7b8 100644 --- a/src/journal/catalog.c +++ b/src/journal/catalog.c @@ -35,6 +35,7 @@ #include "hashmap.h" #include "log.h" #include "mkdir.h" +#include "path-util.h" #include "siphash24.h" #include "sparse-endian.h" #include "strbuf.h" diff --git a/src/shared/install.c b/src/shared/install.c index 9b72f76662..a1720488e8 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -48,13 +48,12 @@ typedef struct { static int in_search_path(const char *path, char **search) { _cleanup_free_ char *parent = NULL; - int r; assert(path); - r = path_get_parent(path, &parent); - if (r < 0) - return r; + parent = dirname_malloc(path); + if (!parent) + return -ENOMEM; return strv_contains(search, parent); } diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 49a4b46fd4..054e9eaa13 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -3479,7 +3479,8 @@ static void print_status_info( dir = mfree(dir); - if (path_get_parent(*dropin, &dir) < 0) { + dir = dirname_malloc(*dropin); + if (!dir) { log_oom(); return; } diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index 89129c9894..86d61b2efb 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -77,20 +77,6 @@ static void test_path(void) { assert_se(streq(basename("/aa///file..."), "file...")); assert_se(streq(basename("file.../"), "")); -#define test_parent(x, y) { \ - _cleanup_free_ char *z = NULL; \ - int r = path_get_parent(x, &z); \ - printf("expected: %s\n", y ? y : "error"); \ - printf("actual: %s\n", r<0 ? "error" : z); \ - assert_se((y==NULL) ^ (r==0)); \ - assert_se(y==NULL || path_equal(z, y)); \ - } - - test_parent("./aa/bb/../file.da.", "./aa/bb/.."); - test_parent("/aa///.file", "/aa///"); - test_parent("/aa///file...", "/aa///"); - test_parent("file.../", NULL); - fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY); assert_se(fd >= 0); assert_se(fd_is_mount_point(fd, "/", 0) > 0); |