chiark / gitweb /
base-filesystem: avoid all searching if the link already exists
[elogind.git] / src / shared / base-filesystem.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Kay Sievers
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 <errno.h>
23 #include <sys/stat.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <unistd.h>
28
29 #include "base-filesystem.h"
30 #include "log.h"
31 #include "macro.h"
32 #include "strv.h"
33 #include "util.h"
34 #include "label.h"
35 #include "mkdir.h"
36
37 typedef struct BaseFilesystem {
38         const char *dir;
39         mode_t mode;
40         const char *target;
41         const char *exists;
42 } BaseFilesystem;
43
44 static const BaseFilesystem table[] = {
45         { "bin",      0, "usr/bin",                             NULL },
46         { "lib",      0, "usr/lib",                             NULL },
47         { "root",  0755, NULL,                                  NULL },
48         { "sbin",     0, "usr/sbin",                            NULL },
49 #if defined(__i386__) || defined(__x86_64__)
50         { "lib64",    0, "usr/lib/x86_64-linux-gnu\0usr/lib64", "ld-linux-x86-64.so.2" },
51 #endif
52 };
53
54 int base_filesystem_create(const char *root) {
55         _cleanup_close_ int fd = -1;
56         unsigned i;
57         int r;
58
59         fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
60         if (fd < 0)
61                 return -errno;
62
63         for (i = 0; i < ELEMENTSOF(table); i ++) {
64                 if (table[i].target) {
65                         const char *target = NULL;
66                         const char *s;
67
68                         if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
69                                 continue;
70
71                         /* check if one of the targets exists */
72                         NULSTR_FOREACH(s, table[i].target) {
73                                 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
74                                         continue;
75
76                                 /* check if a specific file exists at the target path */
77                                 if (table[i].exists) {
78                                         _cleanup_free_ char *p = NULL;
79
80                                         p = strjoin(s, "/", table[i].exists, NULL);
81                                         if (!p)
82                                                 return log_oom();
83
84                                         if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
85                                                 continue;
86                                 }
87
88                                 target = s;
89                                 break;
90                         }
91
92                         if (!target)
93                                 continue;
94
95                         r = symlinkat(target, fd, table[i].dir);
96                         if (r < 0 && errno != EEXIST) {
97                                 log_error("Failed to create symlink at %s/%s: %m", root, table[i].dir);
98                                 return -errno;
99                         }
100                         continue;
101                 }
102
103                 RUN_WITH_UMASK(0000)
104                         r = mkdirat(fd, table[i].dir, table[i].mode);
105                 if (r < 0 && errno != EEXIST) {
106                         log_error("Failed to create directory at %s/%s: %m", root, table[i].dir);
107                         return -errno;
108                 }
109         }
110
111         return 0;
112 }