forked from alphaonex86/Supercopier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCCopier.pas
870 lines (749 loc) · 26.9 KB
/
SCCopier.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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
{
This file is part of SuperCopier.
SuperCopier is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SuperCopier is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
}
unit SCCopier;
{$MODE Delphi}
interface
uses Classes, SCFileList, SCDirList, SCBaseList, Sysutils, Dialogs,
SCCommon, FileUtil, Windows;
const
COPIER_DATA_VERSION=001;
type
TFileCollisionEvent=function(var NewName:WideString):TCollisionAction of object;
TDiskSpaceWarningEvent=function(Drives:TDiskSpaceWarningVolumeArray):Boolean of object;
TCopyErrorEvent=function(ErrorText:WideString):TCopyErrorAction of object;
TGenericErrorEvent=procedure(Action,Target,ErrorText:WideString) of object;
TCopyProgressEvent=function:Boolean of object;
TRecurseProgressEvent=function(CurrentItem:TDirItem):Boolean of object;
ECopyError=class(Exception);
TCopier=class
private
LastBaseListId:Integer;
FCopyAttributes:Boolean;
FCopySecurity:Boolean;
function RecurseSubs(DirItem:TDirItem):Boolean;
protected
FBufferSize:cardinal;
procedure RaiseCopyErrorIfNot(Test:Boolean);
procedure CopyFileAge(HSrc,HDest:THandle);
procedure GenericError(Action,Target:WideString;ErrorText:WideString='');
procedure CopyError;
procedure SetBufferSize(Value:cardinal);virtual;abstract;
public
FileList:TFileList;
DirList:TDirList;
CopiedCount:Cardinal;
CopiedSize,SkippedSize:Int64;
CurrentCopy:record
FileItem:TFileItem;
DirItem:TDirItem;
CopiedSize,SkippedSize:Int64;
NextAction:TCopyAction;
end;
OnFileCollision:TFileCollisionEvent;
OnDiskSpaceWarning:TDiskSpaceWarningEvent;
OnCopyError:TCopyErrorEvent;
OnGenericError:TGenericErrorEvent;
OnCopyProgress:TCopyProgressEvent;
OnRecurseProgress:TRecurseProgressEvent;
constructor Create;
destructor Destroy;override;
procedure SaveToStream(TheStream:TStream);
procedure LoadFromStream(TheStream:TStream);
procedure AddBaseList(BaseList:TBaseList;DestDir:WideString);
procedure RemoveLastBaseList;
function VerifyFreeSpace(FastMode:Boolean=true):Boolean;
function FirstCopy:Boolean;
function NextCopy:Boolean;
function ManageFileAction(ResumeNoAgeVerification:Boolean=False):Boolean;
procedure CreateEmptyDirs;
procedure DeleteSrcDirs;
procedure DeleteSrcFile;
procedure DeleteDestFile;
procedure CopyAttributesAndSecurity;
procedure VerifyOrCreateDir(ADirItem:TDirItem=nil);
function DoCopy:Boolean;virtual;abstract;
property BufferSize:cardinal read FBufferSize write SetBufferSize;
property CopyAttributes:Boolean read FCopyAttributes write FCopyAttributes;
property CopySecurity:Boolean read FCopySecurity write FCopySecurity;
end;
implementation
uses LCLIntf, LCLType, LMessages,SCWin32,SCLocStrings, Math;
//******************************************************************************
//******************************************************************************
//******************************************************************************
// TCopier: classe de base de copie de fichiers, les erreurs et collisions sont
// gйrйes par йvиnements
//******************************************************************************
//******************************************************************************
//******************************************************************************
//******************************************************************************
// Create
//******************************************************************************
constructor TCopier.Create;
begin
// crйation des listes
DirList:=TDirList.Create;
FileList:=TFileList.Create(DirList);
// init des variables
CopiedCount:=0;
CopiedSize:=0;
SkippedSize:=0;
LastBaseListId:=-1;
OnFileCollision:=nil;
OnDiskSpaceWarning:=nil;
OnCopyError:=nil;
OnGenericError:=nil;
OnCopyProgress:=nil;
FBufferSize:=0;
with CurrentCopy do
begin
FileItem:=nil;
DirItem:=nil;
CopiedSize:=0;
SkippedSize:=0;
NextAction:=cpaNextFile;
end;
end;
//******************************************************************************
// Destroy
//******************************************************************************
destructor TCopier.Destroy;
begin
// libйration des listes
FileList.Free;
DirList.Free;
inherited Destroy;
end;
//******************************************************************************
// SaveToStream : sauvegarde des donnйes
//******************************************************************************
procedure TCopier.SaveToStream(TheStream:TStream);
var Sig:String;
Version:integer;
begin
Sig:=SCL_SIGNATURE;
TheStream.Write(Sig[1],Length(Sig));
Version:=COPIER_DATA_VERSION;
TheStream.Write(Version,SizeOf(Integer));
//version 1
DirList.SaveToStream(TheStream);
FileList.SaveToStream(TheStream);
end;
//******************************************************************************
// LoadFromStream : chargement des donnйes
//******************************************************************************
procedure TCopier.LoadFromStream(TheStream:TStream);
var Sig:String;
Version:integer;
begin
SetLength(Sig,SCL_SIGNATURE_LENGTH);
TheStream.Read(Sig[1],SCL_SIGNATURE_LENGTH);
if Sig<>SCL_SIGNATURE then raise Exception.Create('Data file is not a SuperCopier CopyList');
Version:=000;
TheStream.Read(Version,SizeOf(Integer));
if Version>COPIER_DATA_VERSION then raise Exception.Create('Copier: data file is for a newer SuperCopier version');
DirList.LoadFromStream(TheStream);
FileList.LoadFromStream(TheStream);
end;
//******************************************************************************
// RaiseCopyErrorIfNot : dйclenche un exception ECopyError si Test vaut false
//******************************************************************************
procedure TCopier.RaiseCopyErrorIfNot(Test:Boolean);
begin
if not Test then raise ECopyError.Create('Copy Error');
end;
//******************************************************************************
// CopyFileAge : copie la date de modif d'un fichier ouvert vers un autre
//******************************************************************************
procedure TCopier.CopyFileAge(HSrc,HDest:THandle);
var FileTime:Windows.TFileTime;
begin
if (not GetFileTime(HSrc,nil,nil,@FileTime)) or
(not SetFileTime(HDest,nil,nil,@FileTime)) then
begin
GenericError(lsUpdateTimeAction,CurrentCopy.FileItem.DestFullName,GetLastErrorText);
end;
end;
//******************************************************************************
// GenericError : dйclenche un йvиnement d'erreur gйnйrique
//******************************************************************************
procedure TCopier.GenericError(Action,Target:WideString;ErrorText:WideString='');
begin
if Assigned(OnGenericError) then
begin
OnGenericError(Action,Target,ErrorText);
end;
end;
//******************************************************************************
// CopyError : dйclenche un йvиnement d'erreur de copie et gere la valeur de retour
//******************************************************************************
procedure TCopier.CopyError;
var ErrorResult:TCopyErrorAction;
FileItem:TFileItem;
begin
Assert(Assigned(OnCopyError),'OnCopyError not assigned');
ErrorResult:=OnCopyError(SysErrorMessage(GetLastError));
case ErrorResult of
ceaNone:
Assert(False,'ErrorAction=claNone');
ceaSkip:
CurrentCopy.NextAction:=cpaNextFile;
ceaCancel:
CurrentCopy.NextAction:=cpaCancel;
ceaRetry:
CurrentCopy.NextAction:=cpaRetry;
ceaEndOfList:
begin
CurrentCopy.NextAction:=cpaNextFile;
// on ajoute а la filelist un nouvel item contenant les mкmes donnйes que celui en cours
with CurrentCopy.FileItem do
begin
FileItem:=TFileItem.Create;
FileItem.CopyTryCount:=CopyTryCount;
FileItem.BaseListId:=BaseListId;
FileItem.SrcName:=SrcName;
FileItem.DestName:=DestName;
FileItem.SrcSize:=SrcSize;
FileItem.Directory:=Directory;
FileList.Add(FileItem);
end;
end;
end;
end;
//******************************************************************************
// AddBaseList : Ajoute une liste de fichiers au Copier, Copiйs dans DestDir
//******************************************************************************
procedure TCopier.AddBaseList(BaseList:TBaseList;DestDir:WideString);
// FindOrCreateParent
function FindOrCreateParent(SrcPath,DestPath:WideString):TDirItem;
var SrcParent:WideString;
begin
SrcParent:=ExtractFilePath(SrcPath);
Result:=DirList.FindDirItem(SrcParent,DestPath);
if Result=nil then
begin
Result:=TDirItem.Create;
Result.SrcPath:=SrcParent;
Result.DestPath:=DestPath;
Result.ParentDir:=nil;
Result.Created:=DirectoryExists(DestPath);
DirList.Add(Result);
end;
end;
// FindNewName
function FindNewName(OldName,Dir:WideString):WideString;
var NewInc:integer;
NotFound:boolean;
begin
NewInc:=1;
repeat
if NewInc>1 then
Result:=WideFormat(lsCopyOf2,[NewInc,OldName])
else
Result:=WideFormat(lsCopyOf1,[OldName]);
NotFound:=True;
if FileExists(Dir+Result) or DirectoryExists(Dir+Result) then
NotFound:=False;
Inc(NewInc);
until NotFound;
end;
var i:integer;
FileItem:TFileItem;
DirItem:TDirItem;
ShortSourceName,ShortDestName:WideString;
begin
Inc(LastBaseListId);
// tri de la liste
BaseList.SortByFileName;
// forcer le \ terminal
DestDir:=IncludeTrailingBackslash(DestDir);
for i:=0 to BaseList.Count-1 do
with BaseList[i] do
begin
ShortSourceName:=ExtractFileName(SrcName);
ShortDestName:=ShortSourceName;
//copie d'un йlйment sur lui mкme=renommage automatique
if ExtractFilePath(SrcName)=DestDir then
begin
ShortDestName:=FindNewName(ShortDestName,DestDir);
end;
// transfert des BaseItems dans leurs listes respectives
if IsDirectory then
begin
DirItem:=TDiritem.Create;
DirItem.BaseListId:=LastBaseListId;
DirItem.SrcPath:=IncludeTrailingBackslash(SrcName);
DirItem.DestPath:=IncludeTrailingBackslash(DestDir+ShortDestName);
DirItem.ParentDir:=FindOrCreateParent(SrcName,DestDir);
DirItem.Created:=False;
DirList.Add(DirItem);
if not RecurseSubs(DirItem) then
begin
RemoveLastBaseList;
Break;
end;
end
else
begin
FileItem:=TFileItem.Create;
FileItem.CopyTryCount:=0;
FileItem.BaseListId:=LastBaseListId;
FileItem.SrcName:=ShortSourceName;
FileItem.DestName:=ShortDestName;
FileItem.SrcSize:=GetFileSizeByName(SrcName);
FileItem.Directory:=FindOrCreateParent(SrcName,DestDir);
FileList.Add(FileItem);
end;
end;
// libйration de la liste
BaseList.Free;
end;
//******************************************************************************
// RemoveLastBaseList : Enlиve de la liste de copie les derniers fichiers ajoutйs
//******************************************************************************
procedure TCopier.RemoveLastBaseList;
var i:integer;
begin
// suppression des DirItems
try
DirList.Lock;
i:=DirList.Count-1;
while (i>=0) and (DirList[i].BaseListId=LastBaseListId) do
begin
DirList.Delete(i);
Dec(i);
end;
finally
DirList.Unlock;
end;
// suppression des FileItems
try
FileList.Lock;
i:=FileList.Count-1;
while (i>=0) and (FileList[i].BaseListId=LastBaseListId) do
begin
FileList.Delete(i,True);
Dec(i);
end;
finally
FileList.Unlock;
end;
end;
//******************************************************************************
// RecurseSubs : Ajout par rйcursion des fichiers d'un rйpertoire
// Renvoie false si la rйcursion a йtй annulйe
//******************************************************************************
function TCopier.RecurseSubs(DirItem:TDirItem):Boolean;
var FindData:TWin32FindDataW;
FindHandle:THandle;
NewFileItem:TFileItem;
NewDirItem:TDirItem;
begin
Assert(Assigned(OnRecurseProgress),'OnRecurseProgress not assigned');
Result:=OnRecurseProgress(DirItem);
with DirItem do
begin
FindHandle:=SCWin32.FindFirstFile(PWideChar(SrcPath+'\*.*'),FindData);
if FindHandle=INVALID_HANDLE_VALUE then
begin
GenericError(lsListAction,SrcPath,GetLastErrorText);
exit;
end;
repeat
with FindData do
begin
if (WideString(cFileName)='.') or (WideString(cFileName)='..') then continue; //ne pas prendre en compte les reps '.' et '..'
if (dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY)<>0 then //IsDir
begin
NewDirItem:=TDirItem.Create;
NewDirItem.BaseListId:=LastBaseListId;
NewDirItem.SrcPath:=SrcPath+WideString(cFileName)+'\';
NewDirItem.DestPath:=DestPath+WideString(cFileName)+'\';
NewDirItem.ParentDir:=DirItem;
NewDirItem.Created:=False;
DirList.Add(NewDirItem);
Result:=Result and RecurseSubs(NewDirItem);
end
else
begin
NewFileItem:=TFileItem.Create;
NewFileItem.CopyTryCount:=0;
NewFileItem.BaseListId:=LastBaseListId;
NewFileItem.SrcName:=WideString(cFileName);
NewFileItem.DestName:=WideString(cFileName);
NewFileItem.Directory:=DirItem;
NewFileItem.SrcSize:=nFileSizeLow;
Inc(NewFileItem.SrcSize,nFileSizeHigh * $100000000);
FileList.Add(NewFileItem);
end;
end;
until (not SCWin32.FindNextFile(FindHandle,FindData)) or (not Result);
Windows.FindClose(FindHandle); { *Converted from FindClose* }
end;
end;
//******************************************************************************
// VerifyFreeSpace : Vйrifie qu'il y a assez d'espace disque pour copier les
// fichiers et dйclenche un evиnement sinon
// renvoie true si la copie des fichiers n'est pas annulйe
// FastMode=false pour corriger qq pb avec les points de montage NTFS
//******************************************************************************
function TCopier.VerifyFreeSpace(FastMode:Boolean=true):Boolean;
var Volumes:TDiskSpaceWarningVolumeArray;
i:Integer;
ForceCopy:Boolean;
DiskSpaceOk:Boolean;
_FreeSize,_VolumeSize: PLargeInteger;
//AddToVolume
procedure AddToVolume(Volume:WideString;Size:Int64);
var i:integer;
begin
i:=0;
while (i<Length(Volumes)) and (Volumes[i].Volume<>Volume) do
Inc(i);
if i>=Length(Volumes) then // le volume est-il rйpertoriй?
begin
SetLength(Volumes,Length(Volumes)+1);
Volumes[i].Volume:=Volume;
Volumes[i].LackSize:=Size;
end
else
begin
Volumes[i].LackSize:=Volumes[i].LackSize+Size;
end;
end;
//AddToVolumeByPath
procedure AddToVolumeByPath(Path:WideString;Size:Int64);
var i:integer;
begin
i:=0;
while (i<Length(Volumes)) and (Pos(Volumes[i].Volume,Path)=0) do
Inc(i);
if i>=Length(Volumes) then // le volume est-il rйpertoriй?
begin
SetLength(Volumes,Length(Volumes)+1);
Volumes[i].Volume:=GetVolumeNameString(Path);
Volumes[i].LackSize:=Size;
end
else
begin
Volumes[i].LackSize:=Volumes[i].LackSize+Size;
end;
end;
//RemoveVolume
procedure RemoveVolume(VolNum:integer);
var i:integer;
begin
for i:=VolNum to Length(Volumes)-2 do
Volumes[i]:=Volumes[i+1];
SetLength(Volumes,Length(Volumes)-1);
end;
begin
Result:=True;
try
FileList.Lock;
if FastMode then
begin
// recup de la taille des fichiers pour chaque volume
for i:=0 to FileList.Count-1 do
AddToVolumeByPath(FileList[i].Directory.Destpath,FileList[i].SrcSize);
// ne pas compter la taille du fichier en cours (allouйe par avance)
if CurrentCopy.CopiedSize>0 then AddToVolumeByPath(CurrentCopy.DirItem.Destpath,-CurrentCopy.FileItem.SrcSize);
end
else
begin
// recup de la taille des fichiers pour chaque volume
for i:=0 to FileList.Count-1 do
AddToVolume(GetVolumeNameString(FileList[i].Directory.Destpath),FileList[i].SrcSize);
// ne pas compter la taille du fichier en cours (allouйe par avance)
if CurrentCopy.CopiedSize>0 then AddToVolume(GetVolumeNameString(CurrentCopy.DirItem.Destpath),-CurrentCopy.FileItem.SrcSize);
end;
finally
FileList.Unlock;
end;
// йliminer les volumes contenant assez de place
for i:=Length(Volumes)-1 downto 0 do
with Volumes[i] do
begin
_FreeSize := @FreeSize;
_VolumeSize := @VolumeSize;
DiskSpaceOk:=SCWin32.GetDiskFreeSpaceEx(PWideChar(Volume),_FreeSize,_VolumeSize,nil);
if DiskSpaceOk then
begin
LackSize:=LackSize-FreeSize;
if LackSize<0 then RemoveVolume(i);
end
else
begin
RemoveVolume(i);
end;
end;
// dйclencher un йvиnement si au moins 1 volume n'a pas assez de place
if (Length(Volumes)>0) and Assigned(OnDiskSpaceWarning) then
begin
ForceCopy:=OnDiskSpaceWarning(Volumes);
if not ForceCopy then RemoveLastBaseList;
Result:=ForceCopy;
end;
end;
//******************************************************************************
// FirstCopy : Prйpare le Copier pour la premiиre copie
// Renvoie false si rien а copier
//******************************************************************************
function TCopier.FirstCopy:Boolean;
begin
if FileList.Count>0 then
begin
Result:=True;
with CurrentCopy do
begin
FileItem:=FileList[0];
DirItem:=FileItem.Directory;
CopiedSize:=0;
SkippedSize:=0;
NextAction:=cpaNextFile;
end;
end
else
begin
Result:=False;
end;
end;
//******************************************************************************
// NextCopy : Prйpare le Copier pour la prochaine copie
// Renvoie false si plus rien а copier
//******************************************************************************
function TCopier.NextCopy:Boolean;
var NonCopiedSize:Int64;
begin
if CurrentCopy.NextAction<>cpaRetry then
begin
Inc(CopiedCount);
// Ajouter aux SkippedSize tout ce qui n'a pas йtй copiй
with CurrentCopy do
begin
NonCopiedSize:=FileItem.SrcSize-(CopiedSize+SkippedSize);
SkippedSize:=SkippedSize+NonCopiedSize;
Self.SkippedSize:=Self.SkippedSize+NonCopiedSize;
end;
end;
Result:=true;
case CurrentCopy.NextAction of
cpaNextFile:
begin
try
FileList.Lock;
// on enleve le FileItem qui vient d'etre copiй
FileList.Delete(0);
if FileList.Count>0 then
begin
// maj de CurrentCopy
with CurrentCopy do
begin
FileItem:=FileList[0];
DirItem:=FileItem.Directory;
CopiedSize:=0;
SkippedSize:=0;
end;
end
else
begin
// plus d'items -> renvoyer false
Result:=false;
end;
finally
FileList.Unlock;
end;
end;
cpaCancel:
begin
Result:=False;
end;
cpaRetry:
begin
// on recommence la meme copie -> faire comme si l'on avait rien copiй
SkippedSize:=SkippedSize-CurrentCopy.SkippedSize;
CopiedSize:=CopiedSize-CurrentCopy.CopiedSize;
end;
end;
end;
//******************************************************************************
// ManageFileAction : Gиre les collisions de fichiers et effectue les actions demandйes
// Renvoie false si la copie du fichier en cours est annulйe
//******************************************************************************
function TCopier.ManageFileAction(ResumeNoAgeVerification:Boolean):Boolean;
var Action:TCollisionAction;
FullNewName,NewName:WideString;
MustRedo:Boolean;
begin
Result:=true;
// gestion annulation
if CurrentCopy.NextAction=cpaCancel then
begin
Result:=False;
Exit;
end;
repeat
MustRedo:=False;
// rien а faire si pas de collision ou fichier deja traitй
if (not CurrentCopy.FileItem.DestExists) or (CurrentCopy.NextAction<>cpaNextFile) then exit;
// on lance l'йvиnement pour savoir quoi faire
Assert(Assigned(OnFileCollision),'OnFileCollision not assigned');
Action:=OnFileCollision(NewName);
case Action of
claNone:
begin
Assert(False,'CollisionAction=claNone');
end;
claCancel:
begin
Result:=false;
CurrentCopy.NextAction:=cpaCancel;
end;
claSkip:
begin
Result:=false;
CurrentCopy.NextAction:=cpaNextFile;
end;
claResume:
begin
with CurrentCopy.FileItem do
begin
if (ResumeNoAgeVerification or (SrcAge=DestAge)) and (SrcSize>DestSize) then
begin
Result:=true;
CurrentCopy.NextAction:=cpaRetry;
end
else
begin
// la reprise ne peut pas etre effectuйe -> йcraser
Result:=True;
CurrentCopy.NextAction:=cpaNextFile;
end;
end;
end;
claOverwrite:
begin
Result:=true;
CurrentCopy.NextAction:=cpaNextFile;
end;
claOverwriteIfDifferent:
begin
Result:=not CurrentCopy.FileItem.DestIsSameFile;
CurrentCopy.NextAction:=cpaNextFile;
end;
claRenameNew:
begin
Result:=true;
CurrentCopy.NextAction:=cpaNextFile;
CurrentCopy.FileItem.DestName:=NewName;
MustRedo:=True; // le nom du fichier а changй, il peut aussi exister dйjа
end;
claRenameOld:
begin
Result:=true;
CurrentCopy.NextAction:=cpaNextFile;
FullNewName:=IncludeTrailingBackslash(CurrentCopy.DirItem.Destpath)+NewName;
if not SCWin32.MoveFile(PWideChar(CurrentCopy.FileItem.DestFullName),PWideChar(FullNewName)) then
begin
// gestion de l'erreur
GenericError(lsRenameAction,CurrentCopy.FileItem.DestFullName,GetLastErrorText);
MustRedo:=True; // le renommage a йchouй -> la collision n'a pas йtй rйsolue
end;
end;
end;
until not MustRedo;
end;
//******************************************************************************
// CreateEmptyDirs : crйation des rйpertoires vides
//******************************************************************************
procedure TCopier.CreateEmptyDirs;
var i:integer;
begin
for i:=0 to DirList.Count-1 do
VerifyOrCreateDir(DirList[i]);
end;
//******************************************************************************
// DeleteSrcDirs : supprime les rйpertoires contenant les fichiers source
//******************************************************************************
procedure TCopier.DeleteSrcDirs;
var i:integer;
begin
for i:=DirList.Count-1 downto 0 do
if DirList[i].ParentDir<>nil then // un DirItem n'a pas de parent seulement
begin // si c'est le rйpertoire de base des йlйments а dйplacer
if not DirList[i].SrcDelete then
begin
// gestion de l'erreur
GenericError(lsDeleteAction,DirList[i].SrcPath,GetLastErrorText);
end;
end;
end;
//******************************************************************************
// DeleteSrcFile : supprime le fichier source en cours (pour les dйplacements)
//******************************************************************************
procedure TCopier.DeleteSrcFile;
begin
if not CurrentCopy.FileItem.SrcDelete then
begin
// gestion de l'erreur
GenericError(lsDeleteAction,CurrentCopy.FileItem.SrcFullName,GetLastErrorText);
end;
end;
//******************************************************************************
// DeleteDestFile : supprime le fichier destination en cours (copie non terminйe ou erreur)
//******************************************************************************
procedure TCopier.DeleteDestFile;
begin
if not CurrentCopy.FileItem.DestDelete then
begin
// gestion de l'erreur
GenericError(lsDeleteAction,CurrentCopy.FileItem.DestFullName,GetLastErrorText);
end;
end;
//******************************************************************************
// CopyAttributesAndSecurity : Copie les attributs et la sйcuritй du fichier en cours
//******************************************************************************
procedure TCopier.CopyAttributesAndSecurity;
begin
if CopyAttributes and not CurrentCopy.FileItem.DestCopyAttributes then
begin
// gestion de l'erreur
GenericError(lsUpdateAttributesAction,CurrentCopy.FileItem.DestFullName,GetLastErrorText);
end;
if CopySecurity and (Win32Platform=VER_PLATFORM_WIN32_NT) and not CurrentCopy.FileItem.DestCopySecurity then
begin
// gestion de l'erreur
GenericError(lsUpdateSecurityAction,CurrentCopy.FileItem.DestFullName,GetLastErrorText);
end;
end;
//******************************************************************************
// VerifyOrCreateDir : Appelle VerifyOrCreate pour un DirItem et gиre attributs et sйcuritй
//******************************************************************************
procedure TCopier.VerifyOrCreateDir(ADirItem: TDirItem);
var DirItem:TDirItem;
begin
DirItem:=ADirItem;
if DirItem=nil then DirItem:=CurrentCopy.DirItem;
if DirItem.Created then Exit;
DirItem.VerifyOrCreate;
if CopyAttributes and not DirItem.DestCopyAttributes then
begin
// gestion de l'erreur
GenericError(lsUpdateAttributesAction,DirItem.Destpath,GetLastErrorText);
end;
if CopySecurity and (Win32Platform=VER_PLATFORM_WIN32_NT) and not DirItem.DestCopySecurity then
begin
// gestion de l'erreur
GenericError(lsUpdateSecurityAction,DirItem.Destpath,GetLastErrorText);
end;
end;
end.