Debian Codegraph @

src/lib/util/dir.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2019 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"cxx . . $ `<file>/src/lib/util/config.h`/
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <dirent.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31
32 #include "common/common.h"cxx . . $ `<file>/src/common/common.h`/
33 #include "lib/util/dir.h"cxx . . $ `<file>/src/lib/util/dir.h`/
34 #include "lib/util/file.h"cxx . . $ `<file>/src/lib/util/file.h`/
35 #include "lib/util/string_array.h"cxx . . $ `<file>/src/lib/util/string_array.h`/
36
37 static int
38 compare_timespeccxx . . $ compare_timespec(cb715bf0a026b675).(struct timespeccxx . . $ timespec# *a, struct timespeccxx . . $ timespec# *b)
39 {
40 if (a->tv_seccxx . . $ timespec#tv_sec. == b->tv_seccxx . . $ timespec#tv_sec.) {
41 if (a->tv_nseccxx . . $ timespec#tv_nsec. < b->tv_nseccxx . . $ timespec#tv_nsec.) {
42 return -1;
43 }
44
45 if (a->tv_nseccxx . . $ timespec#tv_nsec. > b->tv_nseccxx . . $ timespec#tv_nsec.) {
46 return 1;
47 }
48
49 return 0;
50 }
51
52 if (a->tv_seccxx . . $ timespec#tv_sec. < b->tv_seccxx . . $ timespec#tv_sec.) {
53 return -1;
54 }
55
56 if (a->tv_seccxx . . $ timespec#tv_sec. > b->tv_seccxx . . $ timespec#tv_sec.) {
57 return 1;
58 }
59
60 return 0;
61 }
62
63 static int
64 sort_by_ctimecxx . . $ sort_by_ctime(b8f62d2ccbc12f7d).(const void *a, const void *b, void *arg)
65 {
66 const char *name_a = *(const char **)a;
67 const char *name_b = *(const char **)b;
68 int dirfd = *(int *)arg;
69 const char *base_a;
70 const char *base_b;
71 struct statcxx . . $ stat# stat_a;
72 struct statcxx . . $ stat# stat_b;
73 errno_tcxx . . $ errno_t# ret;
74
75 base_a = file_get_basenamecxx . . $ file_get_basename(fb58d2e09a2a1ce6).(name_a);
76 if (base_a == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
77 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to get basename of [%s]", name_a);
78 return 0;
79 }
80
81 base_b = file_get_basenamecxx . . $ file_get_basename(fb58d2e09a2a1ce6).(name_b);
82 if (base_b == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
83 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to get basename of [%s]", name_b);
84 return 0;
85 }
86
87 ret = fstatatcxx . . $ fstatat(ecc91238199c517e).(dirfd, base_a, &stat_a, 0);
88 if (ret != 0) {
89 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
90 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to stat [%s] [%d]: %s", name_a, ret, strerror(ret));
91 return 0;
92 }
93
94 ret = fstatatcxx . . $ fstatat(ecc91238199c517e).(dirfd, base_b, &stat_b, 0);
95 if (ret != 0) {
96 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
97 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to stat [%s] [%d]: %s", name_b, ret, strerror(ret));
98 return 0;
99 }
100
101 return compare_timespeccxx . . $ compare_timespec(cb715bf0a026b675).(&stat_a.st_ctimcxx . . $ stat#st_ctim., &stat_b.st_ctimcxx . . $ stat#st_ctim.);
102 }
103
104 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
105 is_dot_dircxx . . $ is_dot_dir(c5f7da30cbc0f4e9).(const char *name)
106 {
107 return strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, ".") == 0 || strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, "..") == 0;
108 }
109
110 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
111 is_directorycxx . . $ is_directory(18a237af43fdfe5e).(struct direntcxx . . $ dirent# *entry, int dirfd)
112 {
113 struct statcxx . . $ stat# statres;
114 errno_tcxx . . $ errno_t# ret;
115
116 #ifdef _DIRENT_HAVE_D_TYPEcxx . . $ `<external>/14108450139753054401/dirent.h:52:9`!
117 if (entry->d_typecxx . . $ dirent#d_type. == DT_DIRcxx . . $ `<external>/7464718065909436613/dirent.h:106:10`!) {
118 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
119 } else if (entry->d_typecxx . . $ dirent#d_type. != DT_UNKNOWNcxx . . $ `<external>/7464718065909436613/dirent.h:100:10`!) {
120 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
121 }
122 #endif
123
124 /* We must use stat() if d_type is not available or it couldn't determine
125 * the type (which may happen on some filesystems). */
126
127 ret = fstatatcxx . . $ fstatat(ecc91238199c517e).(dirfd, entry->d_namecxx . . $ dirent#d_name., &statres, 0);
128 if (ret != 0) {
129 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
130 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to stat directory [%d]: %s", ret, strerror(ret));
131 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
132 }
133
134 if (S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`!(statres.st_mode)) {
135 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
136 }
137
138 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
139 }
140
141 static errno_tcxx . . $ errno_t#
142 dir_opencxx . . $ dir_open(858efe1bb33edb6c).(const char *path,
143 DIRcxx . . $ DIR# **_dirstream,
144 int *_descriptor)
145 {
146 DIRcxx . . $ DIR# *dirstream;
147 int descriptor;
148 errno_tcxx . . $ errno_t# ret;
149
150 if (path == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
151 return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
152 }
153
154 dirstream = opendircxx . . $ opendir(f465d4b65cec8dd2).(path);
155 if (dirstream == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
156 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
157 /* To silence static analyzers that assumes that errno can be 0 here. */
158 if (ret == EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
159 ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
160 }
161
162 return ret;
163 }
164
165 /* Descriptor is closed when closedir() is called. */
166 descriptor = dirfdcxx . . $ dirfd(ac30a447bc7c2f80).(dirstream);
167 if (descriptor == -1) {
168 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
169 /* To silence static analyzers that assumes that errno can be 0 here. */
170 if (ret == EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
171 ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
172 }
173
174 closedircxx . . $ closedir(ac30a447bc7c2f80).(dirstream);
175 return ret;
176 }
177
178 *_dirstream = dirstream;
179 *_descriptor = descriptor;
180
181 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
182 }
183
184 errno_tcxx . . $ errno_t#
185 dir_listcxx . . $ dir_list(3003b1af897cb540).(const char *path,
186 uint32_tcxx . . $ uint32_t# flags,
187 char ***_items,
188 int *_dirfd)
189 {
190 struct direntcxx . . $ dirent# *entry;
191 DIRcxx . . $ DIR# *dirstream;
192 char *fullpath;
193 char **items;
194 int dirfd;
195 int dupfd;
196 errno_tcxx . . $ errno_t# ret;
197
198 ret = dir_opencxx . . $ dir_open(858efe1bb33edb6c).(path, &dirstream, &dirfd);
199 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
200 return ret;
201 }
202
203 items = string_array_createcxx . . $ string_array_create(4d3fbc76ec7ad0ba).(1);
204 if (items == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
205 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
206 goto done;
207 }
208
209 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0;
210 while ((entry = readdircxx . . $ readdir(ffbaae344e1a615a).(dirstream)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
211 if (is_dot_dircxx . . $ is_dot_dir(c5f7da30cbc0f4e9).(entry->d_namecxx . . $ dirent#d_name.)) {
212 continue;
213 }
214
215 if (is_directorycxx . . $ is_directory(18a237af43fdfe5e).(entry, dirfd)) {
216 if (!(flags & DIR_LIST_DIRScxx . . $ `src/lib/util/dir.h:30:9`!)) {
217 continue;
218 }
219 } else {
220 if (!(flags & DIR_LIST_FILEScxx . . $ `src/lib/util/dir.h:27:9`!)) {
221 continue;
222 }
223 }
224
225 if (flags & DIR_LIST_FULL_PATHcxx . . $ `src/lib/util/dir.h:33:9`!) {
226 fullpath = formatcxx . . $ format(a849077ff1b8d812).("%s/%s", path, entry->d_namecxx . . $ dirent#d_name.);
227 if (fullpath == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
228 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
229 goto done;
230 }
231
232 items = string_array_add_valuecxx . . $ string_array_add_value(db56f6d53b8c0a98).(items, fullpath, truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!);
233 freecxx . . $ free(5d22bdacc48458e8).(fullpath);
234 } else {
235 items = string_array_add_valuecxx . . $ string_array_add_value(db56f6d53b8c0a98).(items, entry->d_namecxx . . $ dirent#d_name., truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!);
236 }
237
238 if (items == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
239 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
240 goto done;
241 }
242 }
243
244 if (flags & DIR_LIST_SORT_BY_CTIMEcxx . . $ `src/lib/util/dir.h:36:9`!) {
245 qsort_rcxx . . $ qsort_r(f6cd6bfde2f94d8a).(items, string_array_countcxx . . $ string_array_count(44350d944485fca).(items), sizeof(char *),
246 sort_by_ctimecxx . . $ sort_by_ctime(b8f62d2ccbc12f7d)., &dirfd);
247 }
248
249 if (_dirfd != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
250 dupfd = dupcxx . . $ dup(7864480464b09eea).(dirfd);
251 if (dupfd == -1) {
252 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
253 /* To silence static analyzers that assumes that errno
254 * can be 0 here. */
255 if (ret == EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
256 ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
257 }
258 goto done;
259 }
260
261 *_dirfd = dupfd;
262 }
263
264 *_items = items;
265
266 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
267
268 done:
269 if (ret == EOKcxx . . $ `src/common/errno_t.h:34:9`! && _dirfd != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
270 closedircxx . . $ closedir(ac30a447bc7c2f80).(dirstream);
271 }
272
273 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
274 string_array_freecxx . . $ string_array_free(3ed91490e421178f).(items);
275 }
276
277 return ret;
278 }
279
280 errno_tcxx . . $ errno_t#
281 dir_removecxx . . $ dir_remove(5c4306d8553b3696).(const char *path)
282 {
283 char **subdirs = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
284 char **files = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
285 int dirfd = -1;
286 errno_tcxx . . $ errno_t# ret;
287 int i;
288
289 ret = dir_listcxx . . $ dir_list(3003b1af897cb540).(path, DIR_LIST_DIRScxx . . $ `src/lib/util/dir.h:30:9`! | DIR_LIST_FULL_PATHcxx . . $ `src/lib/util/dir.h:33:9`!, &subdirs, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
290 if (ret == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
291 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
292 goto done;
293 } else if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
294 goto done;
295 }
296
297 ret = dir_listcxx . . $ dir_list(3003b1af897cb540).(path, DIR_LIST_FILEScxx . . $ `src/lib/util/dir.h:27:9`!, &files, &dirfd);
298 if (ret == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
299 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
300 goto done;
301 } else if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
302 goto done;
303 }
304
305 for (i = 0; subdirs[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
306 ret = dir_removecxx . . $ dir_remove(5c4306d8553b3696).(subdirs[i]);
307 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
308 goto done;
309 }
310 }
311
312 for (i = 0; files[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
313 INFOcxx . . $ `src/common/common.h:63:9`!("Removing file [%s/%s]", path, files[i]);
314 ret = unlinkatcxx . . $ unlinkat(50b278ac4ac9ad96).(dirfd, files[i], 0);
315 if (ret != 0) {
316 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
317 goto done;
318 }
319 }
320
321 INFOcxx . . $ `src/common/common.h:63:9`!("Removing directory [%s]", path);
322 ret = rmdircxx . . $ rmdir(5c4306d8553b3696).(path);
323 if (ret != 0) {
324 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
325 goto done;
326 }
327
328 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
329
330 done:
331 string_array_freecxx . . $ string_array_free(3ed91490e421178f).(subdirs);
332 string_array_freecxx . . $ string_array_free(3ed91490e421178f).(files);
333
334 if (dirfd != -1) {
335 closecxx . . $ close(7864480464b09eea).(dirfd);
336 }
337
338 return ret;
339 }