]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/mkdir.c
Merge pull request #7042 from vcaputo/iteratedcache
[thirdparty/systemd.git] / src / basic / mkdir.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include <sys/stat.h>
25
26 #include "alloc-util.h"
27 #include "fs-util.h"
28 #include "macro.h"
29 #include "mkdir.h"
30 #include "path-util.h"
31 #include "stat-util.h"
32 #include "user-util.h"
33
34 int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink, mkdir_func_t _mkdir) {
35 struct stat st;
36 int r;
37
38 assert(_mkdir != mkdir);
39
40 if (_mkdir(path, mode) >= 0) {
41 r = chmod_and_chown(path, mode, uid, gid);
42 if (r < 0)
43 return r;
44 }
45
46 if (lstat(path, &st) < 0)
47 return -errno;
48
49 if (follow_symlink && S_ISLNK(st.st_mode)) {
50 _cleanup_free_ char *p = NULL;
51
52 r = chase_symlinks(path, NULL, CHASE_NONEXISTENT, &p);
53 if (r < 0)
54 return r;
55 if (r == 0)
56 return mkdir_safe_internal(p, mode, uid, gid, false, _mkdir);
57
58 if (lstat(p, &st) < 0)
59 return -errno;
60 }
61
62 if ((st.st_mode & 0007) > (mode & 0007) ||
63 (st.st_mode & 0070) > (mode & 0070) ||
64 (st.st_mode & 0700) > (mode & 0700) ||
65 (uid != UID_INVALID && st.st_uid != uid) ||
66 (gid != GID_INVALID && st.st_gid != gid) ||
67 !S_ISDIR(st.st_mode))
68 return -EEXIST;
69
70 return 0;
71 }
72
73 int mkdir_errno_wrapper(const char *pathname, mode_t mode) {
74 if (mkdir(pathname, mode) < 0)
75 return -errno;
76 return 0;
77 }
78
79 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink) {
80 return mkdir_safe_internal(path, mode, uid, gid, follow_symlink, mkdir_errno_wrapper);
81 }
82
83 int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
84 const char *p, *e;
85 int r;
86
87 assert(path);
88 assert(_mkdir != mkdir);
89
90 if (prefix && !path_startswith(path, prefix))
91 return -ENOTDIR;
92
93 /* return immediately if directory exists */
94 e = strrchr(path, '/');
95 if (!e)
96 return -EINVAL;
97
98 if (e == path)
99 return 0;
100
101 p = strndupa(path, e - path);
102 r = is_dir(p, true);
103 if (r > 0)
104 return 0;
105 if (r == 0)
106 return -ENOTDIR;
107
108 /* create every parent directory in the path, except the last component */
109 p = path + strspn(path, "/");
110 for (;;) {
111 char t[strlen(path) + 1];
112
113 e = p + strcspn(p, "/");
114 p = e + strspn(e, "/");
115
116 /* Is this the last component? If so, then we're done */
117 if (*p == 0)
118 return 0;
119
120 memcpy(t, path, e - path);
121 t[e-path] = 0;
122
123 if (prefix && path_startswith(prefix, t))
124 continue;
125
126 r = _mkdir(t, mode);
127 if (r < 0 && r != -EEXIST)
128 return r;
129 }
130 }
131
132 int mkdir_parents(const char *path, mode_t mode) {
133 return mkdir_parents_internal(NULL, path, mode, mkdir_errno_wrapper);
134 }
135
136 int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
137 int r;
138
139 /* Like mkdir -p */
140
141 assert(_mkdir != mkdir);
142
143 r = mkdir_parents_internal(prefix, path, mode, _mkdir);
144 if (r < 0)
145 return r;
146
147 r = _mkdir(path, mode);
148 if (r < 0 && (r != -EEXIST || is_dir(path, true) <= 0))
149 return r;
150
151 return 0;
152 }
153
154 int mkdir_p(const char *path, mode_t mode) {
155 return mkdir_p_internal(NULL, path, mode, mkdir_errno_wrapper);
156 }