chiark / gitweb /
tmpfiles: add new "C" line for copying files or directories
[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_t) -1 && st.st_uid != uid) ||
48             (gid != (gid_t) -1 && 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 is_dir(const char* path) {
62         struct stat st;
63
64         if (stat(path, &st) < 0)
65                 return -errno;
66
67         return S_ISDIR(st.st_mode);
68 }
69
70 int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
71         const char *p, *e;
72         int r;
73
74         assert(path);
75
76         if (prefix && !path_startswith(path, prefix))
77                 return -ENOTDIR;
78
79         /* return immediately if directory exists */
80         e = strrchr(path, '/');
81         if (!e)
82                 return -EINVAL;
83
84         if (e == path)
85                 return 0;
86
87         p = strndupa(path, e - path);
88         r = is_dir(p);
89         if (r > 0)
90                 return 0;
91         if (r == 0)
92                 return -ENOTDIR;
93
94         /* create every parent directory in the path, except the last component */
95         p = path + strspn(path, "/");
96         for (;;) {
97                 char t[strlen(path) + 1];
98
99                 e = p + strcspn(p, "/");
100                 p = e + strspn(e, "/");
101
102                 /* Is this the last component? If so, then we're
103                  * done */
104                 if (*p == 0)
105                         return 0;
106
107                 memcpy(t, path, e - path);
108                 t[e-path] = 0;
109
110                 if (prefix && path_startswith(prefix, t))
111                         continue;
112
113                 r = _mkdir(t, mode);
114                 if (r < 0 && errno != EEXIST)
115                         return -errno;
116         }
117 }
118
119 int mkdir_parents(const char *path, mode_t mode) {
120         return mkdir_parents_internal(NULL, path, mode, mkdir);
121 }
122
123 int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
124         int r;
125
126         /* Like mkdir -p */
127
128         r = mkdir_parents_internal(prefix, path, mode, _mkdir);
129         if (r < 0)
130                 return r;
131
132         r = _mkdir(path, mode);
133         if (r < 0 && (errno != EEXIST || is_dir(path) <= 0))
134                 return -errno;
135
136         return 0;
137 }
138
139 int mkdir_p(const char *path, mode_t mode) {
140         return mkdir_p_internal(NULL, path, mode, mkdir);
141 }
142
143 int mkdir_p_prefix(const char *prefix, const char *path, mode_t mode) {
144         return mkdir_p_internal(prefix, path, mode, mkdir);
145 }