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