chiark / gitweb /
mkdir: provide all functions with and without selinux label application
[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 "mkdir.h"
30 #include "label.h"
31 #include "util.h"
32 #include "log.h"
33
34 int mkdir_label(const char *path, mode_t mode) {
35         return label_mkdir(path, mode, true);
36 }
37
38 static int makedir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool apply) {
39         struct stat st;
40
41         if (label_mkdir(path, mode, apply) >= 0)
42                 if (chmod_and_chown(path, mode, uid, gid) < 0)
43                         return -errno;
44
45         if (lstat(path, &st) < 0)
46                 return -errno;
47
48         if ((st.st_mode & 0777) != mode ||
49             st.st_uid != uid ||
50             st.st_gid != gid ||
51             !S_ISDIR(st.st_mode)) {
52                 errno = EEXIST;
53                 return -errno;
54         }
55
56         return 0;
57 }
58
59 int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid) {
60         return makedir_safe(path, mode, uid, gid, false);
61 }
62
63 int mkdir_safe_label(const char *path, mode_t mode, uid_t uid, gid_t gid) {
64         return makedir_safe(path, mode, uid, gid, true);
65 }
66
67 static int makedir_parents(const char *path, mode_t mode, bool apply) {
68         struct stat st;
69         const char *p, *e;
70
71         assert(path);
72
73         /* return immediately if directory exists */
74         e = strrchr(path, '/');
75         if (!e)
76                 return -EINVAL;
77         p = strndupa(path, e - path);
78         if (stat(p, &st) >= 0) {
79                 if ((st.st_mode & S_IFMT) == S_IFDIR)
80                         return 0;
81                 else
82                         return -ENOTDIR;
83         }
84
85         /* create every parent directory in the path, except the last component */
86         p = path + strspn(path, "/");
87         for (;;) {
88                 int r;
89                 char *t;
90
91                 e = p + strcspn(p, "/");
92                 p = e + strspn(e, "/");
93
94                 /* Is this the last component? If so, then we're
95                  * done */
96                 if (*p == 0)
97                         return 0;
98
99                 t = strndup(path, e - path);
100                 if (!t)
101                         return -ENOMEM;
102
103                 r = label_mkdir(t, mode, apply);
104                 free(t);
105
106                 if (r < 0 && errno != EEXIST)
107                         return -errno;
108         }
109 }
110
111 int mkdir_parents(const char *path, mode_t mode) {
112         return makedir_parents(path, mode, false);
113 }
114
115 int mkdir_parents_label(const char *path, mode_t mode) {
116         return makedir_parents(path, mode, true);
117 }
118
119 static int makedir_p(const char *path, mode_t mode, bool apply) {
120         int r;
121
122         /* Like mkdir -p */
123
124         r = makedir_parents(path, mode, apply);
125         if (r < 0)
126                 return r;
127
128         if (label_mkdir(path, mode, apply) < 0 && errno != EEXIST)
129                 return -errno;
130
131         return 0;
132 }
133
134 int mkdir_p(const char *path, mode_t mode) {
135         return makedir_p(path, mode, false);
136 }
137
138 int mkdir_p_label(const char *path, mode_t mode) {
139         return makedir_p(path, mode, true);
140 }