| 207 | |
| 208 | |
| 209 | bool DirIvCache::store(LPCWSTR path, const unsigned char *dir_iv, const FILETIME& last_write_time) |
| 210 | { |
| 211 | |
| 212 | bool rval = true; |
| 213 | |
| 214 | wstring key = path; |
| 215 | |
| 216 | normalize_key(key); |
| 217 | |
| 218 | lock(); |
| 219 | |
| 220 | try { |
| 221 | |
| 222 | // see if it's already there |
| 223 | auto mp = m_map.emplace(key, (DirIvCacheNode*)NULL); |
| 224 | |
| 225 | if (mp.second) { |
| 226 | |
| 227 | DirIvCacheNode *node = NULL; |
| 228 | |
| 229 | // if it isn't, then see if the cache is full |
| 230 | |
| 231 | // if so, remove oldest entry (from tail of linked list) |
| 232 | |
| 233 | if (m_map.size() > DIR_IV_CACHE_ENTRIES) { |
| 234 | node = m_lru_list.back(); |
| 235 | m_lru_list.pop_back(); |
| 236 | m_map.erase(node->m_key); |
| 237 | } |
| 238 | |
| 239 | // re-use node if we removed one, otherwise get one from spare list, otherwise make a new one |
| 240 | |
| 241 | if (!node) { |
| 242 | if (!m_spare_node_list.empty()) { |
| 243 | node = m_spare_node_list.front(); |
| 244 | m_spare_node_list.pop_front(); |
| 245 | } else { |
| 246 | node = new DirIvCacheNode; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | mp.first->second = node; |
| 251 | |
| 252 | node->m_key = mp.first->first; |
| 253 | memcpy(node->m_dir_iv, dir_iv, DIR_IV_LEN); |
| 254 | node->m_timestamp = GetTickCount64(); |
| 255 | node->m_last_write_time = last_write_time; |
| 256 | m_lru_list.push_front(node); |
| 257 | node->m_list_it = m_lru_list.begin(); |
| 258 | |
| 259 | } else { |
| 260 | // copy dir_iv to node at that path (key) |
| 261 | memcpy(mp.first->second->m_dir_iv, dir_iv, DIR_IV_LEN); |
| 262 | mp.first->second->m_timestamp = GetTickCount64(); |
| 263 | mp.first->second->m_last_write_time = last_write_time; |
| 264 | |
| 265 | update_lru(mp.first->second); |
| 266 | |