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].
This commit is contained in:
@@ -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)
|
||||
|
||||
+30
-45
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user