| 98 | |
| 99 | |
| 100 | bool CKnownFileList::Init() |
| 101 | { |
| 102 | CFile file; |
| 103 | |
| 104 | CPath fullpath = CPath(thePrefs::GetConfigDir() + m_filename); |
| 105 | if (!fullpath.FileExists()) { |
| 106 | // This is perfectly normal. The file was probably either |
| 107 | // deleted, or this is the first time running aMule. |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if (!file.Open(fullpath)) { |
| 112 | AddLogLineC(CFormat(_("WARNING: %s cannot be opened.")) % m_filename); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | try { |
| 117 | uint8 version = file.ReadUInt8(); |
| 118 | if ((version != MET_HEADER) && (version != MET_HEADER_WITH_LARGEFILES)) { |
| 119 | AddLogLineC(_("WARNING: Known file list corrupted, contains invalid header.")); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | wxMutexLocker sLock(list_mut); |
| 124 | uint32 RecordsNumber = file.ReadUInt32(); |
| 125 | AddDebugLogLineN(logKnownFiles, CFormat("Reading %i known files from file format 0x%2.2x.") |
| 126 | % RecordsNumber % version); |
| 127 | |
| 128 | // Keep the size-map index live during the load. Append() is O(log N) |
| 129 | // on every record, but on each MD4 hash collision (real-world |
| 130 | // libraries hit these whenever the same content was indexed under |
| 131 | // two paths/names) it falls back to IsOnDuplicates(name, date, size). |
| 132 | // Without a duplicate-size index, IsOnDuplicates scans |
| 133 | // m_duplicateFileList linearly, so the dedup cost grows with each |
| 134 | // duplicate appended — O(N^2) over the whole load. Prebuilding the |
| 135 | // (empty) index here lets Append maintain it incrementally, giving |
| 136 | // the O(log N) equal_range fast path on every collision check. |
| 137 | // Issue #562 startup gap, ~36 s on a 200 k-file library. |
| 138 | PrepareIndex(); |
| 139 | for (uint32 i = 0; i < RecordsNumber; i++) { |
| 140 | CScopedPtr<CKnownFile> record; |
| 141 | if (record->LoadFromFile(&file)) { |
| 142 | AddDebugLogLineN(logKnownFiles, |
| 143 | CFormat("Known file read: %s") % record->GetFileName()); |
| 144 | Append(record.release()); |
| 145 | } else { |
| 146 | AddLogLineC(_("Failed to load entry in known file list, file may be corrupt")); |
| 147 | } |
| 148 | } |
| 149 | ReleaseIndex(); |
| 150 | AddDebugLogLineN(logKnownFiles, "Finished reading known files"); |
| 151 | |
| 152 | return true; |
| 153 | } catch (const CInvalidPacket& e) { |
| 154 | ReleaseIndex(); |
| 155 | AddLogLineC(_("Invalid entry in known file list, file may be corrupt: ") + e.what()); |
| 156 | } catch (const CSafeIOException& e) { |
| 157 | ReleaseIndex(); |
nothing calls this directly
no test coverage detected