1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
12 #include "path-util.h"
13 #include "stat-util.h"
14 //#include "stdio-util.h"
15 #include "user-util.h"
17 /// Additional includes needed by elogind
20 int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink, mkdir_func_t _mkdir) {
21 int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags, mkdir_func_t _mkdir) {
25 assert(_mkdir != mkdir);
27 if (_mkdir(path, mode) >= 0) {
28 r = chmod_and_chown(path, mode, uid, gid);
33 if (lstat(path, &st) < 0)
36 if ((flags & MKDIR_FOLLOW_SYMLINK) && S_ISLNK(st.st_mode)) {
37 _cleanup_free_ char *p = NULL;
39 r = chase_symlinks(path, NULL, CHASE_NONEXISTENT, &p);
43 return mkdir_safe_internal(p, mode, uid, gid,
44 flags & ~MKDIR_FOLLOW_SYMLINK,
47 if (lstat(p, &st) < 0)
51 if (!S_ISDIR(st.st_mode)) {
52 log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG,
53 "Path \"%s\" already exists and is not a directory, refusing.", path);
56 if ((st.st_mode & 0007) > (mode & 0007) ||
57 (st.st_mode & 0070) > (mode & 0070) ||
58 (st.st_mode & 0700) > (mode & 0700)) {
59 log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG,
60 "Directory \"%s\" already exists, but has mode %04o that is too permissive (%04o was requested), refusing.",
61 path, st.st_mode & 0777, mode);
64 if ((uid != UID_INVALID && st.st_uid != uid) ||
65 (gid != GID_INVALID && st.st_gid != gid)) {
66 char u[DECIMAL_STR_MAX(uid_t)] = "-", g[DECIMAL_STR_MAX(gid_t)] = "-";
68 if (uid != UID_INVALID)
69 xsprintf(u, UID_FMT, uid);
70 if (gid != UID_INVALID)
71 xsprintf(g, GID_FMT, gid);
72 log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG,
73 "Directory \"%s\" already exists, but is owned by "UID_FMT":"GID_FMT" (%s:%s was requested), refusing.",
74 path, st.st_uid, st.st_gid, u, g);
81 int mkdir_errno_wrapper(const char *pathname, mode_t mode) {
82 if (mkdir(pathname, mode) < 0)
87 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags) {
88 return mkdir_safe_internal(path, mode, uid, gid, flags, mkdir_errno_wrapper);
91 int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
96 assert(_mkdir != mkdir);
98 if (prefix && !path_startswith(path, prefix))
101 /* return immediately if directory exists */
102 e = strrchr(path, '/');
109 p = strndupa(path, e - path);
116 /* create every parent directory in the path, except the last component */
117 p = path + strspn(path, "/");
119 char t[strlen(path) + 1];
121 e = p + strcspn(p, "/");
122 p = e + strspn(e, "/");
124 /* Is this the last component? If so, then we're done */
128 memcpy(t, path, e - path);
131 if (prefix && path_startswith(prefix, t))
135 if (r < 0 && r != -EEXIST)
140 int mkdir_parents(const char *path, mode_t mode) {
141 return mkdir_parents_internal(NULL, path, mode, mkdir_errno_wrapper);
144 int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
149 assert(_mkdir != mkdir);
151 r = mkdir_parents_internal(prefix, path, mode, _mkdir);
155 r = _mkdir(path, mode);
156 if (r < 0 && (r != -EEXIST || is_dir(path, true) <= 0))
162 int mkdir_p(const char *path, mode_t mode) {
163 return mkdir_p_internal(NULL, path, mode, mkdir_errno_wrapper);