-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPLM.Files.pas
355 lines (312 loc) · 9.38 KB
/
JPLM.Files.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
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
unit JPLM.Files;
{
Jacek Pazera
http://www.pazera-software.com
}
{$mode objfpc}{$H+}
{$WARN 5044 off : Symbol "$1" is not portable}
interface
uses
SysUtils,// Classes,
MFPC.Classes.Streams, dateutils,
{$IFDEF MSWINDOWS} Windows, {$ENDIF}
{$IFDEF LINUX} BaseUnix, {$ENDIF}
JPL.Strings;
type
TFileDates = record
Creation: TDateTime;
LastWrite: TDateTime;
LastAccess: TDateTime;
end;
TFileAttrs = record
Attrs: LongInt;
Hidden: Boolean;
System: Boolean;
ReadOnly: Boolean;
Archive: Boolean;
Directory: Boolean;
SymLink: Boolean;
Compressed: Boolean;
Encrypted: Boolean;
end;
TFileInfoRec = record
FullFileName: string;
Directory: string;
// directory + path delimiter
Path: string;
ShortFileName: string;
BaseFileName: string;
Extension: string;
AttrsOK: Boolean;
FileAttrs: TFileAttrs;
StatOK: Boolean;
DeviceNo: QWord;
InodeNo: Cardinal;
FileMode: Cardinal;
HardLinks: QWord;
OwnerUserID: Cardinal;
OwnerGroupID: Cardinal;
Size: Int64;
BlockSize: Int64;
Blocks: Int64;
CreationTime: TDateTime;
LastWriteTime: TDateTime;
LastAccessTime: TDateTime;
//ReadOnly: Boolean;
end;
procedure ClearFileInfoRec(var fir: TFileInfoRec);
function GetFileInfoRec(const FileName: string; out fir: TFileInfoRec; bOnlyNames: Boolean = False): Boolean;
function FileSizeInt(const FileName: string): int64;
function TryGetFileLastWriteTime(const FileName: string; out LastWriteTime: TDateTime): Boolean;
{$IFDEF MSWINDOWS}
function FileGetCreationTime(const FileName: string): TDateTime;
function FileGetTimes(const FileName: string; out CreationTime, LastAccessTime, LastWriteTime: TDateTime): Boolean;
function FileAttributesToStr(const Attrs: LongInt; NotSetStr: string = '-'; bExtendedInfo: Boolean = False): string;
{$ENDIF}
implementation
{$IFDEF MSWINDOWS}
// From DSiWin32.pas by Primož Gabrijelčič: https://github.com/gabr42/OmniThreadLibrary/tree/master/src
function DSiFileSize(const fileName: string): int64;
var
fHandle: THandle;
begin
fHandle := CreateFile(
PChar(fileName), 0,
FILE_SHARE_READ OR FILE_SHARE_WRITE OR FILE_SHARE_DELETE, nil, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0
);
if fHandle = INVALID_HANDLE_VALUE then Result := -1
else
try
Int64Rec(Result).Lo := GetFileSize(fHandle, @Int64Rec(Result).Hi);
finally
CloseHandle(fHandle);
end;
end;
{$ENDIF}
function _FileSizeInt(const FileName: string): int64;
var
fs: TM_FileStream;
begin
Result := 0;
if not FileExists(FileName) then Exit;
fs := TM_FileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
try
Result := fs.Size;
finally
fs.Free;
end;
end;
function FileSizeInt(const FileName: string): int64;
{$IFNDEF MSWINDOWS}
var
fir: TFileInfoRec;
{$ENDIF}
begin
Result := 0;
try
Result := _FileSizeInt(FileName);
except
on E: Exception do
try
{$IFDEF MSWINDOWS}
Result := DSiFileSize(FileName);
{$ELSE}
if not GetFileInfoRec(FileName, fir, False) then Exit(-1);
Result := fir.Size;
{$ENDIF}
except
Result := -1;
end;
end;
end;
{$IFDEF MSWINDOWS}
// http://forum.lazarus.freepascal.org/index.php/topic,6705.0.html
function FileGetCreationTime(const FileName: string): TDateTime;
var
SearchRec: TSearchRec;
SysTime: SYSTEMTIME;
FileTime: TFILETIME;
begin
if FindFirst(FileName, faAnyFile, SearchRec) = 0 then
begin
FileTimeToLocalFileTime(SearchRec.FindData.ftCreationTime, FileTime);
FileTimeToSystemTime(FileTime, SysTime);
Result := SystemTimeToDateTime(SysTime);
Result := UniversalTimeToLocal(Result);
end
else Result := 0;
end;
function FileGetTimes(const FileName: string; out CreationTime, LastAccessTime, LastWriteTime: TDateTime): Boolean;
var
SearchRec: TSearchRec;
SysTime: SYSTEMTIME;
FileTime: TFILETIME;
begin
if FindFirst(FileName, faAnyFile, SearchRec) = 0 then
begin
FileTimeToLocalFileTime(SearchRec.FindData.ftCreationTime, FileTime);
FileTimeToSystemTime(FileTime, SysTime);
CreationTime := SystemTimeToDateTime(SysTime); //DONE: bad dates
CreationTime := UniversalTimeToLocal(CreationTime);
FileTimeToLocalFileTime(SearchRec.FindData.ftLastAccessTime, FileTime);
FileTimeToSystemTime(FileTime, SysTime);
LastAccessTime := SystemTimeToDateTime(SysTime);
LastAccessTime := UniversalTimeToLocal(LastAccessTime);
FileTimeToLocalFileTime(SearchRec.FindData.ftLastWriteTime, FileTime);
FileTimeToSystemTime(FileTime, SysTime);
LastWriteTime := SystemTimeToDateTime(SysTime);
LastWriteTime := UniversalTimeToLocal(LastWriteTime);
Result := True;
end
else Result := False;
end;
function FileAttributesToStr(const Attrs: LongInt; NotSetStr: string = '-'; bExtendedInfo: Boolean = False): string;
begin
{
faReadOnly = $00000001;
faHidden = $00000002 platform;
faSysFile = $00000004 platform;
faVolumeId = $00000008 platform deprecated;
faDirectory = $00000010;
faArchive = $00000020;
faNormal = $00000080;
faTemporary = $00000100 platform;
faSymLink = $00000400 platform;
faCompressed = $00000800 platform;
faEncrypted = $00004000 platform;
faVirtual = $00010000 platform;
faAnyFile = $000001FF;
}
Result := '';
if (Attrs and faHidden) <> 0 then Result := Result + 'H' else Result := Result + NotSetStr;
if (Attrs and faSysFile) <> 0 then Result := Result + 'S' else Result := Result + NotSetStr;
if (Attrs and faReadOnly) <> 0 then Result := Result + 'R' else Result := Result + NotSetStr;
if (Attrs and faArchive) <> 0 then Result := Result + 'A' else Result := Result + NotSetStr;
if bExtendedInfo then
begin
if (Attrs and faSymLink) <> 0 then Result := Result + 'L' else Result := Result + NotSetStr;
if (Attrs and faCompressed) <> 0 then Result := Result + 'C' else Result := Result + NotSetStr;
if (Attrs and faEncrypted) <> 0 then Result := Result + 'E' else Result := Result + NotSetStr;
end;
end;
{$ENDIF}
function TryGetFileLastWriteTime(const FileName: string; out LastWriteTime: TDateTime): Boolean;
var
{$IFDEF MSWINDOWS}
SearchRec: TSearchRec;
SysTime: SYSTEMTIME;
FileTime: TFILETIME;
{$ELSE}
st: BaseUnix.stat;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
if FindFirst(FileName, faAnyFile, SearchRec) = 0 then
begin
FileTimeToLocalFileTime(SearchRec.FindData.ftLastWriteTime, FileTime);
FileTimeToSystemTime(FileTime, SysTime);
LastWriteTime := UniversalTimeToLocal(SystemTimeToDateTime(SysTime));
Result := True;
end
else Result := False;
{$ELSE}
if FpStat(FileName, st{%H-}) = 0 then
begin
LastWriteTime := UniversalTimeToLocal(FileDateToDateTime(st.st_mtime));
Result := True;
end
else Result := False;
{$ENDIF}
end;
procedure ClearFileInfoRec(var fir: TFileInfoRec);
begin
fir.FullFileName := '';
fir.Directory := '';
fir.Path := '';
fir.ShortFileName := '';
fir.BaseFileName := '';
fir.Extension := '';
fir.AttrsOK := False;
FillChar(fir.FileAttrs, SizeOf(fir.FileAttrs), 0);
fir.FileAttrs.Attrs := -1;
fir.StatOK := False;
fir.DeviceNo := 0;
fir.InodeNo := 0;
fir.FileMode := 0;
fir.HardLinks := 0;
fir.OwnerUserID := 0;
fir.OwnerGroupID := 0;
fir.Size := 0;
fir.BlockSize := 0;
fir.Blocks := 0;
fir.CreationTime := 0;
fir.LastWriteTime := 0;
fir.LastAccessTime := 0;
//fir.ReadOnly := True;
end;
function GetFileInfoRec(const FileName: string; out fir: TFileInfoRec; bOnlyNames: Boolean = False): Boolean;
var
{$IFDEF LINUX}
st: BaseUnix.stat;
{$ENDIF}
{$IFDEF MSWINDOWS}
tc, ta, tw: TDateTime;
{$ENDIF}
begin
Result := False;
if not FileExists(FileName) then Exit;
ClearFileInfoRec(fir{%H-});
fir.FullFileName := ExpandFileName(FileName);
fir.Directory := ExtractFileDir(fir.FullFileName);
fir.Path := ExtractFilePath(fir.FullFileName);
fir.ShortFileName := ExtractFileName(FileName);
fir.BaseFileName := ChangeFileExt(fir.ShortFileName, '');
fir.Extension := GetFileExt(FileName, True);
if bOnlyNames then Exit(True);
{$IFDEF LINUX}
fir.FileAttrs.ReadOnly := FileIsReadOnly(fir.FullFileName);
fir.AttrsOK := True;
{$ELSE}
fir.FileAttrs.Attrs := SysUtils.FileGetAttr(FileName);
if fir.FileAttrs.Attrs < 0 then fir.AttrsOK := False
else
begin
fir.AttrsOK := True;
fir.FileAttrs.System := (fir.FileAttrs.Attrs and faSysFile) <> 0;
fir.FileAttrs.Hidden := (fir.FileAttrs.Attrs and faHidden) <> 0;
fir.FileAttrs.ReadOnly := (fir.FileAttrs.Attrs and faReadOnly) <> 0;
fir.FileAttrs.Archive := (fir.FileAttrs.Attrs and faArchive) <> 0;
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
fir.Size := FileSizeInt(FileName);
if FileGetTimes(FileName, tc, ta, tw) then
begin
fir.CreationTime := tc;
fir.LastAccessTime := ta;
fir.LastWriteTime := tw;
end;
{$ENDIF}
{$IFDEF LINUX}
if FpStat(FileName, st{%H-}) = 0 then
begin
fir.StatOK := True;
fir.DeviceNo := st.st_dev;
fir.InodeNo := st.st_ino;
fir.FileMode := st.st_mode;
fir.HardLinks := st.st_nlink;
fir.OwnerUserID := st.st_uid;
fir.OwnerGroupID := st.st_gid;
fir.Size := st.st_size;
fir.BlockSize := st.st_blksize;
fir.Blocks := st.st_blocks;
fir.CreationTime := UniversalTimeToLocal( FileDateToDateTime(st.st_ctime) );
fir.LastAccessTime := UniversalTimeToLocal( FileDateToDateTime(st.st_atime) );
fir.LastWriteTime := UniversalTimeToLocal( FileDateToDateTime(st.st_mtime) );
end
else fir.StatOK := False;
{$ENDIF}
Result := True;
end;
end.