[go: up one dir, main page]

Menu

[r19]: / source / ogfile.pas  Maximize  Restore  History

Download this file

359 lines (312 with data), 10.3 kB

  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
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is TurboPower OnGuard
*
* The Initial Developer of the Original Code is
* TurboPower Software
*
* Portions created by the Initial Developer are Copyright (C) 1996-2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Andrew Haines andrew@haines.name {AH.01}
* conversion to CLX {AH.01}
* December 30, 2003 {AH.01}
*
* ***** END LICENSE BLOCK ***** *)
{*********************************************************}
{* OGFILE.PAS 1.15 *}
{* Copyright (c) 1996-02 TurboPower Software Co *}
{* All rights reserved. *}
{*********************************************************}
{$I onguard.inc}
unit ogfile;
interface
uses
{$IFDEF Win16} WinTypes, WinProcs, {$ENDIF}
{$IFDEF Win32} Windows, {$ENDIF}
{$IFDEF KYLIX} Libc, {$ENDIF} {AH.01}
{$IFDEF FPC}{$IFDEF UNIX} BaseUnix, {$ENDIF}{$ENDIF} {BB.01}
{$IFDEF UsingCLX} Types, {$ENDIF} {AH.01}
{$IFDEF MACOS}Posix.Base, Posix.SysStat, Posix.Fcntl, Posix.Stdio, {$ENDIF}
SysUtils,
OgUtil;
function GetFileSize(Handle : THandle) : Cardinal;
{$IFDEF Win16}
function LockFile(Handle : THandle; FileOffsetLow, FileOffsetHigh,
LockCountLow, LockCountHigh : Word) : Boolean;
function UnlockFile(Handle : THandle; FileOffsetLow, FileOffsetHigh,
UnLockCountLow, UnLockCountHigh : Word) : Boolean;
function FlushFileBuffers(Handle : THandle) : Boolean;
{$ENDIF}
{$IFDEF KYLIX}
function LockFile(Handle : THandle; FileOffsetLow, FileOffsetHigh,
LockCountLow, LockCountHigh : Word) : Boolean;
function UnlockFile(Handle : THandle; FileOffsetLow, FileOffsetHigh,
UnLockCountLow, UnLockCountHigh : Word) : Boolean;
function FlushFileBuffers(Handle : THandle) : Boolean;
{$ENDIF}
{$IFDEF FPC}
{$IFNDEF MSWINDOWS}
function LockFile(hFile: THandle; dwFileOffsetLow, dwFileOffsetHigh: DWORD; nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh: DWORD): Boolean;
function UnlockFile(hFile: THandle; dwFileOffsetLow, dwFileOffsetHigh: DWORD; nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh: DWORD): Boolean;
function FlushFileBuffers(Handle : THandle) : Boolean;
{$ENDIF}
{$ENDIF}
{$IFDEF DELPHI}
{$IFDEF MACOS}
function LockFile(Handle : THandle; FileOffsetLow, FileOffsetHigh,
LockCountLow, LockCountHigh : Word) : Boolean;
function UnlockFile(Handle : THandle; FileOffsetLow, FileOffsetHigh,
UnLockCountLow, UnLockCountHigh : Word) : Boolean;
function FlushFileBuffers(Handle : THandle) : Boolean;
{$ENDIF}
{$ENDIF}
implementation
{$IFDEF Win32}
function GetFileSize(Handle : THandle) : Cardinal;
begin
Result := Windows.GetFileSize(Handle, nil);
end;
{$ENDIF}
{$IFDEF Win64}
function GetFileSize(Handle : THandle) : Cardinal;
begin
Result := Windows.GetFileSize(Handle, nil);
end;
{$ENDIF}
{$IFDEF Win16}
function GetFileSize(Handle : THandle) : Cardinal;
var
Save : LongInt;
begin
Save := FileSeek(Handle, 0, 0); {save current file position}
Result := FileSeek(Handle, 0, 2); {get file size}
FileSeek(Handle, Save, 0); {restore previous position}
end;
function LockFile(Handle : THandle;
FileOffsetLow, FileOffsetHigh,
LockCountLow, LockCountHigh : Word) : Boolean;
var
Error : Word;
begin
asm
mov ax,$5C00
mov bx,Handle
mov cx,FileOffsetHigh
mov dx,FileOffsetLow
mov si,LockCountHigh
mov di,LockCountLow
int $21
jc @@001
xor ax,ax
@@001:
mov Error,ax
end;
Result := Error = 0;
end;
function UnlockFile(Handle : THandle;
FileOffsetLow, FileOffsetHigh,
UnLockCountLow, UnLockCountHigh : Word) : Boolean;
var
Error : Word;
begin
asm
mov ax, $5C01
mov bx,Handle
mov cx,FileOffsetHigh
mov dx,FileOffsetLow
mov si,UnLockCountHigh
mov di,UnLockCountLow
int $21
jc @@001
xor ax, ax
@@001:
mov Error, ax
end;
Result := Error = 0;
end;
function FlushFileBuffers(Handle : THandle) : Boolean;
var
Error : Word;
begin
asm
mov ah, $68
mov bx, Handle
int $21
jc @@001
xor ax, ax
@@001:
mov Error, ax
end;
Result := Error = 0;
end;
{$ENDIF}
{$IFDEF KYLIX}
function GetFileSize(Handle : THandle) : Cardinal;
var
Save : LongInt;
begin
Save := FileSeek(Handle, 0, 0); {save current file position}
Result := FileSeek(Handle, 0, 2); {get file size}
FileSeek(Handle, Save, 0); {restore previous position}
end;
function LockFile(Handle : THandle;
FileOffsetLow, FileOffsetHigh,
LockCountLow, LockCountHigh : Word) : Boolean;
var
LockVar : TFlock;
begin
LockVar.l_whence := SEEK_SET;
LockVar.l_start := FileOffSetHigh;
LockVar.l_start := (LockVar.l_start shl 16) + FileOffSetLow;
LockVar.l_len := LockCountHigh;
LockVar.l_len := (LockVar.l_len shl 16) + LockCountLow;
LockVar.l_type := F_WRLCK;
if fcntl(Handle, F_SETLK, LockVar) = 0 then Result := True else Result := False;
end;
function UnlockFile(Handle : THandle;
FileOffsetLow, FileOffsetHigh,
UnLockCountLow, UnLockCountHigh : Word) : Boolean;
var
LockVar : TFlock;
begin
LockVar.l_whence := SEEK_SET;
LockVar.l_start := FileOffSetHigh;
LockVar.l_start := (LockVar.l_start shl 16) + FileOffSetLow;
LockVar.l_len := UnLockCountHigh;
LockVar.l_len := (LockVar.l_len shl 16) + UnLockCountLow;
LockVar.l_type := F_UNLCK;
if fcntl(Handle, F_SETLK, LockVar) = 0 then Result := True else Result := False;
end;
function FlushFileBuffers(Handle : THandle) : Boolean;
begin
Result := True;
end;
{$ENDIF}
{$IFDEF FPC}
{$IFNDEF MSWINDOWS}
const
F_RDLCK = 0;
F_WRLCK = 1;
F_UNLCK = 2;
F_EXLCK = 4;
F_SHLCK = 8;
LOCK_SH = 1;
LOCK_EX = 2;
LOCK_NB = 4;
LOCK_UN = 8;
LOCK_MAND = 32;
LOCK_READ = 64;
LOCK_WRITE = 128;
LOCK_RW = 192;
EACCES = ESysEACCES;
EAGAIN = ESysEAGAIN;
function GetLastError: Integer;
begin
Result := FpGetErrno;
end;
procedure SetLastError(Value: Integer);
begin
FpSetErrno(Value);
end;
function GetFileSize(Handle : THandle) : Cardinal;
var
Save : LongInt;
begin
Save := FileSeek(Handle, 0, 0); {save current file position}
Result := FileSeek(Handle, 0, 2); {get file size}
FileSeek(Handle, Save, 0); {restore previous position}
end;
function LockFile(hFile: THandle; dwFileOffsetLow, dwFileOffsetHigh: DWORD; nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh: DWORD): Boolean;
var
FLockInfo: {$ifdef FPC}BaseUnix.FLock{$else}TFLock{$endif};
FLastError: Cardinal;
begin
Result := false;
FLockInfo.l_type := F_WRLCK;
FLockInfo.l_whence := SEEK_SET;
FLockInfo.l_start := dwFileOffsetLow;
FLockInfo.l_len := nNumberOfBytesToLockLow;
FLockInfo.l_pid := {$ifdef FPC}fpgetpid{$else}getpid{$endif}();
Result := {$ifdef FPC}fpfcntl{$else}fcntl{$endif}(hFile, F_SETLK, FLockInfo) <> -1;
if not Result then
begin
FLastError := GetLastError();
if (FLastError = EACCES) or (FLastError = EAGAIN) then
SetLastError(ESysEACCES)
else
Result := True; // If errno is ENOLCK or EINVAL
end;
end;
function UnlockFile(hFile: THandle; dwFileOffsetLow, dwFileOffsetHigh: DWORD; nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh: DWORD): Boolean;
var
FLockInfo: {$ifdef FPC}BaseUnix.FLock{$else}TFLock{$endif};
begin
FLockInfo.l_type := F_UNLCK;
FLockInfo.l_whence := SEEK_SET;
FLockInfo.l_start := dwFileOffsetLow;
FLockInfo.l_len := nNumberOfBytesToUnLockLow;
FLockInfo.l_pid := {$ifdef FPC}fpgetpid{$else}getpid{$endif}();
Result := {$ifdef FPC}fpfcntl{$else}fcntl{$endif}(hFile, F_SETLK, FLockInfo) <> -1;
end;
function FlushFileBuffers(Handle : THandle) : Boolean;
begin
Result := True;
end;
{$ENDIF}
{$ENDIF}
{$IFDEF DELPHI}
{$IFDEF MACOS}
function GetFileSize(Handle : THandle) : Cardinal;
var
stat: _stat;
begin
if fstat(Handle, stat) = 0 then
Result := stat.st_size;
end;
function LockFile(Handle : THandle; FileOffsetLow, FileOffsetHigh,
LockCountLow, LockCountHigh : Word) : Boolean;
var
LockVar : flock;
begin
LockVar.l_whence := SEEK_SET;
LockVar.l_start := FileOffSetHigh;
LockVar.l_start := (LockVar.l_start shl 16) + FileOffSetLow;
LockVar.l_len := LockCountHigh;
LockVar.l_len := (LockVar.l_len shl 16) + LockCountLow;
LockVar.l_type := F_WRLCK;
if fcntl(Handle, F_SETLK, LockVar) = 0 then Result := True else Result := False;
end;
function UnlockFile(Handle : THandle; FileOffsetLow, FileOffsetHigh,
UnLockCountLow, UnLockCountHigh : Word) : Boolean;
var
LockVar : flock;
begin
LockVar.l_whence := SEEK_SET;
LockVar.l_start := FileOffSetHigh;
LockVar.l_start := (LockVar.l_start shl 16) + FileOffSetLow;
LockVar.l_len := UnLockCountHigh;
LockVar.l_len := (LockVar.l_len shl 16) + UnLockCountLow;
LockVar.l_type := F_UNLCK;
if fcntl(Handle, F_SETLK, LockVar) = 0 then Result := True else Result := False;
end;
function FlushFileBuffers(Handle : THandle) : Boolean;
begin
Result := True;
end;
{$ENDIF}
{$ENDIF}
end.