-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathSharedFileList.cpp
More file actions
1456 lines (1227 loc) · 44.5 KB
/
Copy pathSharedFileList.cpp
File metadata and controls
1456 lines (1227 loc) · 44.5 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 "SharedFileList.h" // Interface declarations // Do_not_auto_remove
#include "SharedDirWatcher.h"
#include <map>
#include <unordered_set>
#include <protocol/Protocols.h>
#include <protocol/kad/Constants.h>
#include <tags/FileTags.h>
#include <wx/utils.h>
#include "Packet.h" // Needed for CPacket
#include "MemFile.h" // Needed for CMemFile
#include "ServerConnect.h" // Needed for CServerConnect
#include "KnownFileList.h" // Needed for CKnownFileList
#include "ThreadTasks.h" // Needed for CThreadScheduler and CHasherTask
#include "Preferences.h" // Needed for thePrefs
#include "DownloadQueue.h" // Needed for CDownloadQueue
#include "amule.h" // Needed for theApp
#include "PartFile.h" // Needed for PartFile
#include "Server.h" // Needed for CServer
#include "Statistics.h" // Needed for theStats
#include "Logger.h"
#include <common/Format.h>
#include <common/FileFunctions.h>
#include "GuiEvents.h" // Needed for Notify_*
#include "SHAHashSet.h" // Needed for CAICHHash
#include "kademlia/kademlia/Kademlia.h"
#include "kademlia/kademlia/Search.h"
#include "ClientList.h"
typedef std::deque<CKnownFile*> KnownFileArray;
///////////////////////////////////////////////////////////////////////////////
// CPublishKeyword
class CPublishKeyword
{
public:
CPublishKeyword(const wxString& rstrKeyword)
{
m_strKeyword = rstrKeyword;
// min. keyword char is allowed to be < 3 in some cases (see also 'CSearchManager::getWords')
//ASSERT( rstrKeyword.GetLength() >= 3 );
wxASSERT( !rstrKeyword.IsEmpty() );
KadGetKeywordHash(rstrKeyword, &m_nKadID);
SetNextPublishTime(0);
SetPublishedCount(0);
}
const Kademlia::CUInt128& GetKadID() const { return m_nKadID; }
const wxString& GetKeyword() const { return m_strKeyword; }
int GetRefCount() const { return m_aFiles.size(); }
const KnownFileArray& GetReferences() const { return m_aFiles; }
uint32 GetNextPublishTime() const { return m_tNextPublishTime; }
void SetNextPublishTime(uint32 tNextPublishTime) { m_tNextPublishTime = tNextPublishTime; }
uint32 GetPublishedCount() const { return m_uPublishedCount; }
void SetPublishedCount(uint32 uPublishedCount) { m_uPublishedCount = uPublishedCount; }
void IncPublishedCount() { m_uPublishedCount++; }
bool AddRef(CKnownFile* pFile) {
if (std::find(m_aFiles.begin(), m_aFiles.end(), pFile) != m_aFiles.end()) {
wxFAIL;
return false;
}
m_aFiles.push_back(pFile);
return true;
}
int RemoveRef(CKnownFile* pFile) {
KnownFileArray::iterator it = std::find(m_aFiles.begin(), m_aFiles.end(), pFile);
if (it != m_aFiles.end()) {
m_aFiles.erase(it);
}
return m_aFiles.size();
}
void RemoveAllReferences() {
m_aFiles.clear();
}
void RotateReferences(unsigned iRotateSize) {
wxCHECK_RET(m_aFiles.size(), "RotateReferences: Rotating empty array");
unsigned shift = (iRotateSize % m_aFiles.size());
std::rotate(m_aFiles.begin(), m_aFiles.begin() + shift, m_aFiles.end());
}
protected:
wxString m_strKeyword;
Kademlia::CUInt128 m_nKadID;
uint32 m_tNextPublishTime;
uint32 m_uPublishedCount;
KnownFileArray m_aFiles;
};
///////////////////////////////////////////////////////////////////////////////
// CPublishKeywordList
class CPublishKeywordList
{
public:
CPublishKeywordList();
~CPublishKeywordList();
void AddKeyword(const wxString& keyword, CKnownFile *file);
void AddKeywords(CKnownFile* pFile);
void RemoveKeyword(const wxString& keyword, CKnownFile *file);
void RemoveKeywords(CKnownFile* pFile);
void RemoveAllKeywords();
void RemoveAllKeywordReferences();
void PurgeUnreferencedKeywords();
int GetCount() const { return m_lstKeywords.size(); }
CPublishKeyword* GetNextKeyword();
void ResetNextKeyword();
uint32 GetNextPublishTime() const { return m_tNextPublishKeywordTime; }
void SetNextPublishTime(uint32 tNextPublishKeywordTime) { m_tNextPublishKeywordTime = tNextPublishKeywordTime; }
protected:
// The list is the canonical container — its insertion order is
// load-bearing for GetNextKeyword()'s round-robin publish cursor
// (m_posNextKeyword), so we cannot replace it with a map.
typedef std::list<CPublishKeyword*> CKeyWordList;
CKeyWordList m_lstKeywords;
CKeyWordList::iterator m_posNextKeyword;
uint32 m_tNextPublishKeywordTime;
// Secondary index: keyword string -> position in m_lstKeywords. Lets
// FindKeyword() do an O(log N) lookup instead of a linear scan,
// collapsing AddKeywords()'s hot path on large shared sets
// (CSharedFileList::Reload, called once per shared file at startup)
// from O(N²) to O(N log N). std::list iterators are stable across
// other inserts/erases, so caching them here is safe.
std::map<wxString, CKeyWordList::iterator> m_keywordIndex;
CPublishKeyword* FindKeyword(const wxString& rstrKeyword, CKeyWordList::iterator* ppos = NULL);
};
CPublishKeywordList::CPublishKeywordList()
{
ResetNextKeyword();
SetNextPublishTime(0);
}
CPublishKeywordList::~CPublishKeywordList()
{
RemoveAllKeywords();
}
CPublishKeyword* CPublishKeywordList::GetNextKeyword()
{
if (m_posNextKeyword == m_lstKeywords.end()) {
m_posNextKeyword = m_lstKeywords.begin();
if (m_posNextKeyword == m_lstKeywords.end()) {
return NULL;
}
}
return *m_posNextKeyword++;
}
void CPublishKeywordList::ResetNextKeyword()
{
m_posNextKeyword = m_lstKeywords.begin();
}
CPublishKeyword* CPublishKeywordList::FindKeyword(const wxString& rstrKeyword, CKeyWordList::iterator* ppos)
{
std::map<wxString, CKeyWordList::iterator>::iterator idx = m_keywordIndex.find(rstrKeyword);
if (idx == m_keywordIndex.end()) {
return NULL;
}
if (ppos) {
*ppos = idx->second;
}
return *(idx->second);
}
void CPublishKeywordList::AddKeyword(const wxString& keyword, CKnownFile *file)
{
CPublishKeyword* pubKw = FindKeyword(keyword);
if (pubKw == NULL) {
pubKw = new CPublishKeyword(keyword);
m_lstKeywords.push_back(pubKw);
CKeyWordList::iterator it = m_lstKeywords.end();
--it;
m_keywordIndex[keyword] = it;
SetNextPublishTime(0);
}
pubKw->AddRef(file);
}
void CPublishKeywordList::AddKeywords(CKnownFile* pFile)
{
const Kademlia::WordList& wordlist = pFile->GetKadKeywords();
Kademlia::WordList::const_iterator it;
for (it = wordlist.begin(); it != wordlist.end(); ++it) {
AddKeyword(*it, pFile);
}
}
void CPublishKeywordList::RemoveKeyword(const wxString& keyword, CKnownFile *file)
{
CKeyWordList::iterator pos;
CPublishKeyword* pubKw = FindKeyword(keyword, &pos);
if (pubKw != NULL) {
if (pubKw->RemoveRef(file) == 0) {
if (pos == m_posNextKeyword) {
++m_posNextKeyword;
}
m_lstKeywords.erase(pos);
m_keywordIndex.erase(keyword);
delete pubKw;
SetNextPublishTime(0);
}
}
}
void CPublishKeywordList::RemoveKeywords(CKnownFile* pFile)
{
const Kademlia::WordList& wordlist = pFile->GetKadKeywords();
Kademlia::WordList::const_iterator it;
for (it = wordlist.begin(); it != wordlist.end(); ++it) {
RemoveKeyword(*it, pFile);
}
}
void CPublishKeywordList::RemoveAllKeywords()
{
DeleteContents(m_lstKeywords);
m_keywordIndex.clear();
ResetNextKeyword();
SetNextPublishTime(0);
}
void CPublishKeywordList::RemoveAllKeywordReferences()
{
CKeyWordList::iterator it = m_lstKeywords.begin();
for (; it != m_lstKeywords.end(); ++it) {
(*it)->RemoveAllReferences();
}
}
void CPublishKeywordList::PurgeUnreferencedKeywords()
{
CKeyWordList::iterator it = m_lstKeywords.begin();
while (it != m_lstKeywords.end()) {
CPublishKeyword* pPubKw = *it;
if (pPubKw->GetRefCount() == 0) {
if (it == m_posNextKeyword) {
++m_posNextKeyword;
}
m_keywordIndex.erase(pPubKw->GetKeyword());
m_lstKeywords.erase(it++);
delete pPubKw;
SetNextPublishTime(0);
} else {
++it;
}
}
}
CSharedFileList::CSharedFileList(CKnownFileList* in_filelist){
filelist = in_filelist;
reloading = false;
m_lastPublishED2K = 0;
m_lastPublishED2KFlag = true;
/* Kad Stuff */
m_keywords = new CPublishKeywordList;
m_currFileSrc = 0;
m_currFileNotes = 0;
m_lastPublishKadSrc = 0;
m_lastPublishKadNotes = 0;
m_currFileKey = 0;
m_dirWatcher = NULL;
}
CSharedFileList::~CSharedFileList()
{
delete m_dirWatcher;
delete m_keywords;
}
void CSharedFileList::EnableDirectoryWatcher(bool enable)
{
if (enable) {
if (!m_dirWatcher) {
m_dirWatcher = new CSharedDirWatcher(this);
}
m_dirWatcher->Enable();
} else if (m_dirWatcher) {
m_dirWatcher->Disable();
}
}
void CSharedFileList::FindSharedFiles(const ReloadYieldCb & yieldCb, bool & aborted)
{
/* Abort loading if we are shutting down. */
if(theApp->IsOnShutDown()) {
return;
}
// Clear statistics.
theStats::ClearSharedFilesInfo();
// Reload shareddir.dat
theApp->glob_prefs->ReloadSharedFolders();
{
wxMutexLocker lock(list_mut);
m_Files_map.clear();
}
// All part files are automatically shared.
for ( uint32 i = 0; i < theApp->downloadqueue->GetFileCount(); ++i ) {
CPartFile* file = theApp->downloadqueue->GetFileByIndex( i );
if ( file->GetStatus(true) == PS_READY ) {
AddLogLineNS(CFormat(_("Adding file %s to shares"))
% file->GetFullName().GetPrintable());
AddFile(file);
}
}
// Create a list of all shared paths and weed out duplicates.
std::list<CPath> sharedPaths;
// Global incoming dir and all category incoming directories are automatically shared.
sharedPaths.push_back(thePrefs::GetIncomingDir());
for (unsigned int i = 1;i < theApp->glob_prefs->GetCatCount(); ++i) {
sharedPaths.push_back(theApp->glob_prefs->GetCatPath(i));
}
const thePrefs::PathList& shared = theApp->glob_prefs->shareddir_list;
sharedPaths.insert(sharedPaths.end(), shared.begin(), shared.end());
sharedPaths.sort();
sharedPaths.unique();
filelist->PrepareIndex();
// Gathering is done in the foreground and can be slowed down severely by parallel background hashing.
// So just store the hashing tasks for now.
TaskList hashTasks;
size_t scanned = 0;
for (std::list<CPath>::iterator it = sharedPaths.begin(); it != sharedPaths.end(); ++it) {
AddFilesFromDirectory(*it, hashTasks, yieldCb, scanned, aborted);
if (aborted) {
break;
}
}
filelist->ReleaseIndex();
// Now that the shared files are gathered feed the hashing tasks to the scheduler to start hashing.
unsigned addedFiles = 0;
for (TaskList::iterator it = hashTasks.begin(); it != hashTasks.end(); ++it) {
if (CThreadScheduler::AddTask(*it)) {
addedFiles++;
}
}
if (addedFiles == 0) {
AddLogLineN(CFormat(wxPLURAL("Found %i known shared file", "Found %i known shared files", GetCount())) % GetCount());
// Make sure the AICH-hashes are up to date.
CThreadScheduler::AddTask(new CAICHSyncTask());
} else {
// New files, AICH thread will be run at the end of the hashing thread.
AddLogLineN(CFormat(wxPLURAL("Found %i known shared file, %i unknown", "Found %i known shared files, %i unknown", GetCount())) % GetCount() % addedFiles);
}
}
// Checks if the dir a is the same as b. If they are, then logs the message and returns true.
static bool CheckDirectory(const wxString& a, const CPath& b)
{
if (CPath(a).IsSameDir(b)) {
AddLogLineC(CFormat( _("ERROR: Attempted to share %s") ) % a);
return true;
}
return false;
}
unsigned CSharedFileList::AddFilesFromDirectory(const CPath& directory, TaskList & hashTasks,
const ReloadYieldCb & yieldCb, size_t & scanned, bool & aborted)
{
// Do not allow these folders to be shared:
// - The .aMule folder
// - The Temp folder
// - The users home-dir
if (CheckDirectory(wxGetHomeDir(), directory)) {
return 0;
} else if (CheckDirectory(thePrefs::GetConfigDir(), directory)) {
return 0;
} else if (CheckDirectory(thePrefs::GetTempDir().GetRaw(), directory)) {
return 0;
}
if (!directory.DirExists()) {
AddLogLineNS(CFormat(_("Shared directory not found, skipping: %s"))
% directory.GetPrintable());
return 0;
}
CDirIterator::FileType searchFor = CDirIterator::FileNoHidden;
if (thePrefs::ShareHiddenFiles()) {
searchFor = CDirIterator::File;
}
const int extraFlags = thePrefs::FollowSymlinksInShares() ? 0 : wxDIR_NO_FOLLOW;
unsigned knownFiles = 0;
unsigned addedFiles = 0;
// Yield to the caller every kYieldEvery files so the UI can stay
// responsive on big shared trees. 256 strikes a balance between
// progress-bar responsiveness (~4 updates/s on a 1 ms-per-file
// machine) and the overhead of the callback itself.
constexpr size_t kYieldEvery = 256;
CDirIterator SharedDir(directory);
for (CPath fname = SharedDir.GetFirstFile(searchFor, wxEmptyString, extraFlags); fname.IsOk(); fname = SharedDir.GetNextFile()) {
if (yieldCb && ++scanned % kYieldEvery == 0) {
if (!yieldCb(scanned)) {
aborted = true;
return addedFiles;
}
} else if (!yieldCb) {
++scanned;
}
switch (AddPathToShares(directory, fname, hashTasks)) {
case kAddPathQueued: addedFiles++; break;
case kAddPathKnown: knownFiles++; break;
case kAddPathSkipped: break;
}
}
if ((addedFiles == 0) && (knownFiles == 0)) {
AddLogLineN(CFormat(_("No shareable files found in directory: %s"))
% directory.GetPrintable());
}
return addedFiles;
}
// Per-path attach. Three outcomes:
// kAddPathSkipped — broken link, zero size, stat failed; do nothing.
// kAddPathKnown — matched a CKnownFile in known.met and was either
// newly attached to the shared list or already there.
// kAddPathQueued — unknown file; a CHashingTask was pushed into
// hashTasks. The shared-list attach happens later
// when the hashing thread finishes and calls
// SafeAddKFile() on the resulting CKnownFile.
//
// Shared between the bulk directory walk (AddFilesFromDirectory above)
// and the incremental watcher path (NotifyPathAdded below) so the two
// agree on shareability rules.
CSharedFileList::AddPathResult
CSharedFileList::AddPathToShares(const CPath& directory, const CPath& fname,
TaskList & hashTasks)
{
CPath fullPath = directory.JoinPaths(fname);
if (!fullPath.FileExists()) {
AddDebugLogLineN(logKnownFiles,
CFormat("Shared file does not exist (possibly a broken link): %s") % fullPath);
return kAddPathSkipped;
}
AddDebugLogLineN(logKnownFiles,
CFormat("Found shared file: %s") % fullPath);
time_t fdate = CPath::GetModificationTime(fullPath);
sint64 fsize = fullPath.GetFileSize();
// This will also catch files with too strict permissions.
if ((fdate == (time_t)-1) || (fsize == wxInvalidOffset)) {
AddDebugLogLineN(logKnownFiles,
CFormat("Failed to retrieve modification time or size for '%s', skipping.") % fullPath);
return kAddPathSkipped;
}
if (fsize == 0) {
AddDebugLogLineN(logKnownFiles,
CFormat("Skip zero size file '%s'") % fullPath);
return kAddPathSkipped;
}
CKnownFile* toadd = filelist->FindKnownFile(fname, fdate, fsize);
if (toadd) {
// Set the path BEFORE AddFile so the path index that AddFile
// maintains keys off the file's current GetFilePath() rather
// than whatever stale path was stamped on the CKnownFile by
// a previous shared-list membership.
toadd->SetFilePath(directory);
if (AddFile(toadd)) {
AddDebugLogLineN(logKnownFiles,
CFormat("Added known file '%s' to shares")
% fname);
} else {
AddDebugLogLineN(logKnownFiles,
CFormat("File already shared, skipping: %s")
% fname);
}
return kAddPathKnown;
}
// Not in knownfilelist - start adding thread to hash file.
AddDebugLogLineN(logKnownFiles,
CFormat("Hashing new unknown shared file '%s'") % fname);
hashTasks.push_back(new CHashingTask(directory, fname));
return kAddPathQueued;
}
bool CSharedFileList::AddFile(CKnownFile* pFile)
{
wxASSERT(pFile->GetHashCount() == pFile->GetED2KPartHashCount());
wxMutexLocker lock(list_mut);
CKnownFileMap::value_type entry(pFile->GetFileHash(), pFile);
if (m_Files_map.insert(entry).second) {
/* Keywords to publish on Kad */
m_keywords->AddKeywords(pFile);
theStats::AddSharedFile(pFile->GetFileSize());
// Mirror into the path index so the watcher's per-event
// dispatch can resolve DELETE / MODIFY events to the
// CKnownFile* in O(1). Empty key (e.g. a CPartFile whose
// SetFilePath has not run yet) is harmless: it lives in
// m_pathIndex under "" until SafeAddKFile attaches the real
// path via the post-completion path. Stale entries left
// over from a previous shared-list membership are
// overwritten here.
const wxString key =
pFile->GetFilePath().JoinPaths(pFile->GetFileName()).GetRaw();
m_pathIndex[key] = pFile;
return true;
}
return false;
}
void CSharedFileList::SafeAddKFile(CKnownFile* toadd, bool bOnlyAdd)
{
// Straight insert first. If the hash isn't already in m_Files_map
// this succeeds and fires the notifier as before.
if (AddFile(toadd)) {
Notify_SharedFilesShowFile(toadd);
} else {
// AddFile failed because some CKnownFile under this hash is
// already in m_Files_map. Two possibilities:
//
// 1. The exact same pointer was re-added — no-op.
// 2. CKnownFileList::Append fired the rename-during-hash
// branch (same hash, same size, different name): it
// demoted the prior CKnownFile to m_duplicateFileList and
// installed `toadd` as the canonical entry in
// m_knownFileMap. The shared-files view still points at
// the demoted pointer, which has a filename that no
// longer matches disk and which the duplicate-list prune
// may delete later (dangling pointer in m_Files_map /
// m_pathIndex). Detach the stale entry and install the
// live one so the view mirrors knownfiles.
CKnownFile *stale = NULL;
{
wxMutexLocker lock(list_mut);
CKnownFileMap::iterator it =
m_Files_map.find(toadd->GetFileHash());
if (it != m_Files_map.end() && it->second != toadd) {
stale = it->second;
}
}
if (stale) {
AddDebugLogLineN(logKnownFiles,
CFormat("SafeAddKFile: rename-during-hash swap, "
"detaching stale '%s' for live '%s'")
% stale->GetFilePath().JoinPaths(stale->GetFileName())
% toadd->GetFilePath().JoinPaths(toadd->GetFileName()));
RemoveFile(stale);
if (AddFile(toadd)) {
Notify_SharedFilesShowFile(toadd);
}
}
}
if (!bOnlyAdd && theApp->IsConnectedED2K()) {
// Publishing of files is not anymore handled here.
// Instead, the timer does it by itself.
m_lastPublishED2KFlag = true;
}
}
// removes first occurrence of 'toremove' in 'list'
void CSharedFileList::RemoveFile(CKnownFile* toremove){
Notify_SharedFilesRemoveFile(toremove);
wxMutexLocker lock(list_mut);
if (m_Files_map.erase(toremove->GetFileHash()) > 0) {
theStats::RemoveSharedFile(toremove->GetFileSize());
}
// Same path key we wrote into the index in AddFile(). erase() is a
// no-op if the entry isn't present (e.g. the file was inserted
// before m_pathIndex existed in an older save snapshot).
const wxString key =
toremove->GetFilePath().JoinPaths(toremove->GetFileName()).GetRaw();
m_pathIndex.erase(key);
/* This file keywords must not be published to kad anymore */
m_keywords->RemoveKeywords(toremove);
}
// Incremental rescan entry points used by CSharedDirWatcher.
//
// These exist so the watcher can apply a single fs-watcher event
// without firing the bulk Reload() path, which on a 100 k+ file
// shareset blocks the GUI for minutes per event. See issue #745.
void CSharedFileList::NotifyPathAdded(const wxString& fullPath)
{
if (fullPath.IsEmpty()) {
return;
}
// Already shared? CPartFile::CompleteFile() and SafeAddKFile() are
// the canonical add paths for completed downloads — by the time
// the watcher's CREATE event fires for a freshly-renamed file in
// Incoming, the CKnownFile is usually already in m_Files_map and
// the path index. Nothing to do in that case. Scoped lock so we
// drop list_mut before doing any filesystem work.
{
wxMutexLocker existsCheck(list_mut);
if (m_pathIndex.find(fullPath) != m_pathIndex.end()) {
return;
}
}
CPath full(fullPath);
if (!full.IsOk()) {
return;
}
const CPath directory = full.GetPath();
const CPath fname = CPath(full.GetFullName());
if (!directory.IsOk() || !fname.IsOk()) {
return;
}
TaskList hashTasks;
switch (AddPathToShares(directory, fname, hashTasks)) {
case kAddPathQueued:
// Hand the new hashing task to the scheduler. The thread
// will call SafeAddKFile() when it finishes, which is
// what publishes the file to peers + the GUI.
for (TaskList::iterator it = hashTasks.begin(); it != hashTasks.end(); ++it) {
CThreadScheduler::AddTask(*it);
}
break;
case kAddPathKnown:
case kAddPathSkipped:
// AddPathToShares already wrote a debug log line; no
// further action needed.
break;
}
}
void CSharedFileList::NotifyPathRemoved(const wxString& fullPath)
{
if (fullPath.IsEmpty()) {
return;
}
// RemoveFile re-acquires list_mut itself, so we hold list_mut
// only long enough to resolve the path → CKnownFile* lookup and
// then drop it before calling RemoveFile.
CKnownFile* file = NULL;
{
wxMutexLocker lock(list_mut);
auto it = m_pathIndex.find(fullPath);
if (it == m_pathIndex.end()) {
return;
}
file = it->second;
}
AddDebugLogLineN(logKnownFiles,
CFormat("Watcher: detaching deleted file '%s' from shares") % fullPath);
RemoveFile(file);
}
void CSharedFileList::NotifyPathModified(const wxString& fullPath)
{
if (fullPath.IsEmpty()) {
return;
}
// MODIFY events fire on metadata touches (utime, chmod, etc.) as
// well as on content writes. Only a size/mtime delta warrants
// re-hashing. Look up the file in the path index and compare its
// known mtime/size against what's on disk.
CKnownFile* file = NULL;
{
wxMutexLocker lock(list_mut);
auto it = m_pathIndex.find(fullPath);
if (it == m_pathIndex.end()) {
// Path appeared via MODIFY but wasn't already shared
// — treat as add. List_mut is dropped at scope exit
// before NotifyPathAdded re-acquires it.
file = NULL;
} else {
file = it->second;
}
}
if (file == NULL) {
NotifyPathAdded(fullPath);
return;
}
CPath full(fullPath);
time_t fdiskDate = CPath::GetModificationTime(full);
sint64 fdiskSize = full.GetFileSize();
if (fdiskDate == (time_t)-1 || fdiskSize == wxInvalidOffset) {
// File vanished or unreadable. Treat as removal.
AddDebugLogLineN(logKnownFiles,
CFormat("Watcher: file '%s' became unreadable on MODIFY, detaching") % fullPath);
RemoveFile(file);
return;
}
if (fdiskDate == file->GetLastChangeDatetime() && fdiskSize == (sint64)file->GetFileSize()) {
// Same size, same mtime — content unchanged. Drop the event.
return;
}
// Size or mtime moved. Content has changed and the existing
// hashes are stale. Detach + re-add forces a fresh CHashingTask.
AddDebugLogLineN(logKnownFiles,
CFormat("Watcher: content changed on '%s' (size/mtime delta), re-hashing") % fullPath);
RemoveFile(file);
NotifyPathAdded(fullPath);
}
void CSharedFileList::Reload()
{
Reload(nullptr);
}
bool CSharedFileList::Reload(ReloadYieldCb yieldCb)
{
// Madcat - Disable reloading if reloading already in progress.
// Kry - Fixed to let non-english language users use the 'Reload' button :P
// deltaHF - removed the old ugly button and changed the code to use the new small one
// Kry - bah, let's use a var.
if (reloading) {
// Already running. Surface that to the caller as a non-abort,
// non-complete state — they shouldn't react as if they
// cancelled, but also haven't completed a fresh scan.
return true;
}
AddDebugLogLineN(logKnownFiles, "Reload shared files");
reloading = true;
Notify_SharedFilesRemoveAllItems();
/* All Kad keywords must be removed.
*
* m_keywords has no internal locking; CSharedFileList::list_mut is
* the outer lock for both m_Files_map and m_keywords (every other
* AddFile / RemoveFile call takes it around m_keywords operations).
* Without the lock here we race CUploadDiskIOThread, which calls
* theApp->sharedfiles->RemoveFile(srcfile) from a worker thread when
* a previously-shared file disappears under it (e.g. user renaming
* a file in Incoming with shared-dir watching enabled, issue #685).
* The worker holds list_mut while it mutates m_keywords via
* RemoveKeywords; concurrent unlocked iteration over m_lstKeywords /
* m_keywordIndex here invalidates iterators / uses freed
* CPublishKeyword*. Lock only around the keyword ops, NOT around
* FindSharedFiles -- that walks the filesystem and would block the
* worker pool for seconds at a time. */
{
wxMutexLocker lock(list_mut);
m_keywords->RemoveAllKeywordReferences();
}
/* Public identifiers must be erased as they might be invalid now */
m_PublicSharedDirNames.clear();
bool aborted = false;
FindSharedFiles(yieldCb, aborted);
/* And now the unreferenced keywords must be removed also */
{
wxMutexLocker lock(list_mut);
m_keywords->PurgeUnreferencedKeywords();
}
Notify_SharedFilesShowFileList();
// Re-sync the watcher's path set so dirs added or removed from
// shareddir_list since the previous Reload are picked up.
if (m_dirWatcher) {
m_dirWatcher->Refresh();
}
// Tell KnownFileList that a full scan has now run -- this
// gates the duplicate-list cap-prune in Save(), so the prune
// never fires while the pin set is unpopulated (which would
// drop records the scan was about to pin). Only on non-aborted
// scans: a cancelled mid-scan leaves the pin set partial.
if (!aborted && filelist) {
filelist->MarkInitialShareScanComplete();
}
reloading = false;
return !aborted;
}
const CKnownFile *CSharedFileList::GetFileByIndex(unsigned int index) const
{
wxMutexLocker lock(list_mut);
if ( index >= m_Files_map.size() ) {
return NULL;
}
CKnownFileMap::const_iterator pos = m_Files_map.begin();
std::advance(pos, index);
return pos->second;
}
CKnownFile* CSharedFileList::GetFileByID(const CMD4Hash& filehash)
{
wxMutexLocker lock(list_mut);
CKnownFileMap::iterator it = m_Files_map.find(filehash);
if ( it != m_Files_map.end() ) {
return it->second;
} else {
return NULL;
}
}
short CSharedFileList::GetFilePriorityByID(const CMD4Hash& filehash)
{
CKnownFile* tocheck = GetFileByID(filehash);
if (tocheck)
return tocheck->GetUpPriority();
else
return -10; // file doesn't exist
}
void CSharedFileList::CopyFileList(std::vector<CKnownFile*>& out_list) const
{
wxMutexLocker lock(list_mut);
out_list.reserve(m_Files_map.size());
for (
CKnownFileMap::const_iterator it = m_Files_map.begin();
it != m_Files_map.end();
++it
) {
out_list.push_back(it->second);
}
}
void CSharedFileList::UpdateItem(CKnownFile* toupdate)
{
Notify_SharedFilesUpdateItem(toupdate);
}
void CSharedFileList::GetSharedFilesByDirectory(const wxString& directory,
CKnownFilePtrList& list)
{
wxMutexLocker lock(list_mut);
const CPath dir = CPath(directory);
for (CKnownFileMap::iterator pos = m_Files_map.begin();
pos != m_Files_map.end(); ++pos ) {
CKnownFile *cur_file = pos->second;
if (dir.IsSameDir(cur_file->GetFilePath())) {
list.push_back(cur_file);
}
}
}
/* ---------------- Network ----------------- */
void CSharedFileList::ClearED2KPublishInfo(){
CKnownFile* cur_file;
m_lastPublishED2KFlag = true;
wxMutexLocker lock(list_mut);
// Suppress per-row GUI updates while we walk every shared file.
// SetPublishedED2K() notifies the SharedFilesCtrl which does an
// O(N) FindItem per call; without this, a 100k-file shared list
// makes every server disconnect freeze the main thread for
// minutes. SetPublishedED2K() is also a no-op when the value
// didn't change, so the genuinely-false→false majority is free.
// See #302.
Notify_SharedFilesBeginBulkUpdate();
for (CKnownFileMap::iterator pos = m_Files_map.begin(); pos != m_Files_map.end(); ++pos ) {
cur_file = pos->second;
cur_file->SetPublishedED2K(false);
}
Notify_SharedFilesEndBulkUpdate();
}
void CSharedFileList::ClearKadSourcePublishInfo()
{
wxMutexLocker lock(list_mut);
CKnownFile* cur_file;
for (CKnownFileMap::iterator pos = m_Files_map.begin(); pos != m_Files_map.end(); ++pos ) {
cur_file = pos->second;
cur_file->SetLastPublishTimeKadSrc(0,0);
}
}
void CSharedFileList::RepublishFile(CKnownFile* pFile)
{
CServer* server = theApp->serverconnect->GetCurrentServer();
if (server && (server->GetTCPFlags() & SRV_TCPFLG_COMPRESSION)) {
m_lastPublishED2KFlag = true;
pFile->SetPublishedED2K(false); // FIXME: this creates a wrong 'No' for the ed2k shared info in the listview until the file is shared again.
}
}
static uint8 GetRealPrio(uint8 in)
{
switch(in) {
case 4 : return 0;
case 0 : return 1;
case 1 : return 2;
case 2 : return 3;
case 3 : return 4;
}
return 0;
}
static bool SortFunc( const CKnownFile* fileA, const CKnownFile* fileB )
{
return GetRealPrio(fileA->GetUpPriority()) < GetRealPrio(fileB->GetUpPriority());
}