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