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