Debian Codegraph @

src/basic/recurse-dir.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "dirent-util.h"
8 #include "fd-util.h"
9 #include "fs-util.h"
10 #include "log.h"
11 #include "mountpoint-util.h"
12 #include "path-util.h"
13 #include "recurse-dir.h"
14 #include "sort-util.h"
15
16 #define DEFAULT_RECURSION_MAX 100
17
18 static int sort_func(struct dirent * const *a, struct dirent * const *b) {
19 assert(a);
20 assert(b);
21
22 return strcmp((*a)->d_name, (*b)->d_name);
23 }
24
25 static bool ignore_dirent(const struct dirent *de, RecurseDirFlags flags) {
26 assert(de);
27
28 /* Depending on flag either ignore everything starting with ".", or just "." itself and ".." */
29
30 return FLAGS_SET(flags, RECURSE_DIR_IGNORE_DOT) ?
31 de->d_name[0] == '.' :
32 dot_or_dot_dot(de->d_name);
33 }
34
35 int readdir_all(int dir_fd, RecurseDirFlags flags, DirectoryEntries **ret) {
36 _cleanup_free_ DirectoryEntries *de = NULL;
37 DirectoryEntries *nde;
38 int r;
39
40 assert(dir_fd >= 0);
41
42 /* Returns an array with pointers to "struct dirent" directory entries, optionally sorted.
43 *
44 * Start with space for up to 8 directory entries. We expect at least 2 ("." + ".."), hence hopefully
45 * 8 will cover most cases comprehensively. (Note that most likely a lot more entries will actually
46 * fit in the buffer, given we calculate maximum file name length here.) */
47 /* Silence static analyzers */
48 assert_cc(offsetof(DirectoryEntries, buffer) <= SIZE_MAX - DIRENT_SIZE_MAX * 8);
49 de = malloc(offsetof(DirectoryEntries, buffer) + DIRENT_SIZE_MAX * 8);
50 if (!de)
51 return -ENOMEM;
52
53 de->buffer_size = 0;
54 for (;;) {
55 size_t bs;
56 ssize_t n;
57
58 /* Silence static analyzers, MALLOC_SIZEOF_SAFE is at least as large as the allocation */
59 assert(MALLOC_SIZEOF_SAFE(de) >= offsetof(DirectoryEntries, buffer));
60 bs = MIN(MALLOC_SIZEOF_SAFE(de) - offsetof(DirectoryEntries, buffer), (size_t) SSIZE_MAX);
61 assert(bs > de->buffer_size);
62
63 n = getdents64(dir_fd, (struct dirent*) ((uint8_t*) de->buffer + de->buffer_size), bs - de->buffer_size);
64 if (n < 0)
65 return -errno;
66 if (n == 0)
67 break;
68
69 msan_unpoison((uint8_t*) de->buffer + de->buffer_size, n);
70
71 de->buffer_size += n;
72
73 if (de->buffer_size < bs - DIRENT_SIZE_MAX) /* Still room for one more entry, then try to
74 * fill it up without growing the structure. */
75 continue;
76
77 if (bs >= SSIZE_MAX - offsetof(DirectoryEntries, buffer))
78 return -EFBIG;
79 bs = bs >= (SSIZE_MAX - offsetof(DirectoryEntries, buffer))/2 ? SSIZE_MAX - offsetof(DirectoryEntries, buffer) : bs * 2;
80
81 nde = realloc(de, bs);
82 if (!nde)
83 return -ENOMEM;
84 de = nde;
85 }
86
87 de->n_entries = 0;
88 struct dirent *entry;
89 FOREACH_DIRENT_IN_BUFFER(entry, de->buffer, de->buffer_size) {
90 if (ignore_dirent(entry, flags))
91 continue;
92
93 if (FLAGS_SET(flags, RECURSE_DIR_ENSURE_TYPE)) {
94 r = dirent_ensure_type(dir_fd, entry);
95 if (r == -ENOENT)
96 /* dentry gone by now? no problem, let's just suppress it */
97 continue;
98 if (r < 0)
99 return r;
100 }
101
102 de->n_entries++;
103 }
104
105 size_t sz, j;
106
107 sz = ALIGN(offsetof(DirectoryEntries, buffer) + de->buffer_size);
108 if (!INC_SAFE(&sz, sizeof(struct dirent*) * de->n_entries))
109 return -ENOMEM;
110
111 nde = realloc(de, sz);
112 if (!nde)
113 return -ENOMEM;
114 de = nde;
115
116 de->entries = (struct dirent**) ((uint8_t*) de + ALIGN(offsetof(DirectoryEntries, buffer) + de->buffer_size));
117
118 j = 0;
119 FOREACH_DIRENT_IN_BUFFER(entry, de->buffer, de->buffer_size) {
120 if (ignore_dirent(entry, flags))
121 continue;
122
123 /* If d_type == DT_UNKNOWN that means we failed to ensure the type in the earlier loop and
124 * didn't include the dentry in de->n_entries and as such should skip it here as well. */
125 if (FLAGS_SET(flags, RECURSE_DIR_ENSURE_TYPE) && entry->d_type == DT_UNKNOWN)
126 continue;
127
128 de->entries[j++] = entry;
129 }
130 assert(j == de->n_entries);
131
132 if (FLAGS_SET(flags, RECURSE_DIR_SORT))
133 typesafe_qsort(de->entries, de->n_entries, sort_func);
134
135 if (ret)
136 *ret = TAKE_PTR(de);
137
138 return 0;
139 }
140
141 int readdir_all_at(int fd, const char *path, RecurseDirFlags flags, DirectoryEntries **ret) {
142 _cleanup_close_ int dir_fd = -EBADF;
143
144 assert(fd >= 0 || fd == AT_FDCWD);
145
146 dir_fd = xopenat(fd, path, O_DIRECTORY|O_CLOEXEC);
147 if (dir_fd < 0)
148 return dir_fd;
149
150 return readdir_all(dir_fd, flags, ret);
151 }
152
153 int recurse_dir(
154 int dir_fd,
155 const char *path,
156 unsigned statx_mask,
157 unsigned n_depth_max,
158 RecurseDirFlags flags,
159 recurse_dir_func_t func,
160 void *userdata) {
161
162 _cleanup_free_ DirectoryEntries *de = NULL;
163 struct statx root_sx;
164 int r;
165
166 assert(dir_fd >= 0);
167 assert(func);
168
169 /* This is a lot like ftw()/nftw(), but a lot more modern, i.e. built around openat()/statx()/O_PATH,
170 * and under the assumption that fds are not as 'expensive' as they used to be. */
171
172 if (n_depth_max == 0)
173 return -EOVERFLOW;
174 if (n_depth_max == UINT_MAX) /* special marker for "default" */
175 n_depth_max = DEFAULT_RECURSION_MAX;
176
177 if (FLAGS_SET(flags, RECURSE_DIR_TOPLEVEL)) {
178 if (statx_mask != 0) {
179 if (statx(dir_fd, "", AT_EMPTY_PATH, statx_mask, &root_sx) < 0)
180 return -errno;
181 }
182
183 r = func(RECURSE_DIR_ENTER,
184 path,
185 -EBADF, /* we have no parent fd */
186 dir_fd,
187 NULL, /* we have no dirent */
188 statx_mask != 0 ? &root_sx : NULL,
189 userdata);
190 if (IN_SET(r, RECURSE_DIR_LEAVE_DIRECTORY, RECURSE_DIR_SKIP_ENTRY))
191 return 0;
192 if (r != RECURSE_DIR_CONTINUE)
193 return r;
194 }
195
196 /* Mask out RECURSE_DIR_ENSURE_TYPE so we can do it ourselves and avoid an extra statx() call. */
197 r = readdir_all(dir_fd, flags & ~RECURSE_DIR_ENSURE_TYPE, &de);
198 if (r < 0)
199 return r;
200
201 FOREACH_ARRAY(entry, de->entries, de->n_entries) {
202 struct dirent *i = *entry;
203 _cleanup_close_ int inode_fd = -EBADF, subdir_fd = -EBADF;
204 _cleanup_free_ char *joined = NULL;
205 struct statx sx;
206 bool sx_valid = false;
207 const char *p;
208
209 /* For each directory entry we'll do one of the following:
210 *
211 * 1) If the entry refers to a directory, we'll open it as O_DIRECTORY 'subdir_fd' and then statx() the opened directory via that new fd (if requested)
212 * 2) Otherwise, if RECURSE_DIR_INODE_FD is set we'll open it as O_PATH 'inode_fd' and then statx() the opened inode via that new fd (if requested)
213 * 3) Otherwise, we'll statx() the directory entry via the directory fd we are currently looking at (if requested)
214 */
215
216 if (path) {
217 joined = path_join(path, i->d_name);
218 if (!joined)
219 return -ENOMEM;
220
221 p = joined;
222 } else
223 p = i->d_name;
224
225 if (IN_SET(i->d_type, DT_UNKNOWN, DT_DIR)) {
226 subdir_fd = openat(dir_fd, i->d_name, O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC);
227 if (subdir_fd < 0) {
228 if (errno == ENOENT) /* Vanished by now, go for next file immediately */
229 continue;
230
231 /* If it is a subdir but we failed to open it, then fail */
232 if (!IN_SET(errno, ENOTDIR, ELOOP)) {
233 log_debug_errno(errno, "Failed to open directory '%s': %m", p);
234
235 assert(errno <= RECURSE_DIR_SKIP_OPEN_DIR_ERROR_MAX - RECURSE_DIR_SKIP_OPEN_DIR_ERROR_BASE);
236
237 r = func(RECURSE_DIR_SKIP_OPEN_DIR_ERROR_BASE + errno,
238 p,
239 dir_fd,
240 /* inode_fd= */ -EBADF,
241 i,
242 /* sx= */ NULL,
243 userdata);
244 if (r == RECURSE_DIR_LEAVE_DIRECTORY)
245 break;
246 if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
247 return r;
248
249 continue;
250 }
251
252 /* If it's not a subdir, then let's handle it like a regular inode below */
253
254 } else {
255 /* If we managed to get a DIR* off the inode, it's definitely a directory. */
256 i->d_type = DT_DIR;
257
258 if (statx_mask != 0 || (flags & RECURSE_DIR_SAME_MOUNT)) {
259 if (statx(subdir_fd, "", AT_EMPTY_PATH, statx_mask, &sx) < 0)
260 return -errno;
261
262 sx_valid = true;
263 }
264 }
265 }
266
267 if (subdir_fd < 0) {
268 /* It's not a subdirectory. */
269
270 if (flags & RECURSE_DIR_INODE_FD) {
271
272 inode_fd = openat(dir_fd, i->d_name, O_PATH|O_NOFOLLOW|O_CLOEXEC);
273 if (inode_fd < 0) {
274 if (errno == ENOENT) /* Vanished by now, go for next file immediately */
275 continue;
276
277 log_debug_errno(errno, "Failed to open directory entry '%s': %m", p);
278
279 assert(errno <= RECURSE_DIR_SKIP_OPEN_INODE_ERROR_MAX - RECURSE_DIR_SKIP_OPEN_INODE_ERROR_BASE);
280
281 r = func(RECURSE_DIR_SKIP_OPEN_INODE_ERROR_BASE + errno,
282 p,
283 dir_fd,
284 /* inode_fd= */ -EBADF,
285 i,
286 /* sx= */ NULL,
287 userdata);
288 if (r == RECURSE_DIR_LEAVE_DIRECTORY)
289 break;
290 if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
291 return r;
292
293 continue;
294 }
295
296 /* If we open the inode, then verify it's actually a non-directory, like we
297 * assume. Let's guarantee that we never pass statx data of a directory where
298 * caller expects a non-directory */
299
300 if (statx(inode_fd, "", AT_EMPTY_PATH, statx_mask | STATX_TYPE, &sx) < 0)
301 return -errno;
302
303 assert(sx.stx_mask & STATX_TYPE);
304 sx_valid = true;
305
306 if (S_ISDIR(sx.stx_mode)) {
307 /* What? It's a directory now? Then someone must have quickly
308 * replaced it. Let's handle that gracefully: convert it to a
309 * directory fd — which should be riskless now that we pinned the
310 * inode. */
311
312 subdir_fd = fd_reopen(inode_fd, O_DIRECTORY|O_CLOEXEC);
313 if (subdir_fd < 0)
314 return subdir_fd;
315
316 inode_fd = safe_close(inode_fd);
317 }
318
319 } else if (statx_mask != 0 || (i->d_type == DT_UNKNOWN && (flags & RECURSE_DIR_ENSURE_TYPE))) {
320
321 if (statx(dir_fd, i->d_name, AT_SYMLINK_NOFOLLOW, statx_mask | STATX_TYPE, &sx) < 0) {
322 if (errno == ENOENT) /* Vanished by now? Go for next file immediately */
323 continue;
324
325 log_debug_errno(errno, "Failed to stat directory entry '%s': %m", p);
326
327 assert(errno <= RECURSE_DIR_SKIP_STAT_INODE_ERROR_MAX - RECURSE_DIR_SKIP_STAT_INODE_ERROR_BASE);
328
329 r = func(RECURSE_DIR_SKIP_STAT_INODE_ERROR_BASE + errno,
330 p,
331 dir_fd,
332 /* inode_fd= */ -EBADF,
333 i,
334 /* sx= */ NULL,
335 userdata);
336 if (r == RECURSE_DIR_LEAVE_DIRECTORY)
337 break;
338 if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
339 return r;
340
341 continue;
342 }
343
344 assert(sx.stx_mask & STATX_TYPE);
345 sx_valid = true;
346
347 if (S_ISDIR(sx.stx_mode)) {
348 /* So it suddenly is a directory, but we couldn't open it as such
349 * earlier? That is weird, and probably means somebody is racing
350 * against us. We could of course retry and open it as a directory
351 * again, but the chance to win here is limited. Hence, let's
352 * propagate this as EISDIR error instead. That way we make this
353 * something that can be reasonably handled, even though we give the
354 * guarantee that RECURSE_DIR_ENTRY is strictly issued for
355 * non-directory dirents. */
356
357 log_debug("Non-directory entry '%s' suddenly became a directory.", p);
358
359 r = func(RECURSE_DIR_SKIP_STAT_INODE_ERROR_BASE + EISDIR,
360 p,
361 dir_fd,
362 /* inode_fd= */ -EBADF,
363 i,
364 /* sx= */ NULL,
365 userdata);
366 if (r == RECURSE_DIR_LEAVE_DIRECTORY)
367 break;
368 if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
369 return r;
370
371 continue;
372 }
373 }
374 }
375
376 if (sx_valid) {
377 /* Copy over the data we acquired through statx() if we acquired any */
378 if (sx.stx_mask & STATX_TYPE) {
379 assert((subdir_fd < 0) == !S_ISDIR(sx.stx_mode));
380 i->d_type = IFTODT(sx.stx_mode);
381 }
382
383 if (sx.stx_mask & STATX_INO)
384 i->d_ino = sx.stx_ino;
385 }
386
387 if (subdir_fd >= 0) {
388 if (FLAGS_SET(flags, RECURSE_DIR_SAME_MOUNT)) {
389 bool is_mount;
390
391 if (sx_valid && FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT))
392 is_mount = FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT);
393 else {
394 r = is_mount_point_at(dir_fd, i->d_name, /* flags= */ 0);
395 if (r < 0)
396 log_debug_errno(r, "Failed to determine whether %s is a submount, assuming not: %m", p);
397
398 is_mount = r > 0;
399 }
400
401 if (is_mount) {
402 r = func(RECURSE_DIR_SKIP_MOUNT,
403 p,
404 dir_fd,
405 subdir_fd,
406 i,
407 statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
408 userdata);
409 if (r == RECURSE_DIR_LEAVE_DIRECTORY)
410 break;
411 if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
412 return r;
413
414 continue;
415 }
416 }
417
418 if (n_depth_max <= 1) {
419 /* When we reached max depth, generate a special event */
420
421 r = func(RECURSE_DIR_SKIP_DEPTH,
422 p,
423 dir_fd,
424 subdir_fd,
425 i,
426 statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
427 userdata);
428 if (r == RECURSE_DIR_LEAVE_DIRECTORY)
429 break;
430 if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
431 return r;
432
433 continue;
434 }
435
436 r = func(RECURSE_DIR_ENTER,
437 p,
438 dir_fd,
439 subdir_fd,
440 i,
441 statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
442 userdata);
443 if (r == RECURSE_DIR_CONTINUE) {
444 r = recurse_dir(subdir_fd,
445 p,
446 statx_mask,
447 n_depth_max - 1,
448 flags & ~RECURSE_DIR_TOPLEVEL, /* we already called the callback for this entry */
449 func,
450 userdata);
451 if (r != 0)
452 return r;
453
454 r = func(RECURSE_DIR_LEAVE,
455 p,
456 dir_fd,
457 subdir_fd,
458 i,
459 statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
460 userdata);
461 }
462 } else
463 /* Non-directory inode */
464 r = func(RECURSE_DIR_ENTRY,
465 p,
466 dir_fd,
467 inode_fd,
468 i,
469 statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
470 userdata);
471
472 if (r == RECURSE_DIR_LEAVE_DIRECTORY)
473 break;
474
475 if (IN_SET(r, RECURSE_DIR_UNLINK, RECURSE_DIR_UNLINK_GRACEFUL)) {
476 int f = subdir_fd >= 0 ? AT_REMOVEDIR : 0;
477
478 /* Close inodes before we try to delete them */
479 subdir_fd = safe_close(subdir_fd);
480 inode_fd = safe_close(inode_fd);
481
482 if (unlinkat(dir_fd, i->d_name, f) < 0) {
483 if (r != RECURSE_DIR_UNLINK_GRACEFUL)
484 return -errno;
485
486 log_debug_errno(errno, "Unable to remove '%s', ignoring: %m", p);
487 }
488 } else if (!IN_SET(r, RECURSE_DIR_SKIP_ENTRY, RECURSE_DIR_CONTINUE))
489 return r;
490 }
491
492 if (FLAGS_SET(flags, RECURSE_DIR_TOPLEVEL)) {
493
494 r = func(RECURSE_DIR_LEAVE,
495 path,
496 -EBADF, /* we have no parent fd */
497 dir_fd,
498 NULL, /* we have no dirent */
499 statx_mask != 0 ? &root_sx : NULL,
500 userdata);
501 if (!IN_SET(r, RECURSE_DIR_LEAVE_DIRECTORY, RECURSE_DIR_SKIP_ENTRY, RECURSE_DIR_CONTINUE))
502 return r;
503 }
504
505 return 0;
506 }
507
508 int recurse_dir_at(
509 int atfd,
510 const char *path,
511 unsigned statx_mask,
512 unsigned n_depth_max,
513 RecurseDirFlags flags,
514 recurse_dir_func_t func,
515 void *userdata) {
516
517 _cleanup_close_ int fd = -EBADF;
518
519 assert(atfd >= 0 || atfd == AT_FDCWD);
520 assert(func);
521
522 fd = openat(atfd, path ?: ".", O_DIRECTORY|O_CLOEXEC);
523 if (fd < 0)
524 return -errno;
525
526 return recurse_dir(fd, path, statx_mask, n_depth_max, flags, func, userdata);
527 }