]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/mkdir.c
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 "user-util.h"
34 int mkdir_safe_internal(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
, bool follow_symlink
, mkdir_func_t _mkdir
) {
38 assert(_mkdir
!= mkdir
);
40 if (_mkdir(path
, mode
) >= 0) {
41 r
= chmod_and_chown(path
, mode
, uid
, gid
);
46 if (lstat(path
, &st
) < 0)
49 if (follow_symlink
&& S_ISLNK(st
.st_mode
)) {
50 _cleanup_free_
char *p
= NULL
;
52 r
= chase_symlinks(path
, NULL
, CHASE_NONEXISTENT
, &p
);
56 return mkdir_safe_internal(p
, mode
, uid
, gid
, false, _mkdir
);
58 if (lstat(p
, &st
) < 0)
62 if ((st
.st_mode
& 0007) > (mode
& 0007) ||
63 (st
.st_mode
& 0070) > (mode
& 0070) ||
64 (st
.st_mode
& 0700) > (mode
& 0700) ||
65 (uid
!= UID_INVALID
&& st
.st_uid
!= uid
) ||
66 (gid
!= GID_INVALID
&& st
.st_gid
!= gid
) ||
73 int mkdir_errno_wrapper(const char *pathname
, mode_t mode
) {
74 if (mkdir(pathname
, mode
) < 0)
79 int mkdir_safe(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
, bool follow_symlink
) {
80 return mkdir_safe_internal(path
, mode
, uid
, gid
, follow_symlink
, mkdir_errno_wrapper
);
83 int mkdir_parents_internal(const char *prefix
, const char *path
, mode_t mode
, mkdir_func_t _mkdir
) {
88 assert(_mkdir
!= mkdir
);
90 if (prefix
&& !path_startswith(path
, prefix
))
93 /* return immediately if directory exists */
94 e
= strrchr(path
, '/');
101 p
= strndupa(path
, e
- path
);
108 /* create every parent directory in the path, except the last component */
109 p
= path
+ strspn(path
, "/");
111 char t
[strlen(path
) + 1];
113 e
= p
+ strcspn(p
, "/");
114 p
= e
+ strspn(e
, "/");
116 /* Is this the last component? If so, then we're done */
120 memcpy(t
, path
, e
- path
);
123 if (prefix
&& path_startswith(prefix
, t
))
127 if (r
< 0 && r
!= -EEXIST
)
132 int mkdir_parents(const char *path
, mode_t mode
) {
133 return mkdir_parents_internal(NULL
, path
, mode
, mkdir_errno_wrapper
);
136 int mkdir_p_internal(const char *prefix
, const char *path
, mode_t mode
, mkdir_func_t _mkdir
) {
141 assert(_mkdir
!= mkdir
);
143 r
= mkdir_parents_internal(prefix
, path
, mode
, _mkdir
);
147 r
= _mkdir(path
, mode
);
148 if (r
< 0 && (r
!= -EEXIST
|| is_dir(path
, true) <= 0))
154 int mkdir_p(const char *path
, mode_t mode
) {
155 return mkdir_p_internal(NULL
, path
, mode
, mkdir_errno_wrapper
);