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