]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/conf-files.c
util-lib: introduce dirent-util.[ch] for directory entry calls
[thirdparty/systemd.git] / src / basic / 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
07630cea 22#include <dirent.h>
2c21044f 23#include <errno.h>
2c21044f 24#include <stdio.h>
07630cea
LP
25#include <stdlib.h>
26#include <string.h>
2c21044f 27
3ffd4af2 28#include "conf-files.h"
a0956174 29#include "dirent-util.h"
3ffd4af2 30#include "fd-util.h"
07630cea
LP
31#include "hashmap.h"
32#include "log.h"
2c21044f 33#include "macro.h"
2c21044f 34#include "missing.h"
9eb977db 35#include "path-util.h"
07630cea
LP
36#include "string-util.h"
37#include "strv.h"
38#include "util.h"
2c21044f 39
cba2ef02 40static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
fabe5c0e 41 _cleanup_closedir_ DIR *dir = NULL;
1d13f648
LP
42 const char *dirpath;
43 int r;
e02caf30 44
cba2ef02 45 assert(path);
cebed500
LP
46 assert(suffix);
47
1d13f648 48 dirpath = prefix_roota(root, path);
cebed500 49
cba2ef02 50 dir = opendir(dirpath);
2c21044f
KS
51 if (!dir) {
52 if (errno == ENOENT)
53 return 0;
54 return -errno;
55 }
56
57 for (;;) {
7d5e9c0f 58 struct dirent *de;
2c21044f
KS
59 char *p;
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)
c33b3297
MS
121 log_debug_errno(r, "Failed to search for files in %s: %m",
122 *p);
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}