summaryrefslogtreecommitdiff
path: root/src/shared/mkdir.c
diff options
context:
space:
mode:
authorKay Sievers <kay@vrfy.org>2012-04-17 00:26:02 +0200
committerKay Sievers <kay@vrfy.org>2012-04-17 01:09:39 +0200
commit9e13dbae509605dba1bde7e7385086b59acb428e (patch)
treeab3b5cdbdcf358212727f2aa181186d4b5973832 /src/shared/mkdir.c
parentf13b388f97bc3ba8db844bd3413d510e2466a0b6 (diff)
udev: replace util_create_path() with mkdir_parents()
Diffstat (limited to 'src/shared/mkdir.c')
-rw-r--r--src/shared/mkdir.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/shared/mkdir.c b/src/shared/mkdir.c
index 3d98221296..fef674c1b3 100644
--- a/src/shared/mkdir.c
+++ b/src/shared/mkdir.c
@@ -53,13 +53,24 @@ int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
}
int mkdir_parents(const char *path, mode_t mode) {
+ struct stat st;
const char *p, *e;
assert(path);
- /* Creates every parent directory in the path except the last
- * component. */
+ /* return immediately if directory exists */
+ e = strrchr(path, '/');
+ if (!e)
+ return -EINVAL;
+ p = strndupa(path, e - path);
+ if (stat(p, &st) >= 0) {
+ if ((st.st_mode & S_IFMT) == S_IFDIR)
+ return 0;
+ else
+ return -ENOTDIR;
+ }
+ /* create every parent directory in the path, except the last component */
p = path + strspn(path, "/");
for (;;) {
int r;
@@ -73,11 +84,10 @@ int mkdir_parents(const char *path, mode_t mode) {
if (*p == 0)
return 0;
- if (!(t = strndup(path, e - path)))
+ if (!(t = strndupa(path, e - path)))
return -ENOMEM;
r = label_mkdir(t, mode);
- free(t);
if (r < 0 && errno != EEXIST)
return -errno;