]> git.proxmox.com Git - systemd.git/blob - src/shared/mkdir.c
f941efb401809333240dbef8e0eeefdbc3919cee
[systemd.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_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, mkdir_func_t _mkdir) {
35 struct stat st;
36
37 if (_mkdir(path, mode) >= 0)
38 if (chmod_and_chown(path, mode, uid, gid) < 0)
39 return -errno;
40
41 if (lstat(path, &st) < 0)
42 return -errno;
43
44 if ((st.st_mode & 0007) > (mode & 0007) ||
45 (st.st_mode & 0070) > (mode & 0070) ||
46 (st.st_mode & 0700) > (mode & 0700) ||
47 (uid != (uid_t) -1 && st.st_uid != uid) ||
48 (gid != (gid_t) -1 && st.st_gid != gid) ||
49 !S_ISDIR(st.st_mode)) {
50 errno = EEXIST;
51 return -errno;
52 }
53
54 return 0;
55 }
56
57 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid) {
58 return mkdir_safe_internal(path, mode, uid, gid, mkdir);
59 }
60
61 int is_dir(const char* path, bool follow) {
62 struct stat st;
63
64 if (follow) {
65 if (stat(path, &st) < 0)
66 return -errno;
67 } else {
68 if (lstat(path, &st) < 0)
69 return -errno;
70 }
71
72 return S_ISDIR(st.st_mode);
73 }
74
75 int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
76 const char *p, *e;
77 int r;
78
79 assert(path);
80
81 if (prefix && !path_startswith(path, prefix))
82 return -ENOTDIR;
83
84 /* return immediately if directory exists */
85 e = strrchr(path, '/');
86 if (!e)
87 return -EINVAL;
88
89 if (e == path)
90 return 0;
91
92 p = strndupa(path, e - path);
93 r = is_dir(p, true);
94 if (r > 0)
95 return 0;
96 if (r == 0)
97 return -ENOTDIR;
98
99 /* create every parent directory in the path, except the last component */
100 p = path + strspn(path, "/");
101 for (;;) {
102 char t[strlen(path) + 1];
103
104 e = p + strcspn(p, "/");
105 p = e + strspn(e, "/");
106
107 /* Is this the last component? If so, then we're
108 * done */
109 if (*p == 0)
110 return 0;
111
112 memcpy(t, path, e - path);
113 t[e-path] = 0;
114
115 if (prefix && path_startswith(prefix, t))
116 continue;
117
118 r = _mkdir(t, mode);
119 if (r < 0 && errno != EEXIST)
120 return -errno;
121 }
122 }
123
124 int mkdir_parents(const char *path, mode_t mode) {
125 return mkdir_parents_internal(NULL, path, mode, mkdir);
126 }
127
128 int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
129 int r;
130
131 /* Like mkdir -p */
132
133 r = mkdir_parents_internal(prefix, path, mode, _mkdir);
134 if (r < 0)
135 return r;
136
137 r = _mkdir(path, mode);
138 if (r < 0 && (errno != EEXIST || is_dir(path, true) <= 0))
139 return -errno;
140
141 return 0;
142 }
143
144 int mkdir_p(const char *path, mode_t mode) {
145 return mkdir_p_internal(NULL, path, mode, mkdir);
146 }
147
148 int mkdir_p_prefix(const char *prefix, const char *path, mode_t mode) {
149 return mkdir_p_internal(prefix, path, mode, mkdir);
150 }