]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/conf-files.c
hashmap: introduce hash_ops to make struct Hashmap smaller
[thirdparty/systemd.git] / src / shared / conf-files.c
CommitLineData
2c21044f
KS
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"
9eb977db 36#include "path-util.h"
2c21044f
KS
37#include "hashmap.h"
38#include "conf-files.h"
39
cba2ef02 40static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
fabe5c0e 41 _cleanup_closedir_ DIR *dir = NULL;
cba2ef02 42 char *dirpath;
e02caf30 43
cba2ef02 44 assert(path);
cebed500
LP
45 assert(suffix);
46
cba2ef02 47 dirpath = strappenda(root ? root : "", path);
cebed500 48
cba2ef02 49 dir = opendir(dirpath);
2c21044f
KS
50 if (!dir) {
51 if (errno == ENOENT)
52 return 0;
53 return -errno;
54 }
55
56 for (;;) {
7d5e9c0f 57 struct dirent *de;
2c21044f 58 char *p;
fabe5c0e 59 int r;
2c21044f 60
a2be63f9
FW
61 errno = 0;
62 de = readdir(dir);
63 if (!de && errno != 0)
64 return -errno;
2c21044f
KS
65
66 if (!de)
67 break;
68
69 if (!dirent_is_file_with_suffix(de, suffix))
70 continue;
71
fabe5c0e
LP
72 p = strjoin(dirpath, "/", de->d_name, NULL);
73 if (!p)
e02caf30 74 return -ENOMEM;
2c21044f 75
2b6bf07d 76 r = hashmap_put(h, basename(p), p);
fabe5c0e
LP
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);
2c21044f
KS
85 free(p);
86 }
87 }
88
e02caf30 89 return 0;
2c21044f
KS
90}
91
92static 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;
2b6bf07d 97 return strcmp(basename(s1), basename(s2));
2c21044f
KS
98}
99
fabe5c0e 100static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, char **dirs) {
e1d75803 101 _cleanup_hashmap_free_ Hashmap *fh = NULL;
fabe5c0e 102 char **files, **p;
578ac060 103 int r;
2c21044f 104
fabe5c0e
LP
105 assert(strv);
106 assert(suffix);
107
108 /* This alters the dirs string array */
7d8da2c9 109 if (!path_strv_resolve_uniq(dirs, root))
fabe5c0e 110 return -ENOMEM;
2c21044f 111
d5099efc 112 fh = hashmap_new(&string_hash_ops);
fabe5c0e
LP
113 if (!fh)
114 return -ENOMEM;
2c21044f
KS
115
116 STRV_FOREACH(p, dirs) {
cba2ef02 117 r = files_add(fh, root, *p, suffix);
fabe5c0e 118 if (r == -ENOMEM) {
fabe5c0e
LP
119 return r;
120 } else if (r < 0)
121 log_debug("Failed to search for files in %s: %s",
122 *p, strerror(-r));
2c21044f
KS
123 }
124
125 files = hashmap_get_strv(fh);
126 if (files == NULL) {
fabe5c0e 127 return -ENOMEM;
2c21044f 128 }
fabe5c0e 129
7ff7394d 130 qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
fabe5c0e 131 *strv = files;
2c21044f 132
fabe5c0e
LP
133 return 0;
134}
135
844ec79b 136int conf_files_list_strv(char ***strv, const char *suffix, const char *root, const char* const* dirs) {
fabe5c0e
LP
137 _cleanup_strv_free_ char **copy = NULL;
138
139 assert(strv);
140 assert(suffix);
141
142 copy = strv_copy((char**) dirs);
143 if (!copy)
144 return -ENOMEM;
145
146 return conf_files_list_strv_internal(strv, suffix, root, copy);
2c21044f
KS
147}
148
7850b3b8 149int conf_files_list(char ***strv, const char *suffix, const char *root, const char *dir, ...) {
8201ad81
LP
150 _cleanup_strv_free_ char **dirs = NULL;
151 va_list ap;
fabe5c0e
LP
152
153 assert(strv);
154 assert(suffix);
2c21044f 155
8201ad81
LP
156 va_start(ap, dir);
157 dirs = strv_new_ap(dir, ap);
158 va_end(ap);
159
160 if (!dirs)
161 return -ENOMEM;
fabe5c0e
LP
162
163 return conf_files_list_strv_internal(strv, suffix, root, dirs);
164}
2c21044f 165
fabe5c0e
LP
166int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, const char *d) {
167 _cleanup_strv_free_ char **dirs = NULL;
168
169 assert(strv);
170 assert(suffix);
171
172 dirs = strv_split_nulstr(d);
173 if (!dirs)
174 return -ENOMEM;
2c21044f 175
fabe5c0e 176 return conf_files_list_strv_internal(strv, suffix, root, dirs);
2c21044f 177}