-
Notifications
You must be signed in to change notification settings - Fork 9
/
fileutils.pas
314 lines (273 loc) · 7.73 KB
/
fileutils.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
unit FileUtils;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
function CopyFile(ASourceFileName, ADestFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
procedure DumpExceptionCallStack(var F: Text; E: Exception);
function LockFile(handle, start, length: LongInt): LongInt;
function OpenFileForAppend(out F: Text; AFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
function OpenFileForOverwrite(out F: File; AFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
function OpenFileForOverwrite(out F: Text; AFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
function OpenFileForRead(out F: Text; AFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
function OpenFileForReadWrite(out F: File; AFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
function ReadFile(AFileName: String; var ASL: TStringList; ATimeoutInMilliseconds: Integer): Boolean;
function RenameFile(AOldFileName, ANewFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
function TrimTopLines(AFileName: String; ALineCount, ATimeoutInMilliseconds: Integer): Boolean;
function UnLockFile(handle, start, length: LongInt): LongInt;
function WriteFile(AFileName: String; var ASL: TStringList; ATimeoutInMilliseconds: Integer): Boolean;
implementation
function CopyFile(ASourceFileName, ADestFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
var
Buffer: Array[1..1024] of Byte;
DestF, SourceF: File;
NumRead, NumWritten: LongInt;
begin
Result := false;
if ((ASourceFileName <> '') AND (ADestFileName <> '') AND (FileExists(ASourceFileName))) then
begin
if (OpenFileForReadWrite(SourceF, ASourceFileName, 2500)) then
begin
if (OpenFileForOverwrite(DestF, ADestFileName, 2500)) then
begin
Buffer[1] := 0; // Make the "does not seem to be initialized" hint go away
NumRead := 0;
NumWritten := 0;
repeat
BlockRead(SourceF, Buffer, SizeOf(Buffer), NumRead);
BlockWrite(DestF, Buffer, NumRead, NumWritten);
if (NumRead <> NumWritten) then
begin
// This shouldn't happen, but we should bail instead of retrying if it does
Exit;
end;
until (NumRead = 0);
Close(SourceF);
Close(DestF);
Result := true;
Exit;
end else
begin
Close(SourceF);
end;
end;
end;
end;
// From: http://wiki.freepascal.org/Logging_exceptions#Dump_exception_call_stack
procedure DumpExceptionCallStack(var F: Text; E: Exception);
var
I: Integer;
Frames: PPointer;
Report: string;
begin
Report := 'Program exception! ' + LineEnding +
'Stacktrace:' + LineEnding + LineEnding;
if E <> nil then begin
Report := Report + 'Exception class: ' + E.ClassName + LineEnding +
'Message: ' + E.Message + LineEnding;
end;
Report := Report + BackTraceStrFunc(ExceptAddr);
Frames := ExceptFrames;
for I := 0 to ExceptFrameCount - 1 do
Report := Report + LineEnding + BackTraceStrFunc(Frames[I]);
WriteLn(F, Report);
end;
function LockFile(handle, start, length: LongInt): LongInt;
begin
WriteLn('REEPORT FileUtils LockFile'); Halt;
Result := 0;
end;
function OpenFileForAppend(out F: Text; AFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
var
I: Integer;
begin
Result := false;
// TODOX Race condition
if (FileExists(AFileName)) then
begin
for I := 1 to ATimeoutInMilliseconds div 100 do
begin
Assign(F, AFileName);
{$I-}Append(F);{$I+}
if (IOResult = 0) then
begin
Result := true;
Exit;
end;
Sleep(100); // Wait 1/10th of a second before retrying
end;
end else
begin
Result := OpenFileForOverwrite(F, AFileName, ATimeoutInMilliseconds);
end;
end;
function OpenFileForOverwrite(out F: File; AFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
var
I: Integer;
begin
Result := false;
for I := 1 to ATimeoutInMilliseconds div 100 do
begin
Assign(F, AFileName);
{$I-}ReWrite(F, 1);{$I+}
if (IOResult = 0) then
begin
Result := true;
Exit;
end;
Sleep(100); // Wait 1/10th of a second before retrying
end;
end;
function OpenFileForOverwrite(out F: Text; AFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
var
I: Integer;
begin
Result := false;
for I := 1 to ATimeoutInMilliseconds div 100 do
begin
Assign(F, AFileName);
{$I-}ReWrite(F);{$I+}
if (IOResult = 0) then
begin
Result := true;
Exit;
end;
Sleep(100); // Wait 1/10th of a second before retrying
end;
end;
function OpenFileForRead(out F: Text; AFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
var
I: Integer;
begin
Result := false;
for I := 1 to ATimeoutInMilliseconds div 100 do
begin
Assign(F, AFileName);
{$I-}Reset(F);{$I+}
if (IOResult = 0) then
begin
Result := true;
Exit;
end;
Sleep(100); // Wait 1/10th of a second before retrying
end;
end;
function OpenFileForReadWrite(out F: File; AFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
var
I: Integer;
begin
Result := false;
// TODOX Race condition
if (FileExists(AFileName)) then
begin
for I := 1 to ATimeoutInMilliseconds div 100 do
begin
Assign(F, AFileName);
{$I-}Reset(F, 1);{$I+}
if (IOResult = 0) then
begin
Result := true;
Exit;
end;
Sleep(100); // Wait 1/10th of a second before retrying
end;
end else
begin
Result := OpenFileForOverwrite(F, AFileName, ATimeoutInMilliseconds);
end;
end;
function ReadFile(AFileName: String; var ASL: TStringList; ATimeoutInMilliseconds: Integer): Boolean;
var
I: Integer;
begin
Result := false;
for I := 1 to ATimeoutInMilliseconds div 100 do
begin
try
ASL.LoadFromFile(AFileName);
Result := true;
Exit;
except
on E: Exception do
begin
Sleep(100); // Wait 1/10th of a second before retrying
end;
end;
end;
end;
function RenameFile(AOldFileName, ANewFileName: String; ATimeoutInMilliseconds: Integer): Boolean;
var
I: Integer;
begin
Result := false;
if ((AOldFileName <> '') AND (ANewFileName <> '') AND (FileExists(AOldFileName))) then
begin
for I := 1 to ATimeoutInMilliseconds div 100 do
begin
if (SysUtils.RenameFile(AOldFileName, ANewFileName)) then
begin
Result := true;
Exit;
end;
Sleep(100); // Wait 1/10th of a second before retrying
end;
end;
end;
function TrimTopLines(AFileName: String; ALineCount, ATimeoutInMilliseconds: Integer): Boolean;
var
I: Integer;
SL: TStringList;
begin
Result := false;
if (FileExists(AFileName)) then
begin
for I := 1 to ATimeoutInMilliseconds div 100 do
begin
try
SL := TStringList.Create;
SL.LoadFromFile(AFileName);
if (SL.Count > ALineCount) then
begin
while (SL.Count > ALineCount) do
begin
SL.Delete(0);
end;
SL.SaveToFile(AFileName);
SL.Free;
Result := true;
Exit;
end;
except
on E: Exception do
begin
Sleep(100); // Wait 1/10th of a second before retrying
end;
end;
end;
end;
end;
function UnLockFile(handle, start, length: LongInt): LongInt;
begin
WriteLn('REEPORT FileUtils UnLockFile'); Halt;
Result := 0;
end;
function WriteFile(AFileName: String; var ASL: TStringList; ATimeoutInMilliseconds: Integer): Boolean;
var
I: Integer;
begin
Result := false;
for I := 1 to ATimeoutInMilliseconds div 100 do
begin
try
ASL.SaveToFile(AFileName);
Result := true;
Exit;
except
on E: Exception do
begin
Sleep(100); // Wait 1/10th of a second before retrying
end;
end;
end;
end;
end.