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