]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-files.c
shared: include root when canonicalizing conf paths
[thirdparty/systemd.git] / src / shared / conf-files.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 #include <sys/stat.h>
29 #include <dirent.h>
30
31 #include "macro.h"
32 #include "util.h"
33 #include "missing.h"
34 #include "log.h"
35 #include "strv.h"
36 #include "path-util.h"
37 #include "hashmap.h"
38 #include "conf-files.h"
39
40 static int files_add(Hashmap *h, const char *dirpath, const char *suffix) {
41 _cleanup_closedir_ DIR *dir = NULL;
42
43 dir = opendir(dirpath);
44 if (!dir) {
45 if (errno == ENOENT)
46 return 0;
47 return -errno;
48 }
49
50 for (;;) {
51 struct dirent *de;
52 char *p;
53 int r;
54
55 errno = 0;
56 de = readdir(dir);
57 if (!de && errno != 0)
58 return -errno;
59
60 if (!de)
61 break;
62
63 if (!dirent_is_file_with_suffix(de, suffix))
64 continue;
65
66 p = strjoin(dirpath, "/", de->d_name, NULL);
67 if (!p)
68 return -ENOMEM;
69
70 r = hashmap_put(h, basename(p), p);
71 if (r == -EEXIST) {
72 log_debug("Skipping overridden file: %s.", p);
73 free(p);
74 } else if (r < 0) {
75 free(p);
76 return r;
77 } else if (r == 0) {
78 log_debug("Duplicate file %s", p);
79 free(p);
80 }
81 }
82
83 return 0;
84 }
85
86 static int base_cmp(const void *a, const void *b) {
87 const char *s1, *s2;
88
89 s1 = *(char * const *)a;
90 s2 = *(char * const *)b;
91 return strcmp(basename(s1), basename(s2));
92 }
93
94 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, char **dirs) {
95 Hashmap *fh;
96 char **files, **p;
97 int r;
98
99 assert(strv);
100 assert(suffix);
101
102 /* This alters the dirs string array */
103 if (!path_strv_canonicalize_absolute_uniq(dirs, root))
104 return -ENOMEM;
105
106 fh = hashmap_new(string_hash_func, string_compare_func);
107 if (!fh)
108 return -ENOMEM;
109
110 STRV_FOREACH(p, dirs) {
111 r = files_add(fh, *p, suffix);
112 if (r == -ENOMEM) {
113 hashmap_free_free(fh);
114 return r;
115 } else if (r < 0)
116 log_debug("Failed to search for files in %s: %s",
117 *p, strerror(-r));
118 }
119
120 files = hashmap_get_strv(fh);
121 if (files == NULL) {
122 hashmap_free_free(fh);
123 return -ENOMEM;
124 }
125
126 qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
127 *strv = files;
128
129 hashmap_free(fh);
130 return 0;
131 }
132
133 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, const char* const* dirs) {
134 _cleanup_strv_free_ char **copy = NULL;
135
136 assert(strv);
137 assert(suffix);
138
139 copy = strv_copy((char**) dirs);
140 if (!copy)
141 return -ENOMEM;
142
143 return conf_files_list_strv_internal(strv, suffix, root, copy);
144 }
145
146 int conf_files_list(char ***strv, const char *suffix, const char *root, const char *dir, ...) {
147 _cleanup_strv_free_ char **dirs = NULL;
148 va_list ap;
149
150 assert(strv);
151 assert(suffix);
152
153 va_start(ap, dir);
154 dirs = strv_new_ap(dir, ap);
155 va_end(ap);
156
157 if (!dirs)
158 return -ENOMEM;
159
160 return conf_files_list_strv_internal(strv, suffix, root, dirs);
161 }
162
163 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, const char *d) {
164 _cleanup_strv_free_ char **dirs = NULL;
165
166 assert(strv);
167 assert(suffix);
168
169 dirs = strv_split_nulstr(d);
170 if (!dirs)
171 return -ENOMEM;
172
173 return conf_files_list_strv_internal(strv, suffix, root, dirs);
174 }