chiark / gitweb /
dbus: introduce parse_unit_info
[elogind.git] / src / shared / mkdir.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28
29 #include "mkdir.h"
30 #include "label.h"
31 #include "util.h"
32
33 int mkdir_label(const char *path, mode_t mode) {
34         return label_mkdir(path, mode, true);
35 }
36
37 static int makedir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool apply) {
38         struct stat st;
39
40         if (label_mkdir(path, mode, apply) >= 0)
41                 if (chmod_and_chown(path, mode, uid, gid) < 0)
42                         return -errno;
43
44         if (lstat(path, &st) < 0)
45                 return -errno;
46
47         if ((st.st_mode & 0777) != mode ||
48             st.st_uid != uid ||
49             st.st_gid != gid ||
50             !S_ISDIR(st.st_mode)) {
51                 errno = EEXIST;
52                 return -errno;
53         }
54
55         return 0;
56 }
57
58 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid) {
59         return makedir_safe(path, mode, uid, gid, false);
60 }
61
62 int mkdir_safe_label(const char *path, mode_t mode, uid_t uid, gid_t gid) {
63         return makedir_safe(path, mode, uid, gid, true);
64 }
65
66 static int makedir_parents(const char *path, mode_t mode, bool apply) {
67         struct stat st;
68         const char *p, *e;
69
70         assert(path);
71
72         /* return immediately if directory exists */
73         e = strrchr(path, '/');
74         if (!e)
75                 return -EINVAL;
76         p = strndupa(path, e - path);
77         if (stat(p, &st) >= 0) {
78                 if ((st.st_mode & S_IFMT) == S_IFDIR)
79                         return 0;
80                 else
81                         return -ENOTDIR;
82         }
83
84         /* create every parent directory in the path, except the last component */
85         p = path + strspn(path, "/");
86         for (;;) {
87                 int r;
88                 char *t;
89
90                 e = p + strcspn(p, "/");
91                 p = e + strspn(e, "/");
92
93                 /* Is this the last component? If so, then we're
94                  * done */
95                 if (*p == 0)
96                         return 0;
97
98                 t = strndup(path, e - path);
99                 if (!t)
100                         return -ENOMEM;
101
102                 r = label_mkdir(t, mode, apply);
103                 free(t);
104
105                 if (r < 0 && errno != EEXIST)
106                         return -errno;
107         }
108 }
109
110 int mkdir_parents(const char *path, mode_t mode) {
111         return makedir_parents(path, mode, false);
112 }
113
114 int mkdir_parents_label(const char *path, mode_t mode) {
115         return makedir_parents(path, mode, true);
116 }
117
118 static int is_dir(const char* path) {
119         struct stat st;
120         if (stat(path, &st) < 0)
121                 return -errno;
122         return S_ISDIR(st.st_mode);
123 }
124
125 static int makedir_p(const char *path, mode_t mode, bool apply) {
126         int r;
127
128         /* Like mkdir -p */
129
130         r = makedir_parents(path, mode, apply);
131         if (r < 0)
132                 return r;
133
134         r = label_mkdir(path, mode, apply);
135         if (r < 0 && (errno != EEXIST || is_dir(path) <= 0))
136                 return -errno;
137
138         return 0;
139 }
140
141 int mkdir_p(const char *path, mode_t mode) {
142         return makedir_p(path, mode, false);
143 }
144
145 int mkdir_p_label(const char *path, mode_t mode) {
146         return makedir_p(path, mode, true);
147 }