forked from OpenSalamander/salamander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwnd5.cpp
1870 lines (1726 loc) · 81.2 KB
/
mainwnd5.cpp
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 2023 Open Salamander Authors
// SPDX-License-Identifier: GPL-2.0-or-later
#include "precomp.h"
#include "plugins.h"
#include "fileswnd.h"
#include "mainwnd.h"
#include "cfgdlg.h"
#include "dialogs.h"
void GetFileDateAndTimeFromPanel(DWORD validFileData, CPluginDataInterfaceEncapsulation* pluginData,
const CFileData* f, BOOL isDir, SYSTEMTIME* st, BOOL* validDate,
BOOL* validTime)
{
*validDate = FALSE;
*validTime = FALSE;
FILETIME ft;
if (validFileData & (VALID_DATA_DATE | VALID_DATA_TIME)) // aspon neco je v LastWrite
{
if (FileTimeToLocalFileTime(&f->LastWrite, &ft) &&
FileTimeToSystemTime(&ft, st))
{
if (validFileData & VALID_DATA_DATE)
*validDate = TRUE;
if (validFileData & VALID_DATA_TIME)
*validTime = TRUE;
}
}
if ((validFileData & VALID_DATA_PL_DATE) &&
pluginData->NotEmpty() &&
pluginData->GetLastWriteDate(f, isDir, st))
{
*validDate = TRUE;
}
if ((validFileData & VALID_DATA_PL_TIME) &&
pluginData->NotEmpty() &&
pluginData->GetLastWriteTime(f, isDir, st))
{
*validTime = TRUE;
}
if (!*validDate) // nemame nastaveny zadny datum, vynulujeme ho...
{
st->wYear = 1602;
st->wMonth = 1;
st->wDay = 1;
st->wDayOfWeek = 2;
}
if (!*validTime) // nemame nastaveny zadny cas, vynulujeme ho...
{
st->wHour = 0;
st->wMinute = 0;
st->wSecond = 0;
st->wMilliseconds = 0;
}
}
int MyCompareFileTime(FILETIME* lf, FILETIME* rf, int* foundDSTShifts, int* compResNoDSTShiftIgn)
{
int res = *compResNoDSTShiftIgn = CompareFileTime(lf, rf);
if (res != 0)
{
CQuadWord left(lf->dwLowDateTime, lf->dwHighDateTime);
CQuadWord right(rf->dwLowDateTime, rf->dwHighDateTime);
// vypocet rozdilu mezi casy a jeho zaokrouhleni
unsigned __int64 diff = (right.Value > left.Value) ? right.Value - left.Value : left.Value - right.Value;
if (diff == (unsigned __int64)3600 * 10000000 ||
diff == (unsigned __int64)2 * 3600 * 10000000)
{
(*foundDSTShifts)++;
if (Configuration.IgnoreDSTShifts)
res = 0;
}
}
return res;
}
// porovna dva soubory 'l' a 'r' podle datumu/casu
int CompareFilesByTime(CFilesWindow* leftPanel, const CFileData* l, BOOL lFAT,
CFilesWindow* rightPanel, CFileData* r, BOOL rFAT,
int* foundDSTShifts, int* compResNoDSTShiftIgn)
{
SLOW_CALL_STACK_MESSAGE3("CompareFilesByTime(, , %d, , , %d, ,)", lFAT, rFAT);
int res = *compResNoDSTShiftIgn = 0;
SYSTEMTIME stLeft;
BOOL validDateLeft, validTimeLeft;
GetFileDateAndTimeFromPanel(leftPanel->ValidFileData, &leftPanel->PluginData, l, FALSE, &stLeft,
&validDateLeft, &validTimeLeft);
SYSTEMTIME stRight;
BOOL validDateRight, validTimeRight;
GetFileDateAndTimeFromPanel(rightPanel->ValidFileData, &rightPanel->PluginData, r, FALSE, &stRight,
&validDateRight, &validTimeRight);
if (validDateLeft && validDateRight || // pokud neni znamy cas, je inicializovan na 0:00:00.000, takze cas neresime...
!validDateLeft && !validDateRight && validTimeLeft && validTimeRight) // datum je neznamy (nulovany), casy jsou oba zname, takze porovnani nic nebrani
{
FILETIME lf, rf;
if (Configuration.UseTimeResolution)
{
// oriznuti casu s presnosti na sekundy
stLeft.wMilliseconds = 0;
stRight.wMilliseconds = 0;
// prevod casu na cisla
if (!SystemTimeToFileTime(&stLeft, &lf))
{
TRACE_E("CompareFilesByTime(): date&time of left file is invalid!");
*compResNoDSTShiftIgn = res = -1;
}
else
{
if (!SystemTimeToFileTime(&stRight, &rf))
{
TRACE_E("CompareFilesByTime(): date&time of right file is invalid!");
*compResNoDSTShiftIgn = res = 1;
}
else
{
CQuadWord left(lf.dwLowDateTime, lf.dwHighDateTime);
CQuadWord right(rf.dwLowDateTime, rf.dwHighDateTime);
unsigned __int64 ld = left.Value / 10000000;
unsigned __int64 rd = right.Value / 10000000;
// vypocet rozdilu mezi casy a jeho zaokrouhleni
unsigned __int64 diff = (rd > ld) ? rd - ld : ld - rd;
*compResNoDSTShiftIgn = res = (left < right) ? -1 : 1;
if (diff < 100000)
{
int d = (int)diff;
if (d <= Configuration.TimeResolution)
*compResNoDSTShiftIgn = res = 0;
else
{
int dIgnOneHour = d < 3600 ? 3600 - d : d - 3600;
int dIgnTwoHours = d < 2 * 3600 ? 2 * 3600 - d : d - 2 * 3600;
if (dIgnOneHour <= Configuration.TimeResolution || dIgnTwoHours <= Configuration.TimeResolution)
{
(*foundDSTShifts)++;
if (Configuration.IgnoreDSTShifts)
res = 0;
}
}
}
}
}
}
else
{
if (!SystemTimeToFileTime(&stLeft, &lf))
{
TRACE_E("CompareFilesByTime(): date&time of left file is invalid!");
*compResNoDSTShiftIgn = res = -1;
}
else
{
if (!SystemTimeToFileTime(&stRight, &rf))
{
TRACE_E("CompareFilesByTime(): date&time of right file is invalid!");
*compResNoDSTShiftIgn = res = 1;
}
else
{
if (lFAT == rFAT)
res = MyCompareFileTime(&lf, &rf, foundDSTShifts, compResNoDSTShiftIgn); // stejne filesystemy -> o.k.
else // FAT + jiny filesystem -> uprava neFAT casu na FAT cas (po 2 sekundach)
{
WORD date, time; // FAT hodnoty
FILETIME t;
if (lFAT)
{
FileTimeToDosDateTime(&rf, &date, &time);
DosDateTimeToFileTime(date, time, &t);
res = MyCompareFileTime(&lf, &t, foundDSTShifts, compResNoDSTShiftIgn);
}
else
{
FileTimeToDosDateTime(&lf, &date, &time);
DosDateTimeToFileTime(date, time, &t);
res = MyCompareFileTime(&t, &rf, foundDSTShifts, compResNoDSTShiftIgn);
}
}
}
}
}
}
else
{
if (validDateLeft)
res = 1; // jakykoliv datum je novejsi nez neznamy datum
else
{
if (validDateRight)
res = -1; // neznamy datum je starsi nez jakykoliv datum
else // ani jeden datum neni znamy
{
if (validTimeLeft)
res = 1; // jakykoliv cas je novejsi nez neznamy cas
else
{
if (validTimeRight)
res = -1; // neznamy cas je starsi nez jakykoliv cas
}
}
}
*compResNoDSTShiftIgn = res;
}
return res;
}
void GetFileSizeFromPanel(DWORD validFileData, CPluginDataInterfaceEncapsulation* pluginData,
const CFileData* f, BOOL isDir, CQuadWord* size, BOOL* validSize)
{
*validSize = FALSE;
if (validFileData & VALID_DATA_SIZE)
{
*validSize = TRUE;
*size = f->Size;
}
else
{
if ((validFileData & VALID_DATA_PL_SIZE) &&
pluginData->NotEmpty() &&
pluginData->GetByteSize(f, isDir, size))
{
*validSize = TRUE;
}
}
}
// porovna dva soubory 'l' a 'r' podle velikosti
int CompareFilesBySize(CFilesWindow* leftPanel, CFileData* l, CFilesWindow* rightPanel, CFileData* r)
{
CALL_STACK_MESSAGE_NONE
BOOL leftValidSize;
CQuadWord leftSize;
GetFileSizeFromPanel(leftPanel->ValidFileData, &leftPanel->PluginData, l, FALSE, &leftSize, &leftValidSize);
BOOL rightValidSize;
CQuadWord rightSize;
GetFileSizeFromPanel(rightPanel->ValidFileData, &rightPanel->PluginData, r, FALSE, &rightSize, &rightValidSize);
if (leftValidSize && rightValidSize)
{
if (leftSize != rightSize)
return (leftSize > rightSize) ? 1 : -1;
}
else
{
if (leftValidSize)
return 1; // cokoliv je vetsi nez neznama velikost
else
{
if (rightValidSize)
return -1; // neznama velikost je mensi nez cokoliv
}
}
return 0;
}
// porovna soubory 'file1' a 'file2', urcene plnou cestou, podle obsahu
// v pripade uspesneho porovnani vraci funkce TRUE a nastavuje promennou
// 'different' (TRUE v pripade nalezeni rozdilu, jinak FALSE)
// v pripade chyby nebo preruseni operace vraci FALSE a nastavuje promennou
// 'canceled' (TRUE pokud uzivatel stornoval operaci, jinak FALSE)
#define COMPARE_BUFFER_SIZE (2 * 1024 * 1024) // velikost bufferu pro porovnani v bytech (nemusi se nutne pouzivat cely)
#define COMPARE_BLOCK_SIZE (32 * 1024) // velikost bloku souvisle cteneho ze souboru; POZOR: COMPARE_BUFFER_SIZE musi byt delitelne COMPARE_BLOCK_SIZE
#define COMPARE_BLOCK_GROUP 8 // po kolika blocich najednou se muze cist pri dost rychlem cteni (vic nez 1 MB/s, viz COMPARE_BUF_TIME_LIMIT); POZOR: pocet bloku v bufferu (COMPARE_BUFFER_SIZE / COMPARE_BLOCK_SIZE) musi byt delitelny COMPARE_BLOCK_GROUP
#define COMPARE_BUF_TIME_LIMIT 2000 // limit v milisekundach pro nacteni celeho bufferu (COMPARE_BUFFER_SIZE) - je-li splneny, cte se po skupinach bloku COMPARE_BLOCK_GROUP, coz na Vista+ 2x az 3x zrychluje cteni ze site; jinak se cte po jednom bloku (COMPARE_BLOCK_SIZE)
void AddProgressSizeWithLimit(CCmpDirProgressDialog* progressDlg, DWORD read, CQuadWord* fileProgressTotal, const CQuadWord& sizeLimit)
{
if (*fileProgressTotal + CQuadWord(read, 0) > sizeLimit)
read = (DWORD)(sizeLimit - *fileProgressTotal).Value;
if (read > 0)
{
progressDlg->AddSize(CQuadWord(read, 0));
*fileProgressTotal += CQuadWord(read, 0);
}
}
BOOL CompareFilesByContent(HWND hWindow, CCmpDirProgressDialog* progressDlg,
const char* file1, const char* file2, const CQuadWord& bothFileSize,
BOOL* different, BOOL* canceled)
{
char message[2 * MAX_PATH + 200]; // potrebujeme 2*MAX_PATH pro cestu a potom rezervu pro chybovou hlasku
BOOL ret = FALSE;
*canceled = FALSE;
// DWORD totalTi = GetTickCount();
HANDLE hFile1 = HANDLES_Q(CreateFile(file1, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL));
HANDLE hFile2 = hFile1 != INVALID_HANDLE_VALUE ? HANDLES_Q(CreateFile(file2, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL))
: INVALID_HANDLE_VALUE;
DWORD err = GetLastError();
if (!progressDlg->Continue()) // dame prilezitost dialogu prekreslit se na 100% predchoziho souboru (otevirani souboru je nejdelsi (3ms) pri kopirovani malych souboru (0,1ms))
*canceled = TRUE;
// nastavime texty v progress dialogu
progressDlg->SetSource(file1);
progressDlg->SetTarget(file2);
// nastavime total ('bothFileSize') a actual (0%) file-progress
CQuadWord fileProgressTotal(0, 0);
progressDlg->SetFileSize(bothFileSize);
progressDlg->SetActualFileSize(CQuadWord(0, 0));
if (hFile1 != INVALID_HANDLE_VALUE)
{
if (hFile2 != INVALID_HANDLE_VALUE)
{
if (!*canceled)
{
char* buffer1 = (char*)malloc(COMPARE_BUFFER_SIZE);
// char *buffer2 = (char *)malloc(COMPARE_BUFFER_SIZE);
char buffer2[COMPARE_BLOCK_GROUP * COMPARE_BLOCK_SIZE];
DWORD read1;
DWORD read2;
// DWORD measureStart = GetTickCount();
// unsigned __int64 measuredSize = 0;
BOOL readErr = FALSE;
BOOL readingIsFast1 = FALSE; // FALSE = cekame az rychlost dosahne limitu a budeme moct cist po skupinach (COMPARE_BLOCK_GROUP)
BOOL readingIsFast2 = FALSE;
while (TRUE)
{
DWORD bufSize = COMPARE_BUFFER_SIZE;
int blockCount = bufSize / COMPARE_BLOCK_SIZE; // pocet bloku v bufSize
// nacteme do 'buffer1' dalsi cast ze souboru 'file1'
// cteme po 32KB blocich cely buffer z jednoho souboru (na W2K a XP je to pri obou souborech z jednoho fyzickeho disku nejrychlejsi, na Viste je to jen mirne pomalejsi nez souvisle cteni)
read1 = 0;
DWORD readBegTime = GetTickCount();
for (int block = 0; block < blockCount;)
{
DWORD locRead;
BOOL canReadGroup = readingIsFast1 && (block % COMPARE_BLOCK_GROUP) == 0 && block + COMPARE_BLOCK_GROUP <= blockCount;
DWORD readBlockSize = canReadGroup ? COMPARE_BLOCK_GROUP * COMPARE_BLOCK_SIZE : COMPARE_BLOCK_SIZE;
if (!ReadFile(hFile1, buffer1 + block * COMPARE_BLOCK_SIZE, readBlockSize, &locRead, NULL))
{
err = GetLastError();
readErr = TRUE;
_snprintf_s(message, _TRUNCATE, LoadStr(IDS_ERROR_READING_FILE), file1, GetErrorText(err));
progressDlg->FlushDataToControls();
if (SalMessageBox(hWindow, message, LoadStr(IDS_ERRORTITLE),
MB_OKCANCEL | MB_ICONEXCLAMATION) == IDCANCEL)
{
*canceled = TRUE;
}
break;
}
AddProgressSizeWithLimit(progressDlg, locRead, &fileProgressTotal, bothFileSize);
if (!progressDlg->Continue()) // dame prilezitost dialogu prekreslit se
{
*canceled = TRUE;
break;
}
read1 += locRead;
if (locRead != readBlockSize)
break; // EOF
if (GetTickCount() - readBegTime > 200)
{ // pomale cteni (asi pomaly sitovy disk (VPN) nebo floppy) - zmensime buffer, v nejhorsim se dostaneme na porovnavani po jednom bloku
blockCount = block + (canReadGroup ? COMPARE_BLOCK_GROUP : 1);
bufSize = blockCount * COMPARE_BLOCK_SIZE;
break;
}
block += canReadGroup ? COMPARE_BLOCK_GROUP : 1;
}
if (readErr || *canceled)
break;
readingIsFast1 = WindowsVistaAndLater && // na W2K/XP by tohle nemelo vest k urychleni, takze tam nebudeme drazdit hada bosou nohou
GetTickCount() - readBegTime < (DWORD)(((unsigned __int64)read1 * COMPARE_BUF_TIME_LIMIT) / COMPARE_BUFFER_SIZE); // pomerime rychlost, aby byla vic nez 1 MB/s
/*
// cteme cely buffer najednou z jednoho souboru (na Viste je to pri obou souborech z jednoho fyzickeho disku a velkem bufferu mirne rychlejsi nez cteni po 32KB blocich)
if (!ReadFile(hFile1, buffer1, bufSize, &read1, NULL))
{
err = GetLastError();
sprintf(message, LoadStr(IDS_ERROR_READING_FILE), file1, GetErrorText(err));
progressDlg->FlushDataToControls();
if (SalMessageBox(hWindow, message, LoadStr(IDS_ERRORTITLE),
MB_OKCANCEL | MB_ICONEXCLAMATION) == IDCANCEL)
{
*canceled = TRUE;
}
break;
}
*/
// nacteme do 'buffer2' dalsi cast ze souboru 'file2'
// cteme po blocich cely buffer z jednoho souboru (na W2K a XP je to pri obou souborech z jednoho fyzickeho disku nejrychlejsi, na Viste je to jen mirne pomalejsi nez souvisle cteni)
read2 = 0;
readBegTime = GetTickCount();
for (int block = 0; block < blockCount;)
{
DWORD locRead;
BOOL canReadGroup = readingIsFast2 && (block % COMPARE_BLOCK_GROUP) == 0 && block + COMPARE_BLOCK_GROUP <= blockCount;
DWORD readBlockSize = canReadGroup ? COMPARE_BLOCK_GROUP * COMPARE_BLOCK_SIZE : COMPARE_BLOCK_SIZE;
if (!ReadFile(hFile2, buffer2, readBlockSize, &locRead, NULL))
{
err = GetLastError();
readErr = TRUE;
_snprintf_s(message, _TRUNCATE, LoadStr(IDS_ERROR_READING_FILE), file2, GetErrorText(err));
progressDlg->FlushDataToControls();
if (SalMessageBox(hWindow, message, LoadStr(IDS_ERRORTITLE),
MB_OKCANCEL | MB_ICONEXCLAMATION) == IDCANCEL)
{
*canceled = TRUE;
}
break;
}
AddProgressSizeWithLimit(progressDlg, locRead, &fileProgressTotal, bothFileSize);
if (!progressDlg->Continue()) // dame prilezitost dialogu prekreslit se
{
*canceled = TRUE;
break;
}
read2 += locRead;
if (read1 < read2 || // soubory jsou ted ruzne dlouhe => obsah je ruzny
locRead > 0 && memcmp(buffer1 + block * COMPARE_BLOCK_SIZE, buffer2, locRead) != 0)
{ // obsah souboru je ruzny, nema smysl pokracovat ve cteni
*different = TRUE;
ret = TRUE;
break;
}
if (locRead != readBlockSize)
{ // uz se nam nepodarilo nacist cely buffer (EOF), soubory jsou shodne
*different = FALSE;
ret = TRUE;
break;
}
block += canReadGroup ? COMPARE_BLOCK_GROUP : 1;
if (readingIsFast2 &&
GetTickCount() - readBegTime > (DWORD)(((unsigned __int64)read2 * COMPARE_BUF_TIME_LIMIT) / COMPARE_BUFFER_SIZE))
{
readingIsFast2 = FALSE; // detekce zpomaleni, dale jedeme po jednotlivych blocich (kvuli plynulosti porgresu a moznosti zcancelovani)
}
}
if (readErr || ret || *canceled)
break;
readingIsFast2 = WindowsVistaAndLater && // na W2K/XP by tohle nemelo vest k urychleni, takze tam nebudeme drazdit hada bosou nohou
GetTickCount() - readBegTime < (DWORD)(((unsigned __int64)read2 * COMPARE_BUF_TIME_LIMIT) / COMPARE_BUFFER_SIZE); // pomerime rychlost, aby byla vic nez 1 MB/s
/*
// cteme cely buffer najednou z jednoho souboru (na Viste je to pri obou souborech z jednoho fyzickeho disku a velkem bufferu mirne rychlejsi nez cteni po 32KB blocich)
if (!ReadFile(hFile2, buffer2, bufSize, &read2, NULL))
{
err = GetLastError();
sprintf(message, LoadStr(IDS_ERROR_READING_FILE), file2, GetErrorText(err));
progressDlg->FlushDataToControls();
if (SalMessageBox(hWindow, message, LoadStr(IDS_ERRORTITLE),
MB_OKCANCEL | MB_ICONEXCLAMATION) == IDCANCEL)
{
*canceled = TRUE;
}
break;
}
*/
/*
// tyto testy se pri blokovem cteni provadi vyse
AddProgressSizeWithLimit(progressDlg, read1 + read2, &fileProgressTotal, bothFileSize);
if (!progressDlg->Continue()) // dame prilezitost dialogu prekreslit se
{
*canceled = TRUE;
break;
}
if (read1 != read2 || // soubory jsou ted ruzne dlouhe => obsah je ruzny
read1 > 0 && memcmp(buffer1, buffer2, read1) != 0)
{ // obsah souboru je ruzny, nema smysl pokracovat ve cteni
*different = TRUE;
ret = TRUE;
break;
}
if (read1 != bufSize)
{ // uz se nam nepodarilo nacist cely buffer, soubory jsou shodne
*different = FALSE;
ret = TRUE;
break;
}
*/
/*
measuredSize += bufSize;
DWORD ti = GetTickCount() - measureStart;
if (ti >= 500)
{
unsigned __int64 speed = (measuredSize * 1000) / ti;
TRACE_I("speed: " << (DWORD)(speed / 1024));
measureStart = GetTickCount();
measuredSize = 0;
}
*/
}
free(buffer1);
// free(buffer2);
}
HANDLES(CloseHandle(hFile2));
}
else
{
_snprintf_s(message, _TRUNCATE, LoadStr(IDS_ERROR_OPENING_FILE), file2, GetErrorText(err));
progressDlg->FlushDataToControls();
if (SalMessageBox(hWindow, message, LoadStr(IDS_ERRORTITLE),
MB_OKCANCEL | MB_ICONEXCLAMATION) == IDCANCEL)
{
*canceled = TRUE;
}
}
HANDLES(CloseHandle(hFile1));
}
else
{
_snprintf_s(message, _TRUNCATE, LoadStr(IDS_ERROR_OPENING_FILE), file1, GetErrorText(err));
progressDlg->FlushDataToControls();
if (SalMessageBox(hWindow, message, LoadStr(IDS_ERRORTITLE),
MB_OKCANCEL | MB_ICONEXCLAMATION) == IDCANCEL)
{
*canceled = TRUE;
}
}
if (!*canceled) // musime dotahnout total progress na konec souboru
progressDlg->AddSize(bothFileSize - fileProgressTotal);
// TRACE_I("CompareFilesByContent(): total time (in secs): " << ((GetTickCount() - totalTi) / 1000));
return ret;
}
// nacte adresare a soubory do poli 'dirs' a 'files'
// zdrojova cesta je urcena souctem cesty v panelu 'panel' a 'subPath'
// 'hWindow' je okno pro zobrazovani messageboxu
// 'progressDlg' slouzi k posouvani progresu po nacteni X souboru/adresaru
// vraci TRUE v pripade uspesneho nacteni obou poli
// vraci FALSE v pripade chyby nebo stornovani operace uzivatelem
// (bude pak nastavena promenna 'canceled')
//
// podporuje ptDisk a ptZIPArchive
BOOL ReadDirsAndFilesAux(HWND hWindow, DWORD flags, CCmpDirProgressDialog* progressDlg,
CFilesWindow* panel, const char* subPath,
CFilesArray* dirs, CFilesArray* files, BOOL* canceled, BOOL getTotal)
{
char message[2 * MAX_PATH + 200];
char path[MAX_PATH];
BOOL ignFileNames = (flags & COMPARE_DIRECTORIES_IGNFILENAMES) != 0;
BOOL ignDirNames = (flags & COMPARE_DIRECTORIES_IGNDIRNAMES) != 0;
if (panel->Is(ptDisk))
{
BOOL pathAppended = TRUE;
lstrcpyn(path, panel->GetPath(), MAX_PATH);
pathAppended &= SalPathAppend(path, subPath, MAX_PATH);
pathAppended &= SalPathAppend(path, "*", MAX_PATH);
if (!pathAppended)
{
SalMessageBox(hWindow, LoadStr(IDS_TOOLONGNAME), LoadStr(IDS_COMPAREDIRSTITLE), MB_OK | MB_ICONEXCLAMATION);
*canceled = TRUE;
return FALSE;
}
DWORD counter = 0;
WIN32_FIND_DATA data;
HANDLE hFind;
hFind = HANDLES_Q(FindFirstFile(path, &data));
if (hFind == INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
if (err != ERROR_FILE_NOT_FOUND && err != ERROR_NO_MORE_FILES)
{
if (getTotal)
*canceled = FALSE; // pouze ziskavame velikost, nema smysl prudit, skipneme chybu
else
{
_snprintf_s(message, _TRUNCATE, LoadStr(IDS_CANNOTREADDIR), path, GetErrorText(err));
progressDlg->FlushDataToControls();
*canceled = SalMessageBox(hWindow, message, LoadStr(IDS_ERRORTITLE),
MB_OKCANCEL | MB_ICONEXCLAMATION) == IDCANCEL;
}
return FALSE;
}
return TRUE;
}
do
{
if (data.cFileName[0] != 0 &&
(data.cFileName[0] != '.' ||
(data.cFileName[1] != 0 && (data.cFileName[1] != '.' || data.cFileName[2] != 0))))
{
if (counter++ > 200) // po nacteni 200 polozek
{
if (!progressDlg->Continue()) // dame prilezitost dialogu prekreslit se
{
HANDLES(FindClose(hFind));
*canceled = TRUE;
return FALSE;
}
counter = 0;
}
CFileData file;
// inicializace clenu struktury, ktere uz dale nebudeme menit
file.DosName = NULL;
file.PluginData = -1;
file.Association = 0;
file.Selected = 0;
file.Shared = 0;
file.Archive = 0;
file.SizeValid = 0;
file.Dirty = 0;
file.CutToClip = 0;
file.IconOverlayIndex = ICONOVERLAYINDEX_NOTUSED;
file.IconOverlayDone = 0;
file.Hidden = 0;
file.IsLink = 0;
file.IsOffline = 0;
int nameLen = (int)strlen(data.cFileName);
//--- jmeno
file.Name = (char*)malloc(nameLen + 1); // alokace
if (file.Name == NULL)
{
TRACE_E(LOW_MEMORY);
HANDLES(FindClose(hFind));
*canceled = TRUE;
return FALSE;
}
memmove(file.Name, data.cFileName, nameLen + 1); // kopie textu
file.NameLen = nameLen;
//--- pripona
if (!Configuration.SortDirsByExt && (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) // jde o ptDisk
{
file.Ext = file.Name + file.NameLen; // adresare nemaji pripony
}
else
{
const char* s = data.cFileName + nameLen;
while (--s >= data.cFileName && *s != '.')
;
// if (s > data.cFileName) file.Ext = file.Name + (s - data.cFileName + 1); // ".cvspass" ve Windows je pripona ...
if (s >= data.cFileName)
file.Ext = file.Name + (s - data.cFileName + 1);
else
file.Ext = file.Name + file.NameLen;
}
//--- ostatni
file.Size = CQuadWord(data.nFileSizeLow, data.nFileSizeHigh);
file.Attr = data.dwFileAttributes;
file.LastWrite = data.ftLastWriteTime;
if (file.Attr & FILE_ATTRIBUTE_DIRECTORY)
{
if (!ignDirNames || !Configuration.CompareIgnoreDirsMasks.AgreeMasks(file.Name, NULL))
{
dirs->Add(file);
if (!dirs->IsGood())
{
TRACE_E(LOW_MEMORY);
free(file.Name);
HANDLES(FindClose(hFind));
*canceled = TRUE;
return FALSE;
}
}
else
free(file.Name);
}
else
{
if (!ignFileNames || !Configuration.CompareIgnoreFilesMasks.AgreeMasks(file.Name, file.Ext))
{
files->Add(file);
if (!files->IsGood())
{
TRACE_E(LOW_MEMORY);
free(file.Name);
HANDLES(FindClose(hFind));
*canceled = TRUE;
return FALSE;
}
}
else
free(file.Name);
}
}
} while (FindNextFile(hFind, &data));
DWORD err = GetLastError();
if (err != ERROR_NO_MORE_FILES)
{
if (getTotal)
*canceled = FALSE; // pouze ziskavame velikost, nema smysl prudit, skipneme chybu
else
{
_snprintf_s(message, _TRUNCATE, LoadStr(IDS_CANNOTREADDIR), path, GetErrorText(err));
progressDlg->FlushDataToControls();
*canceled = SalMessageBox(hWindow, message, LoadStr(IDS_ERRORTITLE),
MB_OK | MB_ICONEXCLAMATION) == IDCANCEL;
}
HANDLES(FindClose(hFind));
return FALSE;
}
HANDLES(FindClose(hFind));
}
else if (panel->Is(ptZIPArchive))
{
CFilesArray* zipDirs = panel->GetArchiveDirDirs(subPath);
CFilesArray* zipFiles = panel->GetArchiveDirFiles(subPath);
if (zipFiles == NULL || zipDirs == NULL)
{
*canceled = TRUE;
return FALSE; // low memory. vypadnem
}
files->SetDeleteData(FALSE); // jen melke kopie dat
dirs->SetDeleteData(FALSE); // jen melke kopie dat
int i;
for (i = 0; i < zipDirs->Count; i++)
{
CFileData* f = &zipDirs->At(i);
if (!ignDirNames || !Configuration.CompareIgnoreDirsMasks.AgreeMasks(f->Name, NULL))
{
dirs->Add(*f);
if (!dirs->IsGood())
{
TRACE_E(LOW_MEMORY);
*canceled = TRUE;
return FALSE; // low memory. vypadnem
}
}
}
for (i = 0; i < zipFiles->Count; i++)
{
CFileData* f = &zipFiles->At(i);
if (!ignFileNames || !Configuration.CompareIgnoreFilesMasks.AgreeMasks(f->Name, f->Ext))
{
files->Add(*f);
if (!files->IsGood())
{
TRACE_E(LOW_MEMORY);
*canceled = TRUE;
return FALSE; // low memory. vypadnem
}
}
}
}
else
{
TRACE_E("not implemented");
*canceled = TRUE;
return FALSE;
}
return TRUE;
}
// rekurzivni funkce hledajici rozdil mezi adresari
// adresare jsou urcene cestou v levem a pravem panelu a promennou 'leftSubDir' a 'rightSubDir'
// leftFAT a rightFAT udavaji, zda je prislusny panel na FAT systemu; pokud je otevreny
// arhiv, bude prislusna xxxFAT nastavena na FALSE
// 'flags' specifikuje kriteria porovnani a je z rodiny COMPARE_DIRECTORIES_xxx
// funkce vraci TRUE v pripade uspesneho porovnani a nastavuje promennou 'different' (TRUE
// pokud se adresare lisi, jinak FALSE).
// v pripade chyby nebo preruseni operace uzivatelem vraci funkce FALSE a nastavuje
// promennou 'canceled' (TRUE v pripade preruseni uzivatelem, jinak FALSE)
// podporuje ptDisk a ptZIPArchive
BOOL CompareDirsAux(HWND hWindow, CCmpDirProgressDialog* progressDlg,
CFilesWindow* leftPanel, const char* leftSubDir, BOOL leftFAT,
CFilesWindow* rightPanel, const char* rightSubDir, BOOL rightFAT,
DWORD flags, BOOL* different, BOOL* canceled,
BOOL getTotal, CQuadWord* total, int* foundDSTShifts)
{
// left/rightPanel a left/rightSubDir urcuji cestu,
// jejiz adresare a soubory budou ulozeny do nasledujicich poli
CFilesArray leftDirs;
CFilesArray rightDirs;
int foundDSTShiftsInThisDir = 0;
{ // lokalni blok kvuli omezeni platnosti leftFiles & rightFiles (setrime pamet pred zarekurzenim do podadresaru)
CFilesArray leftFiles;
CFilesArray rightFiles;
// z predesleho porovnani souboru zustala nastavena hodnota
if ((flags & COMPARE_DIRECTORIES_BYCONTENT) && (!getTotal))
progressDlg->SetActualFileSize(CQuadWord(0, 0)); // nastavime 0%
// nastavime texty v progress dialogu
BOOL pathAppended = TRUE;
char message[MAX_PATH + 200];
strcpy(message, leftPanel->GetPath());
pathAppended &= SalPathAppend(message, leftSubDir, MAX_PATH + 200);
progressDlg->SetSource(message);
strcpy(message, rightPanel->GetPath());
pathAppended &= SalPathAppend(message, rightSubDir, MAX_PATH + 200);
progressDlg->SetTarget(message);
if (!pathAppended)
{
SalMessageBox(hWindow, LoadStr(IDS_TOOLONGNAME), LoadStr(IDS_COMPAREDIRSTITLE), MB_OK | MB_ICONEXCLAMATION);
*canceled = TRUE;
return FALSE;
}
if (!progressDlg->Continue())
{
*canceled = TRUE;
return FALSE;
}
// nacteme adresare a soubory pro levy
if (!ReadDirsAndFilesAux(hWindow, flags, progressDlg, leftPanel, leftSubDir, &leftDirs, &leftFiles, canceled, getTotal))
return FALSE;
// a pravy panel
if (!ReadDirsAndFilesAux(hWindow, flags, progressDlg, rightPanel, rightSubDir, &rightDirs, &rightFiles, canceled, getTotal))
return FALSE;
// pokud se nerovnaji pocty v levem a pravem panelu, jsou adresare ruzne
if (leftDirs.Count != rightDirs.Count || leftFiles.Count != rightFiles.Count)
{
*different = TRUE;
return TRUE;
}
// ted uz plati, ze pocet adresaru soubor je stejny v obou panelech
// seradime pole podle jmena, abychom je mohli vzajeme porovnat
if (leftDirs.Count > 1)
SortNameExt(leftDirs, 0, leftDirs.Count - 1, FALSE);
if (leftFiles.Count > 1)
SortNameExt(leftFiles, 0, leftFiles.Count - 1, FALSE);
if (rightDirs.Count > 1)
SortNameExt(rightDirs, 0, rightDirs.Count - 1, FALSE);
if (rightFiles.Count > 1)
SortNameExt(rightFiles, 0, rightFiles.Count - 1, FALSE);
// napred porovname soubory podle jmena, casu a atributu
// porovnani podle obsahu odlozime, protoze je pomale a pokud
// nalezneme rozdil uz na teto urovni, usetrime cas
BOOL timeDiffWithDSTShiftExists = FALSE;
CFileData *leftFile, *rightFile;
int i;
for (i = 0; i < leftFiles.Count; i++)
{
leftFile = &leftFiles[i];
rightFile = &rightFiles[i];
// By Name
if (CmpNameExtIgnCase(*leftFile, *rightFile) != 0)
{
*different = TRUE;
return TRUE;
}
// By Size
if (flags & COMPARE_DIRECTORIES_BYSIZE)
{
if (CompareFilesBySize(leftPanel, leftFile, rightPanel, rightFile) != 0)
{
*different = TRUE;
return TRUE;
}
}
// By Attributes
if (flags & COMPARE_DIRECTORIES_BYATTR)
{
if ((leftPanel->ValidFileData & VALID_DATA_ATTRIBUTES) &&
(rightPanel->ValidFileData & VALID_DATA_ATTRIBUTES))
{
if ((leftFile->Attr & DISPLAYED_ATTRIBUTES) != (rightFile->Attr & DISPLAYED_ATTRIBUTES))
{
*different = TRUE;
return TRUE;
}
}
}
// porovname soubory podle obsahu -- tady jen porovname velikosti souboru jako trivialni test neshody obsahu souboru
if (flags & COMPARE_DIRECTORIES_BYCONTENT)
{
if (leftFile->Size != rightFile->Size)
{
if (!leftPanel->Is(ptDisk) || !rightPanel->Is(ptDisk))
{
TRACE_E("not implemented");
*canceled = FALSE;
return FALSE;
}
*different = TRUE;
return TRUE;
}
}
// By Time -- kvuli DST time shiftu testujeme casy az jako posledni (nebudeme hlasit DST problem, kdyz se soubory lisi treba jeste ve velikosti nebo atributech)
if (flags & COMPARE_DIRECTORIES_BYTIME)
{
int isDSTShift = 0; // 1 = casy prave porovnavane dvojice souboru se lisi presne o jednu nebo dve hodiny, 0 = nelisi
int compResNoDSTShiftIgn;
if (CompareFilesByTime(leftPanel, leftFile, leftFAT, rightPanel, rightFile, rightFAT,
&isDSTShift, &compResNoDSTShiftIgn) != 0)
{
if (isDSTShift != 0)
timeDiffWithDSTShiftExists = TRUE; // zkusime najit jeste jinou diferenci (idealne bez DST warningu)
else
{
*different = TRUE;
return TRUE;
}
}
else
foundDSTShiftsInThisDir += isDSTShift;
}
}
// porovname adresare
CFileData *leftDir, *rightDir;
for (i = 0; i < leftDirs.Count; i++)
{
leftDir = &leftDirs[i];
rightDir = &rightDirs[i];
// By Name
if (CmpNameExtIgnCase(*leftDir, *rightDir) != 0)
{
*different = TRUE;
return TRUE;
}
// By Attributes
if (flags & COMPARE_DIRECTORIES_SUBDIRS_ATTR)
{
if ((leftPanel->ValidFileData & VALID_DATA_ATTRIBUTES) &&
(rightPanel->ValidFileData & VALID_DATA_ATTRIBUTES))
{
if ((leftDir->Attr & DISPLAYED_ATTRIBUTES) != (rightDir->Attr & DISPLAYED_ATTRIBUTES))
{
*different = TRUE;
return TRUE;
}
}
}
// adresare podle casu neporovnavame
}
if (timeDiffWithDSTShiftExists) // nenasli jsme jiny rozdil, tak ohlasime neignorovany DST time shift vcetne warningu
{
(*foundDSTShifts)++;
*different = TRUE;
return TRUE;
}
// porovname soubory podle obsahu
if (flags & COMPARE_DIRECTORIES_BYCONTENT)
{
if (!leftPanel->Is(ptDisk) || !rightPanel->Is(ptDisk))
{
TRACE_E("not implemented");
*canceled = FALSE;
return FALSE;
}
for (i = 0; i < leftFiles.Count; i++)
{
leftFile = &leftFiles[i];
rightFile = &rightFiles[i];
if (leftFile->Size == rightFile->Size)
{
if (leftFile->Size != CQuadWord(0, 0))
{
if (!getTotal)
{
// slozime plne cesty k oboum souborum
pathAppended = TRUE;
char leftFilePath[2 * MAX_PATH];
strcpy(leftFilePath, leftPanel->GetPath());
pathAppended &= SalPathAppend(leftFilePath, leftSubDir, 2 * MAX_PATH);
pathAppended &= SalPathAppend(leftFilePath, leftFile->Name, 2 * MAX_PATH);
char rightFilePath[2 * MAX_PATH];
strcpy(rightFilePath, rightPanel->GetPath());
pathAppended &= SalPathAppend(rightFilePath, rightSubDir, 2 * MAX_PATH);
pathAppended &= SalPathAppend(rightFilePath, rightFile->Name, 2 * MAX_PATH);