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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
|
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD
/**
* The bftw() implementation consists of the following components:
*
* - struct bftw_file: A file that has been encountered during the traversal.
* They have reference-counted links to their parents in the directory tree.
*
* - struct bftw_list: A linked list of bftw_file's.
*
* - struct bftw_cache: An LRU list of bftw_file's with open file descriptors,
* used for openat() to minimize the amount of path re-traversals.
*
* - struct bftw_state: Represents the current state of the traversal, allowing
* various helper functions to take fewer parameters.
*/
#include "bftw.h"
#include "bfstd.h"
#include "config.h"
#include "dir.h"
#include "dstring.h"
#include "list.h"
#include "mtab.h"
#include "stat.h"
#include "trie.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
/**
* A file.
*/
struct bftw_file {
/** The parent directory, if any. */
struct bftw_file *parent;
/** The root under which this file was found. */
struct bftw_file *root;
/** The next file in the queue, if any. */
struct bftw_file *next;
/** LRU list links. */
struct {
struct bftw_file *prev;
struct bftw_file *next;
} lru;
/** This file's depth in the walk. */
size_t depth;
/** Reference count. */
size_t refcount;
/** An open descriptor to this file, or -1. */
int fd;
/** This file's type, if known. */
enum bfs_type type;
/** The device number, for cycle detection. */
dev_t dev;
/** The inode number, for cycle detection. */
ino_t ino;
/** The offset of this file in the full path. */
size_t nameoff;
/** The length of the file's name. */
size_t namelen;
/** The file's name. */
char name[];
};
/**
* A linked list of bftw_file's.
*/
struct bftw_list {
struct bftw_file *head;
struct bftw_file **tail;
};
/**
* A cache of open directories.
*/
struct bftw_cache {
/** The head of the LRU list. */
struct bftw_file *head;
/** The tail of the LRU list. */
struct bftw_file *tail;
/** The insertion target for the LRU list. */
struct bftw_file *target;
/** The remaining capacity of the LRU list. */
size_t capacity;
};
/** Initialize a cache. */
static void bftw_cache_init(struct bftw_cache *cache, size_t capacity) {
LIST_INIT(cache);
cache->target = NULL;
cache->capacity = capacity;
}
/** Remove a bftw_file from the cache. */
static void bftw_cache_remove(struct bftw_cache *cache, struct bftw_file *file) {
if (cache->target == file) {
cache->target = file->lru.prev;
}
LIST_REMOVE(cache, file, lru);
++cache->capacity;
}
/** Close a bftw_file. */
static void bftw_file_close(struct bftw_cache *cache, struct bftw_file *file) {
assert(file->fd >= 0);
if (LIST_ATTACHED(cache, file, lru)) {
bftw_cache_remove(cache, file);
}
xclose(file->fd);
file->fd = -1;
}
/** Pop the least recently used directory from the cache. */
static int bftw_cache_pop(struct bftw_cache *cache) {
struct bftw_file *file = cache->tail;
if (!file) {
return -1;
}
bftw_file_close(cache, file);
return 0;
}
/** Add a bftw_file to the cache. */
static int bftw_cache_add(struct bftw_cache *cache, struct bftw_file *file) {
assert(file->fd >= 0);
if (cache->capacity == 0 && bftw_cache_pop(cache) != 0) {
bftw_file_close(cache, file);
errno = EMFILE;
return -1;
}
assert(cache->capacity > 0);
--cache->capacity;
LIST_INSERT(cache, cache->target, file, lru);
// Prefer to keep the root paths open by keeping them at the head of the list
if (file->depth == 0) {
cache->target = file;
}
return 0;
}
/** Compute the name offset of a child path. */
static size_t bftw_child_nameoff(const struct bftw_file *parent) {
size_t ret = parent->nameoff + parent->namelen;
if (parent->name[parent->namelen - 1] != '/') {
++ret;
}
return ret;
}
/** Destroy a cache. */
static void bftw_cache_destroy(struct bftw_cache *cache) {
assert(!cache->head);
assert(!cache->tail);
assert(!cache->target);
}
/** Create a new bftw_file. */
static struct bftw_file *bftw_file_new(struct bftw_file *parent, const char *name) {
size_t namelen = strlen(name);
size_t size = BFS_FLEX_SIZEOF(struct bftw_file, name, namelen + 1);
struct bftw_file *file = malloc(size);
if (!file) {
return NULL;
}
file->parent = parent;
if (parent) {
file->root = parent->root;
file->depth = parent->depth + 1;
file->nameoff = bftw_child_nameoff(parent);
++parent->refcount;
} else {
file->root = file;
file->depth = 0;
file->nameoff = 0;
}
file->next = NULL;
file->lru.prev = file->lru.next = NULL;
file->refcount = 1;
file->fd = -1;
file->type = BFS_UNKNOWN;
file->dev = -1;
file->ino = -1;
file->namelen = namelen;
memcpy(file->name, name, namelen + 1);
return file;
}
/**
* Open a bftw_file relative to another one.
*
* @param cache
* The cache to hold the file.
* @param file
* The file to open.
* @param base
* The base directory for the relative path (may be NULL).
* @param at_fd
* The base file descriptor, AT_FDCWD if base == NULL.
* @param at_path
* The relative path to the file.
* @return
* The opened file descriptor, or negative on error.
*/
static int bftw_file_openat(struct bftw_cache *cache, struct bftw_file *file, struct bftw_file *base, const char *at_path) {
assert(file->fd < 0);
int at_fd = AT_FDCWD;
if (base) {
// Remove base from the cache temporarily so it stays open
bftw_cache_remove(cache, base);
at_fd = base->fd;
}
int flags = O_RDONLY | O_CLOEXEC | O_DIRECTORY;
int fd = openat(at_fd, at_path, flags);
if (fd < 0 && errno == EMFILE) {
if (bftw_cache_pop(cache) == 0) {
fd = openat(at_fd, at_path, flags);
}
cache->capacity = 1;
}
if (base) {
bftw_cache_add(cache, base);
}
if (fd >= 0) {
file->fd = fd;
bftw_cache_add(cache, file);
}
return file->fd;
}
/**
* Open a bftw_file.
*
* @param cache
* The cache to hold the file.
* @param file
* The file to open.
* @param path
* The full path to the file.
* @return
* The opened file descriptor, or negative on error.
*/
static int bftw_file_open(struct bftw_cache *cache, struct bftw_file *file, const char *path) {
// Find the nearest open ancestor
struct bftw_file *base = file;
do {
base = base->parent;
} while (base && base->fd < 0);
const char *at_path = path;
if (base) {
at_path += bftw_child_nameoff(base);
}
int fd = bftw_file_openat(cache, file, base, at_path);
if (fd >= 0 || errno != ENAMETOOLONG) {
return fd;
}
// Handle ENAMETOOLONG by manually traversing the path component-by-component
struct bftw_list parents;
SLIST_INIT(&parents);
struct bftw_file *cur;
for (cur = file; cur != base; cur = cur->parent) {
SLIST_PREPEND(&parents, cur);
}
while ((cur = parents.head)) {
if (!cur->parent || cur->parent->fd >= 0) {
bftw_file_openat(cache, cur, cur->parent, cur->name);
}
SLIST_POP(&parents);
}
return file->fd;
}
/**
* Open a bftw_file as a directory.
*
* @param cache
* The cache to hold the file.
* @param file
* The directory to open.
* @param path
* The full path to the directory.
* @return
* The opened directory, or NULL on error.
*/
static struct bfs_dir *bftw_file_opendir(struct bftw_cache *cache, struct bftw_file *file, const char *path) {
int fd = bftw_file_open(cache, file, path);
if (fd < 0) {
return NULL;
}
return bfs_opendir(fd, NULL);
}
/** Free a bftw_file. */
static void bftw_file_free(struct bftw_cache *cache, struct bftw_file *file) {
assert(file->refcount == 0);
if (file->fd >= 0) {
bftw_file_close(cache, file);
}
free(file);
}
/**
* Holds the current state of the bftw() traversal.
*/
struct bftw_state {
/** bftw() callback. */
bftw_callback *callback;
/** bftw() callback data. */
void *ptr;
/** bftw() flags. */
enum bftw_flags flags;
/** Search strategy. */
enum bftw_strategy strategy;
/** The mount table. */
const struct bfs_mtab *mtab;
/** The appropriate errno value, if any. */
int error;
/** The cache of open directories. */
struct bftw_cache cache;
/** The queue of files left to explore. */
struct bftw_list queue;
/** A batch of files to enqueue. */
struct bftw_list batch;
/** The current path. */
char *path;
/** The current file. */
struct bftw_file *file;
/** The previous file. */
struct bftw_file *previous;
/** The currently open directory. */
struct bfs_dir *dir;
/** The current directory entry. */
struct bfs_dirent *de;
/** Storage for the directory entry. */
struct bfs_dirent de_storage;
/** Any error encountered while reading the directory. */
int direrror;
/** Extra data about the current file. */
struct BFTW ftwbuf;
};
/**
* Initialize the bftw() state.
*/
static int bftw_state_init(struct bftw_state *state, const struct bftw_args *args) {
state->callback = args->callback;
state->ptr = args->ptr;
state->flags = args->flags;
state->strategy = args->strategy;
state->mtab = args->mtab;
state->error = 0;
if (args->nopenfd < 1) {
errno = EMFILE;
return -1;
}
state->path = dstralloc(0);
if (!state->path) {
return -1;
}
bftw_cache_init(&state->cache, args->nopenfd);
SLIST_INIT(&state->queue);
SLIST_INIT(&state->batch);
state->file = NULL;
state->previous = NULL;
state->dir = NULL;
state->de = NULL;
state->direrror = 0;
return 0;
}
/** Cached bfs_stat(). */
static const struct bfs_stat *bftw_stat_impl(struct BFTW *ftwbuf, struct bftw_stat *cache, enum bfs_stat_flags flags) {
if (!cache->buf) {
if (cache->error) {
errno = cache->error;
} else if (bfs_stat(ftwbuf->at_fd, ftwbuf->at_path, flags, &cache->storage) == 0) {
cache->buf = &cache->storage;
} else {
cache->error = errno;
}
}
return cache->buf;
}
const struct bfs_stat *bftw_stat(const struct BFTW *ftwbuf, enum bfs_stat_flags flags) {
struct BFTW *mutbuf = (struct BFTW *)ftwbuf;
const struct bfs_stat *ret;
if (flags & BFS_STAT_NOFOLLOW) {
ret = bftw_stat_impl(mutbuf, &mutbuf->lstat_cache, BFS_STAT_NOFOLLOW);
if (ret && !S_ISLNK(ret->mode) && !mutbuf->stat_cache.buf) {
// Non-link, so share stat info
mutbuf->stat_cache.buf = ret;
}
} else {
ret = bftw_stat_impl(mutbuf, &mutbuf->stat_cache, BFS_STAT_FOLLOW);
if (!ret && (flags & BFS_STAT_TRYFOLLOW) && is_nonexistence_error(errno)) {
ret = bftw_stat_impl(mutbuf, &mutbuf->lstat_cache, BFS_STAT_NOFOLLOW);
}
}
return ret;
}
const struct bfs_stat *bftw_cached_stat(const struct BFTW *ftwbuf, enum bfs_stat_flags flags) {
if (flags & BFS_STAT_NOFOLLOW) {
return ftwbuf->lstat_cache.buf;
} else if (ftwbuf->stat_cache.buf) {
return ftwbuf->stat_cache.buf;
} else if ((flags & BFS_STAT_TRYFOLLOW) && is_nonexistence_error(ftwbuf->stat_cache.error)) {
return ftwbuf->lstat_cache.buf;
} else {
return NULL;
}
}
enum bfs_type bftw_type(const struct BFTW *ftwbuf, enum bfs_stat_flags flags) {
if (flags & BFS_STAT_NOFOLLOW) {
if (ftwbuf->type == BFS_LNK || (ftwbuf->stat_flags & BFS_STAT_NOFOLLOW)) {
return ftwbuf->type;
}
} else if (flags & BFS_STAT_TRYFOLLOW) {
if (ftwbuf->type != BFS_LNK || (ftwbuf->stat_flags & BFS_STAT_TRYFOLLOW)) {
return ftwbuf->type;
}
} else {
if (ftwbuf->type != BFS_LNK) {
return ftwbuf->type;
} else if (ftwbuf->stat_flags & BFS_STAT_TRYFOLLOW) {
return BFS_ERROR;
}
}
const struct bfs_stat *statbuf = bftw_stat(ftwbuf, flags);
if (statbuf) {
return bfs_mode_to_type(statbuf->mode);
} else {
return BFS_ERROR;
}
}
/**
* Update the path for the current file.
*/
static int bftw_update_path(struct bftw_state *state, const char *name) {
const struct bftw_file *file = state->file;
size_t length = file ? file->nameoff + file->namelen : 0;
assert(dstrlen(state->path) >= length);
dstresize(&state->path, length);
if (name) {
if (length > 0 && state->path[length - 1] != '/') {
if (dstrapp(&state->path, '/') != 0) {
return -1;
}
}
if (dstrcat(&state->path, name) != 0) {
return -1;
}
}
return 0;
}
/** Check if a stat() call is needed for this visit. */
static bool bftw_need_stat(const struct bftw_state *state) {
if (state->flags & BFTW_STAT) {
return true;
}
const struct BFTW *ftwbuf = &state->ftwbuf;
if (ftwbuf->type == BFS_UNKNOWN) {
return true;
}
if (ftwbuf->type == BFS_LNK && !(ftwbuf->stat_flags & BFS_STAT_NOFOLLOW)) {
return true;
}
if (ftwbuf->type == BFS_DIR) {
if (state->flags & (BFTW_DETECT_CYCLES | BFTW_SKIP_MOUNTS | BFTW_PRUNE_MOUNTS)) {
return true;
}
#if __linux__
} else if (state->mtab) {
// Linux fills in d_type from the underlying inode, even when
// the directory entry is a bind mount point. In that case, we
// need to stat() to get the correct type. We don't need to
// check for directories because they can only be mounted over
// by other directories.
if (bfs_might_be_mount(state->mtab, ftwbuf->path)) {
return true;
}
#endif
}
return false;
}
/** Initialize bftw_stat cache. */
static void bftw_stat_init(struct bftw_stat *cache) {
cache->buf = NULL;
cache->error = 0;
}
/**
* Open a file if necessary.
*
* @param file
* The file to open.
* @param path
* The path to that file or one of its descendants.
* @return
* The opened file descriptor, or -1 on error.
*/
static int bftw_ensure_open(struct bftw_cache *cache, struct bftw_file *file, const char *path) {
int ret = file->fd;
if (ret < 0) {
char *copy = strndup(path, file->nameoff + file->namelen);
if (!copy) {
return -1;
}
ret = bftw_file_open(cache, file, copy);
free(copy);
}
return ret;
}
/**
* Initialize the buffers with data about the current path.
*/
static void bftw_init_ftwbuf(struct bftw_state *state, enum bftw_visit visit) {
struct bftw_file *file = state->file;
const struct bfs_dirent *de = state->de;
struct BFTW *ftwbuf = &state->ftwbuf;
ftwbuf->path = state->path;
ftwbuf->root = file ? file->root->name : ftwbuf->path;
ftwbuf->depth = 0;
ftwbuf->visit = visit;
ftwbuf->type = BFS_UNKNOWN;
ftwbuf->error = state->direrror;
ftwbuf->at_fd = AT_FDCWD;
ftwbuf->at_path = ftwbuf->path;
ftwbuf->stat_flags = BFS_STAT_NOFOLLOW;
bftw_stat_init(&ftwbuf->lstat_cache);
bftw_stat_init(&ftwbuf->stat_cache);
struct bftw_file *parent = NULL;
if (de) {
parent = file;
ftwbuf->depth = file->depth + 1;
ftwbuf->type = de->type;
ftwbuf->nameoff = bftw_child_nameoff(file);
} else if (file) {
parent = file->parent;
ftwbuf->depth = file->depth;
ftwbuf->type = file->type;
ftwbuf->nameoff = file->nameoff;
}
if (parent) {
// Try to ensure the immediate parent is open, to avoid ENAMETOOLONG
if (bftw_ensure_open(&state->cache, parent, state->path) >= 0) {
ftwbuf->at_fd = parent->fd;
ftwbuf->at_path += ftwbuf->nameoff;
} else {
ftwbuf->error = errno;
}
}
if (ftwbuf->depth == 0) {
// Compute the name offset for root paths like "foo/bar"
ftwbuf->nameoff = xbaseoff(ftwbuf->path);
}
if (ftwbuf->error != 0) {
ftwbuf->type = BFS_ERROR;
return;
}
int follow_flags = BFTW_FOLLOW_ALL;
if (ftwbuf->depth == 0) {
follow_flags |= BFTW_FOLLOW_ROOTS;
}
bool follow = state->flags & follow_flags;
if (follow) {
ftwbuf->stat_flags = BFS_STAT_TRYFOLLOW;
}
const struct bfs_stat *statbuf = NULL;
if (bftw_need_stat(state)) {
statbuf = bftw_stat(ftwbuf, ftwbuf->stat_flags);
if (statbuf) {
ftwbuf->type = bfs_mode_to_type(statbuf->mode);
} else {
ftwbuf->type = BFS_ERROR;
ftwbuf->error = errno;
return;
}
}
if (ftwbuf->type == BFS_DIR && (state->flags & BFTW_DETECT_CYCLES)) {
for (const struct bftw_file *ancestor = parent; ancestor; ancestor = ancestor->parent) {
if (ancestor->dev == statbuf->dev && ancestor->ino == statbuf->ino) {
ftwbuf->type = BFS_ERROR;
ftwbuf->error = ELOOP;
return;
}
}
}
}
/** Check if the current file is a mount point. */
static bool bftw_is_mount(struct bftw_state *state, const char *name) {
const struct bftw_file *file = state->file;
if (!file) {
return false;
}
const struct bftw_file *parent = name ? file : file->parent;
if (!parent) {
return false;
}
const struct BFTW *ftwbuf = &state->ftwbuf;
const struct bfs_stat *statbuf = bftw_stat(ftwbuf, ftwbuf->stat_flags);
return statbuf && statbuf->dev != parent->dev;
}
/** Fill file identity information from an ftwbuf. */
static void bftw_fill_id(struct bftw_file *file, const struct BFTW *ftwbuf) {
const struct bfs_stat *statbuf = ftwbuf->stat_cache.buf;
if (!statbuf || (ftwbuf->stat_flags & BFS_STAT_NOFOLLOW)) {
statbuf = ftwbuf->lstat_cache.buf;
}
if (statbuf) {
file->dev = statbuf->dev;
file->ino = statbuf->ino;
}
}
/**
* Visit a path, invoking the callback.
*/
static enum bftw_action bftw_visit(struct bftw_state *state, const char *name, enum bftw_visit visit) {
if (bftw_update_path(state, name) != 0) {
state->error = errno;
return BFTW_STOP;
}
const struct BFTW *ftwbuf = &state->ftwbuf;
bftw_init_ftwbuf(state, visit);
// Never give the callback BFS_ERROR unless BFTW_RECOVER is specified
if (ftwbuf->type == BFS_ERROR && !(state->flags & BFTW_RECOVER)) {
state->error = ftwbuf->error;
return BFTW_STOP;
}
if ((state->flags & BFTW_SKIP_MOUNTS) && bftw_is_mount(state, name)) {
return BFTW_PRUNE;
}
enum bftw_action ret = state->callback(ftwbuf, state->ptr);
switch (ret) {
case BFTW_CONTINUE:
break;
case BFTW_PRUNE:
case BFTW_STOP:
goto done;
default:
state->error = EINVAL;
return BFTW_STOP;
}
if (visit != BFTW_PRE || ftwbuf->type != BFS_DIR) {
ret = BFTW_PRUNE;
goto done;
}
if ((state->flags & BFTW_PRUNE_MOUNTS) && bftw_is_mount(state, name)) {
ret = BFTW_PRUNE;
goto done;
}
done:
if (state->file && !name) {
bftw_fill_id(state->file, ftwbuf);
}
return ret;
}
/**
* Push a new file onto the queue.
*/
static int bftw_push(struct bftw_state *state, const char *name, bool fill_id) {
struct bftw_file *parent = state->file;
struct bftw_file *file = bftw_file_new(parent, name);
if (!file) {
state->error = errno;
return -1;
}
if (state->de) {
file->type = state->de->type;
}
if (fill_id) {
bftw_fill_id(file, &state->ftwbuf);
}
SLIST_APPEND(&state->batch, file);
return 0;
}
/**
* Build the path to the current file.
*/
static int bftw_build_path(struct bftw_state *state) {
const struct bftw_file *file = state->file;
size_t pathlen = file->nameoff + file->namelen;
if (dstresize(&state->path, pathlen) != 0) {
state->error = errno;
return -1;
}
// Try to find a common ancestor with the existing path
const struct bftw_file *ancestor = state->previous;
while (ancestor && ancestor->depth > file->depth) {
ancestor = ancestor->parent;
}
// Build the path backwards
while (file && file != ancestor) {
if (file->nameoff > 0) {
state->path[file->nameoff - 1] = '/';
}
memcpy(state->path + file->nameoff, file->name, file->namelen);
if (ancestor && ancestor->depth == file->depth) {
ancestor = ancestor->parent;
}
file = file->parent;
}
state->previous = state->file;
return 0;
}
/**
* Pop the next file from the queue.
*/
static int bftw_pop(struct bftw_state *state) {
state->file = state->queue.head;
if (!state->file) {
return 0;
}
SLIST_POP(&state->queue);
if (bftw_build_path(state) != 0) {
return -1;
}
return 1;
}
/**
* Open the current directory.
*/
static void bftw_opendir(struct bftw_state *state) {
assert(!state->dir);
assert(!state->de);
state->direrror = 0;
state->dir = bftw_file_opendir(&state->cache, state->file, state->path);
if (!state->dir) {
state->direrror = errno;
}
}
/**
* Read an entry from the current directory.
*/
static int bftw_readdir(struct bftw_state *state) {
if (!state->dir) {
return -1;
}
int ret = bfs_readdir(state->dir, &state->de_storage);
if (ret > 0) {
state->de = &state->de_storage;
} else if (ret == 0) {
state->de = NULL;
} else {
state->de = NULL;
state->direrror = errno;
}
return ret;
}
/**
* Flags controlling which files get visited when done with a directory.
*/
enum bftw_gc_flags {
/** Don't visit anything. */
BFTW_VISIT_NONE = 0,
/** Visit the file itself. */
BFTW_VISIT_FILE = 1 << 0,
/** Visit the file's ancestors. */
BFTW_VISIT_PARENTS = 1 << 1,
/** Visit both the file and its ancestors. */
BFTW_VISIT_ALL = BFTW_VISIT_FILE | BFTW_VISIT_PARENTS,
};
/**
* Close the current directory.
*/
static enum bftw_action bftw_closedir(struct bftw_state *state, enum bftw_gc_flags flags) {
struct bftw_file *file = state->file;
enum bftw_action ret = BFTW_CONTINUE;
if (state->dir) {
assert(file->fd >= 0);
if (file->refcount > 1) {
// Keep the fd around if any subdirectories exist
file->fd = bfs_freedir(state->dir);
} else {
bfs_closedir(state->dir);
file->fd = -1;
}
if (file->fd < 0) {
bftw_cache_remove(&state->cache, file);
}
}
state->de = NULL;
state->dir = NULL;
if (state->direrror != 0) {
if (flags & BFTW_VISIT_FILE) {
ret = bftw_visit(state, NULL, BFTW_PRE);
} else {
state->error = state->direrror;
}
state->direrror = 0;
}
return ret;
}
/**
* Finalize and free a file we're done with.
*/
static enum bftw_action bftw_gc_file(struct bftw_state *state, enum bftw_gc_flags flags) {
enum bftw_action ret = BFTW_CONTINUE;
if (!(state->flags & BFTW_POST_ORDER)) {
flags = 0;
}
bool visit = flags & BFTW_VISIT_FILE;
while (state->file) {
struct bftw_file *file = state->file;
if (--file->refcount > 0) {
state->file = NULL;
break;
}
if (visit && bftw_visit(state, NULL, BFTW_POST) == BFTW_STOP) {
ret = BFTW_STOP;
flags &= ~BFTW_VISIT_PARENTS;
}
visit = flags & BFTW_VISIT_PARENTS;
struct bftw_file *parent = file->parent;
if (state->previous == file) {
state->previous = parent;
}
bftw_file_free(&state->cache, file);
state->file = parent;
}
return ret;
}
/**
* Drain all the files from the queue.
*/
static void bftw_drain_queue(struct bftw_state *state) {
while (state->queue.head) {
state->file = state->queue.head;
SLIST_POP(&state->queue);
bftw_gc_file(state, BFTW_VISIT_NONE);
}
}
/**
* Dispose of the bftw() state.
*
* @return
* The bftw() return value.
*/
static int bftw_state_destroy(struct bftw_state *state) {
dstrfree(state->path);
bftw_closedir(state, BFTW_VISIT_NONE);
bftw_gc_file(state, BFTW_VISIT_NONE);
bftw_drain_queue(state);
bftw_cache_destroy(&state->cache);
errno = state->error;
return state->error ? -1 : 0;
}
/** Sort a bftw_list by filename. */
static void bftw_list_sort(struct bftw_list *queue) {
if (!queue->head || !queue->head->next) {
return;
}
struct bftw_list left, right;
SLIST_INIT(&left);
SLIST_INIT(&right);
// Split
for (struct bftw_file *hare = queue->head; hare && (hare = hare->next); hare = hare->next) {
struct bftw_file *tortoise = queue->head;
SLIST_POP(queue);
SLIST_APPEND(&left, tortoise);
}
SLIST_EXTEND(&right, queue);
// Recurse
bftw_list_sort(&left);
bftw_list_sort(&right);
// Merge
while (left.head && right.head) {
struct bftw_file *lf = left.head;
struct bftw_file *rf = right.head;
if (strcoll(lf->name, rf->name) <= 0) {
SLIST_POP(&left);
SLIST_APPEND(queue, lf);
} else {
SLIST_POP(&right);
SLIST_APPEND(queue, rf);
}
}
SLIST_EXTEND(queue, &left);
SLIST_EXTEND(queue, &right);
}
/** Finish adding a batch of files. */
static void bftw_batch_finish(struct bftw_state *state) {
if (state->flags & BFTW_SORT) {
bftw_list_sort(&state->batch);
}
if (state->strategy == BFTW_DFS) {
SLIST_EXTEND(&state->batch, &state->queue);
}
SLIST_EXTEND(&state->queue, &state->batch);
}
/**
* Streaming mode: visit files as they are encountered.
*/
static int bftw_stream(const struct bftw_args *args) {
struct bftw_state state;
if (bftw_state_init(&state, args) != 0) {
return -1;
}
assert(!(state.flags & (BFTW_SORT | BFTW_BUFFER)));
for (size_t i = 0; i < args->npaths; ++i) {
const char *path = args->paths[i];
switch (bftw_visit(&state, path, BFTW_PRE)) {
case BFTW_CONTINUE:
break;
case BFTW_PRUNE:
continue;
case BFTW_STOP:
goto done;
}
if (bftw_push(&state, path, true) != 0) {
goto done;
}
}
bftw_batch_finish(&state);
while (bftw_pop(&state) > 0) {
bftw_opendir(&state);
while (bftw_readdir(&state) > 0) {
const char *name = state.de->name;
switch (bftw_visit(&state, name, BFTW_PRE)) {
case BFTW_CONTINUE:
break;
case BFTW_PRUNE:
continue;
case BFTW_STOP:
goto done;
}
if (bftw_push(&state, name, true) != 0) {
goto done;
}
}
bftw_batch_finish(&state);
if (bftw_closedir(&state, BFTW_VISIT_ALL) == BFTW_STOP) {
goto done;
}
if (bftw_gc_file(&state, BFTW_VISIT_ALL) == BFTW_STOP) {
goto done;
}
}
done:
return bftw_state_destroy(&state);
}
/**
* Batching mode: queue up all children before visiting them.
*/
static int bftw_batch(const struct bftw_args *args) {
struct bftw_state state;
if (bftw_state_init(&state, args) != 0) {
return -1;
}
for (size_t i = 0; i < args->npaths; ++i) {
if (bftw_push(&state, args->paths[i], false) != 0) {
goto done;
}
}
bftw_batch_finish(&state);
while (bftw_pop(&state) > 0) {
enum bftw_gc_flags gcflags = BFTW_VISIT_ALL;
switch (bftw_visit(&state, NULL, BFTW_PRE)) {
case BFTW_CONTINUE:
break;
case BFTW_PRUNE:
gcflags &= ~BFTW_VISIT_FILE;
goto next;
case BFTW_STOP:
goto done;
}
bftw_opendir(&state);
while (bftw_readdir(&state) > 0) {
if (bftw_push(&state, state.de->name, false) != 0) {
goto done;
}
}
bftw_batch_finish(&state);
if (bftw_closedir(&state, gcflags) == BFTW_STOP) {
goto done;
}
next:
if (bftw_gc_file(&state, gcflags) == BFTW_STOP) {
goto done;
}
}
done:
return bftw_state_destroy(&state);
}
/** Select bftw_stream() or bftw_batch() appropriately. */
static int bftw_auto(const struct bftw_args *args) {
if (args->flags & (BFTW_SORT | BFTW_BUFFER)) {
return bftw_batch(args);
} else {
return bftw_stream(args);
}
}
/**
* Iterative deepening search state.
*/
struct bftw_ids_state {
/** The wrapped callback. */
bftw_callback *delegate;
/** The wrapped callback arguments. */
void *ptr;
/** Which visit this search corresponds to. */
enum bftw_visit visit;
/** Whether to override the bftw_visit. */
bool force_visit;
/** The current minimum depth (inclusive). */
size_t min_depth;
/** The current maximum depth (exclusive). */
size_t max_depth;
/** The set of pruned paths. */
struct trie pruned;
/** An error code to report. */
int error;
/** Whether the bottom has been found. */
bool bottom;
/** Whether to quit the search. */
bool quit;
};
/** Iterative deepening callback function. */
static enum bftw_action bftw_ids_callback(const struct BFTW *ftwbuf, void *ptr) {
struct bftw_ids_state *state = ptr;
if (state->force_visit) {
struct BFTW *mutbuf = (struct BFTW *)ftwbuf;
mutbuf->visit = state->visit;
}
if (ftwbuf->type == BFS_ERROR) {
if (ftwbuf->depth + 1 >= state->min_depth) {
return state->delegate(ftwbuf, state->ptr);
} else {
return BFTW_PRUNE;
}
}
if (ftwbuf->depth < state->min_depth) {
if (trie_find_str(&state->pruned, ftwbuf->path)) {
return BFTW_PRUNE;
} else {
return BFTW_CONTINUE;
}
} else if (state->visit == BFTW_POST) {
if (trie_find_str(&state->pruned, ftwbuf->path)) {
return BFTW_PRUNE;
}
}
enum bftw_action ret = BFTW_CONTINUE;
if (ftwbuf->visit == state->visit) {
ret = state->delegate(ftwbuf, state->ptr);
}
switch (ret) {
case BFTW_CONTINUE:
if (ftwbuf->type == BFS_DIR && ftwbuf->depth + 1 >= state->max_depth) {
state->bottom = false;
ret = BFTW_PRUNE;
}
break;
case BFTW_PRUNE:
if (ftwbuf->type == BFS_DIR) {
if (!trie_insert_str(&state->pruned, ftwbuf->path)) {
state->error = errno;
state->quit = true;
ret = BFTW_STOP;
}
}
break;
case BFTW_STOP:
state->quit = true;
break;
}
return ret;
}
/** Initialize iterative deepening state. */
static void bftw_ids_init(const struct bftw_args *args, struct bftw_ids_state *state, struct bftw_args *ids_args) {
state->delegate = args->callback;
state->ptr = args->ptr;
state->visit = BFTW_PRE;
state->force_visit = false;
state->min_depth = 0;
state->max_depth = 1;
trie_init(&state->pruned);
state->error = 0;
state->bottom = false;
state->quit = false;
*ids_args = *args;
ids_args->callback = bftw_ids_callback;
ids_args->ptr = state;
ids_args->flags &= ~BFTW_POST_ORDER;
ids_args->strategy = BFTW_DFS;
}
/** Finish an iterative deepening search. */
static int bftw_ids_finish(struct bftw_ids_state *state) {
int ret = 0;
if (state->error) {
ret = -1;
} else {
state->error = errno;
}
trie_destroy(&state->pruned);
errno = state->error;
return ret;
}
/**
* Iterative deepening bftw() wrapper.
*/
static int bftw_ids(const struct bftw_args *args) {
struct bftw_ids_state state;
struct bftw_args ids_args;
bftw_ids_init(args, &state, &ids_args);
while (!state.quit && !state.bottom) {
state.bottom = true;
if (bftw_auto(&ids_args) != 0) {
state.error = errno;
state.quit = true;
}
++state.min_depth;
++state.max_depth;
}
if (args->flags & BFTW_POST_ORDER) {
state.visit = BFTW_POST;
state.force_visit = true;
while (!state.quit && state.min_depth > 0) {
--state.max_depth;
--state.min_depth;
if (bftw_auto(&ids_args) != 0) {
state.error = errno;
state.quit = true;
}
}
}
return bftw_ids_finish(&state);
}
/**
* Exponential deepening bftw() wrapper.
*/
static int bftw_eds(const struct bftw_args *args) {
struct bftw_ids_state state;
struct bftw_args ids_args;
bftw_ids_init(args, &state, &ids_args);
while (!state.quit && !state.bottom) {
state.bottom = true;
if (bftw_auto(&ids_args) != 0) {
state.error = errno;
state.quit = true;
}
state.min_depth = state.max_depth;
state.max_depth *= 2;
}
if (!state.quit && (args->flags & BFTW_POST_ORDER)) {
state.visit = BFTW_POST;
state.min_depth = 0;
ids_args.flags |= BFTW_POST_ORDER;
if (bftw_auto(&ids_args) != 0) {
state.error = errno;
}
}
return bftw_ids_finish(&state);
}
int bftw(const struct bftw_args *args) {
switch (args->strategy) {
case BFTW_BFS:
return bftw_auto(args);
case BFTW_DFS:
return bftw_batch(args);
case BFTW_IDS:
return bftw_ids(args);
case BFTW_EDS:
return bftw_eds(args);
}
errno = EINVAL;
return -1;
}
|