chiark / gitweb /
macro: add DIV_ROUND_UP()
[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 & 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                 errno = EEXIST;
51                 return -errno;
52         }
53
54         return 0;
55 }
56
57 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid) {
58         return mkdir_safe_internal(path, mode, uid, gid, mkdir);
59 }
60
61 int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
62         const char *p, *e;
63         int r;
64
65         assert(path);
66
67         if (prefix && !path_startswith(path, prefix))
68                 return -ENOTDIR;
69
70         /* return immediately if directory exists */
71         e = strrchr(path, '/');
72         if (!e)
73                 return -EINVAL;
74
75         if (e == path)
76                 return 0;
77
78         p = strndupa(path, e - path);
79         r = is_dir(p, true);
80         if (r > 0)
81                 return 0;
82         if (r == 0)
83                 return -ENOTDIR;
84
85         /* create every parent directory in the path, except the last component */
86         p = path + strspn(path, "/");
87         for (;;) {
88                 char t[strlen(path) + 1];
89
90                 e = p + strcspn(p, "/");
91                 p = e + strspn(e, "/");
92
93                 /* Is this the last component? If so, then we're
94                  * done */
95                 if (*p == 0)
96                         return 0;
97
98                 memcpy(t, path, e - path);
99                 t[e-path] = 0;
100
101                 if (prefix && path_startswith(prefix, t))
102                         continue;
103
104                 r = _mkdir(t, mode);
105                 if (r < 0 && errno != EEXIST)
106                         return -errno;
107         }
108 }
109
110 int mkdir_parents(const char *path, mode_t mode) {
111         return mkdir_parents_internal(NULL, path, mode, mkdir);
112 }
113
114 int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
115         int r;
116
117         /* Like mkdir -p */
118
119         r = mkdir_parents_internal(prefix, path, mode, _mkdir);
120         if (r < 0)
121                 return r;
122
123         r = _mkdir(path, mode);
124         if (r < 0 && (errno != EEXIST || is_dir(path, true) <= 0))
125                 return -errno;
126
127         return 0;
128 }
129
130 int mkdir_p(const char *path, mode_t mode) {
131         return mkdir_p_internal(NULL, path, mode, mkdir);
132 }