]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/mkdir.c
Merge pull request #6918 from ssahani/issue-5625
[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 if (_mkdir(path, mode) >= 0) {
39 r = chmod_and_chown(path, mode, uid, gid);
40 if (r < 0)
41 return r;
42 }
43
44 if (lstat(path, &st) < 0)
45 return -errno;
46
47 if (follow_symlink && S_ISLNK(st.st_mode)) {
48 _cleanup_free_ char *p = NULL;
49
50 r = chase_symlinks(path, NULL, CHASE_NONEXISTENT, &p);
51 if (r < 0)
52 return r;
53 if (r == 0)
54 return mkdir_safe_internal(p, mode, uid, gid, false, _mkdir);
55
56 if (lstat(p, &st) < 0)
57 return -errno;
58 }
59
60 if ((st.st_mode & 0007) > (mode & 0007) ||
61 (st.st_mode & 0070) > (mode & 0070) ||
62 (st.st_mode & 0700) > (mode & 0700) ||
63 (uid != UID_INVALID && st.st_uid != uid) ||
64 (gid != GID_INVALID && st.st_gid != gid) ||
65 !S_ISDIR(st.st_mode))
66 return -EEXIST;
67
68 return 0;
69 }
70
71 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink) {
72 return mkdir_safe_internal(path, mode, uid, gid, follow_symlink, mkdir);
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 }