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 "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/hash.h" | 8 #include "base/hash.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/pickle.h" | 10 #include "base/pickle.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 bool SimpleIndexFile::IndexMetadata::CheckIndexMetadata() { | 60 bool SimpleIndexFile::IndexMetadata::CheckIndexMetadata() { |
61 return number_of_entries_ <= kMaxEntiresInIndex && | 61 return number_of_entries_ <= kMaxEntiresInIndex && |
62 magic_number_ == disk_cache::kSimpleIndexMagicNumber && | 62 magic_number_ == disk_cache::kSimpleIndexMagicNumber && |
63 version_ == disk_cache::kSimpleVersion; | 63 version_ == disk_cache::kSimpleVersion; |
64 } | 64 } |
65 | 65 |
66 // static | 66 // static |
67 scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::LoadFromDisk( | 67 scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::LoadFromDisk( |
68 const base::FilePath& index_filename) { | 68 const base::FilePath& index_filename) { |
| 69 // Only load if the index is not stale. |
| 70 if (SimpleIndexFile::IsIndexFileStale(index_filename)) |
| 71 return scoped_ptr<SimpleIndex::EntrySet>(NULL); |
69 std::string contents; | 72 std::string contents; |
70 if(!file_util::ReadFileToString(index_filename, &contents)) { | 73 if(!file_util::ReadFileToString(index_filename, &contents)) { |
71 LOG(WARNING) << "Could not read Simple Index file."; | 74 LOG(WARNING) << "Could not read Simple Index file."; |
72 return scoped_ptr<SimpleIndex::EntrySet>(NULL); | 75 return scoped_ptr<SimpleIndex::EntrySet>(NULL); |
73 } | 76 } |
74 | 77 |
75 return SimpleIndexFile::Deserialize(contents.data(), contents.size()); | 78 return SimpleIndexFile::Deserialize(contents.data(), contents.size()); |
76 } | 79 } |
77 | 80 |
78 // static | 81 // static |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 LOG(ERROR) << "Could not write Simple Cache index to temporary file: " | 157 LOG(ERROR) << "Could not write Simple Cache index to temporary file: " |
155 << temp_filename.value(); | 158 << temp_filename.value(); |
156 file_util::Delete(temp_filename, /* recursive = */ false); | 159 file_util::Delete(temp_filename, /* recursive = */ false); |
157 return; | 160 return; |
158 } | 161 } |
159 // Swap temp and index_file. | 162 // Swap temp and index_file. |
160 bool result = file_util::ReplaceFile(temp_filename, index_filename); | 163 bool result = file_util::ReplaceFile(temp_filename, index_filename); |
161 DCHECK(result); | 164 DCHECK(result); |
162 } | 165 } |
163 | 166 |
| 167 // static |
| 168 bool SimpleIndexFile::IsIndexFileStale(const base::FilePath& index_filename) { |
| 169 base::PlatformFileInfo dir_info; |
| 170 base::PlatformFileInfo index_info; |
| 171 if (!file_util::GetFileInfo(index_filename.DirName(), &dir_info)) |
| 172 return false; |
| 173 DCHECK(dir_info.is_directory); |
| 174 if (!file_util::GetFileInfo(index_filename, &index_info)) |
| 175 return false; |
| 176 |
| 177 // Index file last_modified must be equal to the directory last_modified since |
| 178 // the last operation we do is ReplaceFile in the |
| 179 // SimpleIndexFile::WriteToDisk(). |
| 180 // If not true, we need to restore the index. |
| 181 return index_info.last_modified >= dir_info.last_modified; |
| 182 } |
| 183 |
164 } // namespace disk_cache | 184 } // namespace disk_cache |
OLD | NEW |