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