-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemObjects.pas
More file actions
556 lines (508 loc) · 13.6 KB
/
FileSystemObjects.pas
File metadata and controls
556 lines (508 loc) · 13.6 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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
{
Project: FindDupFar http://code.google.com/p/findupfar/
Author: Alexey Suhinin http://x-alexey.narod.ru
License: GPL v.2
}
unit FileSystemObjects;
{$I PRJDefines.inc}
interface
uses
Classes,
{$IFDEF MD5}
MD5,
{$ENDIF}
{$IFDEF CRC32}
CRC32,
{$ENDIF}
Windows,
SysUtils;
type
{Âîçìîæíûå ôëàãè îáúåêòà TFileSystemObject:
flDeleted - îáúåêò óäàë¸í;
flOpenFalse - ïîïûòêà îòêðûòü ôàéë íà ÷òåíèå (äëÿ ïðîñ÷åòà êîíòðîëüíîé ñóììû)
íåóäàëàñü;
flMD5Calc, flCRC32Calc - óñòàíîâëåí, åñëè ñîîòâåòñòâóþùàÿ êîíòðîëüíàÿ ñóììà
óæå ñ÷èòàëàñü è õðàíèòñÿ â TFileObject
}
TFileSystemFlags = set of (flDeleted, flOpenFalse, flMD5Calc, flCRC32Calc);
TCompareFlags = set of (cfSize, cfHash);
TGroupFlags = set of (gfDeleted, gfSort);
TFileSystemObject = class;
TFileSystemObjectList = class(TList)
private
FFlags: TGroupFlags;
protected
function GetItems(Index: Integer): TFileSystemObject;
procedure SetItems(Index: Integer; Item: TFileSystemObject);
public
constructor Create;
// destructor Destroy; override;
function Add(aObject: TFileSystemObject): Integer;
{Èäåíòè÷åí ëè îáúåêò óæå ñîäåðæàùèìñÿ â êîíòåéíåðå? (ñðàâíåíèå ñ ïåðâûì ýëåìåíòîì â êîíòåéíåðå)}
function IsDuplicate(aObject: TFileSystemObject): Boolean;
{Ñðàâíåíèå êîíòåéíåðîâ: ñíà÷àëà ñîðòèðóåì ôàéëû ïî ðàçìåðó,
çàòåì öèêëè÷åñêè ñðàâíèâàåì
!!!à ÷òî åñëè 2 ôàéëà ñ îäèíàêîâûì ðàçìåðîì??? - îøèáêà!!!}
function Compare(aObject: TFileSystemObjectList; aFlags: TCompareFlags): Integer;
procedure Clear; override;
procedure Delete(Index: Integer);
{ñîðòèðóåò ýëåìåíòû ïî ðàçìåðó}
procedure Sort;
// procedure SaveToFile(aFileStream: TFileStream);
property Items[Index: Integer]: TFileSystemObject read GetItems write SetItems; default;
property Flags: TGroupFlags read FFlags write FFlags;
end;
TFileSystemObject = class
private
FRefCount: Byte; {êîëè÷åñòâî ññûëîê íà îáúåêò}
FFileAttributes: DWORD;
FCreationTime: TFileTime;
FLastAccessTime: TFileTime;
FLastWriteTime: TFileTime;
FSize: INT64;
FFileName: PChar;
FFlags: TFileSystemFlags;
function CompInt(aInt1, aInt2: Int64): Integer;
procedure SetFileSystemRecord(const aFileSystemRecord: TWin32FindData);
public
constructor Create(const aFileSystemRecord: TWin32FindData);
destructor Destroy; override;
procedure IncRef;
procedure DecRef;
procedure Free;{ virtual;}
function Compare(aObject: TFileSystemObject; aFlags: TCompareFlags): Integer; virtual; abstract;
{ïðîâåðêà: ñóùåñòâóåò ëè åùå ôàéë, è îáíîâëåíèå åãî äàííûõ}
{$IFDEF VALIDATE}
function IsValid: Boolean; (*virtual; abstract;*)
{$ENDIF}
procedure GetFileSystemRecord(var aFileSystemRecord
{$IFDEF CONTROL_TYPE}: TWin32FindData{$ENDIF});
// property FileAttributes: DWORD read FFileAttributes;
// property FileName: PChar read FFileName;
property Flags: TFileSystemFlags read FFlags write FFlags;
end;
TFileObject = class(TFileSystemObject)
private
{$IFDEF MD5}
FMD5: TMD5Digest;
function CalculateMD5: Boolean;
{$ENDIF}
{$IFDEF CRC32}
FCRC32: LongWord;
function CalculateCRC32: Boolean;
{$ENDIF}
public
function Compare(aObject: TFileSystemObject; aFlags: TCompareFlags): Integer; override;
(*
{$IFDEF VALIDATE}
function IsValid: Boolean; override;
{$ENDIF}
*)
end;
TDirectoryObject = class(TFileSystemObject)
private
FDirectoryList: TFileSystemObjectList;
public
constructor Create(aFileSystemRecord: TWin32FindData);
destructor Destroy; override;
function Compare(aObject: TFileSystemObject; aFlags: TCompareFlags): Integer; override;
(*
{$IFDEF VALIDATE}
function IsValid: Boolean; override;
{$ENDIF}
*)
procedure AddMember(aObject: TFileSystemObject);
// procedure Free; override;
end;
implementation
{ðåàëèçàöèÿ TFileSystemObjectList}
constructor TFileSystemObjectList.Create;
begin
inherited Create;
FFlags:=[];
end;
{
destructor TFileSystemObjectList.Destroy;
begin
inherited Destroy;
end;
}
function TFileSystemObjectList.GetItems(Index: Integer): TFileSystemObject;
begin
Result:=TFileSystemObject(inherited Items[Index]);
end;
procedure TFileSystemObjectList.SetItems(Index: Integer; Item: TFileSystemObject);
begin
inherited Items[Index]:=Item;
end;
function TFileSystemObjectList.Add(aObject: TFileSystemObject): Integer;
begin
Result:=inherited Add(pointer(aObject));
aObject.IncRef;
Exclude(FFlags, gfSort);
end;
function TFileSystemObjectList.IsDuplicate;
begin
Result:=False;
if Count>0 then
Result:=(TFileSystemObject(Items[0]).Compare(aObject, [cfHash]) = 0);
end;
function TFileSystemObjectList.Compare(aObject: TFileSystemObjectList; aFlags: TCompareFlags): Integer;
var
I: LongInt;
begin
if Self<>aObject then
begin
Result:=Count-aObject.Count;
if Result=0 then
begin
Sort;
aObject.Sort;
I:=0;
while (I<Count) and (Result=0) do
begin
Result:=Items[I].Compare(aObject.Items[I], aFlags);
Inc(I);
end;
end;
end
else
Result:=0
end;
procedure TFileSystemObjectList.Clear;
var
i: Integer;
begin
for i:=0 to Count-1 do
with TFileSystemObject(Items[i]) do
begin
DecRef;
Free;
end;
inherited Clear;
end;
procedure TFileSystemObjectList.Delete;
begin
with TFileSystemObject(Items[Index]) do
begin
DecRef;
Free;
end;
inherited Delete(Index);
end;
function CompareFileSystemObject(Item1, Item2: Pointer): Integer;
begin
Result:=TFileSystemObject(Item1).Compare(Item2, []);
end;
procedure TFileSystemObjectList.Sort;
begin
if not (gfSort in FFlags) then
begin
inherited Sort(CompareFileSystemObject);
Include(FFlags, gfSort);
end;
end;
{
procedure TFileSystemObjectList.SaveToFile;
var
i: Integer;
begin
for i:=0 to Count-1 do
TFileObj(Items[i]).SaveToFile(aFileStream);
end;
}
// ðåàëèçàöèÿ TFileSystemObject
constructor TFileSystemObject.Create(const aFileSystemRecord: TWin32FindData);
begin
inherited Create;
FRefCount:=0;
SetFileSystemRecord(aFileSystemRecord);
FFlags:=[];
end;
destructor TFileSystemObject.Destroy;
begin
if FFileName<>nil then StrDispose(FFileName);
inherited Destroy;
end;
procedure TFileSystemObject.IncRef;
begin
Inc(FRefCount);
end;
procedure TFileSystemObject.DecRef;
begin
Dec(FRefCount);
end;
procedure TFileSystemObject.Free;
begin
if FRefCount=0 then
inherited Free;
end;
procedure TFileSystemObject.SetFileSystemRecord(const aFileSystemRecord: TWin32FindData);
begin
with aFileSystemRecord do
begin
FFileAttributes:=dwFileAttributes;
FCreationTime:=ftCreationTime;
FLastAccessTime:=ftLastAccessTime;
FLastWriteTime:=ftLastWriteTime;
Int64Rec(FSize).Hi:=nFileSizeHigh;
Int64Rec(FSize).Lo:=nFileSizeLow;
if FFileName=nil then FFileName:=StrNew(cFileName);
end;
end;
procedure TFileSystemObject.GetFileSystemRecord(var aFileSystemRecord{$IFDEF CONTROL_TYPE}: TWin32FindData{$ENDIF});
begin
{$IFDEF CONTROL_TYPE}
ZeroMemory(@aFileSystemRecord, SizeOf(aFileSystemRecord));
{$ENDIF}
with TWin32FindData(aFileSystemRecord) do
begin
dwFileAttributes:=FFileAttributes;
ftCreationTime:=FCreationTime;
ftLastAccessTime:=FLastAccessTime;
ftLastWriteTime:=FLastWriteTime;
nFileSizeHigh:=Int64Rec(FSize).Hi;
nFileSizeLow:=Int64Rec(FSize).Lo;
StrCopy(cFileName, FFileName);
end;
end;
function TFileSystemObject.CompInt(aInt1, aInt2: Int64): Integer;
begin
if aInt1=aInt2 then
Result:=0
else
if aInt1>aInt2 then
Result:=1
else
Result:=-1;
end;
{$IFDEF VALIDATE}
function TFileSystemObject.IsValid: Boolean;
// êîíå÷íî òàêàÿ ïðîâåðêà íå êîððåêòíà, âåäü ôàéë ìîãëè íå òîëüêî èçìåíèòü,
// íî è îòðåäàêòèðîâàòü
begin
Result:=False;
FFileAttributes:=GetFileAttributes(FFileName);
if FFileAttributes<>INVALID_HANDLE_VALUE then
Result:=True
else
Include(FFlags, flDeleted);
end;
{$ENDIF}
// ðåàëèçàöèÿ TFileObject
{$IFDEF MD5}
function TFileObject.CalculateMD5;
begin
if flOpenFalse in FFlags then
Result:=False
else
try
if not (flMD5Calc in FFlags) then
begin
FMD5:=MD5File(StrPas(FFileName));
Include(FFlags, flMD5Calc);
end;
Result:=True;
except
Include(FFlags, flOpenFalse);
Result:=False;
end;
end;
{$ENDIF}
{$IFDEF CRC32}
function TFileObject.CalculateCRC32;
begin
if flOpenFalse in FFlags then
Result:=False
else
try
if not (flCRC32Calc in FFlags) then
begin
FCRC32:=CRC32File(StrPas(@FFileName));
Include(FFlags, flCRC32Calc);
end;
Result:=True;
except
Include(FFlags, flOpenFalse);
Result:=False;
end;
end;
{$ENDIF}
{$IFDEF CMPDIRECT}
function FileCompare(aFileName1, aFileName2: string): Integer;
function Min(X, Y: LongInt): LongInt;
begin
Result := X;
if X > Y then Result := Y;
end;
const
BufSize = 32768;
var
FileBuffer1, FileBuffer2: array[1..BufSize] of Byte;
File1, File2: TFileStream;
RestBufCount, RestFileCount: LongInt;
begin
Result:=0;
try
File1:=TFileStream.Create(aFileName1, fmOpenRead or fmShareDenyNone);
try
try
File2:=TFileStream.Create(aFileName2, fmOpenRead or fmShareDenyNone);
try
RestFileCount:=Min(File1.Size, File2.Size);
while (Result=0) and (RestFileCount>0) do
begin
RestBufCount:=Min(BufSize, RestFileCount);
File1.ReadBuffer(FileBuffer1, RestBufCount);
File2.ReadBuffer(FileBuffer2, RestBufCount);
while (Result=0) and (RestBufCount>0) do
begin
Result:=FileBuffer1[RestBufCount]-FileBuffer2[RestBufCount];
Dec(RestBufCount);
Dec(RestFileCount);
end;
end;
finally
File2.Free;
end;
except
Result:=1;
end;
finally
File1.Free;
end;
except
Result:=1;
end;
end;
{$ENDIF}
function TFileObject.Compare(aObject: TFileSystemObject; aFlags: TCompareFlags): Integer;
{$IFDEF MD5}
var
I: LongInt;
{$ENDIF}
begin
if Self<>aObject then
if aObject is TFileObject then
begin
Result:=CompInt(FSize,
aObject.FSize);
if (Result=0) and (cfHash in aFlags) then
begin
{$IFDEF MD5}
I:=0;
if CalculateMD5 and (aObject as TFileObject).CalculateMD5 then
while (I<16) and (Result=0) do
begin
Result:=FMD5.V[I]-(aObject as TFileObject).FMD5.V[I];
Inc(I);
end
else
Result:=-1;
{$ENDIF}
{$IFDEF CRC32}
if CalculateCRC32 and (aObject as TFileObject).CalculateCRC32 then
Result:=FCRC32-(aObject as TFileObject).FCRC32
else
Result:=-1;
{$ENDIF}
{$IFDEF CMPDIRECT}
Result:=FileCompare(StrPas(FFileName), StrPas((aObject as TFileObject).FFileName));
{$ENDIF}
end;
end
else
Result:=-1
else
Result:=0;
end;
{
function TFileObject.IsValid: Boolean;
var
FindData: TWin32FindData;
hFindFile: THandle;
begin
Result:=False;
hFindFile:=FindFirstFile(FFileName, FindData);
if hFindFile<>INVALID_HANDLE_VALUE then
begin
SetFileSystemRecord(FindData);
Windows.FindClose(hFindFile);
Result:=True;
end
else
Include(FFlags, flDeleted);
end;
}
(*
{$IFDEF VALIDATE}
// êîíå÷íî òàêàÿ ïðîâåðêà íå êîððåêòíà, âåäü ôàéë ìîãëè íå òîëüêî èçìåíèòü,
// íî è îòðåäàêòèðîâàòü
function TFileObject.IsValid: Boolean;
var
AttributeData: TWin32FileAttributeData;
begin
Result:=False;
if GetFileAttributesEx(FFileName, GetFileExInfoStandard, @AttributeData) then
Result:=True
else
Include(FFlags, flDeleted);
end;
{$ENDIF}
*)
// ðåàëèçàöèÿ TDirectoryObject
constructor TDirectoryObject.Create(aFileSystemRecord: TWin32FindData);
begin
inherited Create(aFileSystemRecord);
FSize:=0;
// FLinksCount:=0;
FDirectoryList:=TFileSystemObjectList.Create;
end;
destructor TDirectoryObject.Destroy;
begin
FDirectoryList.Free;
inherited Destroy;
end;
function TDirectoryObject.Compare(aObject: TFileSystemObject; aFlags: TCompareFlags): Integer;
begin
if Self<>aObject then
if aObject is TDirectoryObject then
begin
Result:=CompInt(FSize, (aObject as TDirectoryObject).FSize);
if (Result=0) then
begin
Result:=FDirectoryList.Compare(
(aObject as TDirectoryObject).FDirectoryList, aFlags);
end;
end
else
Result:=1
else
Result:=0;
end;
(*
{$IFDEF VALIDATE}
function TDirectoryObject.IsValid: Boolean;
begin
Result:=False;
FFileAttributes:=GetFileAttributes(FFileName);
if FFileAttributes<>INVALID_HANDLE_VALUE then
Result:=True
else
Include(FFlags, flDeleted);
end;
{$ENDIF}
*)
procedure TDirectoryObject.AddMember(aObject: TFileSystemObject);
begin
if aObject<>nil then
begin
{
if aObject is TDirectoryObject then
Inc((aObject as TDirectoryObject).FLinksCount);
}
FDirectoryList.Add(aObject);
Inc(FSize, aObject.FSize);
end;
end;
end.