2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "KnownFileList.h" // Interface declarations
29 #include <common/DataFileVersion.h>
31 #include <memory> // Do_not_auto_remove (lionel's Mac, 10.3)
32 #include "PartFile.h" // Needed for CPartFile
35 #include "ScopedPtr.h"
36 #include "SearchList.h" // Needed for UpdateSearchFileByHash
37 #include <common/Format.h>
40 // This function is inlined for performance
41 inline bool CKnownFileList::KnownFileMatches(
42 CKnownFile
*knownFile
,
43 const CPath
& filename
,
48 (knownFile
->GetLastChangeDatetime() == (time_t)in_date
) &&
49 (knownFile
->GetFileSize() == in_size
) &&
50 (knownFile
->GetFileName() == filename
);
54 CKnownFileList::CKnownFileList()
59 m_filename
= wxT("known.met");
60 m_knownSizeMap
= NULL
;
61 m_duplicateSizeMap
= NULL
;
66 CKnownFileList::~CKnownFileList()
72 bool CKnownFileList::Init()
76 CPath fullpath
= CPath(theApp
->ConfigDir
+ m_filename
);
77 if (!fullpath
.FileExists()) {
78 // This is perfectly normal. The file was probably either
79 // deleted, or this is the first time running aMule.
83 if (!file
.Open(fullpath
)) {
84 AddLogLineM(true, CFormat(_("WARNING: %s cannot be opened.")) % m_filename
);
89 uint8 version
= file
.ReadUInt8();
90 if ((version
!= MET_HEADER
) && (version
!= MET_HEADER_WITH_LARGEFILES
)) {
91 AddLogLineM(true, _("WARNING: Known file list corrupted, contains invalid header."));
95 wxMutexLocker
sLock(list_mut
);
96 uint32 RecordsNumber
= file
.ReadUInt32();
97 AddDebugLogLineM(false, logKnownFiles
,
98 wxString::Format(wxT("Reading %i known files from file format 0x%2.2x."),
99 RecordsNumber
, version
));
100 for (uint32 i
= 0; i
< RecordsNumber
; i
++) {
101 CScopedPtr
<CKnownFile
> record(new CKnownFile());
102 if (record
->LoadFromFile(&file
)) {
103 AddDebugLogLineM(false, logKnownFiles
,
104 CFormat(wxT("Known file read: %s")) % record
->GetFileName());
105 Append(record
.release());
107 AddLogLineC(_("Failed to load entry in known file list, file may be corrupt"));
110 AddDebugLogLineM(false, logKnownFiles
, wxT("Finished reading known files"));
113 } catch (const CInvalidPacket
& e
) {
114 AddLogLineC(_("Invalid entry in known file list, file may be corrupt: ") + e
.what());
115 } catch (const CSafeIOException
& e
) {
116 AddLogLineM(true, CFormat(_("IO error while reading %s file: %s")) % m_filename
% e
.what());
123 void CKnownFileList::Save()
125 CFile
file(theApp
->ConfigDir
+ m_filename
, CFile::write
);
126 if (!file
.IsOpened()) {
130 wxMutexLocker
sLock(list_mut
);
133 // Kry - This is the version, but we don't know it till
134 // we know if any largefile is saved. This allows the list
135 // to be compatible with previous versions.
136 bool bContainsAnyLargeFiles
= false;
139 file
.WriteUInt32(m_knownFileMap
.size() + m_duplicateFileList
.size());
141 // Duplicates handling. Duplicates needs to be saved first,
142 // since it is the last entry that gets used.
143 KnownFileList::iterator itDup
= m_duplicateFileList
.begin();
144 for ( ; itDup
!= m_duplicateFileList
.end(); ++itDup
) {
145 (*itDup
)->WriteToFile(&file
);
146 if ((*itDup
)->IsLargeFile()) {
147 bContainsAnyLargeFiles
= true;
151 CKnownFileMap::iterator it
= m_knownFileMap
.begin();
152 for (; it
!= m_knownFileMap
.end(); ++it
) {
153 it
->second
->WriteToFile(&file
);
154 if (it
->second
->IsLargeFile()) {
155 bContainsAnyLargeFiles
= true;
160 file
.WriteUInt8(bContainsAnyLargeFiles
? MET_HEADER_WITH_LARGEFILES
: MET_HEADER
);
161 } catch (const CIOFailureException
& e
) {
162 AddLogLineM(true, CFormat(_("Error while saving %s file: %s")) % m_filename
% e
.what());
167 void CKnownFileList::Clear()
169 wxMutexLocker
sLock(list_mut
);
171 DeleteContents(m_knownFileMap
);
172 DeleteContents(m_duplicateFileList
);
177 CKnownFile
* CKnownFileList::FindKnownFile(
178 const CPath
& filename
,
182 wxMutexLocker
sLock(list_mut
);
184 if (m_knownSizeMap
) {
185 std::pair
<KnownFileSizeMap::const_iterator
, KnownFileSizeMap::const_iterator
> p
;
186 p
= m_knownSizeMap
->equal_range((uint32
) in_size
);
187 for (KnownFileSizeMap::const_iterator it
= p
.first
; it
!= p
.second
; it
++) {
188 CKnownFile
*cur_file
= it
->second
;
189 if (KnownFileMatches(cur_file
, filename
, in_date
, in_size
)) {
194 for (CKnownFileMap::const_iterator it
= m_knownFileMap
.begin();
195 it
!= m_knownFileMap
.end(); ++it
) {
196 CKnownFile
*cur_file
= it
->second
;
197 if (KnownFileMatches(cur_file
, filename
, in_date
, in_size
)) {
203 return IsOnDuplicates(filename
, in_date
, in_size
);
207 CKnownFile
*CKnownFileList::IsOnDuplicates(
208 const CPath
& filename
,
210 uint64 in_size
) const
212 if (m_duplicateSizeMap
) {
213 std::pair
<KnownFileSizeMap::const_iterator
, KnownFileSizeMap::const_iterator
> p
;
214 p
= m_duplicateSizeMap
->equal_range((uint32
) in_size
);
215 for (KnownFileSizeMap::const_iterator it
= p
.first
; it
!= p
.second
; it
++) {
216 CKnownFile
*cur_file
= it
->second
;
217 if (KnownFileMatches(cur_file
, filename
, in_date
, in_size
)) {
222 for (KnownFileList::const_iterator it
= m_duplicateFileList
.begin();
223 it
!= m_duplicateFileList
.end(); ++it
) {
224 CKnownFile
*cur_file
= *it
;
225 if (KnownFileMatches(cur_file
, filename
, in_date
, in_size
)) {
234 bool CKnownFileList::IsKnownFile(const CKnownFile
*file
)
236 wxCHECK(file
, false);
238 wxMutexLocker
sLock(list_mut
);
240 // For the map, search with the key
241 const CMD4Hash
&key
= file
->GetFileHash();
242 CKnownFileMap::const_iterator itMap
= m_knownFileMap
.find(key
);
243 if (itMap
!= m_knownFileMap
.end()) {
246 // For the list, we have to iterate to search
247 for (KnownFileList::iterator it
= m_duplicateFileList
.begin();
248 it
!= m_duplicateFileList
.end(); ++it
) {
257 CKnownFile
* CKnownFileList::FindKnownFileByID(const CMD4Hash
& hash
)
259 wxMutexLocker
sLock(list_mut
);
261 if (!hash
.IsEmpty()) {
262 if (m_knownFileMap
.find(hash
) != m_knownFileMap
.end()) {
263 return m_knownFileMap
[hash
];
273 bool CKnownFileList::SafeAddKFile(CKnownFile
* toadd
)
277 wxMutexLocker
sLock(list_mut
);
281 theApp
->searchlist
->UpdateSearchFileByHash(toadd
->GetFileHash());
287 bool CKnownFileList::Append(CKnownFile
*Record
)
289 if (Record
->GetFileSize() > 0) {
290 const CMD4Hash
& tkey
= Record
->GetFileHash();
291 CKnownFileMap::iterator it
= m_knownFileMap
.find(tkey
);
292 if (it
== m_knownFileMap
.end()) {
293 m_knownFileMap
[tkey
] = Record
;
297 time_t in_date
= it
->second
->GetLastChangeDatetime();
298 uint64 in_size
= it
->second
->GetFileSize();
299 CPath filename
= it
->second
->GetFileName();
300 if (KnownFileMatches(Record
, filename
, in_date
, in_size
) ||
301 IsOnDuplicates(filename
, in_date
, in_size
)) {
302 // The file is already on the list, ignore it.
305 // The file is a duplicated hash. Add THE OLD ONE to the duplicates list.
306 m_duplicateFileList
.push_back(m_knownFileMap
[tkey
]);
307 // Is this thread-safe? If John is not sure and I'm not sure either...
308 if (theApp
->sharedfiles
) {
309 // Removing the old kad keywords created with the old filename
310 theApp
->sharedfiles
->RemoveKeywords(it
->second
);
312 m_knownFileMap
[tkey
] = Record
;
317 AddDebugLogLineM(false, logGeneral
,
318 CFormat(wxT("%s is 0-size, not added")) %
319 Record
->GetFileName());
325 // Make an index by size to speed up FindKnownFile
326 // Size modulo 2^32 is enough here
327 void CKnownFileList::PrepareIndex()
330 m_knownSizeMap
= new KnownFileSizeMap
;
331 for (CKnownFileMap::const_iterator it
= m_knownFileMap
.begin(); it
!= m_knownFileMap
.end(); it
++) {
332 m_knownSizeMap
->insert(std::pair
<uint32
, CKnownFile
*>((uint32
) it
->second
->GetFileSize(), it
->second
));
334 m_duplicateSizeMap
= new KnownFileSizeMap
;
335 for (KnownFileList::const_iterator it
= m_duplicateFileList
.begin(); it
!= m_duplicateFileList
.end(); it
++) {
336 m_duplicateSizeMap
->insert(std::pair
<uint32
, CKnownFile
*>((uint32
) (*it
)->GetFileSize(), *it
));
341 void CKnownFileList::ReleaseIndex()
343 delete m_knownSizeMap
;
344 delete m_duplicateSizeMap
;
345 m_knownSizeMap
= NULL
;
346 m_duplicateSizeMap
= NULL
;
349 // File_checked_for_headers