-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfileutils.pas
More file actions
259 lines (240 loc) · 7.33 KB
/
fileutils.pas
File metadata and controls
259 lines (240 loc) · 7.33 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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit fileutils;
interface
uses
baseunix;
type
TFileData = record
Start: Pointer;
Length: size_t; // QWord, not Int64
procedure Destroy();
end;
function ReadFile(const FileName: AnsiString): TFileData; // This is efficient
function ReadFileTail(const FileName: AnsiString; Length: QWord; out EndOffset: QWord): TFileData; // This is efficient
function ReadFilePart(const FileName: AnsiString; Length, EndOffset: QWord): TFileData; // This is efficient
function ReadTextFile(const FileName: AnsiString): UTF8String; // This is not
procedure WriteFile(const FileName: AnsiString; const FileData: TFileData);
procedure WriteTextFile(const FileName: AnsiString; const Data: UTF8String);
function IsEmptyDirectory(const Path: AnsiString): Boolean;
procedure DeleteDirectoryRecursively(const Path: AnsiString);
implementation
uses
exceptions, sysutils;
function ReadFile(const FileName: AnsiString): TFileData;
var
FileDescriptor: CInt;
StatInfo: Stat;
MapResult: Pointer;
begin
FileDescriptor := fpOpen(FileName, O_RDONLY);
if (FileDescriptor < 0) then
raise EKernelError.Create(fpGetErrNo);
if (fpFStat(FileDescriptor, StatInfo) <> 0) then // $DFA- for StatInfo
raise EKernelError.Create(fpGetErrNo);
try
if (StatInfo.st_size = 0) then
begin
Result.Length := 0;
Result.Start := nil;
exit;
end;
MapResult := fpMMap(nil, StatInfo.st_size, PROT_READ, MAP_PRIVATE, FileDescriptor, 0); // $R-
if (MapResult = MAP_FAILED) then
raise EKernelError.Create(fpGetErrNo);
finally
fpClose(FileDescriptor);
end;
Result.Length := StatInfo.st_size; // $R-
Result.Start := Pointer(MapResult);
end;
function ReadFileTail(const FileName: AnsiString; Length: QWord; out EndOffset: QWord): TFileData;
var
FileDescriptor: CInt;
StatInfo: Stat;
MapResult: Pointer;
Offset: Off_T;
begin
FileDescriptor := fpOpen(FileName, O_RDONLY);
if (FileDescriptor < 0) then
raise EKernelError.Create(fpGetErrNo);
if (fpFStat(FileDescriptor, StatInfo) <> 0) then // $DFA- for StatInfo
raise EKernelError.Create(fpGetErrNo);
try
if (StatInfo.st_size = 0) then
begin
Result.Length := 0;
Result.Start := nil;
EndOffset := 0;
exit;
end;
if (StatInfo.st_size < Length) then
begin
Length := StatInfo.st_size; // $R-
EndOffset := Length;
Offset := 0;
end
else
begin
EndOffset := StatInfo.st_size; // $R- (st_size is a cLong)
Offset := StatInfo.st_size - Length;
end;
MapResult := fpMMap(nil, Length, PROT_READ, MAP_PRIVATE, FileDescriptor, Offset); // $R-
if (MapResult = MAP_FAILED) then
raise EKernelError.Create(fpGetErrNo);
finally
fpClose(FileDescriptor);
end;
Result.Length := Length; // $R-
Result.Start := Pointer(MapResult);
end;
function ReadFilePart(const FileName: AnsiString; Length, EndOffset: QWord): TFileData;
var
FileDescriptor: CInt;
StatInfo: Stat;
MapResult: Pointer;
Offset: Off_T;
begin
FileDescriptor := fpOpen(FileName, O_RDONLY);
if (FileDescriptor < 0) then
raise EKernelError.Create(fpGetErrNo);
if (fpFStat(FileDescriptor, StatInfo) <> 0) then // $DFA- for StatInfo
raise EKernelError.Create(fpGetErrNo);
try
if (StatInfo.st_size < EndOffset) then
begin
raise Exception.Create('File is shorter that requested end offset.');
end;
if (StatInfo.st_size = 0) then
begin
Result.Length := 0;
Result.Start := nil;
exit;
end;
if (EndOffset < Length) then
begin
Length := EndOffset;
Offset := 0;
end
else
begin
Offset := EndOffset - Length; // $R-
end;
MapResult := fpMMap(nil, Length, PROT_READ, MAP_PRIVATE, FileDescriptor, Offset); // $R-
if (MapResult = MAP_FAILED) then
raise EKernelError.Create(fpGetErrNo);
finally
fpClose(FileDescriptor);
end;
Result.Length := Length; // $R-
Result.Start := Pointer(MapResult);
end;
procedure TFileData.Destroy();
begin
if ((Length > 0) and (fpMUnMap(Start, Length) <> 0)) then
raise EKernelError.Create(fpGetErrNo);
end;
function ReadTextFile(const FileName: AnsiString): UTF8String;
var
Source: TFileData;
begin
Source := ReadFile(FileName);
if (Source.Length > High(Integer)) then
raise Exception.Create('text file too big');
SetLength(Result, Source.Length); // {BOGUS Hint: Function result variable of a managed type does not seem to be initialized}
Move(Source.Start^, Result[1], Source.Length); // $R-
Source.Destroy();
end;
procedure WriteFile(const FileName: AnsiString; const FileData: TFileData);
var
FileDescriptor: CInt;
Written: Int64;
Buffer: PAnsiChar;
Remaining, SegmentSize: size_t;
begin
FileDescriptor := fpOpen(FileName, O_CREAT or O_TRUNC or O_WRONLY);
if (FileDescriptor < 0) then
raise EKernelError.Create(fpGetErrNo);
try
Buffer := FileData.Start;
Remaining := FileData.Length;
while (Remaining > 0) do
begin
SegmentSize := Remaining;
if (SegmentSize > High(Written)) then
SegmentSize := High(Written);
Written := fpWrite(FileDescriptor, Buffer, SegmentSize);
if (Written < 0) then
raise EKernelError.Create(fpGetErrNo);
if (Written <> SegmentSize) then
raise Exception.Create('could not write entire file');
Inc(Buffer, Written);
Dec(Remaining, Written);
end;
finally
fpClose(FileDescriptor);
end;
end;
procedure WriteTextFile(const FileName: AnsiString; const Data: UTF8String);
var
F: Text;
begin
Assign(F, FileName);
Rewrite(F);
Write(F, Data);
Close(F);
end;
function IsEmptyDirectory(const Path: AnsiString): Boolean;
var
FileRecord: TSearchRec;
GotOneDot, GotTwoDots, GotOther: Boolean;
begin
if (DirectoryExists(Path)) then
begin
GotOneDot := False;
GotTwoDots := False;
GotOther := False;
if (FindFirst(Path + '/*', faDirectory, FileRecord) = 0) then
repeat
if (FileRecord.Name = '.') then
GotOneDot := True
else
if (FileRecord.Name = '..') then
GotTwoDots := True
else
begin
GotOther := True;
break;
end;
until (FindNext(FileRecord) <> 0);
Result := GotOneDot and GotTwoDots and not GotOther;
FindClose(FileRecord);
end
else
Result := False;
end;
procedure DeleteDirectoryRecursively(const Path: AnsiString);
var
FileEntry: TRawbyteSearchRec;
begin
Assert(Length(Path) > 1, 'Path is empty');
Assert(Path[Length(Path)] = '/', 'Path does not end with a slash');
if (FindFirst(Path + '*', faAnyFile, FileEntry) = 0) then
begin
repeat
if ((FileEntry.Name = '.') or (FileEntry.Name = '..')) then
continue;
if ((FileEntry.Attr and faDirectory) > 0) then
begin
DeleteDirectoryRecursively(Path + FileEntry.Name + '/');
end
else
begin
DeleteFile(Path + FileEntry.Name);
end;
until FindNext(FileEntry) <> 0;
FindClose(FileEntry);
end;
RemoveDir(Path);
end;
end.