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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
|
#include "synchronization.h"
#include <wx/intl.h>
#include <wx/msgdlg.h>
#include <wx/log.h>
#include "library/resources.h"
#include "algorithm.h"
#include "library/globalFunctions.h"
#include "library/statusHandler.h"
#include "library/fileHandling.h"
#include <utility>
#ifdef FFS_WIN
#include <wx/msw/wrapwin.h> //includes "windows.h"
#endif
using namespace FreeFileSync;
inline
SyncConfiguration::Direction getSyncDirection(const CompareFilesResult cmpResult, const SyncConfiguration& config)
{
switch (cmpResult)
{
case FILE_LEFT_SIDE_ONLY:
return config.exLeftSideOnly;
break;
case FILE_RIGHT_SIDE_ONLY:
return config.exRightSideOnly;
break;
case FILE_RIGHT_NEWER:
return config.rightNewer;
break;
case FILE_LEFT_NEWER:
return config.leftNewer;
break;
case FILE_DIFFERENT:
return config.different;
break;
default:
return SyncConfiguration::SYNC_DIR_NONE;
}
}
inline
bool getBytesToTransfer(const FileCompareLine& fileCmpLine,
const SyncConfiguration& config,
int& objectsToCreate,
int& objectsToOverwrite,
int& objectsToDelete,
wxULongLong& dataToProcess)
{ //return false if nothing has to be done
objectsToCreate = 0; //always initialize variables
objectsToOverwrite = 0;
objectsToDelete = 0;
dataToProcess = 0;
//do not add filtered entries
if (!fileCmpLine.selectedForSynchronization)
return false;
switch (fileCmpLine.cmpResult)
{
case FILE_LEFT_SIDE_ONLY:
//get data to process
switch (getSyncDirection(fileCmpLine.cmpResult, config))
{
case SyncConfiguration::SYNC_DIR_LEFT: //delete file on left
dataToProcess = 0;
objectsToDelete = 1;
break;
case SyncConfiguration::SYNC_DIR_RIGHT: //copy from left to right
dataToProcess = fileCmpLine.fileDescrLeft.fileSize;
objectsToCreate = 1;
break;
case SyncConfiguration::SYNC_DIR_NONE:
return false;
}
break;
case FILE_RIGHT_SIDE_ONLY:
switch (getSyncDirection(fileCmpLine.cmpResult, config))
{
case SyncConfiguration::SYNC_DIR_LEFT: //copy from right to left
dataToProcess = fileCmpLine.fileDescrRight.fileSize;
objectsToCreate = 1;
break;
case SyncConfiguration::SYNC_DIR_RIGHT: //delete file on right
dataToProcess = 0;
objectsToDelete = 1;
break;
case SyncConfiguration::SYNC_DIR_NONE:
return false;
}
break;
case FILE_LEFT_NEWER:
case FILE_RIGHT_NEWER:
case FILE_DIFFERENT:
//get data to process
switch (getSyncDirection(fileCmpLine.cmpResult, config))
{
case SyncConfiguration::SYNC_DIR_LEFT: //copy from right to left
dataToProcess = fileCmpLine.fileDescrRight.fileSize;
objectsToOverwrite = 1;
break;
case SyncConfiguration::SYNC_DIR_RIGHT: //copy from left to right
dataToProcess = fileCmpLine.fileDescrLeft.fileSize;
objectsToOverwrite = 1;
break;
case SyncConfiguration::SYNC_DIR_NONE:
return false;
}
break;
case FILE_EQUAL:
return false;
default:
assert(false);
return false;
};
return true;
}
//runs at folder pair level
void calcBytesToSync(const FileComparison& fileCmp,
const SyncConfiguration& config,
int& objectsToCreate,
int& objectsToOverwrite,
int& objectsToDelete,
wxULongLong& dataToProcess)
{
objectsToCreate = 0;
objectsToOverwrite = 0;
objectsToDelete = 0;
dataToProcess = 0;
int toCreate = 0;
int toOverwrite = 0;
int toDelete = 0;
wxULongLong data;
for (FileComparison::const_iterator i = fileCmp.begin(); i != fileCmp.end(); ++i)
{ //only sum up sizes of files AND directories
if (getBytesToTransfer(*i, config, toCreate, toOverwrite, toDelete, data))
{
objectsToCreate += toCreate;
objectsToOverwrite += toOverwrite;
objectsToDelete += toDelete;
dataToProcess += data;
}
}
}
//aggregate over all folder pairs
void FreeFileSync::calcTotalBytesToSync(const FolderComparison& folderCmp,
const SyncConfiguration& config,
int& objectsToCreate,
int& objectsToOverwrite,
int& objectsToDelete,
wxULongLong& dataToProcess)
{
objectsToCreate = 0;
objectsToOverwrite = 0;
objectsToDelete = 0;
dataToProcess = 0;
for (FolderComparison::const_iterator j = folderCmp.begin(); j != folderCmp.end(); ++j)
{
const FileComparison& fileCmp = j->fileCmp;
int toCreate = 0;
int toOverwrite = 0;
int toDelete = 0;
wxULongLong data;
calcBytesToSync(fileCmp, config, toCreate, toOverwrite, toDelete, data);
objectsToCreate += toCreate;
objectsToOverwrite += toOverwrite;
objectsToDelete += toDelete;
dataToProcess += data;
}
}
template <bool recyclerUsed>
std::pair<wxLongLong, wxLongLong> spaceNeededSub(const FileComparison& fileCmp, const SyncConfiguration& config)
{
wxLongLong spaceNeededLeft;
wxLongLong spaceNeededRight;
for (FileComparison::const_iterator i = fileCmp.begin(); i != fileCmp.end(); ++i)
{
if (i->selectedForSynchronization) //do not add filtered entries
{
//get data to process
switch (getSyncDirection(i->cmpResult, config))
{
case SyncConfiguration::SYNC_DIR_LEFT: //copy from right to left
if (!recyclerUsed)
spaceNeededLeft -= globalFunctions::convertToSigned(i->fileDescrLeft.fileSize);
spaceNeededLeft += globalFunctions::convertToSigned(i->fileDescrRight.fileSize);
break;
case SyncConfiguration::SYNC_DIR_RIGHT: //copy from left to right
if (!recyclerUsed)
spaceNeededRight -= globalFunctions::convertToSigned(i->fileDescrRight.fileSize);
spaceNeededRight += globalFunctions::convertToSigned(i->fileDescrLeft.fileSize);
break;
case SyncConfiguration::SYNC_DIR_NONE:
break;
}
}
}
return std::pair<wxLongLong, wxLongLong>(spaceNeededLeft, spaceNeededRight);
}
std::pair<wxLongLong, wxLongLong> freeDiskSpaceNeeded(const FileComparison& fileCmp, const SyncConfiguration& config, const bool recyclerUsed)
{
if (recyclerUsed)
return spaceNeededSub<true>(fileCmp, config);
else
return spaceNeededSub<false>(fileCmp, config);
}
bool FreeFileSync::synchronizationNeeded(const FolderComparison& folderCmp, const SyncConfiguration& config)
{
int objectsToCreate = 0;
int objectsToOverwrite = 0;
int objectsToDelete = 0;
wxULongLong dataToProcess;
FreeFileSync::calcTotalBytesToSync(folderCmp,
config,
objectsToCreate,
objectsToOverwrite,
objectsToDelete,
dataToProcess);
return objectsToCreate + objectsToOverwrite + objectsToDelete != 0;
}
//test if more then 50% of files will be deleted/overwritten
bool significantDifferenceDetected(const FileComparison& fileCmp, const SyncConfiguration& config)
{
int objectsToCreate = 0;
int objectsToOverwrite = 0;
int objectsToDelete = 0;
wxULongLong dataToProcess;
calcBytesToSync(fileCmp,
config,
objectsToCreate,
objectsToOverwrite,
objectsToDelete,
dataToProcess);
const int changedFiles = objectsToCreate + objectsToOverwrite + objectsToDelete; //include objectsToCreate also!
return changedFiles >= 10 && changedFiles > 0.5 * fileCmp.size();
}
SyncProcess::SyncProcess(const bool useRecycler,
const bool copyFileSymLinks,
const bool traverseDirSymLinks,
bool& warningSignificantDifference,
bool& warningNotEnoughDiskSpace,
StatusHandler* handler) :
m_useRecycleBin(useRecycler),
m_copyFileSymLinks(copyFileSymLinks),
m_traverseDirSymLinks(traverseDirSymLinks),
m_warningSignificantDifference(warningSignificantDifference),
m_warningNotEnoughDiskSpace(warningNotEnoughDiskSpace),
statusUpdater(handler),
txtCopyingFile(Zstring(_("Copying file %x to %y")).Replace(wxT("%x"), wxT("\"%x\""), false).Replace(wxT("%y"), wxT("\n\"%y\""), false)),
txtOverwritingFile(Zstring(_("Copying file %x to %y overwriting target")).Replace(wxT("%x"), wxT("\"%x\""), false).Replace(wxT("%y"), wxT("\n\"%y\""), false)),
txtCreatingFolder(Zstring(_("Creating folder %x")).Replace( wxT("%x"), wxT("\n\"%x\""), false)),
txtDeletingFile(Zstring(_("Deleting file %x")).Replace(wxT("%x"), wxT("\n\"%x\""), false)),
txtDeletingFolder(Zstring(_("Deleting folder %x")).Replace( wxT("%x"), wxT("\n\"%x\""), false)) {}
inline
bool SyncProcess::synchronizeFile(const FileCompareLine& cmpLine, const SyncConfiguration& config, const FolderPair& folderPair)
{ //return false if nothing had to be done
if (!cmpLine.selectedForSynchronization) return false;
Zstring statusText;
Zstring target;
//synchronize file:
switch (cmpLine.cmpResult)
{
case FILE_LEFT_SIDE_ONLY:
switch (config.exLeftSideOnly)
{
case SyncConfiguration::SYNC_DIR_LEFT: //delete files on left
statusText = txtDeletingFile;
statusText.Replace(wxT("%x"), cmpLine.fileDescrLeft.fullName, false);
statusUpdater->updateStatusText(statusText);
removeFile(cmpLine.fileDescrLeft.fullName, m_useRecycleBin);
break;
case SyncConfiguration::SYNC_DIR_RIGHT: //copy files to right
target = folderPair.rightDirectory + cmpLine.fileDescrLeft.relativeName.c_str();
statusText = txtCopyingFile;
statusText.Replace(wxT("%x"), cmpLine.fileDescrLeft.fullName.AfterLast(GlobalResources::FILE_NAME_SEPARATOR), false);
statusText.Replace(wxT("%y"), target.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR), false);
statusUpdater->updateStatusText(statusText);
copyFileUpdating(cmpLine.fileDescrLeft.fullName, target, cmpLine.fileDescrLeft.fileSize);
break;
case SyncConfiguration::SYNC_DIR_NONE:
return false;
}
break;
case FILE_RIGHT_SIDE_ONLY:
switch (config.exRightSideOnly)
{
case SyncConfiguration::SYNC_DIR_LEFT: //copy files to left
target = folderPair.leftDirectory + cmpLine.fileDescrRight.relativeName.c_str();
statusText = txtCopyingFile;
statusText.Replace(wxT("%x"), cmpLine.fileDescrRight.fullName.AfterLast(GlobalResources::FILE_NAME_SEPARATOR), false);
statusText.Replace(wxT("%y"), target.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR), false);
statusUpdater->updateStatusText(statusText);
copyFileUpdating(cmpLine.fileDescrRight.fullName, target, cmpLine.fileDescrRight.fileSize);
break;
case SyncConfiguration::SYNC_DIR_RIGHT: //delete files on right
statusText = txtDeletingFile;
statusText.Replace(wxT("%x"), cmpLine.fileDescrRight.fullName, false);
statusUpdater->updateStatusText(statusText);
removeFile(cmpLine.fileDescrRight.fullName, m_useRecycleBin);
break;
case SyncConfiguration::SYNC_DIR_NONE:
return false;
}
break;
case FILE_LEFT_NEWER:
case FILE_RIGHT_NEWER:
case FILE_DIFFERENT:
switch (getSyncDirection(cmpLine.cmpResult, config))
{
case SyncConfiguration::SYNC_DIR_LEFT: //copy from right to left
statusText = txtOverwritingFile;
statusText.Replace(wxT("%x"), cmpLine.fileDescrRight.fullName.AfterLast(GlobalResources::FILE_NAME_SEPARATOR), false);
statusText.Replace(wxT("%y"), cmpLine.fileDescrLeft.fullName.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR), false);
statusUpdater->updateStatusText(statusText);
removeFile(cmpLine.fileDescrLeft.fullName, m_useRecycleBin); //only used if switch activated by user, else file is simply deleted
copyFileUpdating(cmpLine.fileDescrRight.fullName, cmpLine.fileDescrLeft.fullName, cmpLine.fileDescrRight.fileSize);
break;
case SyncConfiguration::SYNC_DIR_RIGHT: //copy from left to right
statusText = txtOverwritingFile;
statusText.Replace(wxT("%x"), cmpLine.fileDescrLeft.fullName.AfterLast(GlobalResources::FILE_NAME_SEPARATOR), false);
statusText.Replace(wxT("%y"), cmpLine.fileDescrRight.fullName.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR), false);
statusUpdater->updateStatusText(statusText);
removeFile(cmpLine.fileDescrRight.fullName, m_useRecycleBin); //only used if switch activated by user, else file is simply deleted
copyFileUpdating(cmpLine.fileDescrLeft.fullName, cmpLine.fileDescrRight.fullName, cmpLine.fileDescrLeft.fileSize);
break;
case SyncConfiguration::SYNC_DIR_NONE:
return false;
}
break;
case FILE_EQUAL:
return false;
default:
assert (false);
}
return true;
}
inline
bool SyncProcess::synchronizeFolder(const FileCompareLine& cmpLine, const SyncConfiguration& config, const FolderPair& folderPair)
{ //return false if nothing had to be done
if (!cmpLine.selectedForSynchronization) return false;
Zstring statusText;
Zstring target;
//synchronize folders:
switch (cmpLine.cmpResult)
{
case FILE_LEFT_SIDE_ONLY:
switch (config.exLeftSideOnly)
{
case SyncConfiguration::SYNC_DIR_LEFT: //delete folders on left
statusText = txtDeletingFolder;
statusText.Replace(wxT("%x"), cmpLine.fileDescrLeft.fullName, false);
statusUpdater->updateStatusText(statusText);
removeDirectory(cmpLine.fileDescrLeft.fullName, m_useRecycleBin);
break;
case SyncConfiguration::SYNC_DIR_RIGHT: //create folders on right
target = folderPair.rightDirectory + cmpLine.fileDescrLeft.relativeName.c_str();
statusText = txtCreatingFolder;
statusText.Replace(wxT("%x"), target, false);
statusUpdater->updateStatusText(statusText);
//some check to catch the error that directory on source has been deleted externally after "compare"...
if (!wxDirExists(cmpLine.fileDescrLeft.fullName))
throw FileError(Zstring(_("Error: Source directory does not exist anymore:")) + wxT("\n\"") + cmpLine.fileDescrLeft.fullName + wxT("\""));
createDirectory(target, cmpLine.fileDescrLeft.fullName, !m_traverseDirSymLinks); //traverse symlinks <=> !copy symlinks
break;
case SyncConfiguration::SYNC_DIR_NONE:
return false;
}
break;
case FILE_RIGHT_SIDE_ONLY:
switch (config.exRightSideOnly)
{
case SyncConfiguration::SYNC_DIR_LEFT: //create folders on left
target = folderPair.leftDirectory + cmpLine.fileDescrRight.relativeName.c_str();
statusText = txtCreatingFolder;
statusText.Replace(wxT("%x"), target, false);
statusUpdater->updateStatusText(statusText);
//some check to catch the error that directory on source has been deleted externally after "compare"...
if (!wxDirExists(cmpLine.fileDescrRight.fullName))
throw FileError(Zstring(_("Error: Source directory does not exist anymore:")) + wxT("\n\"") + cmpLine.fileDescrRight.fullName + wxT("\""));
createDirectory(target, cmpLine.fileDescrRight.fullName, !m_traverseDirSymLinks); //traverse symlinks <=> !copy symlinks
break;
case SyncConfiguration::SYNC_DIR_RIGHT: //delete folders on right
statusText = txtDeletingFolder;
statusText.Replace(wxT("%x"), cmpLine.fileDescrRight.fullName, false);
statusUpdater->updateStatusText(statusText);
removeDirectory(cmpLine.fileDescrRight.fullName, m_useRecycleBin);
break;
case SyncConfiguration::SYNC_DIR_NONE:
return false;
}
break;
case FILE_EQUAL:
return false;
case FILE_RIGHT_NEWER:
case FILE_LEFT_NEWER:
case FILE_DIFFERENT:
default:
assert (false);
}
return true;
}
inline
bool deletionImminent(const FileCompareLine& line, const SyncConfiguration& config)
{ //test if current sync-line will result in deletion of files -> used to avoid disc space bottlenecks
if ( (line.cmpResult == FILE_LEFT_SIDE_ONLY && config.exLeftSideOnly == SyncConfiguration::SYNC_DIR_LEFT) ||
(line.cmpResult == FILE_RIGHT_SIDE_ONLY && config.exRightSideOnly == SyncConfiguration::SYNC_DIR_RIGHT))
return true;
else
return false;
}
class RemoveAtExit //this class ensures, that the result of the method below is ALWAYS written on exit, even if exceptions are thrown!
{
public:
RemoveAtExit(FileComparison& fileCmp) :
gridToWrite(fileCmp) {}
~RemoveAtExit()
{
removeRowsFromVector(gridToWrite, rowsToDelete);
}
void markRow(int nr)
{
rowsToDelete.insert(nr);
}
private:
FileComparison& gridToWrite;
std::set<int> rowsToDelete;
};
//synchronize and returns all rows that have not been synced
void SyncProcess::startSynchronizationProcess(FolderComparison& folderCmp, const SyncConfiguration& config)
{
#ifndef __WXDEBUG__
wxLogNull noWxLogs; //prevent wxWidgets logging
#endif
//-------------------some basic checks:------------------------------------------
//test existence of Recycle Bin
if (m_useRecycleBin && !FreeFileSync::recycleBinExists())
{
statusUpdater->reportFatalError(_("Unable to initialize Recycle Bin!"));
return; //should be obsolete!
}
for (FolderComparison::const_iterator j = folderCmp.begin(); j != folderCmp.end(); ++j)
{
const FileComparison& fileCmp = j->fileCmp;
//check if more than 50% of total number of files/dirs are to be created/overwritten/deleted
if (m_warningSignificantDifference) //test if check should be executed
{
if (significantDifferenceDetected(fileCmp, config))
{
bool dontShowAgain = false;
statusUpdater->reportWarning(Zstring(_("Significant difference detected:")) + wxT("\n") +
j->syncPair.leftDirectory + wxT(" <-> ") + j->syncPair.rightDirectory + wxT("\n\n") +
_("More than 50% of the total number of files will be copied or deleted!"),
dontShowAgain);
m_warningSignificantDifference = !dontShowAgain;
}
}
//check for sufficient free diskspace in left directory
if (m_warningNotEnoughDiskSpace)
{
const std::pair<wxLongLong, wxLongLong> spaceNeeded = freeDiskSpaceNeeded(fileCmp, config, m_useRecycleBin);
wxLongLong freeDiskSpaceLeft;
if (wxGetDiskSpace(j->syncPair.leftDirectory.c_str(), NULL, &freeDiskSpaceLeft))
{
if (freeDiskSpaceLeft < spaceNeeded.first)
{
bool dontShowAgain = false;
statusUpdater->reportWarning(Zstring(_("Not enough free disk space available in:")) + wxT("\n") +
j->syncPair.leftDirectory + wxT("\n\n") +
_("Total required free disk space:") + wxT(" ") + formatFilesizeToShortString(spaceNeeded.first).c_str(),
dontShowAgain);
m_warningNotEnoughDiskSpace = !dontShowAgain;
}
}
//check for sufficient free diskspace in right directory
if (m_warningNotEnoughDiskSpace)
{
wxLongLong freeDiskSpaceRight;
if (wxGetDiskSpace(j->syncPair.rightDirectory.c_str(), NULL, &freeDiskSpaceRight))
{
if (freeDiskSpaceRight < spaceNeeded.second)
{
bool dontShowAgain = false;
statusUpdater->reportWarning(Zstring(_("Not enough free disk space available in:")) + wxT("\n") +
j->syncPair.rightDirectory + wxT("\n\n") +
_("Total required free disk space:") + wxT(" ") + formatFilesizeToShortString(spaceNeeded.second).c_str(),
dontShowAgain);
m_warningNotEnoughDiskSpace = !dontShowAgain;
}
}
}
}
}
//-------------------end of basic checks------------------------------------------
//inform about the total amount of data that will be processed from now on
int objectsToCreate = 0;
int objectsToOverwrite = 0;
int objectsToDelete = 0;
wxULongLong dataToProcess;
FreeFileSync::calcTotalBytesToSync(folderCmp,
config,
objectsToCreate,
objectsToOverwrite,
objectsToDelete,
dataToProcess);
statusUpdater->initNewProcess(objectsToCreate + objectsToOverwrite + objectsToDelete,
globalFunctions::convertToSigned(dataToProcess),
StatusHandler::PROCESS_SYNCHRONIZING);
try
{
//loop over folder pairs
for (FolderComparison::iterator j = folderCmp.begin(); j != folderCmp.end(); ++j)
{
FileComparison& fileCmp = j->fileCmp;
//ensure that folderCmp is always written to, even if method is left via exceptions
RemoveAtExit removeRowsAtExit(fileCmp); //note: running at individual folder pair level!
const FolderPair& currentPair = j->syncPair;
// it should never happen, that a directory on left side has same name as file on right side. startCompareProcess() should take care of this
// and split into two "exists on one side only" cases
// Note: this case is not handled by this tool as this is considered to be a bug and must be solved by the user
//synchronize folders first; advantage: in case of deletions the whole folder is moved to recycle bin instead of single files
for (FileComparison::const_iterator i = fileCmp.begin(); i != fileCmp.end(); ++i)
{
if ( i->fileDescrLeft.objType == FileDescrLine::TYPE_DIRECTORY ||
i->fileDescrRight.objType == FileDescrLine::TYPE_DIRECTORY)
{
while (true)
{ //trigger display refresh
statusUpdater->requestUiRefresh();
try
{
if (synchronizeFolder(*i, config, currentPair))
//progress indicator update
//indicator is updated only if directory is sync'ed correctly (and if some work was done)!
statusUpdater->updateProcessedData(1, 0); //each call represents one processed file/directory
removeRowsAtExit.markRow(i - fileCmp.begin());
break;
}
catch (FileError& error)
{
ErrorHandler::Response rv = statusUpdater->reportError(error.show());
if ( rv == ErrorHandler::IGNORE_ERROR)
break;
else if (rv == ErrorHandler::RETRY)
; //continue with loop
else
assert (false);
}
}
}
}
//PERF_START;
//synchronize files:
for (int k = 0; k < 2; ++k) //loop over all files twice; reason: first delete, then copy (no sorting necessary: performance)
{
bool deleteLoop = true;
deleteLoop = k == 0; //-> first loop: delete files, second loop: copy files
for (FileComparison::const_iterator i = fileCmp.begin(); i != fileCmp.end(); ++i)
{
if ( i->fileDescrLeft.objType == FileDescrLine::TYPE_FILE ||
i->fileDescrRight.objType == FileDescrLine::TYPE_FILE)
{
if ( (deleteLoop && deletionImminent(*i, config)) ||
(!deleteLoop && !deletionImminent(*i, config)))
{
while (true)
{ //trigger display refresh
statusUpdater->requestUiRefresh();
try
{
if (synchronizeFile(*i, config, currentPair))
{
//progress indicator update
//indicator is updated only if file is sync'ed correctly (and if some sync was done)!
int objectsToCreate = 0;
int objectsToOverwrite = 0;
int objectsToDelete = 0;
wxULongLong dataToProcess;
if (getBytesToTransfer(*i,
config,
objectsToCreate,
objectsToOverwrite,
objectsToDelete,
dataToProcess)) //update status if some work was done (answer is always "yes" in this context)
statusUpdater->updateProcessedData(objectsToCreate + objectsToOverwrite + objectsToDelete, 0); //processed data is communicated in subfunctions!
}
removeRowsAtExit.markRow(i - fileCmp.begin());
break;
}
catch (FileError& error)
{
ErrorHandler::Response rv = statusUpdater->reportError(error.show());
if ( rv == ErrorHandler::IGNORE_ERROR)
break;
else if (rv == ErrorHandler::RETRY)
; //continue with loop
else
assert (false);
}
}
}
}
}
}
}
}
catch (const RuntimeException& theException)
{
statusUpdater->reportFatalError(theException.show().c_str());
return; //should be obsolete!
}
}
//###########################################################################################
//callback functionality for smooth progress indicators
struct CallBackData
{
StatusHandler* handler;
wxLongLong bytesTransferredLast;
};
#ifdef FFS_WIN
const DWORD COPY_FILE_COPY_SYMLINK = 0x00000800;
DWORD CALLBACK copyProcessCallback(
LARGE_INTEGER totalFileSize,
LARGE_INTEGER totalBytesTransferred,
LARGE_INTEGER streamSize,
LARGE_INTEGER streamBytesTransferred,
DWORD dwStreamNumber,
DWORD dwCallbackReason,
HANDLE hSourceFile,
HANDLE hDestinationFile,
LPVOID lpData)
{
//small performance optimization: it seems this callback function is called for every 64 kB (depending on cluster size).
static unsigned callNr = 0;
if (++callNr % 50 == 0) //reduce by factor of 50 =^ 10-20 calls/sec
{
CallBackData* sharedData = static_cast<CallBackData*>(lpData);
assert(totalBytesTransferred.HighPart >= 0);
if (totalBytesTransferred.HighPart < 0) //let's see if someone answers the call...
wxMessageBox(wxT("You've just discovered a bug in WIN32 API function \"CopyFileEx\"! \n\n\
Please write a mail to the author of FreeFileSync at zhnmju123@gmx.de and simply state that\n\
\"totalBytesTransferred.HighPart can be below zero\"!\n\n\
This will then be handled in future versions of FreeFileSync.\n\nThanks -ZenJu"), wxT("Warning"));
//inform about the (differential) processed amount of data
const wxLongLong currentBytesTransferred(totalBytesTransferred.HighPart, totalBytesTransferred.LowPart);
sharedData->handler->updateProcessedData(0, currentBytesTransferred - sharedData->bytesTransferredLast);
sharedData->bytesTransferredLast = currentBytesTransferred;
sharedData->handler->requestUiRefresh(false); //don't allow throwing exception within this call
if (sharedData->handler->abortIsRequested())
return PROGRESS_CANCEL;
}
return PROGRESS_CONTINUE;
}
#elif defined FFS_LINUX
void copyProcessCallback(const wxULongLong& totalBytesTransferred, void* data)
{ //called every 512 kB
CallBackData* sharedData = static_cast<CallBackData*>(data);
//convert to unsigned
const wxLongLong totalBytes = globalFunctions::convertToSigned(totalBytesTransferred);
//inform about the (differential) processed amount of data
sharedData->handler->updateProcessedData(0, totalBytes - sharedData->bytesTransferredLast);
sharedData->bytesTransferredLast = totalBytes;
sharedData->handler->requestUiRefresh(); //exceptions may be thrown here!
}
#endif
//copy file while executing statusUpdater->requestUiRefresh() calls
void SyncProcess::copyFileUpdating(const Zstring& source, const Zstring& target, const wxULongLong& totalBytesToCpy)
{
//create folders first (see http://sourceforge.net/tracker/index.php?func=detail&aid=2628943&group_id=234430&atid=1093080)
const Zstring targetDir = target.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR);
if (!wxDirExists(targetDir.c_str()))
{
const Zstring templateDir = source.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR);
FreeFileSync::createDirectory(targetDir, templateDir, !m_traverseDirSymLinks); //might throw FileError() exception
}
//start of (possibly) long-running copy process: ensure status updates are performed regularly
#ifdef FFS_WIN //update via call-back function
CallBackData sharedData;
sharedData.handler = statusUpdater;
//data.bytesTransferredLast =
DWORD copyFlags = COPY_FILE_FAIL_IF_EXISTS;
if (m_copyFileSymLinks) //copy symbolic links instead of the files pointed at
copyFlags |= COPY_FILE_COPY_SYMLINK;
if (!CopyFileEx( //same performance as CopyFile()
source.c_str(),
target.c_str(),
copyProcessCallback,
&sharedData,
NULL,
copyFlags))
{
//error situation: undo communication of processed amount of data
statusUpdater->updateProcessedData(0, sharedData.bytesTransferredLast * -1 );
statusUpdater->requestUiRefresh(); //abort if requested, don't show error message if cancelled by user!
Zstring errorMessage = Zstring(_("Error copying file:")) + wxT("\n\"") + source + wxT("\" -> \"") + target + wxT("\"");
errorMessage += Zstring(wxT("\n\n"));
errorMessage += FreeFileSync::getLastErrorFormatted(); //some additional windows error information
throw FileError(errorMessage);
}
//inform about the (remaining) processed amount of data
statusUpdater->updateProcessedData(0, globalFunctions::convertToSigned(totalBytesToCpy) - sharedData.bytesTransferredLast);
#elif defined FFS_LINUX //update via call-back function
CallBackData sharedData;
sharedData.handler = statusUpdater;
//data.bytesTransferredLast =
try
{
FreeFileSync::copyFile(source,
target,
m_copyFileSymLinks,
copyProcessCallback,
&sharedData);
}
catch (...)
{
//error situation: undo communication of processed amount of data
statusUpdater->updateProcessedData(0, sharedData.bytesTransferredLast * -1 );
throw;
}
//inform about the (remaining) processed amount of data
statusUpdater->updateProcessedData(0, globalFunctions::convertToSigned(totalBytesToCpy) - sharedData.bytesTransferredLast);
#endif
}
/*
//##### OLD IMPLEMENTATION WITHOUT SMOOTH PROGRESS INDICATOR ################
#ifdef FFS_LINUX
//handle execution of a method while updating the UI
class UpdateWhileCopying : public UpdateWhileExecuting
{
public:
UpdateWhileCopying() {}
~UpdateWhileCopying() {}
Zstring source;
Zstring target;
bool success;
Zstring errorMessage;
private:
virtual void longRunner()
{
try
{
FreeFileSync::copyFile(source, target);
}
catch (const FileError& e)
{
success = false;
errorMessage = e.show();
return;
}
success = true;
}
};
void copyFileUpdating(const Zstring& source, const Zstring& target, const wxULongLong& totalBytesToCpy, StatusHandler* updateClass)
{
//create folders first (see http://sourceforge.net/tracker/index.php?func=detail&aid=2628943&group_id=234430&atid=1093080)
const Zstring targetDir = target.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR);
if (!wxDirExists(targetDir.c_str()))
{
const Zstring templateDir = source.BeforeLast(GlobalResources::FILE_NAME_SEPARATOR);
FreeFileSync::createDirectory(targetDir, templateDir); //might throw FileError() exception
}
//update while copying via additional worker thread
static UpdateWhileCopying copyAndUpdate; //single instantiation: thread enters wait phase after each execution
copyAndUpdate.waitUntilReady();
//longRunner is called from thread, but no mutex needed here, since thread is in waiting state!
copyAndUpdate.source = source;
copyAndUpdate.target = target;
copyAndUpdate.execute(updateClass);
//no mutex needed here since longRunner is finished
if (!copyAndUpdate.success)
throw FileError(copyAndUpdate.errorMessage);
//inform about the processed amount of data
updateClass->updateProcessedData(0, totalBytesToCpy.ToDouble());
#endif
}
*/
|