chiark / gitweb /
basic: include only what we use
[elogind.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 <stdbool.h>
24 #include <string.h>
25 #include <sys/stat.h>
26
27 #include "fs-util.h"
28 #include "mkdir.h"
29 #include "path-util.h"
30 #include "macro.h"
31 #include "stat-util.h"
32 #include "user-util.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 & 0007) > (mode & 0007) ||
45             (st.st_mode & 0070) > (mode & 0070) ||
46             (st.st_mode & 0700) > (mode & 0700) ||
47             (uid != UID_INVALID && st.st_uid != uid) ||
48             (gid != GID_INVALID && st.st_gid != gid) ||
49             !S_ISDIR(st.st_mode))
50                 return -EEXIST;
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 int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
60         const char *p, *e;
61         int r;
62
63         assert(path);
64
65         if (prefix && !path_startswith(path, prefix))
66                 return -ENOTDIR;
67
68         /* return immediately if directory exists */
69         e = strrchr(path, '/');
70         if (!e)
71                 return -EINVAL;
72
73         if (e == path)
74                 return 0;
75
76         p = strndupa(path, e - path);
77         r = is_dir(p, true);
78         if (r > 0)
79                 return 0;
80         if (r == 0)
81                 return -ENOTDIR;
82
83         /* create every parent directory in the path, except the last component */
84         p = path + strspn(path, "/");
85         for (;;) {
86                 char t[strlen(path) + 1];
87
88                 e = p + strcspn(p, "/");
89                 p = e + strspn(e, "/");
90
91                 /* Is this the last component? If so, then we're
92                  * done */
93                 if (*p == 0)
94                         return 0;
95
96                 memcpy(t, path, e - path);
97                 t[e-path] = 0;
98
99                 if (prefix && path_startswith(prefix, t))
100                         continue;
101
102                 r = _mkdir(t, mode);
103                 if (r < 0 && errno != EEXIST)
104                         return -errno;
105         }
106 }
107
108 int mkdir_parents(const char *path, mode_t mode) {
109         return mkdir_parents_internal(NULL, path, mode, mkdir);
110 }
111
112 int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
113         int r;
114
115         /* Like mkdir -p */
116
117         r = mkdir_parents_internal(prefix, path, mode, _mkdir);
118         if (r < 0)
119                 return r;
120
121         r = _mkdir(path, mode);
122         if (r < 0 && (errno != EEXIST || is_dir(path, true) <= 0))
123                 return -errno;
124
125         return 0;
126 }
127
128 int mkdir_p(const char *path, mode_t mode) {
129         return mkdir_p_internal(NULL, path, mode, mkdir);
130 }