-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathPartFile.cpp
More file actions
4609 lines (4009 loc) · 141 KB
/
Copy pathPartFile.cpp
File metadata and controls
4609 lines (4009 loc) · 141 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
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
//
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2026 aMule Team ( https://amule-org.github.io )
// Copyright (c) 2002-2011 Merkur ( [email protected] / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program 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.
//
// This program 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.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include <wx/wx.h>
#include "PartFile.h" // Interface declarations.
#include "PartFileWriteThread.h" // Needed for PB_READY etc.
#include "PartFileHashThread.h" // Needed for QueueHashCheck
#include "config.h" // Needed for VERSION
#include <protocol/kad/Constants.h>
#include <protocol/ed2k/Client2Client/TCP.h>
#include <protocol/Protocols.h>
#include <common/DataFileVersion.h>
#include <common/Constants.h>
#include <tags/FileTags.h>
#include <wx/utils.h>
#include <wx/tokenzr.h> // Needed for wxStringTokenizer
#ifndef AMULE_DAEMON
#include <wx/notifmsg.h> // Needed for wxNotificationMessage
#endif
#include "KnownFileList.h" // Needed for CKnownFileList
#include "CanceledFileList.h"
#include "UploadQueue.h" // Needed for CFileHash
#include "IPFilter.h" // Needed for CIPFilter
#include "Server.h" // Needed for CServer
#include "ServerConnect.h" // Needed for CServerConnect
#ifdef CLIENT_GUI
#include "UpDownClientEC.h" // Needed for CUpDownClient
#else
#include "updownclient.h" // Needed for CUpDownClient
#endif
#include "MemFile.h" // Needed for CMemFile
#include "Preferences.h" // Needed for CPreferences
#include "DownloadQueue.h" // Needed for CDownloadQueue
#include "amule.h" // Needed for theApp
#include "ED2KLink.h" // Needed for CED2KLink
#include "Packet.h" // Needed for CTag
#include "SearchList.h" // Needed for CSearchFile
#include "ClientList.h" // Needed for clientlist
#include "Statistics.h" // Needed for theStats
#include "Logger.h"
#include <common/Format.h> // Needed for CFormat
#include <common/FileFunctions.h> // Needed for GetLastModificationTime
#include "ThreadTasks.h" // Needed for CHashingTask/CCompletionTask/CAllocateFileTask
#include "GuiEvents.h" // Needed for Notify_*
#include "DataToText.h" // Needed for OriginToText()
#include "PlatformSpecific.h" // Needed for CreateSparseFile()
#include "FileArea.h" // Needed for CFileArea
#include "ScopedPtr.h" // Needed for CScopedArray
#include "CorruptionBlackBox.h"
#include "kademlia/kademlia/Kademlia.h"
#include "kademlia/kademlia/Search.h"
SFileRating::SFileRating(const wxString &u, const wxString &f, sint16 r, const wxString &c)
:
UserName(u),
FileName(f),
Rating(r),
Comment(c)
{
}
#ifndef CLIENT_GUI
SFileRating::SFileRating(const CUpDownClient &client)
:
UserName(client.GetUserName()),
FileName(client.GetClientFilename()),
Rating(client.GetFileRating()),
Comment(client.GetFileComment())
{
}
#endif
// PartFileBufferedData is defined in PartFile.h
typedef std::list<Chunk> ChunkList;
#ifndef CLIENT_GUI
CPartFile::CPartFile()
{
Init();
}
CPartFile::CPartFile(CSearchFile* searchresult)
{
Init();
m_abyFileHash = searchresult->GetFileHash();
SetFileName(searchresult->GetFileName());
SetFileSize(searchresult->GetFileSize());
for (unsigned int i = 0; i < searchresult->m_taglist.size(); ++i){
const CTag& pTag = searchresult->m_taglist[i];
bool bTagAdded = false;
if (pTag.GetNameID() == 0 && !pTag.GetName().IsEmpty() && (pTag.IsStr() || pTag.IsInt())) {
static const struct {
wxString pszName;
uint8 nType;
} _aMetaTags[] =
{
{ FT_ED2K_MEDIA_ARTIST, 2 },
{ FT_ED2K_MEDIA_ALBUM, 2 },
{ FT_ED2K_MEDIA_TITLE, 2 },
{ FT_ED2K_MEDIA_LENGTH, 2 },
{ FT_ED2K_MEDIA_BITRATE, 3 },
{ FT_ED2K_MEDIA_CODEC, 2 }
};
for (unsigned int t = 0; t < itemsof(_aMetaTags); ++t) {
if ( pTag.GetType() == _aMetaTags[t].nType &&
(pTag.GetName() == _aMetaTags[t].pszName)) {
// skip string tags with empty string values
if (pTag.IsStr() && pTag.GetStr().IsEmpty()) {
break;
}
// skip "length" tags with "0: 0" values
if (pTag.GetName() == FT_ED2K_MEDIA_LENGTH) {
if (pTag.GetStr().IsSameAs("0: 0") ||
pTag.GetStr().IsSameAs("0:0")) {
break;
}
}
// skip "bitrate" tags with '0' values
if ((pTag.GetName() == FT_ED2K_MEDIA_BITRATE) && !pTag.GetInt()) {
break;
}
AddDebugLogLineN( logPartFile,
"CPartFile::CPartFile(CSearchFile*): added tag " +
pTag.GetFullInfo() );
m_taglist.push_back(pTag);
bTagAdded = true;
break;
}
}
} else if (pTag.GetNameID() != 0 && pTag.GetName().IsEmpty() && (pTag.IsStr() || pTag.IsInt())) {
static const struct {
uint8 nID;
uint8 nType;
} _aMetaTags[] =
{
{ FT_FILETYPE, 2 },
{ FT_FILEFORMAT, 2 }
};
for (unsigned int t = 0; t < itemsof(_aMetaTags); ++t) {
if (pTag.GetType() == _aMetaTags[t].nType && pTag.GetNameID() == _aMetaTags[t].nID) {
// skip string tags with empty string values
if (pTag.IsStr() && pTag.GetStr().IsEmpty()) {
break;
}
AddDebugLogLineN( logPartFile,
"CPartFile::CPartFile(CSearchFile*): added tag " +
pTag.GetFullInfo() );
m_taglist.push_back(pTag);
bTagAdded = true;
break;
}
}
}
if (!bTagAdded) {
AddDebugLogLineN( logPartFile,
"CPartFile::CPartFile(CSearchFile*): ignored tag " +
pTag.GetFullInfo() );
}
}
CreatePartFile();
}
CPartFile::CPartFile(const CED2KFileLink* fileLink)
{
Init();
SetFileName(CPath(fileLink->GetName()));
SetFileSize(fileLink->GetSize());
m_abyFileHash = fileLink->GetHashKey();
CreatePartFile();
if (fileLink->m_hashset) {
if (!LoadHashsetFromFile(fileLink->m_hashset, true)) {
AddDebugLogLineC(logPartFile, "eD2K link contained invalid hashset: " + fileLink->GetLink());
}
}
}
CPartFile::~CPartFile()
{
// Gate Phase 3 below; see m_inDestructor comment in PartFile.h.
m_inDestructor = true;
AddDebugLogLineN(logPartFile, CFormat(
"~CPartFile entered, m_inDestructor set: '%s'") % GetFileName());
// Wait for any in-flight HashJobs targeting this file to finish.
// CPartFileHashThread reads m_hpartfile via HashSinglePart; we must
// not close the file out from under it. m_inDestructor was set above
// so Phase 3 will not enqueue anything new.
if (m_pendingHashes > 0) {
AddDebugLogLineN(logPartFile, CFormat(
"~CPartFile waiting for %d pending hash job(s) of '%s'")
% (int)m_pendingHashes % GetFileName());
while (m_pendingHashes > 0) {
wxMilliSleep(10);
}
}
// if it's not opened, it was completed or deleted
if (m_hpartfile.IsOpened()) {
// FlushBuffer drains buffered writes / harvests Phase 2's
// PB_WRITTEN items into m_aChangedPart. Phase 3 itself returns
// early due to m_inDestructor.
FlushBuffer();
// Sync-hash any parts still flagged dirty in m_aChangedPart.
//
// Why this is needed: in normal operation Phase 3 enqueues
// dirty parts to CPartFileHashThread asynchronously. But at
// shutdown the hash thread is torn down before downloadqueue
// (see CamuleApp::OnExit ordering), so async enqueue isn't
// available here — and a forced-quit during an active download
// can leave many parts harvested-but-not-yet-hashed
// (m_aChangedPart-true but never enqueued because the
// quiescent guard never opened during the receive burst).
//
// Without this, .met would be saved with gaplist marking
// those parts complete and m_corrupted_list empty, so on
// next launch they would be treated as implicitly verified
// (the existing "verified iff IsComplete && !IsCorruptedPart"
// invariant) — even though they were never hashed.
//
// Run the verification synchronously on the main thread.
// Slow shutdown is acceptable for the rare cancel-mid-download
// case; the common paths (natural completion, pause/resume,
// idle drain) keep m_aChangedPart drained throughout the
// session, so the loop usually has nothing to do.
if (m_aChangedPart.size() == GetPartCount()) {
uint16 verified = 0, corrupt = 0;
for (uint16 i = 0; i < GetPartCount(); ++i) {
if (!m_aChangedPart[i]) {
continue;
}
m_aChangedPart[i] = false;
// Only hash parts whose data is actually on disk.
if (!IsComplete(i)) {
continue;
}
// Lock m_hpartfileMutex against CPartFileWriteThread:
// Phase 1 of FlushBuffer above may have queued writes
// that the write thread is still draining concurrently
// with this sync-hash loop. See m_hpartfileMutex.
bool ok;
{
std::lock_guard<std::mutex> lock(m_hpartfileMutex);
ok = HashSinglePart(i);
}
if (ok) {
// Part is good. If it was carried in
// m_corrupted_list from before, drop it so the
// .met save below records the new clean state.
if (IsCorruptedPart(i)) {
EraseFirstValue(m_corrupted_list, i);
}
++verified;
} else {
// Part is bad. Re-open the gap and record in
// m_corrupted_list so next launch knows to
// re-download + retry.
AddGap(i);
if (!IsCorruptedPart(i)) {
m_corrupted_list.push_back(i);
}
m_iLostDueToCorruption +=
(uint64)GetPartSize(i);
++corrupt;
}
}
if (verified > 0 || corrupt > 0) {
AddDebugLogLineN(logPartFile, CFormat(
"~CPartFile sync-hashed %u remaining dirty part(s) "
"(%u verified, %u corrupt) for '%s'")
% (verified + corrupt) % verified % corrupt
% GetFileName());
}
}
m_hpartfile.Close();
// Update met file (with current directory entry)
SavePartFile();
}
DeleteContents(m_BufferedData_list);
delete m_CorruptionBlackBox;
wxASSERT(m_SrcList.empty());
wxASSERT(m_A4AFsrclist.empty());
}
void CPartFile::CreatePartFile(bool isImporting)
{
// use lowest free partfilenumber for free file (InterCeptor)
int i = 0;
do {
++i;
m_partmetfilename = CPath(CFormat("%03i.part.met") % i);
m_fullname = thePrefs::GetTempDir().JoinPaths(m_partmetfilename);
} while (m_fullname.FileExists());
m_CorruptionBlackBox->SetPartFileInfo(GetFileName().GetPrintable(), m_partmetfilename.RemoveAllExt().GetPrintable());
m_gaplist.Init(GetFileSize(), true); // Init empty
m_PartPath = m_fullname.RemoveExt();
bool fileCreated;
if (thePrefs::GetAllocFullFile() || !thePrefs::CreateFilesSparse()) {
fileCreated = m_hpartfile.Create(m_PartPath, true);
m_hpartfile.Close();
} else {
fileCreated = PlatformSpecific::CreateSparseFile(m_PartPath, GetFileSize());
}
if (!fileCreated) {
AddLogLineN(_("ERROR: Failed to create partfile"));
SetStatus(PS_ERROR);
}
SetFilePath(thePrefs::GetTempDir());
if (!isImporting && thePrefs::GetAllocFullFile()) {
SetStatus(PS_ALLOCATING);
CThreadScheduler::AddTask(new CAllocateFileTask(this, thePrefs::AddNewFilesPaused()));
} else {
AllocationFinished();
}
m_hashsetneeded = (GetED2KPartHashCount() > 0);
SavePartFile(true);
SetActive(theApp->IsConnected());
}
uint8 CPartFile::LoadPartFile(const CPath& in_directory, const CPath& filename, bool from_backup, bool getsizeonly)
{
bool isnewstyle = false;
uint8 version,partmettype=PMT_UNKNOWN;
std::map<uint16, Gap_Struct*> gap_map; // Slugfiller
transferred = 0;
m_partmetfilename = filename;
m_CorruptionBlackBox->SetPartFileInfo(GetFileName().GetPrintable(), m_partmetfilename.RemoveAllExt().GetPrintable());
m_filePath = in_directory;
m_fullname = m_filePath.JoinPaths(m_partmetfilename);
m_PartPath = m_fullname.RemoveExt();
// readfile data form part.met file
CPath curMetFilename = m_fullname;
if (from_backup) {
curMetFilename = curMetFilename.AppendExt(PARTMET_BAK_EXT);
AddLogLineN(CFormat( _("Trying to load backup of met-file from %s") )
% curMetFilename );
}
try {
CFile metFile(curMetFilename, CFile::read);
if (!metFile.IsOpened()) {
AddLogLineN(CFormat( _("ERROR: Failed to open part.met file: %s ==> %s") )
% curMetFilename
% GetFileName() );
return false;
} else if (metFile.GetLength() == 0) {
AddLogLineN(CFormat( _("ERROR: part.met file is 0 size: %s ==> %s") )
% m_partmetfilename
% GetFileName() );
return false;
}
version = metFile.ReadUInt8();
if (version != PARTFILE_VERSION && version != PARTFILE_SPLITTEDVERSION && version != PARTFILE_VERSION_LARGEFILE){
metFile.Close();
//if (version == 83) return ImportShareazaTempFile(...)
AddLogLineN(CFormat( _("ERROR: Invalid part.met file version: %s ==> %s") )
% m_partmetfilename
% GetFileName() );
return false;
}
isnewstyle = (version == PARTFILE_SPLITTEDVERSION);
partmettype = isnewstyle ? PMT_SPLITTED : PMT_DEFAULTOLD;
if (version == PARTFILE_VERSION) {// Do we still need this check ?
uint8 test[4]; // It will fail for certain files.
metFile.Seek(24, wxFromStart);
metFile.Read(test,4);
metFile.Seek(1, wxFromStart);
if (test[0]==0 && test[1]==0 && test[2]==2 && test[3]==1) {
isnewstyle=true; // edonkeys so called "old part style"
partmettype=PMT_NEWOLD;
}
}
if (isnewstyle) {
uint32 temp = metFile.ReadUInt32();
if (temp==0) { // 0.48 partmets - different again
LoadHashsetFromFile(&metFile, false);
} else {
metFile.Seek(2, wxFromStart);
LoadDateFromFile(&metFile);
m_abyFileHash = metFile.ReadHash();
}
} else {
LoadDateFromFile(&metFile);
LoadHashsetFromFile(&metFile, false);
}
uint32 tagcount = metFile.ReadUInt32();
for (uint32 j = 0; j < tagcount; ++j) {
CTag newtag(metFile,true);
if ( !getsizeonly ||
(getsizeonly &&
(newtag.GetNameID() == FT_FILESIZE ||
newtag.GetNameID() == FT_FILENAME))) {
switch(newtag.GetNameID()) {
case FT_FILENAME: {
if (!GetFileName().IsOk()) {
// If it's not empty, we already loaded the unicoded one
SetFileName(CPath(newtag.GetStr()));
}
break;
}
case FT_LASTSEENCOMPLETE: {
lastseencomplete = newtag.GetInt();
break;
}
case FT_FILESIZE: {
SetFileSize(newtag.GetInt());
break;
}
case FT_TRANSFERRED: {
transferred = newtag.GetInt();
break;
}
case FT_FILETYPE:{
//#warning needs setfiletype string
//SetFileType(newtag.GetStr());
break;
}
case FT_CATEGORY: {
m_category = newtag.GetInt();
if (m_category > theApp->glob_prefs->GetCatCount() - 1 ) {
m_category = 0;
}
break;
}
case FT_OLDDLPRIORITY:
case FT_DLPRIORITY: {
if (!isnewstyle){
m_iDownPriority = newtag.GetInt();
if( m_iDownPriority == PR_AUTO ){
m_iDownPriority = PR_HIGH;
SetAutoDownPriority(true);
}
else{
if ( m_iDownPriority != PR_LOW &&
m_iDownPriority != PR_NORMAL &&
m_iDownPriority != PR_HIGH)
m_iDownPriority = PR_NORMAL;
SetAutoDownPriority(false);
}
}
break;
}
case FT_STATUS: {
m_paused = (newtag.GetInt() == 1);
m_stopped = m_paused;
break;
}
case FT_OLDULPRIORITY:
case FT_ULPRIORITY: {
if (!isnewstyle){
SetUpPriority(newtag.GetInt(), false);
if( GetUpPriority() == PR_AUTO ){
SetUpPriority(PR_HIGH, false);
SetAutoUpPriority(true);
} else {
SetAutoUpPriority(false);
}
}
break;
}
case FT_KADLASTPUBLISHSRC:{
SetLastPublishTimeKadSrc(newtag.GetInt(), 0);
if(GetLastPublishTimeKadSrc() > (uint32)time(NULL)+KADEMLIAREPUBLISHTIMES) {
//There may be a possibility of an older client that saved a random number here.. This will check for that..
SetLastPublishTimeKadSrc(0,0);
}
break;
}
case FT_KADLASTPUBLISHNOTES:{
SetLastPublishTimeKadNotes(newtag.GetInt());
break;
}
// old tags: as long as they are not needed, take the chance to purge them
case FT_PERMISSIONS:
case FT_KADLASTPUBLISHKEY:
case FT_PARTFILENAME:
break;
case FT_DL_ACTIVE_TIME:
if (newtag.IsInt()) {
m_nDlActiveTime = newtag.GetInt();
}
break;
case FT_CORRUPTEDPARTS: {
wxASSERT(m_corrupted_list.empty());
wxString strCorruptedParts(newtag.GetStr());
wxStringTokenizer tokenizer(strCorruptedParts, ",");
while ( tokenizer.HasMoreTokens() ) {
wxString token = tokenizer.GetNextToken();
unsigned long uPart;
if (token.ToULong(&uPart)) {
if (uPart < GetPartCount() && !IsCorruptedPart(uPart)) {
m_corrupted_list.push_back(uPart);
}
}
}
break;
}
case FT_AICH_HASH:{
CAICHHash hash;
bool hashSizeOk =
hash.DecodeBase32(newtag.GetStr()) == CAICHHash::GetHashSize();
wxASSERT(hashSizeOk);
if (hashSizeOk) {
m_pAICHHashSet->SetMasterHash(hash, AICH_VERIFIED);
// EC exports GetAICHMasterHash().
MarkECChanged();
}
break;
}
case FT_ATTRANSFERRED:{
statistic.SetAllTimeTransferred(statistic.GetAllTimeTransferred() + (uint64)newtag.GetInt());
break;
}
case FT_ATTRANSFERREDHI:{
statistic.SetAllTimeTransferred(statistic.GetAllTimeTransferred() + (((uint64)newtag.GetInt()) << 32));
break;
}
case FT_ATREQUESTED:{
statistic.SetAllTimeRequests(newtag.GetInt());
break;
}
case FT_ATACCEPTED:{
statistic.SetAllTimeAccepts(newtag.GetInt());
break;
}
default: {
// Start Changes by Slugfiller for better exception handling
wxCharBuffer tag_ansi_name = newtag.GetName().ToAscii();
char gap_mark = tag_ansi_name.data() ? tag_ansi_name[0u] : 0;
if ( newtag.IsInt() && (newtag.GetName().Length() > 1) &&
((gap_mark == FT_GAPSTART) ||
(gap_mark == FT_GAPEND))) {
Gap_Struct *gap = NULL;
unsigned long int gapkey;
if (newtag.GetName().Mid(1).ToULong(&gapkey)) {
if ( gap_map.find( gapkey ) == gap_map.end() ) {
gap = new Gap_Struct;
gap_map[gapkey] = gap;
gap->start = (uint64)-1;
gap->end = (uint64)-1;
} else {
gap = gap_map[ gapkey ];
}
if (gap_mark == FT_GAPSTART) {
gap->start = newtag.GetInt();
}
if (gap_mark == FT_GAPEND) {
gap->end = newtag.GetInt()-1;
}
} else {
AddDebugLogLineN(logPartFile, "Wrong gap map key while reading met file!");
wxFAIL;
}
// End Changes by Slugfiller for better exception handling
} else {
m_taglist.push_back(newtag);
}
}
}
} else {
// Nothing. Else, nothing.
}
}
// load the hashsets from the hybridstylepartmet
if (isnewstyle && !getsizeonly && (metFile.GetPosition()<metFile.GetLength()) ) {
metFile.Seek(1, wxFromCurrent);
uint16 parts=GetPartCount(); // assuming we will get all hashsets
for (uint16 i = 0; i < parts && (metFile.GetPosition()+16<metFile.GetLength()); ++i){
CMD4Hash cur_hash = metFile.ReadHash();
m_hashlist.push_back(cur_hash);
}
CMD4Hash checkhash;
if (!m_hashlist.empty()) {
CreateHashFromHashlist(m_hashlist, &checkhash);
}
if (m_abyFileHash != checkhash) {
m_hashlist.clear();
}
}
} catch (const CInvalidPacket& e) {
AddLogLineC(CFormat(_("Error: %s (%s) is corrupt (bad tags: %s), unable to load file."))
% m_partmetfilename
% GetFileName()
% e.what());
return false;
} catch (const CIOFailureException& e) {
AddDebugLogLineC(logPartFile, CFormat( "IO failure while loading '%s': %s" )
% m_partmetfilename
% e.what() );
return false;
} catch (const CEOFException& WXUNUSED(e)) {
AddLogLineC(CFormat( _("ERROR: %s (%s) is corrupt (wrong tagcount), unable to load file.") )
% m_partmetfilename
% GetFileName() );
AddLogLineC(_("Trying to recover file info..."));
// Safe file is that who have
// - FileSize
if (GetFileSize()) {
// We have filesize, try other needed info
// Do we need to check gaps? I think not,
// because they are checked below. Worst
// scenario will only mark file as 0 bytes downloaded.
// -Filename
if (!GetFileName().IsOk()) {
// Not critical, let's put a random filename.
AddLogLineC(_(
"Recovering no-named file - will try to recover it as RecoveredFile.dat"));
SetFileName(CPath("RecoveredFile.dat"));
}
AddLogLineC(_("Recovered all available file info :D - Trying to use it..."));
} else {
AddLogLineC(_("Unable to recover file info :("));
return false;
}
}
if (getsizeonly) {
return partmettype;
}
// Init Gaplist
m_gaplist.Init(GetFileSize(), false); // Init full, then add gaps
// Now to flush the map into the list (Slugfiller)
std::map<uint16, Gap_Struct*>::iterator it = gap_map.begin();
for ( ; it != gap_map.end(); ++it ) {
Gap_Struct* gap = it->second;
// SLUGFILLER: SafeHash - revised code, and extra safety
if ( (gap->start != (uint64)-1) &&
(gap->end != (uint64)-1) &&
gap->start <= gap->end &&
gap->start < GetFileSize()) {
if (gap->end >= GetFileSize()) {
gap->end = GetFileSize()-1; // Clipping
}
m_gaplist.AddGap(gap->start, gap->end); // All tags accounted for, use safe adding
}
delete gap;
// SLUGFILLER: SafeHash
}
//check if this is a backup
if ( m_fullname.GetExt().MakeLower() == "backup") {
m_fullname = m_fullname.RemoveExt();
}
// open permanent handle
if ( !m_hpartfile.Open(m_PartPath, CFile::read_write)) {
AddLogLineN(CFormat( _("Failed to open %s (%s)") )
% m_fullname
% GetFileName() );
return false;
}
SetStatus(PS_EMPTY);
try {
// SLUGFILLER: SafeHash - final safety, make sure any missing part of the file is gap
if (m_hpartfile.GetLength() < GetFileSize())
AddGap(m_hpartfile.GetLength(), GetFileSize()-1);
// Goes both ways - Partfile should never be too large
if (m_hpartfile.GetLength() > GetFileSize()) {
AddDebugLogLineC(logPartFile, CFormat( "Partfile \"%s\" is too large! Truncating %llu bytes." ) % GetFileName() % (m_hpartfile.GetLength() - GetFileSize()));
m_hpartfile.SetLength(GetFileSize());
}
// SLUGFILLER: SafeHash
} catch (const CIOFailureException& e) {
AddDebugLogLineC(logPartFile, CFormat( "Error while accessing partfile \"%s\": %s" ) % GetFileName() % e.what());
SetStatus(PS_ERROR);
}
// now close the file again until needed
m_hpartfile.Release(true);
// check hashcount, file status etc
if (GetHashCount() != GetED2KPartHashCount()){
m_hashsetneeded = true;
ClearMetDirty();
ClearStatsDirty();
m_lastMetSaveTick = ::GetTickCount64();
return true;
} else {
m_hashsetneeded = false;
for (size_t i = 0; i < m_hashlist.size(); ++i) {
if (IsComplete(i)) {
SetStatus(PS_READY);
}
}
}
if (m_gaplist.IsComplete()) { // is this file complete already?
CompleteFile(false);
return true;
}
if (!isnewstyle) { // not for importing
const time_t file_date = CPath::GetModificationTime(m_PartPath);
if (m_lastDateChanged != file_date) {
// It's pointless to rehash an empty file, since the case
// where a user has zero'd a file is handled above ...
if (m_hpartfile.GetLength()) {
AddLogLineN(CFormat( _("WARNING: %s might be corrupted (%i)") )
% m_PartPath
% (m_lastDateChanged - file_date) );
// rehash
SetStatus(PS_WAITING_FOR_HASH);
CPath partFileName = m_partmetfilename.RemoveExt();
CThreadScheduler::AddTask(new CHashingTask(m_filePath, partFileName, this));
}
}
}
UpdateCompletedInfos();
if (completedsize > transferred) {
m_iGainDueToCompression = completedsize - transferred;
} else if (completedsize != transferred) {
m_iLostDueToCorruption = transferred - completedsize;
}
// In-memory state now matches the just-loaded .part.met. Setters
// invoked during tag parsing (SetUpPriority, SetAutoUpPriority,
// SetAutoDownPriority, SetStatus, etc.) may have set m_metDirty;
// drop it so the first FlushBuffer tick after load does not rewrite
// the .met with byte-identical content. Also seed m_lastMetSaveTick
// from load time so the stats-heartbeat is measured from now.
ClearMetDirty();
ClearStatsDirty();
m_lastMetSaveTick = ::GetTickCount64();
return true;
}
bool CPartFile::SavePartFile(bool Initial)
{
switch (status) {
case PS_WAITING_FOR_HASH:
case PS_HASHING:
case PS_COMPLETE:
return false;
}
/* Don't write anything to disk if less than 100 KB of free space is left. */
sint64 free = CPath::GetFreeSpaceAt(GetFilePath());
if ((free != wxInvalidOffset) && (free < (100 * 1024))) {
return false;
}
// Atomic-rename save.
//
// Old sequence (per partfile, every save tick):
// 1. CPath::BackupFile(m_fullname, ".backup") -- full content copy
// of .part.met into .part.met.backup as an in-flight intermediate.
// 2. CPath::RemoveFile(m_fullname).
// 3. Write new .part.met from scratch.
// 4. CPath::RemoveFile(".backup") on success.
// 5. CPath::BackupFile(m_fullname, PARTMET_BAK_EXT) -- full content
// copy of the freshly-written .part.met into .part.met.bak as the
// long-term recovery backup.
//
// Three full-size content copies per save plus a delete/create dance on
// the live .part.met. On a sharer with hundreds of dirty partfiles per
// timer tick the aggregate runs the main thread continuously through
// disk I/O (see issue #669) -- write() syscalls are microseconds each,
// but the loop length itself is the bottleneck.
//
// New sequence:
// 1. Write new content into .part.met.tmp.
// 2. rename(.part.met, .part.met.bak) -- O(1) metadata op, promotes
// the previous .part.met to long-term backup.
// 3. rename(.part.met.tmp, .part.met) -- atomic install of new
// content, POSIX rename guarantees the target is either fully
// old-content or fully new-content at every observable moment.
//
// One content write plus two metadata renames. ~3x reduction in
// per-save disk work, and stronger crash safety because the live
// .part.met is never absent or partial.
const CPath tmpName = m_fullname.AppendExt(".tmp");
const CPath bakName = m_fullname.AppendExt(PARTMET_BAK_EXT);
CFile file;
try {
if (!m_PartPath.FileExists()) {
throw wxString(".part file not found");
}
uint32 lsc = lastseencomplete;
file.Open(tmpName, CFile::write);
if (!file.IsOpened()) {
throw wxString("Failed to open part.met.tmp file");
}
// version
file.WriteUInt8(IsLargeFile() ? PARTFILE_VERSION_LARGEFILE : PARTFILE_VERSION);
file.WriteUInt32(CPath::GetModificationTime(m_PartPath));
// hash
file.WriteHash(m_abyFileHash);
uint16 parts = m_hashlist.size();
file.WriteUInt16(parts);
for (int x = 0; x < parts; ++x) {
file.WriteHash(m_hashlist[x]);
}
// tags
#define FIXED_TAGS 15
uint32 tagcount = m_taglist.size() + FIXED_TAGS + (m_gaplist.size()*2);
if (!m_corrupted_list.empty()) {
++tagcount;
}
if (m_pAICHHashSet->HasValidMasterHash() && (m_pAICHHashSet->GetStatus() == AICH_VERIFIED)){
++tagcount;
}
if (GetLastPublishTimeKadSrc()){
++tagcount;
}
if (GetLastPublishTimeKadNotes()){
++tagcount;
}
if (GetDlActiveTime()){
++tagcount;
}
file.WriteUInt32(tagcount);
//#warning Kry - Where are lost by corruption and gained by compression?
// 0 (unicoded part file name)
// We write it with BOM to keep eMule compatibility. Note that the 'printable' filename is saved,
// as presently the filename does not represent an actual file.
CTagString( FT_FILENAME, GetFileName().GetPrintable()).WriteTagToFile( &file, utf8strOptBOM );
CTagString( FT_FILENAME, GetFileName().GetPrintable()).WriteTagToFile( &file ); // 1
CTagIntSized( FT_FILESIZE, GetFileSize(), IsLargeFile() ? 64 : 32).WriteTagToFile( &file );// 2
CTagIntSized( FT_TRANSFERRED, transferred, IsLargeFile() ? 64 : 32).WriteTagToFile( &file ); // 3
CTagInt32( FT_STATUS, (m_paused?1:0)).WriteTagToFile( &file ); // 4
if ( IsAutoDownPriority() ) {
CTagInt32( FT_DLPRIORITY, (uint8)PR_AUTO ).WriteTagToFile( &file ); // 5
CTagInt32( FT_OLDDLPRIORITY, (uint8)PR_AUTO ).WriteTagToFile( &file ); // 6
} else {
CTagInt32( FT_DLPRIORITY, m_iDownPriority ).WriteTagToFile( &file ); // 5
CTagInt32( FT_OLDDLPRIORITY, m_iDownPriority ).WriteTagToFile( &file ); // 6
}
CTagInt32( FT_LASTSEENCOMPLETE, lsc ).WriteTagToFile( &file ); // 7
if ( IsAutoUpPriority() ) {
CTagInt32( FT_ULPRIORITY, (uint8)PR_AUTO ).WriteTagToFile( &file ); // 8
CTagInt32( FT_OLDULPRIORITY, (uint8)PR_AUTO ).WriteTagToFile( &file ); // 9
} else {
CTagInt32( FT_ULPRIORITY, GetUpPriority() ).WriteTagToFile( &file ); // 8
CTagInt32( FT_OLDULPRIORITY, GetUpPriority() ).WriteTagToFile( &file ); // 9
}
CTagInt32(FT_CATEGORY, m_category).WriteTagToFile( &file ); // 10
CTagInt32(FT_ATTRANSFERRED, statistic.GetAllTimeTransferred() & 0xFFFFFFFF).WriteTagToFile( &file );// 11
CTagInt32(FT_ATTRANSFERREDHI, statistic.GetAllTimeTransferred() >>32).WriteTagToFile( &file );// 12
CTagInt32(FT_ATREQUESTED, statistic.GetAllTimeRequests()).WriteTagToFile( &file ); // 13
CTagInt32(FT_ATACCEPTED, statistic.GetAllTimeAccepts()).WriteTagToFile( &file ); // 14
// corrupt part infos
if (!m_corrupted_list.empty()) {
wxString strCorruptedParts;
std::list<uint16>::iterator it = m_corrupted_list.begin();
for (; it != m_corrupted_list.end(); ++it) {
uint16 uCorruptedPart = *it;
if (!strCorruptedParts.IsEmpty()) {
strCorruptedParts += ",";
}
strCorruptedParts += CFormat("%u") % uCorruptedPart;
}
wxASSERT( !strCorruptedParts.IsEmpty() );
CTagString( FT_CORRUPTEDPARTS, strCorruptedParts ).WriteTagToFile( &file); // 11?
}
//AICH Filehash
if (m_pAICHHashSet->HasValidMasterHash() && (m_pAICHHashSet->GetStatus() == AICH_VERIFIED)){
CTagString aichtag(FT_AICH_HASH, m_pAICHHashSet->GetMasterHash().GetString() );
aichtag.WriteTagToFile(&file); // 12?
}
if (GetLastPublishTimeKadSrc()){
CTagInt32(FT_KADLASTPUBLISHSRC, GetLastPublishTimeKadSrc()).WriteTagToFile(&file); // 15?
}
if (GetLastPublishTimeKadNotes()){
CTagInt32(FT_KADLASTPUBLISHNOTES, GetLastPublishTimeKadNotes()).WriteTagToFile(&file); // 16?
}
if (GetDlActiveTime()){
CTagInt32(FT_DL_ACTIVE_TIME, GetDlActiveTime()).WriteTagToFile(&file); // 17
}
for (uint32 j = 0; j < (uint32)m_taglist.size();++j) {
m_taglist[j].WriteTagToFile(&file);
}
// gaps
unsigned i_pos = 0;
for (CGapList::const_iterator it = m_gaplist.begin(); it != m_gaplist.end(); ++it) {
wxString tagName = CFormat(" %u") % i_pos;
// gap start = first missing byte but gap ends = first non-missing byte
// in edonkey but I think its easier to user the real limits
tagName[0] = FT_GAPSTART;
CTagIntSized(tagName, it.start(), IsLargeFile() ? 64 : 32).WriteTagToFile( &file );
tagName[0] = FT_GAPEND;