1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "alloc-util.h"
#include "dirent-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "mountpoint-util.h"
#include "recurse-dir.h"
#include "sort-util.h"
#define DEFAULT_RECURSION_MAX 100
static int sort_func(struct dirent * const *a, struct dirent * const *b) {
return strcmp((*a)->d_name, (*b)->d_name);
}
struct dirent** readdir_all_free(struct dirent **array) {
/* Destructor that relies on the fact that the array of dirent structure pointers is NULL
* terminated */
if (!array)
return NULL;
for (struct dirent **i = array; *i; i++)
free(*i);
return mfree(array);
}
int readdir_all(DIR *d,
RecurseDirFlags flags,
struct dirent ***ret) {
_cleanup_(readdir_all_freep) struct dirent **de_array = NULL;
size_t n_de = 0;
assert(d);
/* Returns an array with pointers to "struct dirent" directory entries, optionally sorted. Free the
* array with readdir_all_freep(). */
for (;;) {
_cleanup_free_ struct dirent *copy = NULL;
struct dirent *de;
errno = 0;
de = readdir(d);
if (!de) {
if (errno == 0)
break;
return -errno;
}
/* Depending on flag either ignore everything starting with ".", or just "." itself and ".." */
if (FLAGS_SET(flags, RECURSE_DIR_IGNORE_DOT) ?
de->d_name[0] == '.' :
dot_or_dot_dot(de->d_name))
continue;
if (n_de >= INT_MAX) /* Make sure we can return the number as 'int' return value */
return -ERANGE;
if (!GREEDY_REALLOC(de_array, n_de+2))
return -ENOMEM;
copy = memdup(de, de->d_reclen);
if (!copy)
return -ENOMEM;
de_array[n_de++] = TAKE_PTR(copy);
de_array[n_de] = NULL; /* guarantee array remains NUL terminated */
}
if (FLAGS_SET(flags, RECURSE_DIR_SORT))
typesafe_qsort(de_array, n_de, sort_func);
if (ret)
*ret = TAKE_PTR(de_array);
return (int) n_de;
}
int recurse_dir(
DIR *d,
const char *path,
unsigned statx_mask,
unsigned n_depth_max,
RecurseDirFlags flags,
recurse_dir_func_t func,
void *userdata) {
_cleanup_(readdir_all_freep) struct dirent **de = NULL;
int r, n;
assert(d);
assert(func);
/* This is a lot like ftw()/nftw(), but a lot more modern, i.e. built around openat()/statx(), and
* under the assumption that fds are not as 'expensive' as they used to be. */
if (n_depth_max == 0)
return -EOVERFLOW;
if (n_depth_max == UINT_MAX) /* special marker for "default" */
n_depth_max = DEFAULT_RECURSION_MAX;
n = readdir_all(d, flags, &de);
if (n < 0)
return n;
for (int i = 0; i < n; i++) {
_cleanup_free_ char *joined = NULL;
_cleanup_closedir_ DIR *subdir = NULL;
_cleanup_close_ int inode_fd = -1;
STRUCT_STATX_DEFINE(sx);
bool sx_valid = false;
const char *p;
/* For each directory entry we'll do one of the following:
*
* 1) If the entry refers to a directory, we'll open it as O_DIRECTORY 'subdir' and then statx() the opened directory if requested
* 2) Otherwise and RECURSE_DIR_INODE_FD is set we'll open O_PATH 'inode_fd' and then statx() the opened inode
* 3) Otherwise we'll statx() the directory entry via the directory we are currently looking at
*/
if (path) {
joined = path_join(path, de[i]->d_name);
if (!joined)
return -ENOMEM;
p = joined;
} else
p = de[i]->d_name;
if (IN_SET(de[i]->d_type, DT_UNKNOWN, DT_DIR)) {
subdir = xopendirat(dirfd(d), de[i]->d_name, O_NOFOLLOW);
if (!subdir) {
if (errno == ENOENT) /* Vanished by now, go for next file immediately */
continue;
/* If it is a subdir but we failed to open it, then fail */
if (!IN_SET(errno, ENOTDIR, ELOOP)) {
log_debug_errno(errno, "Failed to open directory '%s': %m", p);
assert(errno <= RECURSE_DIR_SKIP_OPEN_DIR_ERROR_MAX - RECURSE_DIR_SKIP_OPEN_DIR_ERROR_BASE);
r = func(RECURSE_DIR_SKIP_OPEN_DIR_ERROR_BASE + errno,
p,
dirfd(d),
-1,
de[i],
NULL,
userdata);
if (r == RECURSE_DIR_LEAVE_DIRECTORY)
break;
if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
return r;
continue;
}
/* If it's not a subdir, then let's handle it like a regular inode below */
} else {
/* If we managed to get a DIR* off the inode, it's definitely a directory. */
de[i]->d_type = DT_DIR;
if (statx_mask != 0 || (flags & RECURSE_DIR_SAME_MOUNT)) {
r = statx_fallback(dirfd(subdir), "", AT_EMPTY_PATH, statx_mask, &sx);
if (r < 0)
return r;
sx_valid = true;
}
}
}
if (!subdir) {
/* It's not a subdirectory. */
if (flags & RECURSE_DIR_INODE_FD) {
inode_fd = openat(dirfd(d), de[i]->d_name, O_PATH|O_NOFOLLOW|O_CLOEXEC);
if (inode_fd < 0) {
if (errno == ENOENT) /* Vanished by now, go for next file immediately */
continue;
log_debug_errno(errno, "Failed to open directory entry '%s': %m", p);
assert(errno <= RECURSE_DIR_SKIP_OPEN_INODE_ERROR_MAX - RECURSE_DIR_SKIP_OPEN_INODE_ERROR_BASE);
r = func(RECURSE_DIR_SKIP_OPEN_INODE_ERROR_BASE + errno,
p,
dirfd(d),
-1,
de[i],
NULL,
userdata);
if (r == RECURSE_DIR_LEAVE_DIRECTORY)
break;
if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
return r;
continue;
}
/* If we open the inode, then verify it's actually a non-directory, like we
* assume. Let's guarantee that we never pass statx data of a directory where
* caller expects a non-directory */
r = statx_fallback(inode_fd, "", AT_EMPTY_PATH, statx_mask | STATX_TYPE, &sx);
if (r < 0)
return r;
assert(sx.stx_mask & STATX_TYPE);
sx_valid = true;
if (S_ISDIR(sx.stx_mode)) {
/* What? It's a directory now? Then someone must have quickly
* replaced it. Let's handle that gracefully: convert it to a
* directory fd — which sould be riskless now that we pinned the
* inode. */
subdir = xopendirat(AT_FDCWD, FORMAT_PROC_FD_PATH(inode_fd), 0);
if (!subdir)
return -errno;
inode_fd = safe_close(inode_fd);
}
} else if (statx_mask != 0 || (de[i]->d_type == DT_UNKNOWN && (flags & RECURSE_DIR_ENSURE_TYPE))) {
r = statx_fallback(dirfd(d), de[i]->d_name, AT_SYMLINK_NOFOLLOW, statx_mask | STATX_TYPE, &sx);
if (r == -ENOENT) /* Vanished by now? Go for next file immediately */
continue;
if (r < 0) {
log_debug_errno(r, "Failed to stat directory entry '%s': %m", p);
assert(errno <= RECURSE_DIR_SKIP_STAT_INODE_ERROR_MAX - RECURSE_DIR_SKIP_STAT_INODE_ERROR_BASE);
r = func(RECURSE_DIR_SKIP_STAT_INODE_ERROR_BASE + -r,
p,
dirfd(d),
-1,
de[i],
NULL,
userdata);
if (r == RECURSE_DIR_LEAVE_DIRECTORY)
break;
if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
return r;
continue;
}
assert(sx.stx_mask & STATX_TYPE);
sx_valid = true;
if (S_ISDIR(sx.stx_mode)) {
/* So it suddenly is a directory, but we couldn't open it as such
* earlier? That is weird, and probably means somebody is racing
* against us. We could of course retry and open it as a directory
* again, but the chance to win here is limited. Hence, let's
* propagate this as EISDIR error instead. That way we make this
* something that can be reasonably handled, even though we give the
* guarantee that RECURSE_DIR_ENTRY is strictly issued for
* non-directory dirents. */
log_debug_errno(r, "Non-directory entry '%s' suddenly became a directory: %m", p);
r = func(RECURSE_DIR_SKIP_STAT_INODE_ERROR_BASE + EISDIR,
p,
dirfd(d),
-1,
de[i],
NULL,
userdata);
if (r == RECURSE_DIR_LEAVE_DIRECTORY)
break;
if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
return r;
continue;
}
}
}
if (sx_valid) {
/* Copy over the data we acquired through statx() if we acquired any */
if (sx.stx_mask & STATX_TYPE) {
assert(!!subdir == !!S_ISDIR(sx.stx_mode));
de[i]->d_type = stat_mode_to_dirent_type(sx.stx_mode);
}
if (sx.stx_mask & STATX_INO)
de[i]->d_ino = sx.stx_ino;
}
if (subdir) {
if (FLAGS_SET(flags, RECURSE_DIR_SAME_MOUNT)) {
bool is_mount;
if (sx_valid && FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT))
is_mount = FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT);
else {
r = fd_is_mount_point(dirfd(d), de[i]->d_name, 0);
if (r < 0)
log_debug_errno(r, "Failed to determine whether %s is a submount, assuming not: %m", p);
is_mount = r > 0;
}
if (is_mount) {
r = func(RECURSE_DIR_SKIP_MOUNT,
p,
dirfd(d),
dirfd(subdir),
de[i],
statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
userdata);
if (r == RECURSE_DIR_LEAVE_DIRECTORY)
break;
if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
return r;
continue;
}
}
if (n_depth_max <= 1) {
/* When we reached max depth, generate a special event */
r = func(RECURSE_DIR_SKIP_DEPTH,
p,
dirfd(d),
dirfd(subdir),
de[i],
statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
userdata);
if (r == RECURSE_DIR_LEAVE_DIRECTORY)
break;
if (!IN_SET(r, RECURSE_DIR_CONTINUE, RECURSE_DIR_SKIP_ENTRY))
return r;
continue;
}
r = func(RECURSE_DIR_ENTER,
p,
dirfd(d),
dirfd(subdir),
de[i],
statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
userdata);
if (r == RECURSE_DIR_LEAVE_DIRECTORY)
break;
if (r == RECURSE_DIR_SKIP_ENTRY)
continue;
if (r != RECURSE_DIR_CONTINUE)
return r;
r = recurse_dir(subdir,
p,
statx_mask,
n_depth_max - 1,
flags,
func,
userdata);
if (r != 0)
return r;
r = func(RECURSE_DIR_LEAVE,
p,
dirfd(d),
dirfd(subdir),
de[i],
statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
userdata);
} else
/* Non-directory inode */
r = func(RECURSE_DIR_ENTRY,
p,
dirfd(d),
inode_fd,
de[i],
statx_mask != 0 ? &sx : NULL, /* only pass sx if user asked for it */
userdata);
if (r == RECURSE_DIR_LEAVE_DIRECTORY)
break;
if (!IN_SET(r, RECURSE_DIR_SKIP_ENTRY, RECURSE_DIR_CONTINUE))
return r;
}
return 0;
}
int recurse_dir_at(
int atfd,
const char *path,
unsigned statx_mask,
unsigned n_depth_max,
RecurseDirFlags flags,
recurse_dir_func_t func,
void *userdata) {
_cleanup_closedir_ DIR *d = NULL;
d = xopendirat(atfd, path, 0);
if (!d)
return -errno;
return recurse_dir(d, path, statx_mask, n_depth_max, flags, func, userdata);
}
|