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
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2010 by Sven Barth
member of the Free Pascal development team
Sysutils unit for NativeNT
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit sysutils;
interface
{$MODE objfpc}
{$MODESWITCH OUT}
{ force ansistrings }
{$H+}
uses
ndk;
{$DEFINE HAS_SLEEP}
{$DEFINE HAS_CREATEGUID}
type
TNativeNTFindData = record
SearchSpec: String;
NamePos: LongInt;
Handle: THandle;
IsDirObj: Boolean;
SearchAttr: LongInt;
Context: ULONG;
LastRes: NTSTATUS;
end;
{ Include platform independent interface part }
{$i sysutilh.inc}
implementation
uses
sysconst, ndkutils;
{$DEFINE FPC_NOGENERICANSIROUTINES}
{ Include platform independent implementation part }
{$i sysutils.inc}
{****************************************************************************
File Functions
****************************************************************************}
function FileOpen(const FileName : string; Mode : Integer) : THandle;
const
AccessMode: array[0..2] of ACCESS_MASK = (
GENERIC_READ,
GENERIC_WRITE,
GENERIC_READ or GENERIC_WRITE);
ShareMode: array[0..4] of ULONG = (
0,
0,
FILE_SHARE_READ,
FILE_SHARE_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE);
var
ntstr: UNICODE_STRING;
objattr: OBJECT_ATTRIBUTES;
iostatus: IO_STATUS_BLOCK;
begin
AnsiStrToNtStr(FileName, ntstr);
InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
NtCreateFile(@Result, AccessMode[Mode and 3] or NT_SYNCHRONIZE, @objattr,
@iostatus, Nil, FILE_ATTRIBUTE_NORMAL, ShareMode[(Mode and $F0) shr 4],
FILE_OPEN, FILE_NON_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT, Nil, 0);
FreeNtStr(ntstr);
end;
function FileCreate(const FileName : String) : THandle;
begin
FileCreate := FileCreate(FileName, fmShareDenyNone, 0);
end;
function FileCreate(const FileName : String; Rights: longint) : THandle;
begin
FileCreate := FileCreate(FileName, fmShareDenyNone, Rights);
end;
function FileCreate(const FileName : String; ShareMode : longint; Rights: longint) : THandle;
const
ShareModeFlags: array[0..4] of ULONG = (
0,
0,
FILE_SHARE_READ,
FILE_SHARE_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE);
var
ntstr: UNICODE_STRING;
objattr: OBJECT_ATTRIBUTES;
iostatus: IO_STATUS_BLOCK;
res: NTSTATUS;
begin
AnsiStrToNTStr(FileName, ntstr);
InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
NtCreateFile(@Result, GENERIC_READ or GENERIC_WRITE or NT_SYNCHRONIZE,
@objattr, @iostatus, Nil, FILE_ATTRIBUTE_NORMAL,
ShareModeFlags[(ShareMode and $F0) shr 4], FILE_OVERWRITE_IF,
FILE_NON_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT, Nil, 0);
FreeNtStr(ntstr);
end;
function FileRead(Handle : THandle; out Buffer; Count : longint) : Longint;
var
iostatus: IO_STATUS_BLOCK;
res: NTSTATUS;
begin
res := NtReadFile(Handle, 0, Nil, Nil, @iostatus, @Buffer, Count, Nil, Nil);
if res = STATUS_PENDING then begin
res := NtWaitForSingleObject(Handle, False, Nil);
if NT_SUCCESS(res) then
res := iostatus.union1.Status;
end;
if NT_SUCCESS(res) then
Result := LongInt(iostatus.Information)
else
Result := -1;
end;
function FileWrite(Handle : THandle; const Buffer; Count : Longint) : Longint;
var
iostatus: IO_STATUS_BLOCK;
res: NTSTATUS;
begin
res := NtWriteFile(Handle, 0, Nil, Nil, @iostatus, @Buffer, Count, Nil,
Nil);
if res = STATUS_PENDING then begin
res := NtWaitForSingleObject(Handle, False, Nil);
if NT_SUCCESS(res) then
res := iostatus.union1.Status;
end;
if NT_SUCCESS(res) then
Result := LongInt(iostatus.Information)
else
Result := -1;
end;
function FileSeek(Handle : THandle;FOffset,Origin : Longint) : Longint;
begin
Result := longint(FileSeek(Handle, Int64(FOffset), Origin));
end;
function FileSeek(Handle : THandle; FOffset: Int64; Origin: Longint) : Int64;
const
ErrorCode = $FFFFFFFFFFFFFFFF;
var
position: FILE_POSITION_INFORMATION;
standard: FILE_STANDARD_INFORMATION;
iostatus: IO_STATUS_BLOCK;
res: NTSTATUS;
begin
{ determine the new position }
case Origin of
fsFromBeginning:
position.CurrentByteOffset.QuadPart := FOffset;
fsFromCurrent: begin
res := NtQueryInformationFile(Handle, @iostatus, @position,
SizeOf(FILE_POSITION_INFORMATION), FilePositionInformation);
if res < 0 then begin
Result := ErrorCode;
Exit;
end;
position.CurrentByteOffset.QuadPart :=
position.CurrentByteOffset.QuadPart + FOffset;
end;
fsFromEnd: begin
res := NtQueryInformationFile(Handle, @iostatus, @standard,
SizeOf(FILE_STANDARD_INFORMATION), FileStandardInformation);
if res < 0 then begin
Result := ErrorCode;
Exit;
end;
position.CurrentByteOffset.QuadPart := standard.EndOfFile.QuadPart +
FOffset;
end;
else begin
Result := ErrorCode;
Exit;
end;
end;
{ set the new position }
res := NtSetInformationFile(Handle, @iostatus, @position,
SizeOf(FILE_POSITION_INFORMATION), FilePositionInformation);
if res < 0 then
Result := ErrorCode
else
Result := position.CurrentByteOffset.QuadPart;
end;
procedure FileClose(Handle : THandle);
begin
NtClose(Handle);
end;
function FileTruncate(Handle : THandle;Size: Int64) : boolean;
var
endoffileinfo: FILE_END_OF_FILE_INFORMATION;
allocinfo: FILE_ALLOCATION_INFORMATION;
iostatus: IO_STATUS_BLOCK;
res: NTSTATUS;
begin
// based on ReactOS' SetEndOfFile
endoffileinfo.EndOfFile.QuadPart := Size;
res := NtSetInformationFile(Handle, @iostatus, @endoffileinfo,
SizeOf(FILE_END_OF_FILE_INFORMATION), FileEndOfFileInformation);
if NT_SUCCESS(res) then begin
allocinfo.AllocationSize.QuadPart := Size;
res := NtSetInformationFile(handle, @iostatus, @allocinfo,
SizeOf(FILE_ALLOCATION_INFORMATION), FileAllocationInformation);
Result := NT_SUCCESS(res);
end else
Result := False;
end;
function NTToDosTime(const NtTime: LARGE_INTEGER): LongInt;
var
userdata: PKUSER_SHARED_DATA;
local, bias: LARGE_INTEGER;
fields: TIME_FIELDS;
zs: LongInt;
begin
userdata := SharedUserData;
repeat
bias.u.HighPart := userdata^.TimeZoneBias.High1Time;
bias.u.LowPart := userdata^.TimeZoneBias.LowPart;
until bias.u.HighPart = userdata^.TimeZoneBias.High2Time;
local.QuadPart := NtTime.QuadPart - bias.QuadPart;
RtlTimeToTimeFields(@local, @fields);
{ from objpas\datutil.inc\DateTimeToDosDateTime }
Result := - 1980;
Result := Result + fields.Year and 127;
Result := Result shl 4;
Result := Result + fields.Month;
Result := Result shl 5;
Result := Result + fields.Day;
Result := Result shl 16;
zs := fields.Hour;
zs := zs shl 6;
zs := zs + fields.Minute;
zs := zs shl 5;
zs := zs + fields.Second div 2;
Result := Result + (zs and $ffff);
end;
function DosToNtTime(aDTime: LongInt; var aNtTime: LARGE_INTEGER): Boolean;
var
fields: TIME_FIELDS;
local, bias: LARGE_INTEGER;
userdata: PKUSER_SHARED_DATA;
begin
{ from objpas\datutil.inc\DosDateTimeToDateTime }
fields.Second := (aDTime and 31) * 2;
aDTime := aDTime shr 5;
fields.Minute := aDTime and 63;
aDTime := aDTime shr 6;
fields.Hour := aDTime and 31;
aDTime := aDTime shr 5;
fields.Day := aDTime and 31;
aDTime := aDTime shr 5;
fields.Month := aDTime and 15;
aDTime := aDTime shr 4;
fields.Year := aDTime + 1980;
Result := RtlTimeFieldsToTime(@fields, @local);
if not Result then
Exit;
userdata := SharedUserData;
repeat
bias.u.HighPart := userdata^.TimeZoneBias.High1Time;
bias.u.LowPart := userdata^.TimeZoneBias.LowPart;
until bias.u.HighPart = userdata^.TimeZoneBias.High2Time;
aNtTime.QuadPart := local.QuadPart + bias.QuadPart;
end;
function FileAge(const FileName: String): Longint;
begin
{ TODO }
Result := -1;
end;
function FileExists(const FileName: String): Boolean;
var
ntstr: UNICODE_STRING;
objattr: OBJECT_ATTRIBUTES;
res: NTSTATUS;
iostatus: IO_STATUS_BLOCK;
h: THandle;
begin
AnsiStrToNtStr(FileName, ntstr);
InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
res := NtOpenFile(@h, FILE_READ_ATTRIBUTES or NT_SYNCHRONIZE, @objattr,
@iostatus, FILE_SHARE_READ or FILE_SHARE_WRITE,
FILE_NON_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT);
Result := NT_SUCCESS(res);
if Result then
NtClose(h);
FreeNtStr(ntstr);
end;
function DirectoryExists(const Directory : String) : Boolean;
var
ntstr: UNICODE_STRING;
objattr: OBJECT_ATTRIBUTES;
res: NTSTATUS;
iostatus: IO_STATUS_BLOCK;
h: THandle;
begin
AnsiStrToNtStr(Directory, ntstr);
InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
{ first test wether this is a object directory }
res := NtOpenDirectoryObject(@h, DIRECTORY_QUERY, @objattr);
if NT_SUCCESS(res) then
Result := True
else begin
if res = STATUS_OBJECT_TYPE_MISMATCH then begin
{ this is a file object! }
res := NtOpenFile(@h, FILE_READ_ATTRIBUTES or NT_SYNCHRONIZE, @objattr,
@iostatus, FILE_SHARE_READ or FILE_SHARE_WRITE,
FILE_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT);
Result := NT_SUCCESS(res);
end else
Result := False;
end;
if Result then
NtClose(h);
FreeNtStr(ntstr);
end;
{ copied from rtl/unix/sysutils.pp }
Function FNMatch(const Pattern,Name:string):Boolean;
Var
LenPat,LenName : longint;
Function DoFNMatch(i,j:longint):Boolean;
Var
Found : boolean;
Begin
Found:=true;
While Found and (i<=LenPat) Do
Begin
Case Pattern[i] of
'?' : Found:=(j<=LenName);
'*' : Begin
{find the next character in pattern, different of ? and *}
while Found do
begin
inc(i);
if i>LenPat then Break;
case Pattern[i] of
'*' : ;
'?' : begin
if j>LenName then begin DoFNMatch:=false; Exit; end;
inc(j);
end;
else
Found:=false;
end;
end;
Assert((i>LenPat) or ( (Pattern[i]<>'*') and (Pattern[i]<>'?') ));
{Now, find in name the character which i points to, if the * or ?
wasn't the last character in the pattern, else, use up all the
chars in name}
Found:=false;
if (i<=LenPat) then
begin
repeat
{find a letter (not only first !) which maches pattern[i]}
while (j<=LenName) and (name[j]<>pattern[i]) do
inc (j);
if (j<LenName) then
begin
if DoFnMatch(i+1,j+1) then
begin
i:=LenPat;
j:=LenName;{we can stop}
Found:=true;
Break;
end else
inc(j);{We didn't find one, need to look further}
end else
if j=LenName then
begin
Found:=true;
Break;
end;
{ This 'until' condition must be j>LenName, not j>=LenName.
That's because when we 'need to look further' and
j = LenName then loop must not terminate. }
until (j>LenName);
end else
begin
j:=LenName;{we can stop}
Found:=true;
end;
end;
else {not a wildcard character in pattern}
Found:=(j<=LenName) and (pattern[i]=name[j]);
end;
inc(i);
inc(j);
end;
DoFnMatch:=Found and (j>LenName);
end;
Begin {start FNMatch}
LenPat:=Length(Pattern);
LenName:=Length(Name);
FNMatch:=DoFNMatch(1,1);
End;
function FindGetFileInfo(const s: String; var f: TSearchRec): Boolean;
var
ntstr: UNICODE_STRING;
objattr: OBJECT_ATTRIBUTES;
res: NTSTATUS;
h: THandle;
iostatus: IO_STATUS_BLOCK;
attr: LongInt;
filename: String;
isfileobj: Boolean;
buf: array of Byte;
objinfo: OBJECT_BASIC_INFORMATION;
fileinfo: FILE_BASIC_INFORMATION;
time: LongInt;
begin
AnsiStrToNtStr(s, ntstr);
InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
filename := ExtractFileName(s);
{ TODO : handle symlinks }
{ If Assigned(F.FindHandle) and ((((PUnixFindData(f.FindHandle)^.searchattr)) and faSymlink) > 0) then
FindGetFileInfo:=(fplstat(pointer(s),st)=0)
else
FindGetFileInfo:=(fpstat(pointer(s),st)=0);}
attr := 0;
Result := False;
if (faDirectory and f.FindData.SearchAttr <> 0) and
((filename = '.') or (filename = '..')) then begin
attr := faDirectory;
res := STATUS_SUCCESS;
end else
res := STATUS_INVALID_PARAMETER;
isfileobj := False;
if not NT_SUCCESS(res) then begin
{ first check whether it's a directory }
res := NtOpenDirectoryObject(@h, DIRECTORY_QUERY, @objattr);
if not NT_SUCCESS(res) then
if res = STATUS_OBJECT_TYPE_MISMATCH then begin
res := NtOpenFile(@h, FILE_READ_ATTRIBUTES or NT_SYNCHRONIZE, @objattr,
@iostatus, FILE_SHARE_READ or FILE_SHARE_WRITE,
FILE_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT);
isfileobj := NT_SUCCESS(res);
end;
if NT_SUCCESS(res) then
attr := faDirectory;
end;
if not NT_SUCCESS(res) then begin
{ first try whether we have a file object }
res := NtOpenFile(@h, FILE_READ_ATTRIBUTES or NT_SYNCHRONIZE, @objattr,
@iostatus, FILE_SHARE_READ or FILE_SHARE_WRITE,
FILE_NON_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT);
isfileobj := NT_SUCCESS(res);
if res = STATUS_OBJECT_TYPE_MISMATCH then begin
{ is this an object? }
res := NtOpenFile(@h, FILE_READ_ATTRIBUTES or NT_SYNCHRONIZE, @objattr,
@iostatus, FILE_SHARE_READ or FILE_SHARE_WRITE,
FILE_SYNCHRONOUS_IO_NONALERT);
if (res = STATUS_OBJECT_TYPE_MISMATCH)
and (f.FindData.SearchAttr and faSysFile <> 0) then begin
{ this is some other system file like an event or port, so we can only
provide it's name }
res := STATUS_SUCCESS;
attr := faSysFile;
end;
end;
end;
FreeNtStr(ntstr);
if not NT_SUCCESS(res) then
Exit;
time := 0;
if isfileobj then begin
res := NtQueryInformationFile(h, @iostatus, @fileinfo, SizeOf(fileinfo),
FileBasicInformation);
if NT_SUCCESS(res) then begin
time := NtToDosTime(fileinfo.LastWriteTime);
{ copy file attributes? }
end;
end else begin
res := NtQueryObject(h, ObjectBasicInformation, @objinfo, SizeOf(objinfo),
Nil);
if NT_SUCCESS(res) then begin
time := NtToDosTime(objinfo.CreateTime);
{ what about attributes? }
end;
end;
if (attr and not f.FindData.SearchAttr) = 0 then begin
f.Name := filename;
f.Attr := attr;
f.Size := 0;
{$ifndef FPUNONE}
if time = 0 then
{ for now we use "Now" as a fall back; ideally this should be the system
start time }
f.Time := DateTimeToFileDate(Now)
else
f.Time := time;
{$endif}
Result := True;
end else
Result := False;
NtClose(h);
end;
procedure FindClose(var F: TSearchrec);
begin
if f.FindData.Handle <> 0 then
NtClose(f.FindData.Handle);
end;
function FindNext(var Rslt: TSearchRec): LongInt;
{
re-opens dir if not already in array and calls FindGetFileInfo
}
Var
DirName : String;
FName,
SName : string;
Found,
Finished : boolean;
ntstr: UNICODE_STRING;
objattr: OBJECT_ATTRIBUTES;
buf: array of WideChar;
len: LongWord;
res: NTSTATUS;
i: LongInt;
dirinfo: POBJECT_DIRECTORY_INFORMATION;
filedirinfo: PFILE_DIRECTORY_INFORMATION;
pc: PChar;
name: AnsiString;
iostatus: IO_STATUS_BLOCK;
begin
{ TODO : relative directories }
Result := -1;
{ SearchSpec='' means that there were no wild cards, so only one file to
find.
}
if Rslt.FindData.SearchSpec = '' then
Exit;
{ relative directories not supported for now }
if Rslt.FindData.NamePos = 0 then
Exit;
if Rslt.FindData.Handle = 0 then begin
if Rslt.FindData.NamePos > 1 then
name := Copy(Rslt.FindData.SearchSpec, 1, Rslt.FindData.NamePos - 1)
else
if Rslt.FindData.NamePos = 1 then
name := Copy(Rslt.FindData.SearchSpec, 1, 1)
else
name := Rslt.FindData.SearchSpec;
AnsiStrToNtStr(name, ntstr);
InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
res := NtOpenDirectoryObject(@Rslt.FindData.Handle,
DIRECTORY_QUERY or DIRECTORY_TRAVERSE, @objattr);
if not NT_SUCCESS(res) then begin
if res = STATUS_OBJECT_TYPE_MISMATCH then
res := NtOpenFile(@Rslt.FindData.Handle,
FILE_LIST_DIRECTORY or NT_SYNCHRONIZE, @objattr,
@iostatus, FILE_SHARE_READ or FILE_SHARE_WRITE,
FILE_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT);
end else
Rslt.FindData.IsDirObj := True;
FreeNTStr(ntstr);
if not NT_SUCCESS(res) then
Exit;
end;
{ if (NTFindData^.SearchType = 0) and
(NTFindData^.Dirptr = Nil) then
begin
If NTFindData^.NamePos = 0 Then
DirName:='./'
Else
DirName:=Copy(NTFindData^.SearchSpec,1,NTFindData^.NamePos);
NTFindData^.DirPtr := fpopendir(Pchar(pointer(DirName)));
end;}
SName := Copy(Rslt.FindData.SearchSpec, Rslt.FindData.NamePos + 1,
Length(Rslt.FindData.SearchSpec));
Found := False;
Finished := not NT_SUCCESS(Rslt.FindData.LastRes)
or (Rslt.FindData.LastRes = STATUS_NO_MORE_ENTRIES);
SetLength(buf, 200);
dirinfo := @buf[0];
filedirinfo := @buf[0];
while not Finished do begin
if Rslt.FindData.IsDirObj then
res := NtQueryDirectoryObject(Rslt.FindData.Handle, @buf[0],
Length(buf) * SizeOf(buf[0]), True, False,
@Rslt.FindData.Context, @len)
else
res := NtQueryDirectoryFile(Rslt.FindData.Handle, 0, Nil, Nil, @iostatus,
@buf[0], Length(buf) * SizeOf(buf[0]), FileDirectoryInformation,
True, Nil, False);
if Rslt.FindData.IsDirObj then begin
Finished := (res = STATUS_NO_MORE_ENTRIES)
or (res = STATUS_NO_MORE_FILES)
or not NT_SUCCESS(res);
Rslt.FindData.LastRes := res;
if dirinfo^.Name.Length > 0 then begin
SetLength(FName, dirinfo^.Name.Length div 2);
pc := PChar(FName);
for i := 0 to dirinfo^.Name.Length div 2 - 1 do begin
if dirinfo^.Name.Buffer[i] < #256 then
pc^ := AnsiChar(Byte(dirinfo^.Name.Buffer[i]))
else
pc^ := '?';
pc := pc + 1;
end;
{$ifdef debug_findnext}
Write(FName, ' (');
for i := 0 to dirinfo^.TypeName.Length div 2 - 1 do
if dirinfo^.TypeName.Buffer[i] < #256 then
Write(AnsiChar(Byte(dirinfo^.TypeName.Buffer[i])))
else
Write('?');
Writeln(')');
{$endif debug_findnext}
end else
FName := '';
end else begin
SetLength(FName, filedirinfo^.FileNameLength div 2);
pc := PChar(FName);
for i := 0 to filedirinfo^.FileNameLength div 2 - 1 do begin
if filedirinfo^.FileName[i] < #256 then
pc^ := AnsiChar(Byte(filedirinfo^.FileName[i]))
else
pc^ := '?';
pc := pc + 1;
end;
end;
if FName = '' then
Finished := True
else begin
if FNMatch(SName, FName) then begin
Found := FindGetFileInfo(Copy(Rslt.FindData.SearchSpec, 1,
Rslt.FindData.NamePos) + FName, Rslt);
if Found then begin
Result := 0;
Exit;
end;
end;
end;
end;
end;
function FindFirst(const Path: String; Attr: Longint; out Rslt: TSearchRec): Longint;
{
opens dir and calls FindNext if needed.
}
Begin
Result := -1;
FillChar(Rslt, SizeOf(Rslt), 0);
if Path = '' then
Exit;
Rslt.FindData.SearchAttr := Attr;
{Wildcards?}
if (Pos('?', Path) = 0) and (Pos('*', Path) = 0) then begin
if FindGetFileInfo(Path, Rslt) then
Result := 0;
end else begin
{Create Info}
Rslt.FindData.SearchSpec := Path;
Rslt.FindData.NamePos := Length(Rslt.FindData.SearchSpec);
while (Rslt.FindData.NamePos > 0)
and (Rslt.FindData.SearchSpec[Rslt.FindData.NamePos] <> DirectorySeparator)
do
Dec(Rslt.FindData.NamePos);
Result := FindNext(Rslt);
end;
if Result <> 0 then
FindClose(Rslt);
end;
function FileGetDate(Handle: THandle): Longint;
var
res: NTSTATUS;
basic: FILE_BASIC_INFORMATION;
iostatus: IO_STATUS_BLOCK;
begin
res := NtQueryInformationFile(Handle, @iostatus, @basic,
SizeOf(FILE_BASIC_INFORMATION), FileBasicInformation);
if NT_SUCCESS(res) then
Result := NtToDosTime(basic.LastWriteTime)
else
Result := -1;
end;
function FileSetDate(Handle: THandle;Age: Longint): Longint;
var
res: NTSTATUS;
basic: FILE_BASIC_INFORMATION;
iostatus: IO_STATUS_BLOCK;
begin
res := NtQueryInformationFile(Handle, @iostatus, @basic,
SizeOf(FILE_BASIC_INFORMATION), FileBasicInformation);
if NT_SUCCESS(res) then begin
if not DosToNtTime(Age, basic.LastWriteTime) then begin
Result := -1;
Exit;
end;
res := NtSetInformationFile(Handle, @iostatus, @basic,
SizeOf(FILE_BASIC_INFORMATION), FileBasicInformation);
if NT_SUCCESS(res) then
Result := 0
else
Result := res;
end else
Result := res;
end;
function FileGetAttr(const FileName: String): Longint;
var
objattr: OBJECT_ATTRIBUTES;
info: FILE_NETWORK_OPEN_INFORMATION;
res: NTSTATUS;
ntstr: UNICODE_STRING;
begin
AnsiStrToNtStr(FileName, ntstr);
InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
res := NtQueryFullAttributesFile(@objattr, @info);
if NT_SUCCESS(res) then
Result := info.FileAttributes
else
Result := 0;
FreeNtStr(ntstr);
end;
function FileSetAttr(const Filename: String; Attr: LongInt): Longint;
var
h: THandle;
objattr: OBJECT_ATTRIBUTES;
ntstr: UNICODE_STRING;
basic: FILE_BASIC_INFORMATION;
res: NTSTATUS;
iostatus: IO_STATUS_BLOCK;
begin
AnsiStrToNtStr(Filename, ntstr);
InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
res := NtOpenFile(@h,
NT_SYNCHRONIZE or FILE_READ_ATTRIBUTES or FILE_WRITE_ATTRIBUTES,
@objattr, @iostatus,
FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE,
FILE_SYNCHRONOUS_IO_NONALERT);
FreeNtStr(ntstr);
if NT_SUCCESS(res) then begin
res := NtQueryInformationFile(h, @iostatus, @basic,
SizeOf(FILE_BASIC_INFORMATION), FileBasicInformation);
if NT_SUCCESS(res) then begin
basic.FileAttributes := Attr;
Result := NtSetInformationFile(h, @iostatus, @basic,
SizeOf(FILE_BASIC_INFORMATION), FileBasicInformation);
end;
NtClose(h);
end else
Result := res;
end;
function DeleteFile(const FileName: String): Boolean;
var
h: THandle;
objattr: OBJECT_ATTRIBUTES;
ntstr: UNICODE_STRING;
dispinfo: FILE_DISPOSITION_INFORMATION;
res: NTSTATUS;
iostatus: IO_STATUS_BLOCK;
begin
AnsiStrToNtStr(Filename, ntstr);
InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
res := NtOpenFile(@h, NT_DELETE, @objattr, @iostatus,
FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE,
FILE_NON_DIRECTORY_FILE);
FreeNtStr(ntstr);
if NT_SUCCESS(res) then begin
dispinfo.DeleteFile := True;
res := NtSetInformationFile(h, @iostatus, @dispinfo,
SizeOf(FILE_DISPOSITION_INFORMATION), FileDispositionInformation);
Result := NT_SUCCESS(res);
NtClose(h);
end else
Result := False;
end;
function RenameFile(const OldName, NewName: String): Boolean;
var
h: THandle;
objattr: OBJECT_ATTRIBUTES;
iostatus: IO_STATUS_BLOCK;
dest, src: UNICODE_STRING;
renameinfo: PFILE_RENAME_INFORMATION;
res: LongInt;
begin
{ check whether the destination exists first }
AnsiStrToNtStr(NewName, dest);
InitializeObjectAttributes(objattr, @dest, 0, 0, Nil);
res := NtCreateFile(@h, 0, @objattr, @iostatus, Nil, 0,
FILE_SHARE_READ or FILE_SHARE_WRITE, FILE_OPEN,
FILE_NON_DIRECTORY_FILE, Nil, 0);
if NT_SUCCESS(res) then begin
{ destination already exists => error }
NtClose(h);
Result := False;
end else begin
AnsiStrToNtStr(OldName, src);
InitializeObjectAttributes(objattr, @src, 0, 0, Nil);
res := NtCreateFile(@h,
GENERIC_ALL or NT_SYNCHRONIZE or FILE_READ_ATTRIBUTES,
@objattr, @iostatus, Nil, 0, FILE_SHARE_READ or FILE_SHARE_WRITE,
FILE_OPEN, FILE_OPEN_FOR_BACKUP_INTENT or FILE_OPEN_REMOTE_INSTANCE
or FILE_NON_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT, Nil,
0);
if NT_SUCCESS(res) then begin
renameinfo := GetMem(SizeOf(FILE_RENAME_INFORMATION) + dest.Length);
with renameinfo^ do begin
ReplaceIfExists := False;
RootDirectory := 0;
FileNameLength := dest.Length;
Move(dest.Buffer^, renameinfo^.FileName, dest.Length);
end;
res := NtSetInformationFile(h, @iostatus, renameinfo,
SizeOf(FILE_RENAME_INFORMATION) + dest.Length,
FileRenameInformation);
if not NT_SUCCESS(res) then begin
{ this could happen if src and destination reside on different drives,
so we need to copy the file manually }
{$message warning 'RenameFile: Implement file copy!'}
Result := False;
end else
Result := True;
NtClose(h);
end else
Result := False;
FreeNtStr(src);
end;
FreeNtStr(dest);
end;
{****************************************************************************
Disk Functions
****************************************************************************}
function diskfree(drive: byte): int64;
begin
{ here the mount manager needs to be queried }
Result := -1;
end;
function disksize(drive: byte): int64;
begin
{ here the mount manager needs to be queried }
Result := -1;
end;
function GetCurrentDir: String;
begin
GetDir(0, result);
end;
function SetCurrentDir(const NewDir: String): Boolean;
begin
{$I-}
ChDir(NewDir);
{$I+}
Result := IOResult = 0;
end;
function CreateDir(const NewDir: String): Boolean;
begin
{$I-}
MkDir(NewDir);
{$I+}
Result := IOResult = 0;
end;
function RemoveDir(const Dir: String): Boolean;
begin
{$I-}
RmDir(Dir);
{$I+}
Result := IOResult = 0;
end;
{****************************************************************************
Time Functions
****************************************************************************}
procedure GetLocalTime(var SystemTime: TSystemTime);
var
bias, syst: LARGE_INTEGER;
fields: TIME_FIELDS;
userdata: PKUSER_SHARED_DATA;
begin
// get UTC time
userdata := SharedUserData;
repeat
syst.u.HighPart := userdata^.SystemTime.High1Time;
syst.u.LowPart := userdata^.SystemTime.LowPart;
until syst.u.HighPart = userdata^.SystemTime.High2Time;
// adjust to local time
repeat
bias.u.HighPart := userdata^.TimeZoneBias.High1Time;
bias.u.LowPart := userdata^.TimeZoneBias.LowPart;
until bias.u.HighPart = userdata^.TimeZoneBias.High2Time;
syst.QuadPart := syst.QuadPart - bias.QuadPart;
RtlTimeToTimeFields(@syst, @fields);
SystemTime.Year := fields.Year;
SystemTime.Month := fields.Month;
SystemTime.Day := fields.Day;
SystemTime.Hour := fields.Hour;
SystemTime.Minute := fields.Minute;
SystemTime.Second := fields.Second;
SystemTime.Millisecond := fields.MilliSeconds;
end;
{****************************************************************************
Misc Functions
****************************************************************************}
procedure sysbeep;
begin
{ empty }
end;
procedure InitInternational;
begin
InitInternationalGeneric;
end;
{****************************************************************************
Target Dependent
****************************************************************************}
function SysErrorMessage(ErrorCode: Integer): String;
begin
Result := 'NT error code: 0x' + IntToHex(ErrorCode, 8);
end;
{****************************************************************************
Initialization code
****************************************************************************}
function wstrlen(p: PWideChar): SizeInt; external name 'FPC_PWIDECHAR_LENGTH';
function GetEnvironmentVariable(const EnvVar: String): String;
var
s : string;
i : longint;
hp: pwidechar;
len: sizeint;
begin
{ TODO : test once I know how to execute processes }
Result:='';
hp:=PPEB(CurrentPEB)^.ProcessParameters^.Environment;
while hp^<>#0 do
begin
len:=UnicodeToUTF8(Nil, hp, 0);
SetLength(s,len);
UnicodeToUTF8(PChar(s), hp, len);
//s:=strpas(hp);
i:=pos('=',s);
if uppercase(copy(s,1,i-1))=upcase(envvar) then
begin
Result:=copy(s,i+1,length(s)-i);
break;
end;
{ next string entry}
hp:=hp+wstrlen(hp)+1;
end;
end;
function GetEnvironmentVariableCount: Integer;
var
hp : pwidechar;
begin
Result:=0;
hp:=PPEB(CurrentPEB)^.ProcessParameters^.Environment;
If (Hp<>Nil) then
while hp^<>#0 do
begin
Inc(Result);
hp:=hp+wstrlen(hp)+1;
end;
end;
function GetEnvironmentString(Index: Integer): String;
var
hp : pwidechar;
len: sizeint;
begin
Result:='';
hp:=PPEB(CurrentPEB)^.ProcessParameters^.Environment;
If (Hp<>Nil) then
begin
while (hp^<>#0) and (Index>1) do
begin
Dec(Index);
hp:=hp+wstrlen(hp)+1;
end;
If (hp^<>#0) then
begin
len:=UnicodeToUTF8(Nil, hp, 0);
SetLength(Result, len);
UnicodeToUTF8(PChar(Result), hp, len);
end;
end;
end;
function ExecuteProcess(const Path: AnsiString; const ComLine: AnsiString;
Flags: TExecuteFlags = []): Integer;
begin
{ TODO : implement }
Result := 0;
end;
function ExecuteProcess(const Path: AnsiString;
const ComLine: Array of AnsiString; Flags:TExecuteFlags = []): Integer;
var
CommandLine: AnsiString;
I: integer;
begin
Commandline := '';
for I := 0 to High (ComLine) do
if Pos (' ', ComLine [I]) <> 0 then
CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
else
CommandLine := CommandLine + ' ' + Comline [I];
ExecuteProcess := ExecuteProcess (Path, CommandLine,Flags);
end;
procedure Sleep(Milliseconds: Cardinal);
const
DelayFactor = 10000;
var
interval: LARGE_INTEGER;
begin
interval.QuadPart := - Milliseconds * DelayFactor;
NtDelayExecution(False, @interval);
end;
{****************************************************************************
Initialization code
****************************************************************************}
initialization
InitExceptions; { Initialize exceptions. OS independent }
InitInternational; { Initialize internationalization settings }
OnBeep := @SysBeep;
finalization
DoneExceptions;
end.
|