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