chiark / gitweb /
replace tabs with spaces in various files
[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 "label.h"
30 #include "util.h"
31 #include "path-util.h"
32 #include "mkdir.h"
33
34 int mkdir_label(const char *path, mode_t mode) {
35         return label_mkdir(path, mode, true);
36 }
37
38 static int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, bool apply) {
39         struct stat st;
40
41         if (label_mkdir(path, mode, apply) >= 0)
42                 if (chmod_and_chown(path, mode, uid, gid) < 0)
43                         return -errno;
44
45         if (lstat(path, &st) < 0)
46                 return -errno;
47
48         if ((st.st_mode & 0777) != mode ||
49             st.st_uid != uid ||
50             st.st_gid != gid ||
51             !S_ISDIR(st.st_mode)) {
52                 errno = EEXIST;
53                 return -errno;
54         }
55
56         return 0;
57 }
58
59 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid) {
60         return mkdir_safe_internal(path, mode, uid, gid, false);
61 }
62
63 int mkdir_safe_label(const char *path, mode_t mode, uid_t uid, gid_t gid) {
64         return mkdir_safe_internal(path, mode, uid, gid, true);
65 }
66
67 static int is_dir(const char* path) {
68         struct stat st;
69
70         if (stat(path, &st) < 0)
71                 return -errno;
72
73         return S_ISDIR(st.st_mode);
74 }
75
76 static int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, bool apply) {
77         const char *p, *e;
78         int r;
79
80         assert(path);
81
82         if (prefix && !path_startswith(path, prefix))
83                 return -ENOTDIR;
84
85         /* return immediately if directory exists */
86         e = strrchr(path, '/');
87         if (!e)
88                 return -EINVAL;
89
90         if (e == path)
91                 return 0;
92
93         p = strndupa(path, e - path);
94         r = is_dir(p);
95         if (r > 0)
96                 return 0;
97         if (r == 0)
98                 return -ENOTDIR;
99
100         /* create every parent directory in the path, except the last component */
101         p = path + strspn(path, "/");
102         for (;;) {
103                 char t[strlen(path) + 1];
104
105                 e = p + strcspn(p, "/");
106                 p = e + strspn(e, "/");
107
108                 /* Is this the last component? If so, then we're
109                  * done */
110                 if (*p == 0)
111                         return 0;
112
113                 memcpy(t, path, e - path);
114                 t[e-path] = 0;
115
116                 if (prefix && path_startswith(prefix, t))
117                         continue;
118
119                 r = label_mkdir(t, mode, apply);
120                 if (r < 0 && errno != EEXIST)
121                         return -errno;
122         }
123 }
124
125 int mkdir_parents(const char *path, mode_t mode) {
126         return mkdir_parents_internal(NULL, path, mode, false);
127 }
128
129 int mkdir_parents_label(const char *path, mode_t mode) {
130         return mkdir_parents_internal(NULL, path, mode, true);
131 }
132
133 int mkdir_parents_prefix(const char *prefix, const char *path, mode_t mode) {
134         return mkdir_parents_internal(prefix, path, mode, true);
135 }
136
137 static int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, bool apply) {
138         int r;
139
140         /* Like mkdir -p */
141
142         r = mkdir_parents_internal(prefix, path, mode, apply);
143         if (r < 0)
144                 return r;
145
146         r = label_mkdir(path, mode, apply);
147         if (r < 0 && (errno != EEXIST || is_dir(path) <= 0))
148                 return -errno;
149
150         return 0;
151 }
152
153 int mkdir_p(const char *path, mode_t mode) {
154         return mkdir_p_internal(NULL, path, mode, false);
155 }
156
157 int mkdir_p_label(const char *path, mode_t mode) {
158         return mkdir_p_internal(NULL, path, mode, true);
159 }
160
161 int mkdir_p_prefix(const char *prefix, const char *path, mode_t mode) {
162         return mkdir_p_internal(prefix, path, mode, false);
163 }