Upstream tarball 9597
[amule.git] / src / KnownFileList.cpp
blob7ccef6bbd4e585d1e3a99bf227d824a05cc1cc63
1 //
2 // This file is part of the aMule Project.
3 //
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 )
6 //
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
9 // respective authors.
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.
20 //
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
33 #include "amule.h"
34 #include "Logger.h"
35 #include "ScopedPtr.h"
36 #include <common/Format.h>
39 // This function is inlined for performance
40 inline bool CKnownFileList::KnownFileMatches(
41 CKnownFile *knownFile,
42 const CPath& filename,
43 uint32 in_date,
44 uint64 in_size) const
46 return
47 (knownFile->GetLastChangeDatetime() == (time_t)in_date) &&
48 (knownFile->GetFileSize() == in_size) &&
49 (knownFile->GetFileName() == filename);
53 CKnownFileList::CKnownFileList()
55 accepted = 0;
56 requested = 0;
57 transferred = 0;
58 Init();
62 CKnownFileList::~CKnownFileList()
64 Clear();
68 bool CKnownFileList::Init()
70 CFile file;
72 CPath fullpath = CPath(theApp->ConfigDir + wxT("known.met"));
73 if (!fullpath.FileExists()) {
74 // This is perfectly normal. The file was probably either
75 // deleted, or this is the first time running aMule.
76 return false;
79 if (!file.Open(fullpath)) {
80 AddLogLineM(true, _("WARNING: known.met cannot be opened."));
81 return false;
84 try {
85 uint8 version = file.ReadUInt8();
86 if ((version != MET_HEADER) && (version != MET_HEADER_WITH_LARGEFILES)) {
87 AddLogLineM(true, _("WARNING: Known file list corrupted, contains invalid header."));
88 return false;
91 wxMutexLocker sLock(list_mut);
92 uint32 RecordsNumber = file.ReadUInt32();
93 AddDebugLogLineM(false, logKnownFiles,
94 wxString::Format(wxT("Reading %i known files from file format 0x%2.2x."),
95 RecordsNumber, version));
96 for (uint32 i = 0; i < RecordsNumber; i++) {
97 CScopedPtr<CKnownFile> record(new CKnownFile());
98 if (record->LoadFromFile(&file)) {
99 AddDebugLogLineM(false, logKnownFiles,
100 CFormat(wxT("Known file read: %s")) % record->GetFileName());
101 Append(record.release());
102 } else {
103 AddLogLineM(true,
104 wxT("Failed to load entry in knownfilelist, file may be corrupt"));
107 AddDebugLogLineM(false, logKnownFiles, wxT("Finished reading known files"));
109 return true;
110 } catch (const CInvalidPacket& e) {
111 AddLogLineM(true, wxT("Invalid entry in knownfilelist, file may be corrupt: ") + e.what());
112 } catch (const CSafeIOException& e) {
113 AddLogLineM(true, CFormat(_("IO error while reading known.met file: %s")) % e.what());
116 return false;
120 void CKnownFileList::Save()
122 CFile file(theApp->ConfigDir + wxT("known.met"), CFile::write);
123 if (!file.IsOpened()) {
124 return;
127 wxMutexLocker sLock(list_mut);
129 try {
130 // Kry - This is the version, but we don't know it till
131 // we know if any largefile is saved. This allows the list
132 // to be compatible with previous versions.
133 bool bContainsAnyLargeFiles = false;
134 file.WriteUInt8(0);
136 file.WriteUInt32(m_knownFileMap.size() + m_duplicateFileList.size());
138 // Duplicates handling. Duplicates needs to be saved first,
139 // since it is the last entry that gets used.
140 KnownFileList::iterator itDup = m_duplicateFileList.begin();
141 for ( ; itDup != m_duplicateFileList.end(); ++itDup ) {
142 (*itDup)->WriteToFile(&file);
143 if ((*itDup)->IsLargeFile()) {
144 bContainsAnyLargeFiles = true;
148 CKnownFileMap::iterator it = m_knownFileMap.begin();
149 for (; it != m_knownFileMap.end(); ++it) {
150 it->second->WriteToFile(&file);
151 if (it->second->IsLargeFile()) {
152 bContainsAnyLargeFiles = true;
156 file.Seek(0);
157 file.WriteUInt8(bContainsAnyLargeFiles ? MET_HEADER_WITH_LARGEFILES : MET_HEADER);
158 } catch (const CIOFailureException& e) {
159 AddLogLineM(true, CFormat(_("Error while saving known.met file: %s")) % e.what());
164 void CKnownFileList::Clear()
166 wxMutexLocker sLock(list_mut);
168 for (CKnownFileMap::iterator it = m_knownFileMap.begin();
169 it != m_knownFileMap.end(); ++it) {
170 delete it->second;
172 m_knownFileMap.clear();
174 for (KnownFileList::iterator it = m_duplicateFileList.begin();
175 it != m_duplicateFileList.end(); ++it) {
176 delete *it;
178 m_duplicateFileList.clear();
182 CKnownFile* CKnownFileList::FindKnownFile(
183 const CPath& filename,
184 time_t in_date,
185 uint64 in_size)
187 wxMutexLocker sLock(list_mut);
189 for (CKnownFileMap::const_iterator it = m_knownFileMap.begin();
190 it != m_knownFileMap.end(); ++it) {
191 CKnownFile *cur_file = it->second;
192 if (KnownFileMatches(cur_file, filename, in_date, in_size)) {
193 return cur_file;
197 return IsOnDuplicates(filename, in_date, in_size);
201 CKnownFile *CKnownFileList::IsOnDuplicates(
202 const CPath& filename,
203 uint32 in_date,
204 uint64 in_size) const
206 for (KnownFileList::const_iterator it = m_duplicateFileList.begin();
207 it != m_duplicateFileList.end(); ++it) {
208 CKnownFile *cur_file = *it;
209 if (KnownFileMatches(cur_file, filename, in_date, in_size)) {
210 return cur_file;
213 return NULL;
217 bool CKnownFileList::IsKnownFile(const CKnownFile *file)
219 wxCHECK(file, false);
221 wxMutexLocker sLock(list_mut);
223 // For the map, search with the key
224 const CMD4Hash &key = file->GetFileHash();
225 CKnownFileMap::const_iterator itMap = m_knownFileMap.find(key);
226 if (itMap != m_knownFileMap.end()) {
227 return true;
229 // For the list, we have to iterate to search
230 for (KnownFileList::iterator it = m_duplicateFileList.begin();
231 it != m_duplicateFileList.end(); ++it) {
232 if (*it == file) {
233 return true;
236 return false;
240 CKnownFile* CKnownFileList::FindKnownFileByID(const CMD4Hash& hash)
242 wxMutexLocker sLock(list_mut);
244 if (!hash.IsEmpty()) {
245 if (m_knownFileMap.find(hash) != m_knownFileMap.end()) {
246 return m_knownFileMap[hash];
247 } else {
248 return NULL;
251 return NULL;
256 bool CKnownFileList::SafeAddKFile(CKnownFile* toadd)
258 wxMutexLocker sLock(list_mut);
259 return Append(toadd);
263 bool CKnownFileList::Append(CKnownFile *Record)
265 if (Record->GetFileSize() > 0) {
266 const CMD4Hash& tkey = Record->GetFileHash();
267 CKnownFileMap::iterator it = m_knownFileMap.find(tkey);
268 if (it == m_knownFileMap.end()) {
269 m_knownFileMap[tkey] = Record;
270 return true;
271 } else {
272 it->second;
273 time_t in_date = it->second->GetLastChangeDatetime();
274 uint64 in_size = it->second->GetFileSize();
275 CPath filename = it->second->GetFileName();
276 if (KnownFileMatches(Record, filename, in_date, in_size) ||
277 IsOnDuplicates(filename, in_date, in_size)) {
278 // The file is already on the list, ignore it.
279 return false;
280 } else {
281 // The file is a duplicated hash. Add THE OLD ONE to the duplicates list.
282 m_duplicateFileList.push_back(m_knownFileMap[tkey]);
283 // Is this thread-safe? If John is not sure and I'm not sure either...
284 if (theApp->sharedfiles) {
285 // Removing the old kad keywords created with the old filename
286 theApp->sharedfiles->RemoveKeywords(it->second);
288 m_knownFileMap[tkey] = Record;
289 return true;
292 } else {
293 AddDebugLogLineM(false, logGeneral,
294 CFormat(wxT("%s is 0-size, not added")) %
295 Record->GetFileName());
297 return false;
301 // File_checked_for_headers