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