summaryrefslogtreecommitdiff
path: root/fpcsrc/rtl/nativent/sysfile.inc
blob: ca41026e4ab0d31ef3b2142504013e7ce654e55f (plain)
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
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2010 by Sven Barth

    Low leve file functions

    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.

 **********************************************************************}


{*****************************************************************************
                          Low Level File Routines
*****************************************************************************}

function do_isdevice(handle:thandle):boolean;
begin
  do_isdevice := (handle = StdInputHandle) or
                 (handle = StdOutputHandle) or
                 (handle = StdErrorHandle);
end;


procedure do_close(h : thandle);
begin
  if do_isdevice(h) then
    Exit;
  NtClose(h);
end;


procedure do_erase(p : pchar);
var
  ntstr: TNtUnicodeString;
  objattr: TObjectAttributes;
  iostatus: TIOStatusBlock;
  h: THandle;
  disp: TFileDispositionInformation;
  res: LongInt;
begin
  InoutRes := 4;
  DoDirSeparators(p);

  SysPCharToNtStr(ntstr, p, 0);
  SysInitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);

  res := NtCreateFile(@h, NT_DELETE or NT_SYNCHRONIZE, @objattr, @iostatus, Nil,
            0, FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE,
            FILE_OPEN, FILE_NON_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT,
            Nil, 0);

  if res >= 0 then begin
    disp.DeleteFile := True;

    res := NtSetInformationFile(h, @iostatus, @disp,
      SizeOf(TFileDispositionInformation), FileDispositionInformation);

    errno := res;

    NtClose(h);
  end else
  if res = STATUS_FILE_IS_A_DIRECTORY then
    errno := 2
  else
    errno := res;

  SysFreeNtStr(ntstr);
  Errno2InoutRes;
end;


procedure do_rename(p1,p2 : pchar);
var
  h: THandle;
  objattr: TObjectAttributes;
  iostatus: TIOStatusBlock;
  dest, src: TNtUnicodeString;
  renameinfo: PFileRenameInformation;
  res: LongInt;
