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