chiark / gitweb /
terminal-util: remove unnecessary check of result of isatty() (#4000)
[elogind.git] / src / basic / mkdir.c
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
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
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
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include <sys/stat.h>
24
25 #include "fs-util.h"
26 #include "macro.h"
27 #include "mkdir.h"
28 #include "path-util.h"
29 #include "stat-util.h"
30 #include "user-util.h"
31
32 /// Additional includes needed by elogind
33 #include "missing.h"
34
35 int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, mkdir_func_t _mkdir) {
36         struct stat st;
37
38         if (_mkdir(path, mode) >= 0)
39                 if (chmod_and_chown(path, mode, uid, gid) < 0)
40                         return -errno;
41
42         if (lstat(path, &st) < 0)
43                 return -errno;
44
45         if ((st.st_mode & 0007) > (mode & 0007) ||
46             (st.st_mode & 0070) > (mode & 0070) ||
47             (st.st_mode & 0700) > (mode & 0700) ||
48             (uid != UID_INVALID && st.st_uid != uid) ||
49             (gid != GID_INVALID && st.st_gid != gid) ||
50             !S_ISDIR(st.st_mode))
51                 return -EEXIST;
52
53         return 0;
54 }
55
56 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid) {
57         return mkdir_safe_internal(path, mode, uid, gid, mkdir);
58 }
59
60 int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
61         const char *p, *e;
62         int r;
63
64         assert(path);
65
66         if (prefix && !path_startswith(path, prefix))
67                 return -ENOTDIR;
68
69         /* return immediately if directory exists */
70         e = strrchr(path, '/');
71         if (!e)
72                 return -EINVAL;
73
74         if (e == path)
75                 return 0;
76
77         p = strndupa(path, e - path);
78         r = is_dir(p, true);
79         if (r > 0)
80                 return 0;
81         if (r == 0)
82                 return -ENOTDIR;
83
84         /* create every parent directory in the path, except the last component */
85         p = path + strspn(path, "/");
86         for (;;) {
87                 char t[strlen(path) + 1];
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
97                 memcpy(t, path, e - path);
98                 t[e-path] = 0;
99
100                 if (prefix && path_startswith(prefix, t))
101                         continue;
102
103                 r = _mkdir(t, mode);
104                 if (r < 0 && errno != EEXIST)
105                         return -errno;
106         }
107 }
108
109 int mkdir_parents(const char *path, mode_t mode) {
110         return mkdir_parents_internal(NULL, path, mode, mkdir);
111 }
112
113 int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
114         int r;
115
116         /* Like mkdir -p */
117
118         r = mkdir_parents_internal(prefix, path, mode, _mkdir);
119         if (r < 0)
120                 return r;
121
122         r = _mkdir(path, mode);
123         if (r < 0 && (errno != EEXIST || is_dir(path, true) <= 0))
124                 return -errno;
125
126         return 0;
127 }
128
129 int mkdir_p(const char *path, mode_t mode) {
130         return mkdir_p_internal(NULL, path, mode, mkdir);
131 }