begin
  DoDirSeparators(p1);
  DoDirSeparators(p2);

  { check whether the destination exists first }
  SysPCharToNtStr(dest, p2, 0);
  SysInitializeObjectAttributes(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 res >= 0 then begin
    { destination already exists => error }
    NtClose(h);
    errno := 5;
    Errno2InoutRes;
  end else begin
    SysPCharToNtStr(src, p1, 0);
    SysInitializeObjectAttributes(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 res >= 0 then begin
      renameinfo := GetMem(SizeOf(TFileRenameInformation) + 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(TFileRenameInformation) + dest.Length,
               FileRenameInformation);
      if res < 0 then begin
        { this could happen if src and destination reside on different drives,
          so we need to copy the file manually }
        {$message warning 'do_rename: Implement file copy!'}
        errno := res;
        Errno2InoutRes;
      end;

      NtClose(h);
    end else begin
      errno := res;
      Errno2InoutRes;
    end;

    SysFreeNtStr(src);
  end;

  SysFreeNtStr(dest);
end;


function do_write(h:thandle;addr:pointer;len : longint) : longint;
var
  res: LongInt;
  iostatus: TIoStatusBlock;
begin
  res := NtWriteFile(h, 0, Nil, Nil, @iostatus, addr, len, Nil, Nil);

  if res = STATUS_PENDING then begin
    res := NtWaitForSingleObject(h, False, Nil);
    if res >= 0 then
      res := iostatus.Status;
  end;

  if res < 0 then begin
    errno := res;
    Errno2InoutRes;
    do_write := 0;
  end else
    do_write := LongInt(iostatus.Information);
end;


function do_read(h: thandle; addr: pointer; len: longint): longint;
var
  iostatus: TIOStatusBlock;
  res: LongInt;
begin
  res := NtReadFile(h, 0, Nil, Nil, @iostatus, addr, len, Nil, Nil);

  if res = STATUS_PENDING then begin
    res := NtWaitForSingleObject(h, False, Nil);
    if res >= 0 then
      res := iostatus.Status;
  end;

  if (res < 0) and (res <> STATUS_PIPE_BROKEN) then begin
    errno := res;
    Errno2InoutRes;
    do_read := 0;
  end else
  if res = STATUS_PIPE_BROKEN then
    do_read := 0
  else
    do_read := LongInt(iostatus.Information);
end;


function do_filepos(handle : thandle) : Int64;
var
  res: LongInt;
  iostatus: TIoStatusBlock;
  position: TFilePositionInformation;
begin
  res := NtQueryInformationFile(handle, @iostatus, @position,
           SizeOf(TFilePositionInformation), FilePositionInformation);

  if res < 0 then begin
    errno := res;
    Errno2InoutRes;
    do_filepos := 0;
  end else
    do_filepos := position.CurrentByteOffset.QuadPart;
end;


procedure do_seek(handle: thandle; pos: Int64);
var
  position: TFilePositionInformation;
  iostatus: TIoStatusBlock;
  res: LongInt;
begin
  position.CurrentByteOffset.QuadPart := pos;
  res := NtSetInformationFile(handle, @iostatus, @position,
           SizeOf(TFilePositionInformation), FilePositionInformation);
  if res < 0 then begin
    errno := res;
    Errno2InoutRes;
  end;
end;


function do_seekend(handle:thandle):Int64;
var
  res: LongInt;
  standard: TFileStandardInformation;
  position: TFilePositionInformation;
  iostatus: TIoStatusBlock;
begin
  do_seekend := 0;

  res := NtQueryInformationFile(handle, @iostatus, @standard,
           SizeOf(TFileStandardInformation), FileStandardInformation);
  if res >= 0 then begin
    position.CurrentByteOffset.QuadPart := standard.EndOfFile.QuadPart;
    res := NtSetInformationFile(handle, @iostatus, @position,
             SizeOf(TFilePositionInformation), FilePositionInformation);
    if res >= 0 then
      do_seekend := position.CurrentByteOffset.QuadPart;
  end;

  if res < 0 then begin
    errno := res;
    Errno2InoutRes;
  end;
end;


function do_filesize(handle : thandle) : Int64;
var
  res: LongInt;
  iostatus: TIoStatusBlock;
  standard: TFileStandardInformation;
begin
  res := NtQueryInformationFile(handle, @iostatus, @standard,
           SizeOf(TFileStandardInformation), FileStandardInformation);
  if res >= 0 then
    do_filesize := standard.EndOfFile.QuadPart
  else begin
    errno := res;
    Errno2InoutRes;
    do_filesize := 0;
  end;
end;


procedure do_truncate (handle:thandle;pos:Int64);
var
  endoffileinfo: TFileEndOfFileInformation;
  allocinfo: TFileAllocationInformation;
  iostatus: TIoStatusBlock;
  res: LongInt;
begin
  // based on ReactOS' SetEndOfFile
  endoffileinfo.EndOfFile.QuadPart := pos;
  res := NtSetInformationFile(handle, @iostatus, @endoffileinfo,
           SizeOf(TFileEndOfFileInformation), FileEndOfFileInformation);
  if res < 0 then begin
    errno := res;
    Errno2InoutRes;
  end else begin
    allocinfo.AllocationSize.QuadPart := pos;
    res := NtSetInformationFile(handle, @iostatus, @allocinfo,
             SizeOf(TFileAllocationInformation), FileAllocationInformation);
    if res < 0 then begin
      errno := res;
      Errno2InoutRes;
    end;
  end;
end;


procedure do_open(var f;p:pchar;flags:longint);
{
  filerec and textrec have both handle and mode as the first items so
  they could use the same routine for opening/creating.
  when (flags and $100)   the file will be append
  when (flags and $1000)  the file will be truncate/rewritten
  when (flags and $10000) there is no check for close (needed for textfiles)
}
var
  shflags, cd, oflags: LongWord;
  objattr: TObjectAttributes;
  iostatus: TIoStatusBlock;
  ntstr: TNtUnicodeString;
  res: LongInt;
begin
  DoDirSeparators(p);
  { close first if opened }
  if ((flags and $10000)=0) then
   begin
     case filerec(f).mode of
       fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
       fmclosed : ;
     else
      begin
        {not assigned}
        inoutres:=102;
        exit;
      end;
     end;
   end;
  { reset file handle }
  filerec(f).handle:=UnusedHandle;
  { convert filesharing }
  shflags := 0;
  if ((filemode and fmshareExclusive) = fmshareExclusive) then
    { no sharing }
  else
    if ((filemode and $F0) = fmShareCompat) or ((filemode and fmshareDenyWrite) = fmshareDenyWrite) then
      shflags := FILE_SHARE_READ
  else
    if ((filemode and fmshareDenyRead) = fmshareDenyRead) then
      shflags := FILE_SHARE_WRITE
  else
    if ((filemode and fmshareDenyNone) = fmshareDenyNone) then
      shflags := FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE;
  { convert filemode to filerec modes }
  case (flags and 3) of
   0 : begin
         filerec(f).mode:=fminput;
         oflags := GENERIC_READ;
       end;
   1 : begin
         filerec(f).mode:=fmoutput;
         oflags := GENERIC_WRITE;
       end;
   2 : begin
         filerec(f).mode:=fminout;
         oflags := GENERIC_WRITE or GENERIC_READ;
       end;
  end;
  oflags := oflags or NT_SYNCHRONIZE or FILE_READ_ATTRIBUTES;
  { create it ? }
  if (flags and $1000) <> 0 then
    cd := FILE_OVERWRITE_IF
  { or Append/Open ? }
  else
    cd := FILE_OPEN;
  { empty name is special }
  { console i/o not supported yet }
  if p[0]=#0 then
   begin
     case FileRec(f).mode of
       fminput :
         FileRec(f).Handle:=StdInputHandle;
       fminout, { this is set by rewrite }
       fmoutput :
         FileRec(f).Handle:=StdOutputHandle;
       fmappend :
         begin
           FileRec(f).Handle:=StdOutputHandle;
           FileRec(f).mode:=fmoutput; {fool fmappend}
         end;
     end;
     exit;
   end;

  SysPCharToNtStr(ntstr, p, 0);

  SysInitializeObjectAttributes(objattr, @ntstr, OBJ_INHERIT, 0, Nil);

  res := NtCreateFile(@filerec(f).handle, oflags, @objattr, @iostatus, Nil,
           FILE_ATTRIBUTE_NORMAL, shflags, cd,
           FILE_NON_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT, Nil, 0);

  SysFreeNtStr(ntstr);

  { append mode }
  if (flags and $100 <> 0) and (res >= 0) then begin
    do_seekend(filerec(f).handle);
    filerec(f).mode := fmoutput; {fool fmappend}
  end;

  { get errors }
  if res < 0 then begin
    errno := res;
    Errno2InoutRes;
  end;
end;