-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Files.Directories.pas
815 lines (682 loc) · 25.8 KB
/
NtUtils.Files.Directories.pas
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
unit NtUtils.Files.Directories;
{
The module allows enumerating and traversing directories on a filesystem.
}
interface
uses
Ntapi.WinNt, Ntapi.ntioapi, Ntapi.ntseapi, NtUtils, NtUtils.Files,
DelphiApi.Reflection;
type
{
Info class | A/S/T | EA | Short | ID | ID 128 | Reparse | TX | Min version
---------------------------------- | ----- | -- | ----- | -- | ------ | ------- | -- | ------------
FileNamesInformation | | | | | | | |
FileDirectoryInformation | + | | | | | | |
FileFullDirectoryInformation | + | + | | | | | |
FileBothDirectoryInformation | + | + | + | | | | |
FileIdBothDirectoryInformation | + | + | + | + | | | |
FileIdFullDirectoryInformation | + | + | | + | | | |
FileIdGlobalTxDirectoryInformation | + | | | + | | | + |
FileIdExtdDirectoryInformation | + | + | | + | + | + | | Win 8+
FileIdExtdBothDirectoryInformation | + | + | + | + | + | + | | Win 10 TH1+
A/S/T - Attributes, Size, Times
}
TDirectoryFileFields = set of (
fcAttributes,
fcSize,
fcTimes,
fcShortName,
fcEaSize,
fcFileId,
fcFileId128,
fcReparseTag,
fcTransactionInfo
);
TDirectoryFileEntry = record
OptionalFields: TDirectoryFileFields;
[Aggregate] Common: TFileDirectoryCommonInformation;
FileName: String;
ShortName: String;
[Bytes] EaSize: Cardinal;
FileId: TFileId;
FileId128: TFileId128;
ReparsePointTag: TReparseTag;
LockingTransactionId: TGuid;
TxInfoFlags: TFileTxInfoFlags;
end;
// Note: ContinueTraversing allows callers to explicitly cancel traversing of
// specific locations, as well as enable it back when skipping reparse points
// or reached the maximum depth.
TFileTraverseCallback = reference to function(
const FileInfo: TDirectoryFileEntry;
const Root: IHandle;
const RootName: String;
var ContinueTraversing: Boolean
): TNtxStatus;
TFileTraverseBulkCallback = reference to function(
const Files: TArray<TDirectoryFileEntry>;
const Root: IHandle;
const RootName: String;
var ContinueTraversing: TArray<Boolean>
): TNtxStatus;
TFileTraverseOptions = set of (
ftInvokeOnFiles,
ftInvokeOnDirectories,
ftIgnoreCallbackFailures,
ftIgnoreTraverseFailures,
ftFollowReparsePoints
);
// Iterate a content of a filesystem directory one entry at at time.
function NtxGetNextDirectoryFile(
[Access(FILE_LIST_DIRECTORY)] const hxFile: IHandle;
out Entry: TDirectoryFileEntry;
var FirstScan: Boolean;
InfoClass: TFileInformationClass = FileDirectoryInformation;
[opt] const Pattern: String = ''
): TNtxStatus;
// Enumerate files in a filesystem directory multiple entries at a time
function NtxIterateDirectoryFile(
[Access(FILE_LIST_DIRECTORY)] const hxFile: IHandle;
out Files: TArray<TDirectoryFileEntry>;
var FirstScan: Boolean;
InfoClass: TFileInformationClass = FileDirectoryInformation;
SuggestedBufferSize: NativeUInt = $1000;
[opt] const Pattern: String = ''
): TNtxStatus;
// Enumerate all files in a filesystem directory
function NtxEnumerateDirectoryFile(
[Access(FILE_LIST_DIRECTORY)] const hxFile: IHandle;
out Files: TArray<TDirectoryFileEntry>;
InfoClass: TFileInformationClass = FileDirectoryInformation;
[opt] const Pattern: String = '';
SuggestedBufferSize: NativeUInt = $4000
): TNtxStatus;
// Recursively traverse a filesystem directory and its sub-directories.
// Invokes the callback on one file at a time.
[RequiredPrivilege(SE_BACKUP_PRIVILEGE, rpForBypassingChecks)]
function NtxTraverseDirectoryFile(
[opt, Access(FILE_LIST_DIRECTORY)] hxRoot: IHandle;
[opt, Access(FILE_LIST_DIRECTORY)] OpenParameters: IFileParameters;
Callback: TFileTraverseCallback;
Options: TFileTraverseOptions = [ftInvokeOnFiles, ftInvokeOnDirectories];
InfoClass: TFileInformationClass = FileDirectoryInformation;
MaxDepth: Integer = 32767
): TNtxStatus;
// Recursively traverse a filesystem directory and its sub-directories
// Invokes the callback on all files in a directory at a time.
[RequiredPrivilege(SE_BACKUP_PRIVILEGE, rpForBypassingChecks)]
function NtxTraverseDirectoryFileBulk(
[opt, Access(FILE_LIST_DIRECTORY)] hxRoot: IHandle;
[opt, Access(FILE_LIST_DIRECTORY)] OpenParameters: IFileParameters;
Callback: TFileTraverseBulkCallback;
Options: TFileTraverseOptions = [ftInvokeOnFiles, ftInvokeOnDirectories];
InfoClass: TFileInformationClass = FileDirectoryInformation;
MaxDepth: Integer = 32767
): TNtxStatus;
implementation
uses
Ntapi.ntdef, Ntapi.ntstatus, NtUtils.Files.Open, NtUtils.Synchronization,
NtUtils.Files.Operations, NtUtils.Security.Sid, NtUtils.SysUtils,
DelphiUtils.AutoObjects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
PFolderEntry = ^TDirectoryFileEntry;
function RtlxpCaptureDirectoryInfo(
[in, out] var Buffer: Pointer;
InfoClass: TFileInformationClass;
[out, opt] Entry: PFolderEntry
): Boolean;
var
BufferNames: PFileNamesInformation absolute Buffer;
BufferDir: PFileDirectoryInformation absolute Buffer;
BufferFull: PFileFullDirInformation absolute Buffer;
BufferBoth: PFileBothDirInformation absolute Buffer;
BufferIdBoth: PFileIdBothDirInformation absolute Buffer;
BufferIdFull: PFileIdFullDirInformation absolute Buffer;
BufferIdGlobalTx: PFileIdGlobalTxDirInformation absolute Buffer;
BufferIdExtd: PFileIdExtdDirInformation absolute Buffer;
BufferIdExtdBoth: PFileIdExtdBothDirInformation absolute Buffer;
begin
case InfoClass of
FileNamesInformation:
begin
if Assigned(Entry) then
begin
Entry.OptionalFields := [];
Entry.Common.NextEntryOffset := BufferNames.NextEntryOffset;
Entry.Common.FileIndex := BufferNames.FileIndex;
Entry.Common.FileNameLength := BufferNames.FileNameLength;
SetString(Entry.FileName, BufferNames.FileName,
BufferNames.FileNameLength div SizeOf(WideChar));
end;
Result := BufferNames.NextEntryOffset <> 0;
Inc(PByte(Buffer), BufferNames.NextEntryOffset);
end;
FileDirectoryInformation:
begin
if Assigned(Entry) then
begin
Entry.OptionalFields := [fcAttributes, fcSize, fcTimes];
Entry.Common := BufferDir.Common;
SetString(Entry.FileName, BufferDir.FileName,
BufferDir.Common.FileNameLength div SizeOf(WideChar));
end;
Result := BufferDir.Common.NextEntryOffset <> 0;
Inc(PByte(Buffer), BufferDir.Common.NextEntryOffset);
end;
FileFullDirectoryInformation:
begin
if Assigned(Entry) then
begin
Entry.OptionalFields := [fcAttributes, fcSize, fcTimes, fcEaSize];
Entry.Common := BufferFull.Common;
Entry.EaSize := BufferFull.EaSize;
SetString(Entry.FileName, BufferFull.FileName,
BufferFull.Common.FileNameLength div SizeOf(WideChar));
end;
Result := BufferFull.Common.NextEntryOffset <> 0;
Inc(PByte(Buffer), BufferFull.Common.NextEntryOffset);
end;
FileBothDirectoryInformation:
begin
if Assigned(Entry) then
begin
Entry.OptionalFields := [fcAttributes, fcSize, fcTimes, fcEaSize,
fcShortName];
Entry.Common := BufferBoth.Common;
Entry.EaSize := BufferBoth.EaSize;
SetString(Entry.FileName, BufferBoth.FileName,
BufferBoth.Common.FileNameLength div SizeOf(WideChar));
SetString(Entry.ShortName, BufferBoth.ShortName,
BufferBoth.ShortNameLength div SizeOf(WideChar));
end;
Result := BufferBoth.Common.NextEntryOffset <> 0;
Inc(PByte(Buffer), BufferBoth.Common.NextEntryOffset);
end;
FileIdBothDirectoryInformation:
begin
if Assigned(Entry) then
begin
Entry.OptionalFields := [fcAttributes, fcSize, fcTimes, fcEaSize,
fcShortName, fcFileId];
Entry.Common := BufferIdBoth.Common;
Entry.EaSize := BufferIdBoth.EaSize;
Entry.FileId := BufferIdBoth.FileId;
SetString(Entry.FileName, BufferIdBoth.FileName,
BufferIdBoth.Common.FileNameLength div SizeOf(WideChar));
SetString(Entry.ShortName, BufferIdBoth.ShortName,
BufferIdBoth.ShortNameLength div SizeOf(WideChar));
end;
Result := BufferIdBoth.Common.NextEntryOffset <> 0;
Inc(PByte(Buffer), BufferIdBoth.Common.NextEntryOffset);
end;
FileIdFullDirectoryInformation:
begin
if Assigned(Entry) then
begin
Entry.OptionalFields := [fcAttributes, fcSize, fcTimes, fcEaSize,
fcFileId];
Entry.Common := BufferIdFull.Common;
Entry.EaSize := BufferIdFull.EaSize;
Entry.FileId := BufferIdFull.FileId;
SetString(Entry.FileName, BufferIdFull.FileName,
BufferIdFull.Common.FileNameLength div SizeOf(WideChar));
end;
Result := BufferIdFull.Common.NextEntryOffset <> 0;
Inc(PByte(Buffer), BufferIdFull.Common.NextEntryOffset);
end;
FileIdGlobalTxDirectoryInformation:
begin
if Assigned(Entry) then
begin
Entry.OptionalFields := [fcAttributes, fcSize, fcTimes, fcFileId,
fcTransactionInfo];
Entry.Common := BufferIdGlobalTx.Common;
Entry.FileId := BufferIdGlobalTx.FileId;
Entry.LockingTransactionId := BufferIdGlobalTx.LockingTransactionId;
Entry.TxInfoFlags := BufferIdGlobalTx.TxInfoFlags;
SetString(Entry.FileName, BufferIdGlobalTx.FileName,
BufferIdGlobalTx.Common.FileNameLength div SizeOf(WideChar));
end;
Result := BufferIdGlobalTx.Common.NextEntryOffset <> 0;
Inc(PByte(Buffer), BufferIdGlobalTx.Common.NextEntryOffset);
end;
FileIdExtdDirectoryInformation:
begin
if Assigned(Entry) then
begin
Entry.OptionalFields := [fcAttributes, fcSize, fcTimes, fcEaSize,
fcFileId, fcFileId128, fcReparseTag];
Entry.Common := BufferIdExtd.Common;
Entry.EaSize := BufferIdExtd.EaSize;
Entry.FileId128 := BufferIdExtd.FileId;
Entry.ReparsePointTag := BufferIdExtd.ReparsePointTag;
if Entry.FileId128.High = 0 then
Entry.FileId := Entry.FileId128.Low
else
Entry.FileId := FILE_INVALID_FILE_ID;
SetString(Entry.FileName, BufferIdExtd.FileName,
BufferIdExtd.Common.FileNameLength div SizeOf(WideChar));
end;
Result := BufferIdExtd.Common.NextEntryOffset <> 0;
Inc(PByte(Buffer), BufferIdExtd.Common.NextEntryOffset);
end;
FileIdExtdBothDirectoryInformation:
begin
if Assigned(Entry) then
begin
Entry.OptionalFields := [fcAttributes, fcSize, fcTimes, fcEaSize,
fcFileId, fcFileId128, fcReparseTag, fcShortName];
Entry.Common := BufferIdExtdBoth.Common;
Entry.EaSize := BufferIdExtdBoth.EaSize;
Entry.FileId128 := BufferIdExtdBoth.FileId;
Entry.ReparsePointTag := BufferIdExtdBoth.ReparsePointTag;
if Entry.FileId128.High = 0 then
Entry.FileId := Entry.FileId128.Low
else
Entry.FileId := FILE_INVALID_FILE_ID;
SetString(Entry.FileName, BufferIdExtdBoth.FileName,
BufferIdExtdBoth.Common.FileNameLength div SizeOf(WideChar));
SetString(Entry.ShortName, BufferIdExtdBoth.ShortName,
BufferIdExtdBoth.ShortNameLength div SizeOf(WideChar));
end;
Result := BufferIdExtdBoth.Common.NextEntryOffset <> 0;
Inc(PByte(Buffer), BufferIdExtdBoth.Common.NextEntryOffset);
end;
else
Result := False;
end;
end;
function NtxQueryDirectoryFile(
[Access(FILE_LIST_DIRECTORY)] const hxFile: IHandle;
InfoClass: TFileInformationClass;
out Buffer: IMemory;
ReturnSingleEntry: Boolean;
FirstScan: Boolean;
SuggestedBufferSize: NativeUInt;
[opt] const Pattern: String
): TNtxStatus;
var
Isb: IMemory<PIoStatusBlock>;
PatternStr: TNtUnicodeString;
begin
Result := RtlxInitUnicodeString(PatternStr, Pattern);
if not Result.IsSuccess then
Exit;
IMemory(Isb) := Auto.AllocateDynamic(SizeOf(TIoStatusBlock));
Result.Location := 'NtQueryDirectoryFile';
Result.LastCall.Expects<TIoDirectoryAccessMask>(FILE_LIST_DIRECTORY);
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
IMemory(Buffer) := Auto.AllocateDynamic(SuggestedBufferSize);
repeat
Result.Status := NtQueryDirectoryFile(HandleOrDefault(hxFile), 0, nil, nil,
Isb.Data, Buffer.Data, Buffer.Size, InfoClass, ReturnSingleEntry,
PatternStr.RefOrNil, FirstScan);
AwaitFileOperation(Result, hxFile, Isb);
until not NtxExpandBufferEx(Result, IMemory(Buffer), Buffer.Size shl 1,
nil);
if (Result.Status = STATUS_NO_SUCH_FILE) or
(Result.Status = STATUS_NO_MORE_FILES) then
begin
Result.Location := 'NtxQueryDirectoryFile';
Result.LastCall.Expects<TIoDirectoryAccessMask>(FILE_LIST_DIRECTORY);
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.Status := STATUS_NO_MORE_ENTRIES;
end;
end;
function NtxGetNextDirectoryFile;
var
Buffer: IMemory;
BufferStart: Pointer;
begin
// Check for supported info classes
case InfoClass of
FileNamesInformation,
FileDirectoryInformation, FileFullDirectoryInformation,
FileBothDirectoryInformation, FileIdBothDirectoryInformation,
FileIdFullDirectoryInformation, FileIdGlobalTxDirectoryInformation,
FileIdExtdDirectoryInformation, FileIdExtdBothDirectoryInformation: ;
else
Result.Location := 'NtxGetNextDirectoryFile';
Result.Status := STATUS_INVALID_INFO_CLASS;
end;
Result := NtxQueryDirectoryFile(hxFile, InfoClass, Buffer, True, FirstScan,
MAX_PATH, Pattern);
if not Result.IsSuccess then
Exit;
FirstScan := False;
BufferStart := Buffer.Data;
RtlxpCaptureDirectoryInfo(BufferStart, InfoClass, @Entry);
end;
function NtxIterateDirectoryFile;
var
Buffer: IMemory;
BufferCursor: Pointer;
Count: Integer;
i: Integer;
begin
Files := nil;
// Check for supported modes
case InfoClass of
FileNamesInformation,
FileDirectoryInformation, FileFullDirectoryInformation,
FileBothDirectoryInformation, FileIdBothDirectoryInformation,
FileIdFullDirectoryInformation, FileIdGlobalTxDirectoryInformation,
FileIdExtdDirectoryInformation, FileIdExtdBothDirectoryInformation: ;
else
Result.Location := 'NtxIterateDirectoryFile';
Result.Status := STATUS_INVALID_INFO_CLASS;
end;
Result := NtxQueryDirectoryFile(hxFile, InfoClass, Buffer, False, FirstScan,
SuggestedBufferSize, Pattern);
if not Result.IsSuccess then
Exit;
// Count returned entries
Count := 0;
BufferCursor := Buffer.Data;
repeat
Inc(Count);
until not RtlxpCaptureDirectoryInfo(BufferCursor, InfoClass, nil);
// Save them
SetLength(Files, Count);
BufferCursor := Buffer.Data;
for i := 0 to High(Files) do
RtlxpCaptureDirectoryInfo(BufferCursor, InfoClass, @Files[i]);
FirstScan := False;
end;
function NtxEnumerateDirectoryFile;
var
FirstScan: Boolean;
FilesPortion: TArray<TDirectoryFileEntry>;
begin
Files := nil;
FirstScan := True;
// Collect all entries
while NtxIterateDirectoryFile(hxFile, FilesPortion, FirstScan, InfoClass,
SuggestedBufferSize, Pattern).HasEntry(Result) do
Files := Files + FilesPortion;
end;
function NtxTraverseDirectoryFileWorker(
const hxRoot: IHandle;
const AccumulatedPath: String;
const ParametersTemplate: IFileParameters;
const Callback: TFileTraverseCallback;
InfoClass: TFileInformationClass;
Options: TFileTraverseOptions;
RemainingDepth: Integer
): TNtxStatus;
var
Files: TArray<TDirectoryFileEntry>;
hxSubDirectory: IHandle;
FirstScan, IsDirectory, ContinueTraversing, MoreEntries: Boolean;
i: Integer;
begin
FirstScan := True;
MoreEntries := False;
repeat
// Retrieve a portion of files and sub-directories inside the root
Result := NtxIterateDirectoryFile(hxRoot, Files, FirstScan,
InfoClass);
if Result.Status = STATUS_NO_MORE_ENTRIES then
Break;
if not Result.IsSuccess then
begin
// Allow skipping this location if we cannot traverse it
if ftIgnoreTraverseFailures in Options then
begin
MoreEntries := True;
Break;
end;
Exit;
end;
// Process each file in this block
for i := 0 to High(Files) do
begin
// Skip pseudo-directories
if (Files[i].FileName = '.') or (Files[i].FileName = '..') then
Continue;
IsDirectory := BitTest(Files[i].Common.FileAttributes and
FILE_ATTRIBUTE_DIRECTORY);
// Allow skipping junctions and symlinks
ContinueTraversing := IsDirectory and (RemainingDepth > 0) and (
(ftFollowReparsePoints in Options) or not
BitTest(Files[i].Common.FileAttributes and FILE_ATTRIBUTE_REPARSE_POINT)
);
// Invoke the callback
if (IsDirectory and (ftInvokeOnDirectories in Options)) or
(not IsDirectory and (ftInvokeOnFiles in Options)) then
begin
Result := Callback(Files[i], hxRoot, AccumulatedPath,
ContinueTraversing);
// Fail with callback failures, unless told to ignore them
if not Result.IsSuccess and
not (ftIgnoreCallbackFailures in Options) then
Exit;
end;
if not IsDirectory or not ContinueTraversing then
Continue;
// Open the sub-directory for further traversing
Result := NtxOpenFile(hxSubDirectory, ParametersTemplate
.UseFileName(Files[i].FileName).UseRoot(hxRoot));
if not Result.IsSuccess then
begin
// Allow skipping directories we cannot access
if ftIgnoreTraverseFailures in Options then
begin
MoreEntries := True;
Continue;
end;
Exit;
end;
// Call recursively
Result := NtxTraverseDirectoryFileWorker(hxSubDirectory,
RtlxCombinePaths(AccumulatedPath, Files[i].FileName),
ParametersTemplate, Callback, InfoClass, Options, RemainingDepth - 1);
if Result.Status = STATUS_MORE_ENTRIES then
MoreEntries := True
else if not Result.IsSuccess then
Exit;
end;
until False;
// We reach here only if no unhandled errors occurred.
Result.Location := 'NtxTraverseFolderWorker';
if MoreEntries then
Result.Status := STATUS_MORE_ENTRIES
else
Result.Status := STATUS_SUCCESS;
end;
function NtxTraverseDirectoryFile;
begin
// Check for supported info classes. Note: we don't allow FileNamesInformation
// because we need to know attributes internally.
case InfoClass of
FileDirectoryInformation, FileFullDirectoryInformation,
FileBothDirectoryInformation, FileIdBothDirectoryInformation,
FileIdFullDirectoryInformation, FileIdGlobalTxDirectoryInformation,
FileIdExtdDirectoryInformation, FileIdExtdBothDirectoryInformation: ;
else
Result.Location := 'NtxTraverseDirectoryFile';
Result.Status := STATUS_INVALID_INFO_CLASS;
end;
// Always use synchronous I/O and at least directory listing access
OpenParameters := FileParameters(OpenParameters);
OpenParameters := OpenParameters
.UseOptions(OpenParameters.Options or FILE_DIRECTORY_FILE)
.UseSyncMode(fsSynchronousNonAlert)
.UseAccess(OpenParameters.Access or FILE_LIST_DIRECTORY);
// Open the root folder if not provided
if not Assigned(hxRoot) then
begin
Result := NtxOpenFile(hxRoot, OpenParameters);
if not Result.IsSuccess then
Exit;
end;
// Since we want to reuse the open options, clear the file ID information
OpenParameters := OpenParameters.UseFileId(0);
Result := NtxTraverseDirectoryFileWorker(hxRoot, OpenParameters.FileName,
OpenParameters, Callback, InfoClass, Options, MaxDepth);
end;
function NtxTraverseDirectoryFileBulkWorker(
const hxRoot: IHandle;
const AccumulatedPath: String;
const ParametersTemplate: IFileParameters;
const Callback: TFileTraverseBulkCallback;
InfoClass: TFileInformationClass;
Options: TFileTraverseOptions;
RemainingDepth: Integer
): TNtxStatus;
var
Files, FilesFiltered: TArray<TDirectoryFileEntry>;
ContinueTraversingFiltered: TArray<Boolean>;
IndexToFilteredIndex: TArray<Integer>;
hxSubDirectory: IHandle;
MoreEntries, IsDirectory, ContinueTraversing: Boolean;
i, j: Integer;
begin
MoreEntries := False;
// Retrieve all files from the directory
Result := NtxEnumerateDirectoryFile(hxRoot, Files, InfoClass);
if not Result.IsSuccess then
begin
// Allow skipping this location if we cannot traverse it
if ftIgnoreTraverseFailures in Options then
MoreEntries := True
else
Exit;
end;
// Count entries we want to show to the callback
j := 0;
for i := 0 to High(Files) do
begin
// Skip pseudo-directories
if (Files[i].FileName = '.') or (Files[i].FileName = '..') then
Continue;
IsDirectory := BitTest(Files[i].Common.FileAttributes and
FILE_ATTRIBUTE_DIRECTORY);
if (IsDirectory and (ftInvokeOnDirectories in Options)) or
(not IsDirectory and (ftInvokeOnFiles in Options)) then
Inc(j);
end;
// Filter them, plus build an index conversion table and an array that
// allows the callback to control further traversing
SetLength(FilesFiltered, j);
SetLength(ContinueTraversingFiltered, j);
SetLength(IndexToFilteredIndex, Length(Files));
j := 0;
for i := 0 to High(Files) do
begin
IndexToFilteredIndex[i] := -1;
// Skip pseudo-directories
if (Files[i].FileName = '.') or (Files[i].FileName = '..') then
Continue;
IsDirectory := BitTest(Files[i].Common.FileAttributes and
FILE_ATTRIBUTE_DIRECTORY);
if (IsDirectory and (ftInvokeOnDirectories in Options)) or
(not IsDirectory and (ftInvokeOnFiles in Options)) then
begin
FilesFiltered[j] := Files[i];
IndexToFilteredIndex[i] := j;
// Choose if we plan to further traverse this directory
ContinueTraversingFiltered[j] := IsDirectory and (RemainingDepth > 0) and (
(ftFollowReparsePoints in Options) or not
BitTest(Files[i].Common.FileAttributes and FILE_ATTRIBUTE_REPARSE_POINT)
);
Inc(j);
end;
end;
// Invoke the callback
Result := Callback(FilesFiltered, hxRoot, AccumulatedPath,
ContinueTraversingFiltered);
// Fail with callback failures, unless ignoring them
if not Result.IsSuccess and not (ftIgnoreCallbackFailures in Options) then
Exit;
// Just in case: make sure the callback didn't alter array length
if Length(ContinueTraversingFiltered) <> Length(FilesFiltered) then
begin
Result.Location := 'NtxTraverseDirectoryFileBulkWorker';
Result.Status := STATUS_ASSERTION_FAILURE;
Exit;
end;
// Traverse all sub-directories
for i := 0 to High(Files) do
begin
// Skip non-directories
if not BitTest(Files[i].Common.FileAttributes and
FILE_ATTRIBUTE_DIRECTORY) then
Continue;
// Skip pseudo-directories
if (Files[i].FileName = '.') or (Files[i].FileName = '..') then
Continue;
// Allow the callback to override further traversing options
if IndexToFilteredIndex[i] >= 0 then
ContinueTraversing := ContinueTraversingFiltered[IndexToFilteredIndex[i]]
else
ContinueTraversing := (RemainingDepth > 0) and (
(ftFollowReparsePoints in Options) or not
BitTest(Files[i].Common.FileAttributes and FILE_ATTRIBUTE_REPARSE_POINT)
);
if not ContinueTraversing then
Continue;
// Open the sub-directory
Result := NtxOpenFile(hxSubDirectory, ParametersTemplate
.UseFileName(Files[i].FileName).UseRoot(hxRoot));
if not Result.IsSuccess then
begin
// Allow skipping directories we cannot access
if ftIgnoreTraverseFailures in Options then
begin
MoreEntries := True;
Continue;
end;
Exit;
end;
// Call recursively
Result := NtxTraverseDirectoryFileBulkWorker(hxSubDirectory,
RtlxCombinePaths(AccumulatedPath, Files[i].FileName), ParametersTemplate,
Callback, InfoClass, Options, RemainingDepth - 1);
if Result.Status = STATUS_MORE_ENTRIES then
MoreEntries := True
else if not Result.IsSuccess then
Exit;
end;
// We reach here only if no unhandled errors occurred.
Result.Location := 'NtxTraverseDirectoryFileBulkWorker';
if MoreEntries then
Result.Status := STATUS_MORE_ENTRIES
else
Result.Status := STATUS_SUCCESS;
end;
function NtxTraverseDirectoryFileBulk;
begin
// Check for supported info classes. Note: we don't allow FileNamesInformation
// because we need to know attributes internally.
case InfoClass of
FileDirectoryInformation, FileFullDirectoryInformation,
FileBothDirectoryInformation, FileIdBothDirectoryInformation,
FileIdFullDirectoryInformation, FileIdGlobalTxDirectoryInformation,
FileIdExtdDirectoryInformation, FileIdExtdBothDirectoryInformation: ;
else
Result.Location := 'NtxTraverseDirectoryFileBulk';
Result.Status := STATUS_INVALID_INFO_CLASS;
end;
// Always use synchronous I/O and at least directory listing access
OpenParameters := FileParameters(OpenParameters);
OpenParameters := OpenParameters
.UseOptions(OpenParameters.Options or FILE_DIRECTORY_FILE)
.UseSyncMode(fsSynchronousNonAlert)
.UseAccess(OpenParameters.Access or FILE_LIST_DIRECTORY);
// Open the root folder if not provided
if not Assigned(hxRoot) then
begin
Result := NtxOpenFile(hxRoot, OpenParameters);
if not Result.IsSuccess then
Exit;
end;
// Since we want to reuse the open options, clear the file ID information
OpenParameters := OpenParameters.UseFileId(0);
Result := NtxTraverseDirectoryFileBulkWorker(hxRoot, OpenParameters.FileName,
OpenParameters, Callback, InfoClass, Options, MaxDepth);
end;
end.