1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
34 int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
37 if (label_mkdir(path, mode) >= 0)
38 if (chmod_and_chown(path, mode, uid, gid) < 0)
41 if (lstat(path, &st) < 0)
44 if ((st.st_mode & 0777) != mode ||
47 !S_ISDIR(st.st_mode)) {
55 int mkdir_parents(const char *path, mode_t mode) {
60 /* Creates every parent directory in the path except the last
63 p = path + strspn(path, "/");
68 e = p + strcspn(p, "/");
69 p = e + strspn(e, "/");
71 /* Is this the last component? If so, then we're
76 if (!(t = strndup(path, e - path)))
79 r = label_mkdir(t, mode);
82 if (r < 0 && errno != EEXIST)
87 int mkdir_p(const char *path, mode_t mode) {
92 if ((r = mkdir_parents(path, mode)) < 0)
95 if (label_mkdir(path, mode) < 0 && errno != EEXIST)