]>
Commit | Line | Data |
---|---|---|
49e942b2 KS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
49e942b2 KS |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
49e942b2 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
49e942b2 KS |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
49e942b2 | 20 | #include <errno.h> |
11c3a366 | 21 | #include <stdbool.h> |
cf0fbc49 | 22 | #include <string.h> |
11c3a366 | 23 | #include <sys/stat.h> |
49e942b2 | 24 | |
f4f15635 | 25 | #include "fs-util.h" |
93cc7779 | 26 | #include "macro.h" |
4ad49000 | 27 | #include "mkdir.h" |
f4f15635 | 28 | #include "path-util.h" |
8fcde012 | 29 | #include "stat-util.h" |
ee104e11 | 30 | #include "user-util.h" |
49e942b2 | 31 | |
39bdfa31 | 32 | int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, mkdir_func_t _mkdir) { |
49e942b2 | 33 | struct stat st; |
8f2c2f20 | 34 | int r; |
49e942b2 | 35 | |
8f2c2f20 LP |
36 | if (_mkdir(path, mode) >= 0) { |
37 | r = chmod_and_chown(path, mode, uid, gid); | |
38 | if (r < 0) | |
39 | return r; | |
40 | } | |
49e942b2 KS |
41 | |
42 | if (lstat(path, &st) < 0) | |
43 | return -errno; | |
44 | ||
0cb9fbcd LP |
45 | if ((st.st_mode & 0007) > (mode & 0007) || |
46 | (st.st_mode & 0070) > (mode & 0070) || | |
47 | (st.st_mode & 0700) > (mode & 0700) || | |
fed1e721 LP |
48 | (uid != UID_INVALID && st.st_uid != uid) || |
49 | (gid != GID_INVALID && st.st_gid != gid) || | |
2e6534a9 ZJS |
50 | !S_ISDIR(st.st_mode)) |
51 | return -EEXIST; | |
49e942b2 KS |
52 | |
53 | return 0; | |
54 | } | |
55 | ||
c66e7f04 | 56 | int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid) { |
69c2b6be | 57 | return mkdir_safe_internal(path, mode, uid, gid, mkdir); |
c66e7f04 KS |
58 | } |
59 | ||
39bdfa31 | 60 | int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) { |
49e942b2 | 61 | const char *p, *e; |
4ad49000 | 62 | int r; |
49e942b2 KS |
63 | |
64 | assert(path); | |
65 | ||
4ad49000 LP |
66 | if (prefix && !path_startswith(path, prefix)) |
67 | return -ENOTDIR; | |
68 | ||
9e13dbae KS |
69 | /* return immediately if directory exists */ |
70 | e = strrchr(path, '/'); | |
71 | if (!e) | |
72 | return -EINVAL; | |
4ad49000 LP |
73 | |
74 | if (e == path) | |
75 | return 0; | |
76 | ||
9e13dbae | 77 | p = strndupa(path, e - path); |
e73a03e0 | 78 | r = is_dir(p, true); |
4ad49000 LP |
79 | if (r > 0) |
80 | return 0; | |
81 | if (r == 0) | |
82 | return -ENOTDIR; | |
49e942b2 | 83 | |
9e13dbae | 84 | /* create every parent directory in the path, except the last component */ |
49e942b2 KS |
85 | p = path + strspn(path, "/"); |
86 | for (;;) { | |
4ad49000 | 87 | char t[strlen(path) + 1]; |
49e942b2 KS |
88 | |
89 | e = p + strcspn(p, "/"); | |
90 | p = e + strspn(e, "/"); | |
91 | ||
92 | /* Is this the last component? If so, then we're | |
93 | * done */ | |
94 | if (*p == 0) | |
95 | return 0; | |
96 | ||
4ad49000 LP |
97 | memcpy(t, path, e - path); |
98 | t[e-path] = 0; | |
49e942b2 | 99 | |
4ad49000 LP |
100 | if (prefix && path_startswith(prefix, t)) |
101 | continue; | |
49e942b2 | 102 | |
39bdfa31 | 103 | r = _mkdir(t, mode); |
49e942b2 KS |
104 | if (r < 0 && errno != EEXIST) |
105 | return -errno; | |
106 | } | |
107 | } | |
108 | ||
c66e7f04 | 109 | int mkdir_parents(const char *path, mode_t mode) { |
39bdfa31 | 110 | return mkdir_parents_internal(NULL, path, mode, mkdir); |
5b585b53 ZJS |
111 | } |
112 | ||
39bdfa31 | 113 | int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) { |
49e942b2 KS |
114 | int r; |
115 | ||
116 | /* Like mkdir -p */ | |
117 | ||
39bdfa31 | 118 | r = mkdir_parents_internal(prefix, path, mode, _mkdir); |
c66e7f04 | 119 | if (r < 0) |
49e942b2 KS |
120 | return r; |
121 | ||
39bdfa31 | 122 | r = _mkdir(path, mode); |
e73a03e0 | 123 | if (r < 0 && (errno != EEXIST || is_dir(path, true) <= 0)) |
49e942b2 KS |
124 | return -errno; |
125 | ||
126 | return 0; | |
127 | } | |
c66e7f04 KS |
128 | |
129 | int mkdir_p(const char *path, mode_t mode) { | |
39bdfa31 | 130 | return mkdir_p_internal(NULL, path, mode, mkdir); |
4ad49000 | 131 | } |