]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/mkdir.c
basic: add log_level argument to timezone_is_valid
[thirdparty/systemd.git] / src / basic / mkdir.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
49e942b2
KS
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
49e942b2
KS
6***/
7
49e942b2 8#include <errno.h>
11c3a366 9#include <stdbool.h>
cf0fbc49 10#include <string.h>
11c3a366 11#include <sys/stat.h>
49e942b2 12
c31ad024 13#include "alloc-util.h"
f4f15635 14#include "fs-util.h"
93cc7779 15#include "macro.h"
4ad49000 16#include "mkdir.h"
f4f15635 17#include "path-util.h"
8fcde012 18#include "stat-util.h"
37c1d5e9 19#include "stdio-util.h"
ee104e11 20#include "user-util.h"
49e942b2 21
d50b5839 22int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags, mkdir_func_t _mkdir) {
49e942b2 23 struct stat st;
8f2c2f20 24 int r;
49e942b2 25
dae8b82e
ZJS
26 assert(_mkdir != mkdir);
27
8f2c2f20
LP
28 if (_mkdir(path, mode) >= 0) {
29 r = chmod_and_chown(path, mode, uid, gid);
30 if (r < 0)
31 return r;
32 }
49e942b2
KS
33
34 if (lstat(path, &st) < 0)
35 return -errno;
36
d50b5839 37 if ((flags & MKDIR_FOLLOW_SYMLINK) && S_ISLNK(st.st_mode)) {
c31ad024
YW
38 _cleanup_free_ char *p = NULL;
39
40 r = chase_symlinks(path, NULL, CHASE_NONEXISTENT, &p);
41 if (r < 0)
42 return r;
43 if (r == 0)
d50b5839
ZJS
44 return mkdir_safe_internal(p, mode, uid, gid,
45 flags & ~MKDIR_FOLLOW_SYMLINK,
46 _mkdir);
c31ad024
YW
47
48 if (lstat(p, &st) < 0)
49 return -errno;
50 }
51
37c1d5e9
ZJS
52 if (!S_ISDIR(st.st_mode)) {
53 log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG,
54 "Path \"%s\" already exists and is not a directory, refusing.", path);
55 return -ENOTDIR;
56 }
0cb9fbcd
LP
57 if ((st.st_mode & 0007) > (mode & 0007) ||
58 (st.st_mode & 0070) > (mode & 0070) ||
37c1d5e9
ZJS
59 (st.st_mode & 0700) > (mode & 0700)) {
60 log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG,
61 "Directory \"%s\" already exists, but has mode %04o that is too permissive (%04o was requested), refusing.",
62 path, st.st_mode & 0777, mode);
63 return -EEXIST;
64 }
65 if ((uid != UID_INVALID && st.st_uid != uid) ||
66 (gid != GID_INVALID && st.st_gid != gid)) {
67 char u[DECIMAL_STR_MAX(uid_t)] = "-", g[DECIMAL_STR_MAX(gid_t)] = "-";
68
69 if (uid != UID_INVALID)
70 xsprintf(u, UID_FMT, uid);
71 if (gid != UID_INVALID)
72 xsprintf(g, GID_FMT, gid);
73 log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG,
74 "Directory \"%s\" already exists, but is owned by "UID_FMT":"GID_FMT" (%s:%s was requested), refusing.",
75 path, st.st_uid, st.st_gid, u, g);
2e6534a9 76 return -EEXIST;
37c1d5e9 77 }
49e942b2
KS
78
79 return 0;
80}
81
dae8b82e
ZJS
82int mkdir_errno_wrapper(const char *pathname, mode_t mode) {
83 if (mkdir(pathname, mode) < 0)
84 return -errno;
85 return 0;
86}
87
d50b5839
ZJS
88int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags) {
89 return mkdir_safe_internal(path, mode, uid, gid, flags, mkdir_errno_wrapper);
c66e7f04
KS
90}
91
39bdfa31 92int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
49e942b2 93 const char *p, *e;
4ad49000 94 int r;
49e942b2
KS
95
96 assert(path);
dae8b82e 97 assert(_mkdir != mkdir);
49e942b2 98
4ad49000
LP
99 if (prefix && !path_startswith(path, prefix))
100 return -ENOTDIR;
101
9e13dbae
KS
102 /* return immediately if directory exists */
103 e = strrchr(path, '/');
104 if (!e)
105 return -EINVAL;
4ad49000
LP
106
107 if (e == path)
108 return 0;
109
9e13dbae 110 p = strndupa(path, e - path);
e73a03e0 111 r = is_dir(p, true);
4ad49000
LP
112 if (r > 0)
113 return 0;
114 if (r == 0)
115 return -ENOTDIR;
49e942b2 116
9e13dbae 117 /* create every parent directory in the path, except the last component */
49e942b2
KS
118 p = path + strspn(path, "/");
119 for (;;) {
4ad49000 120 char t[strlen(path) + 1];
49e942b2
KS
121
122 e = p + strcspn(p, "/");
123 p = e + strspn(e, "/");
124
dae8b82e 125 /* Is this the last component? If so, then we're done */
49e942b2
KS
126 if (*p == 0)
127 return 0;
128
4ad49000
LP
129 memcpy(t, path, e - path);
130 t[e-path] = 0;
49e942b2 131
4ad49000
LP
132 if (prefix && path_startswith(prefix, t))
133 continue;
49e942b2 134
39bdfa31 135 r = _mkdir(t, mode);
dae8b82e
ZJS
136 if (r < 0 && r != -EEXIST)
137 return r;
49e942b2
KS
138 }
139}
140
c66e7f04 141int mkdir_parents(const char *path, mode_t mode) {
dae8b82e 142 return mkdir_parents_internal(NULL, path, mode, mkdir_errno_wrapper);
5b585b53
ZJS
143}
144
39bdfa31 145int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
49e942b2
KS
146 int r;
147
148 /* Like mkdir -p */
149
dae8b82e
ZJS
150 assert(_mkdir != mkdir);
151
39bdfa31 152 r = mkdir_parents_internal(prefix, path, mode, _mkdir);
c66e7f04 153 if (r < 0)
49e942b2
KS
154 return r;
155
39bdfa31 156 r = _mkdir(path, mode);
dae8b82e
ZJS
157 if (r < 0 && (r != -EEXIST || is_dir(path, true) <= 0))
158 return r;
49e942b2
KS
159
160 return 0;
161}
c66e7f04
KS
162
163int mkdir_p(const char *path, mode_t mode) {
dae8b82e 164 return mkdir_p_internal(NULL, path, mode, mkdir_errno_wrapper);
4ad49000 165}