chiark / gitweb /
conf-files: continue searching if one dir fails
[elogind.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 *path, const char *suffix) {
41         DIR *dir;
42         struct dirent buffer, *de;
43         int r = 0;
44
45         dir = opendir(path);
46         if (!dir) {
47                 if (errno == ENOENT)
48                         return 0;
49                 return -errno;
50         }
51
52         for (;;) {
53                 int k;
54                 char *p;
55
56                 k = readdir_r(dir, &buffer, &de);
57                 if (k != 0) {
58                         r = -k;
59                         goto finish;
60                 }
61
62                 if (!de)
63                         break;
64
65                 if (!dirent_is_file_with_suffix(de, suffix))
66                         continue;
67
68                 if (asprintf(&p, "%s/%s", path, de->d_name) < 0) {
69                         r = -ENOMEM;
70                         goto finish;
71                 }
72
73                 if (hashmap_put(h, path_get_file_name(p), p) <= 0) {
74                         log_debug("Skip overridden file: %s.", p);
75                         free(p);
76                 }
77         }
78
79 finish:
80         closedir(dir);
81         return r;
82 }
83
84 static int base_cmp(const void *a, const void *b) {
85         const char *s1, *s2;
86
87         s1 = *(char * const *)a;
88         s2 = *(char * const *)b;
89         return strcmp(path_get_file_name(s1), path_get_file_name(s2));
90 }
91
92 int conf_files_list_strv(char ***strv, const char *suffix, const char **dirs) {
93         Hashmap *fh = NULL;
94         char **files = NULL;
95         const char **p;
96         int r;
97
98         assert(dirs);
99
100         fh = hashmap_new(string_hash_func, string_compare_func);
101         if (!fh) {
102                 r = -ENOMEM;
103                 goto finish;
104         }
105
106         STRV_FOREACH(p, dirs) {
107                 r = files_add(fh, *p, suffix);
108                 if (r < 0)
109                         log_warning("Failed to search for files in %s: %s",
110                                     *p, strerror(-r));
111         }
112
113         files = hashmap_get_strv(fh);
114         if (files == NULL) {
115                 log_error("Failed to compose list of files.");
116                 r = -ENOMEM;
117                 goto finish;
118         }
119         qsort(files, hashmap_size(fh), sizeof(char *), base_cmp);
120         r = 0;
121
122 finish:
123         hashmap_free(fh);
124         *strv = files;
125         return r;
126 }
127
128 int conf_files_list(char ***strv, const char *suffix, const char *dir, ...) {
129         char **dirs = NULL;
130         va_list ap;
131         int r;
132
133         va_start(ap, dir);
134         dirs = strv_new_ap(dir, ap);
135         va_end(ap);
136         if (!dirs) {
137                 r = -ENOMEM;
138                 goto finish;
139         }
140
141         if (!path_strv_canonicalize(dirs)) {
142                 r = -ENOMEM;
143                 goto finish;
144         }
145         strv_uniq(dirs);
146
147         r = conf_files_list_strv(strv, suffix, (const char **)dirs);
148
149 finish:
150         strv_free(dirs);
151         return r;
152 }