OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/disk_cache/simple/simple_index_file.h" | 5 #include "net/disk_cache/simple/simple_index_file.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 // For POSIX systems, a last access time is available. However, it's not | 153 // For POSIX systems, a last access time is available. However, it's not |
154 // guaranteed to be more accurate than mtime. It is no worse though. | 154 // guaranteed to be more accurate than mtime. It is no worse though. |
155 last_used_time = last_accessed; | 155 last_used_time = last_accessed; |
156 #endif | 156 #endif |
157 if (last_used_time.is_null()) | 157 if (last_used_time.is_null()) |
158 last_used_time = last_modified; | 158 last_used_time = last_modified; |
159 | 159 |
160 SimpleIndex::EntrySet::iterator it = entries->find(hash_key); | 160 SimpleIndex::EntrySet::iterator it = entries->find(hash_key); |
161 base::CheckedNumeric<uint32_t> total_entry_size = size; | 161 base::CheckedNumeric<uint32_t> total_entry_size = size; |
162 | 162 |
| 163 // Sometimes we see entry sizes here which are nonsense. We can't use them |
| 164 // as-is, as they simply won't fit the type. The options that come to mind |
| 165 // are: |
| 166 // 1) Ignore the file. |
| 167 // 2) Make something up. |
| 168 // 3) Delete the files for the hash. |
| 169 // ("crash the browser" isn't considered a serious alternative). |
| 170 // |
| 171 // The problem with doing (1) is that we are recovering the index here, so if |
| 172 // we don't include the info on the file here, we may completely lose track of |
| 173 // the entry and never clean the file up. |
| 174 // |
| 175 // (2) is actually mostly fine: we may trigger eviction too soon or too late, |
| 176 // but we can't really do better since we can't trust the size. If the entry |
| 177 // is never opened, it will eventually get evicted. If it is opened, we will |
| 178 // re-check the file size, and if it's nonsense delete it there, and if it's |
| 179 // fine we will fix up the index via a UpdateDataFromEntryStat to have the |
| 180 // correct size. |
| 181 // |
| 182 // (3) does the best thing except when the wrong size is some weird interim |
| 183 // thing just on directory listing (in which case it may evict an entry |
| 184 // prematurely). It's a little harder to think about since it involves |
| 185 // mutating the disk while there are other mutations going on, however, |
| 186 // while (2) is single-threaded. |
| 187 // |
| 188 // Hence this picks (2). |
| 189 |
| 190 const int kPlaceHolderSizeWhenInvalid = 32768; |
| 191 if (!total_entry_size.IsValid()) { |
| 192 LOG(WARNING) << "Invalid file size while restoring index from disk: " |
| 193 << size << " on file:" << file_name; |
| 194 } |
| 195 |
163 if (it == entries->end()) { | 196 if (it == entries->end()) { |
164 SimpleIndex::InsertInEntrySet( | 197 SimpleIndex::InsertInEntrySet( |
165 hash_key, EntryMetadata(last_used_time, total_entry_size.ValueOrDie()), | 198 hash_key, |
| 199 EntryMetadata(last_used_time, total_entry_size.ValueOrDefault( |
| 200 kPlaceHolderSizeWhenInvalid)), |
166 entries); | 201 entries); |
167 } else { | 202 } else { |
168 // Summing up the total size of the entry through all the *_[0-1] files | 203 // Summing up the total size of the entry through all the *_[0-1] files |
169 total_entry_size += it->second.GetEntrySize(); | 204 total_entry_size += it->second.GetEntrySize(); |
170 it->second.SetEntrySize(total_entry_size.ValueOrDie()); | 205 it->second.SetEntrySize( |
| 206 total_entry_size.ValueOrDefault(kPlaceHolderSizeWhenInvalid)); |
171 } | 207 } |
172 } | 208 } |
173 | 209 |
174 } // namespace | 210 } // namespace |
175 | 211 |
176 SimpleIndexLoadResult::SimpleIndexLoadResult() | 212 SimpleIndexLoadResult::SimpleIndexLoadResult() |
177 : did_load(false), | 213 : did_load(false), |
178 index_write_reason(SimpleIndex::INDEX_WRITE_REASON_MAX), | 214 index_write_reason(SimpleIndex::INDEX_WRITE_REASON_MAX), |
179 flush_required(false) {} | 215 flush_required(false) {} |
180 | 216 |
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 bool SimpleIndexFile::LegacyIsIndexFileStale( | 605 bool SimpleIndexFile::LegacyIsIndexFileStale( |
570 base::Time cache_last_modified, | 606 base::Time cache_last_modified, |
571 const base::FilePath& index_file_path) { | 607 const base::FilePath& index_file_path) { |
572 base::Time index_mtime; | 608 base::Time index_mtime; |
573 if (!simple_util::GetMTime(index_file_path, &index_mtime)) | 609 if (!simple_util::GetMTime(index_file_path, &index_mtime)) |
574 return true; | 610 return true; |
575 return index_mtime < cache_last_modified; | 611 return index_mtime < cache_last_modified; |
576 } | 612 } |
577 | 613 |
578 } // namespace disk_cache | 614 } // namespace disk_cache |
OLD | NEW |