1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
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.
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.
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/>.
26 #include "alloc-util.h"
30 #include "path-util.h"
31 #include "stat-util.h"
32 //#include "stdio-util.h"
33 #include "user-util.h"
35 /// Additional includes needed by elogind
38 int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink, mkdir_func_t _mkdir) {
39 int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags, mkdir_func_t _mkdir) {
43 assert(_mkdir != mkdir);
45 if (_mkdir(path, mode) >= 0) {
46 r = chmod_and_chown(path, mode, uid, gid);
51 if (lstat(path, &st) < 0)
54 if ((flags & MKDIR_FOLLOW_SYMLINK) && S_ISLNK(st.st_mode)) {
55 _cleanup_free_ char *p = NULL;
57 r = chase_symlinks(path, NULL, CHASE_NONEXISTENT, &p);
61 return mkdir_safe_internal(p, mode, uid, gid,
62 flags & ~MKDIR_FOLLOW_SYMLINK,
65 if (lstat(p, &st) < 0)
69 if (!S_ISDIR(st.st_mode)) {
70 log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG,
71 "Path \"%s\" already exists and is not a directory, refusing.", path);
74 if ((st.st_mode & 0007) > (mode & 0007) ||
75 (st.st_mode & 0070) > (mode & 0070) ||
76 (st.st_mode & 0700) > (mode & 0700)) {
77 log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG,
78 "Directory \"%s\" already exists, but has mode %04o that is too permissive (%04o was requested), refusing.",
79 path, st.st_mode & 0777, mode);
82 if ((uid != UID_INVALID && st.st_uid != uid) ||
83 (gid != GID_INVALID && st.st_gid != gid)) {
84 char u[DECIMAL_STR_MAX(uid_t)] = "-", g[DECIMAL_STR_MAX(gid_t)] = "-";
86 if (uid != UID_INVALID)
87 xsprintf(u, UID_FMT, uid);
88 if (gid != UID_INVALID)
89 xsprintf(g, GID_FMT, gid);
90 log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG,
91 "Directory \"%s\" already exists, but is owned by "UID_FMT":"GID_FMT" (%s:%s was requested), refusing.",
92 path, st.st_uid, st.st_gid, u, g);
99 int mkdir_errno_wrapper(const char *pathname, mode_t mode) {
100 if (mkdir(pathname, mode) < 0)
105 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags) {
106 return mkdir_safe_internal(path, mode, uid, gid, flags, mkdir_errno_wrapper);
109 int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
114 assert(_mkdir != mkdir);
116 if (prefix && !path_startswith(path, prefix))
119 /* return immediately if directory exists */
120 e = strrchr(path, '/');
127 p = strndupa(path, e - path);
134 /* create every parent directory in the path, except the last component */
135 p = path + strspn(path, "/");
137 char t[strlen(path) + 1];
139 e = p + strcspn(p, "/");
140 p = e + strspn(e, "/");
142 /* Is this the last component? If so, then we're done */
146 memcpy(t, path, e - path);
149 if (prefix && path_startswith(prefix, t))
153 if (r < 0 && r != -EEXIST)
158 int mkdir_parents(const char *path, mode_t mode) {
159 return mkdir_parents_internal(NULL, path, mode, mkdir_errno_wrapper);
162 int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
167 assert(_mkdir != mkdir);
169 r = mkdir_parents_internal(prefix, path, mode, _mkdir);
173 r = _mkdir(path, mode);
174 if (r < 0 && (r != -EEXIST || is_dir(path, true) <= 0))
180 int mkdir_p(const char *path, mode_t mode) {
181 return mkdir_p_internal(NULL, path, mode, mkdir_errno_wrapper);