-
Notifications
You must be signed in to change notification settings - Fork 31
/
NtUtils.Files.Operations.pas
1016 lines (835 loc) · 26.7 KB
/
NtUtils.Files.Operations.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
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
unit NtUtils.Files.Operations;
{
The module provides support for various file operations using Native API.
}
interface
uses
Ntapi.WinNt, Ntapi.ntioapi, Ntapi.ntioapi.fsctl, DelphiApi.Reflection,
DelphiUtils.AutoObjects, DelphiUtils.Async, NtUtils, NtUtils.Files;
type
TFileStreamInfo = record
[Bytes] StreamSize: Int64;
[Bytes] StreamAllocationSize: Int64;
StreamName: String;
end;
TFileHardlinkLinkInfo = record
ParentFileID: TFileId;
FileName: String;
end;
TNtxExtendedAttribute = record
Flags: TFileEaFlags;
Name: AnsiString;
[opt] Value: IMemory; // use nil to delete
class function From(
const Name: AnsiString;
[opt] const Value: IMemory;
Flags: TFileEaFlags = 0
): TNtxExtendedAttribute; static;
end;
TNtxEaIterationMode = (
// Iterate using the built-in handle state. Fails the query with
// STATUS_EA_CORRUPT_ERROR if an external modification occurs.
eaiViaRestartScan,
// Iterate using indexes. External modifications might cause skipping EAs.
eaiByIndex
);
{ Operations }
// Synchronously wait for a completion of an operation on an asynchronous handle
procedure AwaitFileOperation(
var Result: TNtxStatus;
[Access(SYNCHRONIZE)] hFile: THandle;
const xIoStatusBlock: IMemory<PIoStatusBlock>
);
// Read from a file into a buffer
function NtxReadFile(
[Access(FILE_READ_DATA)] hFile: THandle;
[out] Buffer: Pointer;
BufferSize: Cardinal;
const Offset: UInt64 = FILE_USE_FILE_POINTER_POSITION;
[opt] AsyncCallback: TAnonymousApcCallback = nil
): TNtxStatus;
// Write to a file from a buffer
function NtxWriteFile(
[Access(FILE_WRITE_DATA)] hFile: THandle;
[in] Buffer: Pointer;
BufferSize: Cardinal;
const Offset: UInt64 = FILE_USE_FILE_POINTER_POSITION;
[opt] AsyncCallback: TAnonymousApcCallback = nil
): TNtxStatus;
// Delete a file
function NtxDeleteFile(
const Name: String;
[opt] const ObjectAttributes: IObjectAttributes = nil
): TNtxStatus;
// Rename a file
function NtxRenameFile(
[Access(_DELETE)] hFile: THandle;
const NewName: String;
Flags: TFileRenameFlags = 0;
[opt] RootDirectory: THandle = 0;
InfoClass: TFileInformationClass = FileRenameInformation
): TNtxStatus;
// Create a hardlink for a file
function NtxHardlinkFile(
[Access(0)] hFile: THandle;
const NewName: String;
Flags: TFileLinkFlags = 0;
[opt] RootDirectory: THandle = 0;
InfoClass: TFileInformationClass = FileLinkInformation
): TNtxStatus;
// Lock a range of bytes in a file
function NtxLockFile(
[Access(FILE_READ_DATA), {or} Access(FILE_WRITE_DATA)] hFile: THandle;
const ByteOffset: UInt64;
const Length: UInt64;
ExclusiveLock: Boolean = True;
FailImmediately: Boolean = True;
Key: Cardinal = 0
): TNtxStatus;
// Unlock a range of bytes in a file
function NtxUnlockFile(
[Access(FILE_READ_DATA), {or} Access(FILE_WRITE_DATA)] hFile: THandle;
const ByteOffset: UInt64;
const Length: UInt64;
Key: Cardinal = 0
): TNtxStatus;
// Lock a range of bytes in a file and automatically unlock it later
function NtxLockFileAuto(
out Unlocker: IAutoReleasable;
[Access(FILE_READ_DATA), {or} Access(FILE_WRITE_DATA)] const hxFile: IHandle;
ByteOffset: UInt64;
Length: UInt64;
ExclusiveLock: Boolean;
FailImmediately: Boolean = True;
Key: Cardinal = 0
): TNtxStatus;
{ Information }
// Query variable-length information
function NtxQueryFile(
hFile: THandle;
InfoClass: TFileInformationClass;
out xMemory: IMemory;
InitialBuffer: Cardinal = 0;
[opt] GrowthMethod: TBufferGrowthMethod = nil
): TNtxStatus;
// Query basic information by file name
function NtxQueryAttributesFile(
out BasicInfo: TFileBasicInformation;
[Access(FILE_READ_ATTRIBUTES)] const Name: String;
[opt] const ObjectAttributes: IObjectAttributes = nil
): TNtxStatus;
// Query extended information by file name
function NtxQueryFullAttributesFile(
out NetworkInfo: TFileNetworkOpenInformation;
[Access(FILE_READ_ATTRIBUTES)] const Name: String;
[opt] const ObjectAttributes: IObjectAttributes = nil
): TNtxStatus;
// Set variable-length information
function NtxSetFile(
hFile: THandle;
InfoClass: TFileInformationClass;
[in] Buffer: Pointer;
BufferSize: Cardinal
): TNtxStatus;
type
NtxFile = class abstract
// Query fixed-size information
class function Query<T>(
hFile: THandle;
InfoClass: TFileInformationClass;
out Buffer: T
): TNtxStatus; static;
// Query fixed-size information by name
class function QueryByName<T>(
const FileName: String;
InfoClass: TFileInformationClass;
out Buffer: T;
[opt] const ObjectAttributes: IObjectAttributes = nil
): TNtxStatus; static;
// Set fixed-size information
class function &Set<T>(
hFile: THandle;
InfoClass: TFileInformationClass;
const Buffer: T
): TNtxStatus; static;
// Read a fixed-size buffer
class function Read<T>(
hFile: THandle;
out Buffer: T;
const Offset: UInt64 = FILE_USE_FILE_POINTER_POSITION
): TNtxStatus; static;
// Write a fixed-size buffer
class function Write<T>(
hFile: THandle;
const Buffer: T;
const Offset: UInt64 = FILE_USE_FILE_POINTER_POSITION
): TNtxStatus; static;
end;
// Query a name of a file without the device name
function NtxQueryNameFile(
[Access(0)] hFile: THandle;
out Name: String;
InfoClass: TFileInformationClass = FileNameInformation
): TNtxStatus;
// Modify a short (alternative) name of a file
function NtxSetShortNameFile(
[Access(_DELETE)] hFile: THandle;
const ShortName: String
): TNtxStatus;
{ Volume information }
// Query variable-length information about a volume of a file
function NtxQueryVolume(
hFile: THandle;
InfoClass: TFsInfoClass;
out Buffer: IMemory;
InitialBuffer: Cardinal = 0;
[opt] GrowthMethod: TBufferGrowthMethod = nil
): TNtxStatus;
type
NtxVolume = class abstract
// Query fixed-size information
class function Query<T>(
hFile: THandle;
InfoClass: TFsInfoClass;
out Buffer: T
): TNtxStatus; static;
end;
{ Enumeration }
// Enumerate file streams
function NtxEnumerateStreamsFile(
[Access(0)] hFile: THandle;
out Streams: TArray<TFileStreamInfo>
): TNtxStatus;
// Enumerate hardlinks pointing to the file
function NtxEnumerateHardLinksFile(
[Access(0)] hFile: THandle;
out Links: TArray<TFileHardlinkLinkInfo>
): TNtxStatus;
// Enumerate processes that use this file. Requires FILE_READ_ATTRIBUTES.
function NtxEnumerateUsingProcessesFile(
[Access(FILE_READ_ATTRIBUTES)] hFile: THandle;
out PIDs: TArray<TProcessId>
): TNtxStatus;
{ Extended Attributes }
// Capture a raw buffer with extended attribtues
function RtlxCaptureFullEaInformation(
[in] Buffer: PFileFullEaInformation
): TArray<TNtxExtendedAttribute>;
// Query a single extended attribute by name
function NtxQueryEaFile(
[Access(FILE_READ_EA)] hFile: THandle;
const Name: AnsiString;
out EA: TNtxExtendedAttribute
): TNtxStatus;
// Query multiple extended attributes by name
function NtxQueryEAsFile(
[Access(FILE_READ_EA)] hFile: THandle;
const Names: TArray<AnsiString>;
out EAs: TArray<TNtxExtendedAttribute>
): TNtxStatus;
// Enumerate all extended attributes on a file
function NtxEnumerateEAsFile(
[Access(FILE_READ_EA)] hFile: THandle;
out EAs: TArray<TNtxExtendedAttribute>
): TNtxStatus;
// Make a for-in iterator for enumerating extended attributes on a file.
// Note: when the Status parameter is not set, the function might raise
// exceptions during enumeration.
function NtxIterateEaFile(
[out, opt] Status: PNtxStatus;
[Access(FILE_READ_EA)] const hxFile: IHandle;
IterationMode: TNtxEaIterationMode = eaiByIndex
): IEnumerable<TNtxExtendedAttribute>;
// Set/delete multiple extended attributes on a file
// Note: use a nil value to delete an attribute
function NtxSetEAsFile(
[Access(FILE_WRITE_EA)] hFile: THandle;
const EAs: TArray<TNtxExtendedAttribute>;
[opt] AsyncCallback: TAnonymousApcCallback = nil
): TNtxStatus;
// Set a single extended attributes on a file
function NtxSetEAFile(
[Access(FILE_WRITE_EA)] hFile: THandle;
const Name: AnsiString;
const Value: TMemory;
Flags: TFileEaFlags = 0;
[opt] AsyncCallback: TAnonymousApcCallback = nil
): TNtxStatus;
// Delete a single extended attributes on a file
function NtxDeleteEAFile(
[Access(FILE_WRITE_EA)] hFile: THandle;
const Name: AnsiString;
[opt] AsyncCallback: TAnonymousApcCallback = nil
): TNtxStatus;
implementation
uses
Ntapi.ntstatus, Ntapi.ntdef, NtUtils.Ldr, NtUtils.Files.Open,
NtUtils.Synchronization;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ Operations }
procedure AwaitFileOperation;
begin
// When performing a synchronous operation on an asynchronous handle, we
// must wait for completion ourselves.
if Result.Status = STATUS_PENDING then
begin
Result := NtxWaitForSingleObject(hFile);
// On success, extract the status. On failure, the only option we
// have is to prolong the lifetime of the I/O status block indefinitely
// because we never know when the system will write to its memory.
if Result.IsSuccess then
Result.Status := xIoStatusBlock.Data.Status
else
xIoStatusBlock.AutoRelease := False;
end;
end;
function NtxReadFile;
var
ApcContext: IAnonymousIoApcContext;
xIsb: IMemory<PIoStatusBlock>;
begin
Result.Location := 'NtReadFile';
Result.LastCall.Expects<TIoFileAccessMask>(FILE_READ_DATA);
Result.Status := NtReadFile(hFile, 0, GetApcRoutine(AsyncCallback),
Pointer(ApcContext), PrepareApcIsbEx(ApcContext, AsyncCallback, xIsb),
Buffer, BufferSize, @Offset, nil);
// Keep the context alive until the callback executes
if Assigned(ApcContext) and Result.IsSuccess then
ApcContext._AddRef;
// Wait on asynchronous handles if no callback is available
if not Assigned(AsyncCallback) then
AwaitFileOperation(Result, hFile, xIsb);
end;
function NtxWriteFile;
var
ApcContext: IAnonymousIoApcContext;
xIsb: IMemory<PIoStatusBlock>;
begin
Result.Location := 'NtWriteFile';
Result.LastCall.Expects<TIoFileAccessMask>(FILE_WRITE_DATA);
Result.Status := NtWriteFile(hFile, 0, GetApcRoutine(AsyncCallback),
Pointer(ApcContext), PrepareApcIsbEx(ApcContext, AsyncCallback, xIsb),
Buffer, BufferSize, @Offset, nil);
// Keep the context alive until the callback executes
if Assigned(ApcContext) and Result.IsSuccess then
ApcContext._AddRef;
// Wait on asynchronous handles if no callback is available
if not Assigned(AsyncCallback) then
AwaitFileOperation(Result, hFile, xIsb);
end;
function NtxDeleteFile;
var
ObjAttr: PObjectAttributes;
begin
Result := AttributeBuilder(ObjectAttributes).UseName(Name).Build(ObjAttr);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtDeleteFile';
Result.Status := NtDeleteFile(ObjAttr^);
end;
function NtxpSetRenameInfoFile(
hFile: THandle;
TargetName: String;
Flags: Cardinal;
RootDirectory: THandle;
InfoClass: TFileInformationClass
): TNtxStatus;
var
xMemory: IMemory<PFileRenameInformationEx>; // aka PFileLinkInformationEx
begin
IMemory(xMemory) := Auto.AllocateDynamic(SizeOf(TFileRenameInformation) +
StringSizeNoZero(TargetName));
// Prepare a variable-length buffer for rename or hardlink operations
xMemory.Data.Flags := Flags;
xMemory.Data.RootDirectory := RootDirectory;
xMemory.Data.FileNameLength := StringSizeNoZero(TargetName);
MarshalString(TargetName, @xMemory.Data.FileName);
Result := NtxSetFile(hFile, InfoClass, xMemory.Data, xMemory.Size);
end;
function NtxRenameFile;
begin
// Note: if you get sharing violation when using RootDirectory, open it with
// FILE_TRAVERSE | FILE_READ_ATTRIBUTES access.
Result := NtxpSetRenameInfoFile(hFile, NewName, Flags,
RootDirectory, InfoClass);
Result.LastCall.Expects<TFileAccessMask>(_DELETE);
end;
function NtxHardlinkFile;
begin
Result := NtxpSetRenameInfoFile(hFile, NewName, Flags,
RootDirectory, InfoClass);
end;
function NtxLockFile;
var
xIsb: IMemory<PIoStatusBlock>;
begin
IMemory(xIsb) := Auto.AllocateDynamic(SizeOf(TIoStatusBlock));
Result.Location := 'NtLockFile';
Result.LastCall.Expects<TIoFileAccessMask>(FILE_READ_DATA);
Result.LastCall.Expects<TIoFileAccessMask>(FILE_WRITE_DATA);
Result.Status := NtLockFile(hFile, 0, nil, nil, xIsb.Data, ByteOffset, Length,
Key, FailImmediately, ExclusiveLock);
AwaitFileOperation(Result, hFile, xIsb);
end;
function NtxUnlockFile;
var
Isb: TIoStatusBlock;
begin
Result.Location := 'NtUnlockFile';
Result.LastCall.Expects<TIoFileAccessMask>(FILE_READ_DATA);
Result.LastCall.Expects<TIoFileAccessMask>(FILE_WRITE_DATA);
Result.Status := NtUnlockFile(hFile, Isb, ByteOffset, Length, Key);
end;
function NtxLockFileAuto;
begin
Result := NtxLockFile(hxFile.Handle, ByteOffset, Length, ExclusiveLock,
FailImmediately, Key);
if Result.IsSuccess then
Unlocker := Auto.Delay(procedure
begin
NtxUnlockFile(hxFile.Handle, ByteOffset, Length, Key);
end
);
end;
{ Information }
function GrowFileDefault(
const Memory: IMemory;
Required: NativeUInt
): NativeUInt;
begin
Result := Memory.Size shl 1 + 256; // x2 + 256 B
end;
function NtxQueryFile;
var
Isb: TIoStatusBlock;
begin
Result.Location := 'NtQueryInformationFile';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects(ExpectedFileQueryAccess(InfoClass));
// NtQueryInformationFile does not return the required size. We either need
// to know how to grow the buffer, or we should guess.
if not Assigned(GrowthMethod) then
GrowthMethod := GrowFileDefault;
xMemory := Auto.AllocateDynamic(InitialBuffer);
repeat
Isb.Information := 0;
Result.Status := NtQueryInformationFile(hFile, Isb, xMemory.Data,
xMemory.Size, InfoClass);
until not NtxExpandBufferEx(Result, xMemory, Isb.Information, GrowthMethod);
end;
function NtxQueryAttributesFile;
var
ObjAttr: PObjectAttributes;
begin
Result := AttributeBuilder(ObjectAttributes).UseName(Name).Build(ObjAttr);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtQueryAttributesFile';
Result.LastCall.Expects<TFileAccessMask>(FILE_READ_ATTRIBUTES);
Result.Status := NtQueryAttributesFile(ObjAttr^, BasicInfo);
end;
function NtxQueryFullAttributesFile;
var
ObjAttr: PObjectAttributes;
begin
Result := AttributeBuilder(ObjectAttributes).UseName(Name).Build(ObjAttr);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtQueryFullAttributesFile';
Result.LastCall.Expects<TFileAccessMask>(FILE_READ_ATTRIBUTES);
Result.Status := NtQueryFullAttributesFile(ObjAttr^, NetworkInfo);
end;
function NtxSetFile;
var
Isb: TIoStatusBlock;
begin
Result.Location := 'NtSetInformationFile';
Result.LastCall.UsesInfoClass(InfoClass, icSet);
Result.LastCall.Expects(ExpectedFileSetAccess(InfoClass));
Result.Status := NtSetInformationFile(hFile, Isb, Buffer,
BufferSize, InfoClass);
end;
class function NtxFile.Query<T>;
var
Isb: TIoStatusBlock;
begin
Result.Location := 'NtQueryInformationFile';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects(ExpectedFileQueryAccess(InfoClass));
Result.Status := NtQueryInformationFile(hFile, Isb, @Buffer,
SizeOf(Buffer), InfoClass);
end;
class function NtxFile.QueryByName<T>;
var
Isb: TIoStatusBlock;
ObjAttr: PObjectAttributes;
hxFile: IHandle;
begin
if LdrxCheckDelayedImport(delayed_ntdll,
delayed_NtQueryInformationByName).IsSuccess then
begin
Result := AttributeBuilder(ObjectAttributes).UseName(FileName)
.Build(ObjAttr);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtQueryInformationByName';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects<TFileAccessMask>(FILE_READ_ATTRIBUTES);
Result.Status := NtQueryInformationByName(ObjAttr^, Isb, @Buffer,
SizeOf(Buffer), InfoClass);
end
else
begin
// Fallback to opening the file manually on older versions
Result := NtxOpenFile(hxFile, FileParameters
.UseFileName(FileName)
.UseRoot(AttributeBuilder(ObjectAttributes).Root)
.UseAccess(FILE_READ_ATTRIBUTES)
.UseOptions(FILE_OPEN_NO_RECALL)
.UseSyncMode(fsAsynchronous)
);
if Result.IsSuccess then
Result := Query(hxFile.Handle, InfoClass, Buffer);
end;
end;
class function NtxFile.Read<T>;
begin
Result := NtxReadFile(hFile, @Buffer, SizeOf(Buffer), Offset);
end;
class function NtxFile.&Set<T>;
begin
Result := NtxSetFile(hFile, InfoClass, @Buffer, SizeOf(Buffer));
end;
class function NtxFile.Write<T>;
begin
Result := NtxWriteFile(hFile, @Buffer, SizeOf(Buffer), Offset);
end;
function GrowFileName(
const Memory: IMemory;
BufferSize: NativeUInt
): NativeUInt;
begin
Result := SizeOf(Cardinal) + PFileNameInformation(Memory.Data).FileNameLength;
end;
function NtxQueryNameFile;
var
xMemory: IMemory<PFileNameInformation>;
begin
Result := NtxQueryFile(hFile, InfoClass, IMemory(xMemory),
SizeOf(TFileNameInformation) + SizeOf(WideChar) * MAX_PATH, GrowFileName);
if Result.IsSuccess then
SetString(Name, xMemory.Data.FileName, xMemory.Data.FileNameLength div
SizeOf(WideChar));
end;
function NtxSetShortNameFile;
var
Buffer: IMemory<PFileNameInformation>;
begin
IMemory(Buffer) := Auto.AllocateDynamic(SizeOf(TFileNameInformation) +
StringSizeNoZero(ShortName));
Buffer.Data.FileNameLength := StringSizeNoZero(ShortName);
MarshalString(ShortName, @Buffer.Data.FileName);
Result := NtxSetFile(hFile, FileShortNameInformation, Buffer.Data,
Buffer.Size);
end;
{ Volume information }
function NtxQueryVolume;
var
Isb: TIoStatusBlock;
begin
Result.Location := 'NtQueryVolumeInformationFile';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects(ExpectedFsQueryAccess(InfoClass));
if not Assigned(GrowthMethod) then
GrowthMethod := GrowFileDefault;
Buffer := Auto.AllocateDynamic(InitialBuffer);
repeat
Isb.Information := 0;
Result.Status := NtQueryVolumeInformationFile(hFile, Isb, Buffer.Data,
Buffer.Size, InfoClass);
until not NtxExpandBufferEx(Result, Buffer, Isb.Information, GrowthMethod);
end;
class function NtxVolume.Query<T>;
var
Isb: TIoStatusBlock;
begin
Result.Location := 'NtQueryVolumeInformationFile';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects(ExpectedFsQueryAccess(InfoClass));
Result.Status := NtQueryVolumeInformationFile(hFile, Isb, @Buffer,
SizeOf(Buffer), InfoClass);
end;
{ Enumeration }
function NtxEnumerateStreamsFile;
var
xMemory: IMemory;
pStream: PFileStreamInformation;
begin
Result := NtxQueryFile(hFile, FileStreamInformation, xMemory,
SizeOf(TFileStreamInformation));
if not Result.IsSuccess then
Exit;
SetLength(Streams, 0);
pStream := xMemory.Data;
repeat
SetLength(Streams, Length(Streams) + 1);
Streams[High(Streams)].StreamSize := pStream.StreamSize;
Streams[High(Streams)].StreamAllocationSize := pStream.StreamAllocationSize;
SetString(Streams[High(Streams)].StreamName, pStream.StreamName,
pStream.StreamNameLength div SizeOf(WideChar));
if pStream.NextEntryOffset <> 0 then
pStream := Pointer(UIntPtr(pStream) + pStream.NextEntryOffset)
else
Break;
until False;
end;
function GrowFileLinks(
const Memory: IMemory;
Required: NativeUInt
): NativeUInt;
begin
Result := PFileLinksInformation(Memory.Data).BytesNeeded;
end;
function NtxEnumerateHardLinksFile;
var
xMemory: IMemory<PFileLinksInformation>;
pLink: PFileLinkEntryInformation;
i: Integer;
begin
Result := NtxQueryFile(hFile, FileHardLinkInformation, IMemory(xMemory),
SizeOf(TFileLinksInformation), GrowFileLinks);
if not Result.IsSuccess then
Exit;
SetLength(Links, xMemory.Data.EntriesReturned);
pLink := Pointer(@xMemory.Data.Entry);
i := 0;
repeat
if i > High(Links) then
Break;
// Note: we have only the filename and the ID of the parent directory
Links[i].ParentFileId := pLink.ParentFileId;
SetString(Links[i].FileName, pLink.FileName, pLink.FileNameLength);
if pLink.NextEntryOffset <> 0 then
pLink := Pointer(UIntPtr(pLink) + pLink.NextEntryOffset)
else
Break;
Inc(i);
until False;
end;
function NtxEnumerateUsingProcessesFile;
var
xMemory: IMemory<PFileProcessIdsUsingFileInformation>;
i: Integer;
begin
Result := NtxQueryFile(hFile, FileProcessIdsUsingFileInformation,
IMemory(xMemory), SizeOf(TFileProcessIdsUsingFileInformation));
Result.LastCall.Expects<TFileAccessMask>(FILE_READ_ATTRIBUTES);
if not Result.IsSuccess then
Exit;
SetLength(PIDs, xMemory.Data.NumberOfProcessIdsInList);
for i := 0 to High(PIDs) do
PIDs[i] := xMemory.Data.ProcessIdList{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF};
end;
{ Extended Attributes }
class function TNtxExtendedAttribute.From;
begin
Result.Flags := Flags;
Result.Name := Name;
Result.Value := Value;
end;
function RtlxCaptureFullEaInformation;
var
Cursor: PFileFullEaInformation;
Count: Integer;
begin
// Count entries
Count := 1;
Cursor := Buffer;
repeat
if Cursor.NextEntryOffset = 0 then
Break;
Inc(Count);
Cursor := Pointer(PByte(Cursor) + Cursor.NextEntryOffset);
until False;
// Allocate the storage
SetLength(Result, Count);
Count := 0;
Cursor := Buffer;
repeat
// Save each EA
Result[Count].Flags := Cursor.Flags;
SetString(Result[Count].Name, PAnsiChar(@Cursor.EaName[0]),
Cursor.EaNameLength);
Result[Count].Value := Auto.CopyDynamic(
@Cursor.EaName{$R-}[Cursor.EaNameLength]{$IFDEF R+}{$R+}{$ENDIF},
Cursor.EaValueLength);
if Cursor.NextEntryOffset = 0 then
Break;
Inc(Count);
Cursor := Pointer(PByte(Cursor) + Cursor.NextEntryOffset);
until False;
end;
function NtxQueryEaFileInternal(
[Access(FILE_READ_EA)] hFile: THandle;
out EAs: TArray<TNtxExtendedAttribute>;
ReturnSingleEntry: Boolean;
[opt] const Names: TArray<AnsiString>;
[in, opt] Index: PCardinal;
RestartScan: Boolean
): TNtxStatus;
const
INITIAL_SIZE = 80;
var
Buffer: IMemory<PFileFullEaInformation>;
xIsb: IMemory<PIoStatusBlock>;
EaList: IMemory<PFileGetEaInformation>;
EaListCursor: PFileGetEaInformation;
EaListSize: Cardinal;
i: Integer;
begin
EaListSize := 0;
EaListCursor := nil;
// Collect input names
if Length(Names) > 0 then
begin
for i := 0 to High(Names) do
Inc(EaListSize, AlignUp(SizeOf(TFileGetEaInformation) +
Length(Names[i]), 4));
IMemory(EaList) := Auto.AllocateDynamic(EaListSize);
EaListCursor := EaList.Data;
for i := 0 to High(Names) do
begin
EaListCursor.EaNameLength := Length(Names[i]);
Move(PAnsiChar(Names[i])^, EaListCursor.EaName, EaListCursor.EaNameLength);
if i < High(Names) then
begin
// Record offsets and advance to the next entry
EaListCursor.NextEntryOffset := AlignUp(SizeOf(TFileGetEaInformation) +
EaListCursor.EaNameLength, 4);
EaListCursor := Pointer(PByte(EaListCursor) +
EaListCursor.NextEntryOffset);
end;
end;
EaListCursor := EaList.Data;
end;
IMemory(xIsb) := Auto.AllocateDynamic(SizeOf(TIoStatusBlock));
IMemory(Buffer) := Auto.AllocateDynamic(INITIAL_SIZE + EaListSize);
repeat
// Query the information
Result.Location := 'NtQueryEaFile';
Result.Status := NtQueryEaFile(hFile, xIsb.Data, Buffer.Data, Buffer.Size,
ReturnSingleEntry, EaListCursor, EaListSize, Index, RestartScan);
AwaitFileOperation(Result, hFile, xIsb);
until not NtxExpandBufferEx(Result, IMemory(Buffer), Buffer.Size shl 1, nil);
if not Result.IsSuccess then
Exit;
// Collect attributes
EAs := RtlxCaptureFullEaInformation(Buffer.Data);
end;
function NtxQueryEaFile;
var
EAs: TArray<TNtxExtendedAttribute>;
begin
Result := NtxQueryEaFileInternal(hFile, EAs, True, [Name], nil, False);
if Result.IsSuccess then
EA := EAs[0];
end;
function NtxQueryEAsFile;
begin
Result := NtxQueryEaFileInternal(hFile, EAs, False, Names, nil, False);
end;
function NtxEnumerateEAsFile;
begin
Result := NtxQueryEaFileInternal(hFile, EAs, False, nil, nil, True);
end;
function NtxIterateEaFile;
var
RestartScan: Boolean;
Index: Cardinal;
begin
RestartScan := (IterationMode = eaiViaRestartScan);
Index := 1;
Result := NtxAuto.Iterate<TNtxExtendedAttribute>(Status,
function (out Entry: TNtxExtendedAttribute): TNtxStatus
var
EAs: TArray<TNtxExtendedAttribute>;
pIndex: PCardinal;
begin
// Select how we want to iterate EAs
if IterationMode = eaiByIndex then
pIndex := @Index
else
pIndex := nil;
// Retrieve one attribute
Result := NtxQueryEaFileInternal(hxFile.Handle, EAs, True, nil, pIndex,
RestartScan);
if not Result.IsSuccess then
Exit;
// Save and advance
Entry := EAs[0];
RestartScan := False;
Inc(Index);
end
);
end;
function NtxSetEAsFile;
var
ApcContext: IAnonymousIoApcContext;
xIsb: IMemory<PIoStatusBlock>;
Buffer: IMemory;
i: Integer;
Size: Cardinal;
Cursor: PFileFullEaInformation;
begin
// Caclulate the required buffer size
Size := 0;
for i := 0 to High(EAs) do
Inc(Size, AlignUp(SizeOf(TFileFullEaInformation) +
Cardinal(Length(EAs[i].Name)) + Auto.SizeOrZero(EAs[i].Value), 4));
// Write all EAs into the buffer
Buffer := Auto.AllocateDynamic(Size);
Cursor := Buffer.Data;
for i := 0 to High(EAs) do
begin
Cursor.Flags := EAs[i].Flags;
Cursor.EaNameLength := Length(EAs[i].Name);
Cursor.EaValueLength := Auto.SizeOrZero(EAs[i].Value);
Move(PAnsiChar(EAs[i].Name)^, Cursor.EaName, Cursor.EaNameLength);
if Assigned(EAs[i].Value) then
Move(EAs[i].Value.Data^,
Cursor.EaName{$R-}[Succ(Cursor.EaNameLength)]{$IFDEF R+}{$R+}{$ENDIF},
Cursor.EaValueLength);
if i < High(EAs) then
begin
// Record offsets and advance to the next entry
Cursor.NextEntryOffset := AlignUp(SizeOf(TFileFullEaInformation) +
Cardinal(Length(EAs[i].Name)) + EAs[i].Value.Size, 4);
Cursor := Pointer(PByte(Cursor) + Cursor.NextEntryOffset);
end;
end;
Result.Location := 'NtSetEaFile';
Result.LastCall.Expects<TFileAccessMask>(FILE_WRITE_EA);
Result.Status := NtSetEaFile(hFile, PrepareApcIsbEx(ApcContext, AsyncCallback,
xIsb), Buffer.Data, Buffer.Size);
if not Result.IsSuccess then
Exit;
// Keep the context alive until the callback executes
if Assigned(ApcContext) and Result.IsSuccess then
ApcContext._AddRef;
// Wait on asynchronous handles if no callback is available
if not Assigned(AsyncCallback) then