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