chiark / gitweb /
ptyfwd: reset nonblocking mode
[elogind.git] / src / shared / 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 <assert.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28
29 #include "label.h"
30 #include "util.h"
31 #include "path-util.h"
32 #include "mkdir.h"
33
34 int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, mkdir_func_t _mkdir) {
35         struct stat st;
36
37         if (_mkdir(path, mode) >= 0)
38                 if (chmod_and_chown(path, mode, uid, gid) < 0)
39                         return -errno;
40
41         if (lstat(path, &st) < 0)
42                 return -errno;
43
44         if ((st.st_mode & 0777) != mode ||
45             st.st_uid != uid ||
46             st.st_gid != gid ||
47             !S_ISDIR(st.st_mode)) {
48                 errno = EEXIST;
49                 return -errno;
50         }
51
52         return 0;
53 }
54
55 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid) {
56         return mkdir_safe_internal(path, mode, uid, gid, mkdir);
57 }
58
59 static int is_dir(const char* path) {
60         struct stat st;
61
62         if (stat(path, &st) < 0)
63                 return -errno;
64
65         return S_ISDIR(st.st_mode);
66 }
67
68 int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
69         const char *p, *e;
70         int r;
71
72         assert(path);
73
74         if (prefix && !path_startswith(path, prefix))
75                 return -ENOTDIR;
76
77         /* return immediately if directory exists */
78         e = strrchr(path, '/');
79         if (!e)
80                 return -EINVAL;
81
82         if (e == path)
83                 return 0;
84
85         p = strndupa(path, e - path);
86         r = is_dir(p);
87         if (r > 0)
88                 return 0;
89         if (r == 0)
90                 return -ENOTDIR;
91
92         /* create every parent directory in the path, except the last component */
93         p = path + strspn(path, "/");
94         for (;;) {
95                 char t[strlen(path) + 1];
96
97                 e = p + strcspn(p, "/");
98                 p = e + strspn(e, "/");
99
100                 /* Is this the last component? If so, then we're
101                  * done */
102                 if (*p == 0)
103                         return 0;
104
105                 memcpy(t, path, e - path);
106                 t[e-path] = 0;
107
108                 if (prefix && path_startswith(prefix, t))
109                         continue;
110
111                 r = _mkdir(t, mode);
112                 if (r < 0 && errno != EEXIST)
113                         return -errno;
114         }
115 }
116
117 int mkdir_parents(const char *path, mode_t mode) {
118         return mkdir_parents_internal(NULL, path, mode, mkdir);
119 }
120
121 int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
122         int r;
123
124         /* Like mkdir -p */
125
126         r = mkdir_parents_internal(prefix, path, mode, _mkdir);
127         if (r < 0)
128                 return r;
129
130         r = _mkdir(path, mode);
131         if (r < 0 && (errno != EEXIST || is_dir(path) <= 0))
132                 return -errno;
133
134         return 0;
135 }
136
137 int mkdir_p(const char *path, mode_t mode) {
138         return mkdir_p_internal(NULL, path, mode, mkdir);
139 }
140
141 int mkdir_p_prefix(const char *prefix, const char *path, mode_t mode) {
142         return mkdir_p_internal(prefix, path, mode, mkdir);
143 }