summaryrefslogtreecommitdiff
path: root/src/shared/mkdir.c
diff options
context:
space:
mode:
authorAnthony G. Basile <blueness@gentoo.org>2014-08-14 18:12:43 -0400
committerAnthony G. Basile <blueness@gentoo.org>2014-08-14 18:12:43 -0400
commit03070edd5c0f6ae55ed79b9fc1295af39844ff40 (patch)
tree047bc4cd7ebdb01cc4d6aa0bfb111ec5ce73f313 /src/shared/mkdir.c
parent28744043fbaca39dfc9fd1666a8557fd6d8a690f (diff)
src/shared: import more code cleanups from upstream
Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Diffstat (limited to 'src/shared/mkdir.c')
-rw-r--r--src/shared/mkdir.c68
1 files changed, 18 insertions, 50 deletions
diff --git a/src/shared/mkdir.c b/src/shared/mkdir.c
index 770ee994c4..874433dc90 100644
--- a/src/shared/mkdir.c
+++ b/src/shared/mkdir.c
@@ -29,52 +29,24 @@
#include "label.h"
#include "util.h"
+#include "path-util.h"
#include "mkdir.h"
-static int is_dir(const char* path) {
+int is_dir(const char* path, bool follow) {
struct stat st;
- if (stat(path, &st) < 0)
- return -errno;
+ if (follow) {
+ if (stat(path, &st) < 0)
+ return -errno;
+ } else {
+ if (lstat(path, &st) < 0)
+ return -errno;
+ }
return S_ISDIR(st.st_mode);
}
-
-char* path_startswith(const char *path, const char *prefix) {
- assert(path);
- assert(prefix);
-
- if ((path[0] == '/') != (prefix[0] == '/'))
- return NULL;
-
- for (;;) {
- size_t a, b;
-
- path += strspn(path, "/");
- prefix += strspn(prefix, "/");
-
- if (*prefix == 0)
- return (char*) path;
-
- if (*path == 0)
- return NULL;
-
- a = strcspn(path, "/");
- b = strcspn(prefix, "/");
-
- if (a != b)
- return NULL;
-
- if (memcmp(path, prefix, a) != 0)
- return NULL;
-
- path += a;
- prefix += b;
- }
-}
-
-static int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, bool apply) {
+int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
const char *p, *e;
int r;
@@ -97,7 +69,7 @@ static int mkdir_parents_internal(const char *prefix, const char *path, mode_t m
memcpy(buf, path, e-path);
buf[e-path] = 0;
- r = is_dir(p);
+ r = is_dir(p, true);
if (r > 0)
return 0;
if (r == 0)
@@ -122,36 +94,32 @@ static int mkdir_parents_internal(const char *prefix, const char *path, mode_t m
if (prefix && path_startswith(prefix, t))
continue;
- r = label_mkdir(t, mode, apply);
+ r = _mkdir(t, mode);
if (r < 0 && errno != EEXIST)
return -errno;
}
}
int mkdir_parents(const char *path, mode_t mode) {
- return mkdir_parents_internal(NULL, path, mode, false);
-}
-
-int mkdir_parents_label(const char *path, mode_t mode) {
- return mkdir_parents_internal(NULL, path, mode, true);
+ return mkdir_parents_internal(NULL, path, mode, mkdir);
}
-static int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, bool apply) {
+int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
int r;
/* Like mkdir -p */
- r = mkdir_parents_internal(prefix, path, mode, apply);
+ r = mkdir_parents_internal(prefix, path, mode, _mkdir);
if (r < 0)
return r;
- r = label_mkdir(path, mode, apply);
- if (r < 0 && (errno != EEXIST || is_dir(path) <= 0))
+ r = _mkdir(path, mode);
+ if (r < 0 && (errno != EEXIST || is_dir(path, true) <= 0))
return -errno;
return 0;
}
int mkdir_p(const char *path, mode_t mode) {
- return mkdir_p_internal(NULL, path, mode, false);
+ return mkdir_p_internal(NULL, path, mode, mkdir);
}