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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "third_party/zlib/zlib.h" | 25 #include "third_party/zlib/zlib.h" |
26 | 26 |
27 using base::File; | 27 using base::File; |
28 | 28 |
29 namespace disk_cache { | 29 namespace disk_cache { |
30 namespace { | 30 namespace { |
31 | 31 |
32 const int kEntryFilesHashLength = 16; | 32 const int kEntryFilesHashLength = 16; |
33 const int kEntryFilesSuffixLength = 2; | 33 const int kEntryFilesSuffixLength = 2; |
34 | 34 |
35 const uint64 kMaxEntiresInIndex = 100000000; | 35 const uint64_t kMaxEntiresInIndex = 100000000; |
36 | 36 |
37 uint32 CalculatePickleCRC(const base::Pickle& pickle) { | 37 uint32_t CalculatePickleCRC(const base::Pickle& pickle) { |
38 return crc32(crc32(0, Z_NULL, 0), | 38 return crc32(crc32(0, Z_NULL, 0), |
39 reinterpret_cast<const Bytef*>(pickle.payload()), | 39 reinterpret_cast<const Bytef*>(pickle.payload()), |
40 pickle.payload_size()); | 40 pickle.payload_size()); |
41 } | 41 } |
42 | 42 |
43 // Used in histograms. Please only add new values at the end. | 43 // Used in histograms. Please only add new values at the end. |
44 enum IndexFileState { | 44 enum IndexFileState { |
45 INDEX_STATE_CORRUPT = 0, | 45 INDEX_STATE_CORRUPT = 0, |
46 INDEX_STATE_STALE = 1, | 46 INDEX_STATE_STALE = 1, |
47 INDEX_STATE_FRESH = 2, | 47 INDEX_STATE_FRESH = 2, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 static const size_t kEntryFilesLength = | 91 static const size_t kEntryFilesLength = |
92 kEntryFilesHashLength + kEntryFilesSuffixLength; | 92 kEntryFilesHashLength + kEntryFilesSuffixLength; |
93 // Converting to std::string is OK since we never use UTF8 wide chars in our | 93 // Converting to std::string is OK since we never use UTF8 wide chars in our |
94 // file names. | 94 // file names. |
95 const base::FilePath::StringType base_name = file_path.BaseName().value(); | 95 const base::FilePath::StringType base_name = file_path.BaseName().value(); |
96 const std::string file_name(base_name.begin(), base_name.end()); | 96 const std::string file_name(base_name.begin(), base_name.end()); |
97 if (file_name.size() != kEntryFilesLength) | 97 if (file_name.size() != kEntryFilesLength) |
98 return; | 98 return; |
99 const base::StringPiece hash_string( | 99 const base::StringPiece hash_string( |
100 file_name.begin(), file_name.begin() + kEntryFilesHashLength); | 100 file_name.begin(), file_name.begin() + kEntryFilesHashLength); |
101 uint64 hash_key = 0; | 101 uint64_t hash_key = 0; |
102 if (!simple_util::GetEntryHashKeyFromHexString(hash_string, &hash_key)) { | 102 if (!simple_util::GetEntryHashKeyFromHexString(hash_string, &hash_key)) { |
103 LOG(WARNING) << "Invalid entry hash key filename while restoring index from" | 103 LOG(WARNING) << "Invalid entry hash key filename while restoring index from" |
104 << " disk: " << file_name; | 104 << " disk: " << file_name; |
105 return; | 105 return; |
106 } | 106 } |
107 | 107 |
108 File::Info file_info; | 108 File::Info file_info; |
109 if (!base::GetFileInfo(file_path, &file_info)) { | 109 if (!base::GetFileInfo(file_path, &file_info)) { |
110 LOG(ERROR) << "Could not get file info for " << file_path.value(); | 110 LOG(ERROR) << "Could not get file info for " << file_path.value(); |
111 return; | 111 return; |
112 } | 112 } |
113 base::Time last_used_time; | 113 base::Time last_used_time; |
114 #if defined(OS_POSIX) | 114 #if defined(OS_POSIX) |
115 // For POSIX systems, a last access time is available. However, it's not | 115 // For POSIX systems, a last access time is available. However, it's not |
116 // guaranteed to be more accurate than mtime. It is no worse though. | 116 // guaranteed to be more accurate than mtime. It is no worse though. |
117 last_used_time = file_info.last_accessed; | 117 last_used_time = file_info.last_accessed; |
118 #endif | 118 #endif |
119 if (last_used_time.is_null()) | 119 if (last_used_time.is_null()) |
120 last_used_time = file_info.last_modified; | 120 last_used_time = file_info.last_modified; |
121 | 121 |
122 int64 file_size = file_info.size; | 122 int64_t file_size = file_info.size; |
123 SimpleIndex::EntrySet::iterator it = entries->find(hash_key); | 123 SimpleIndex::EntrySet::iterator it = entries->find(hash_key); |
124 if (it == entries->end()) { | 124 if (it == entries->end()) { |
125 SimpleIndex::InsertInEntrySet( | 125 SimpleIndex::InsertInEntrySet( |
126 hash_key, | 126 hash_key, |
127 EntryMetadata(last_used_time, file_size), | 127 EntryMetadata(last_used_time, file_size), |
128 entries); | 128 entries); |
129 } else { | 129 } else { |
130 // Summing up the total size of the entry through all the *_[0-1] files | 130 // Summing up the total size of the entry through all the *_[0-1] files |
131 it->second.SetEntrySize(it->second.GetEntrySize() + file_size); | 131 it->second.SetEntrySize(it->second.GetEntrySize() + file_size); |
132 } | 132 } |
(...skipping 20 matching lines...) Expand all Loading... |
153 const char SimpleIndexFile::kIndexDirectory[] = "index-dir"; | 153 const char SimpleIndexFile::kIndexDirectory[] = "index-dir"; |
154 // static | 154 // static |
155 const char SimpleIndexFile::kTempIndexFileName[] = "temp-index"; | 155 const char SimpleIndexFile::kTempIndexFileName[] = "temp-index"; |
156 | 156 |
157 SimpleIndexFile::IndexMetadata::IndexMetadata() | 157 SimpleIndexFile::IndexMetadata::IndexMetadata() |
158 : magic_number_(kSimpleIndexMagicNumber), | 158 : magic_number_(kSimpleIndexMagicNumber), |
159 version_(kSimpleVersion), | 159 version_(kSimpleVersion), |
160 number_of_entries_(0), | 160 number_of_entries_(0), |
161 cache_size_(0) {} | 161 cache_size_(0) {} |
162 | 162 |
163 SimpleIndexFile::IndexMetadata::IndexMetadata( | 163 SimpleIndexFile::IndexMetadata::IndexMetadata(uint64_t number_of_entries, |
164 uint64 number_of_entries, uint64 cache_size) | 164 uint64_t cache_size) |
165 : magic_number_(kSimpleIndexMagicNumber), | 165 : magic_number_(kSimpleIndexMagicNumber), |
166 version_(kSimpleVersion), | 166 version_(kSimpleVersion), |
167 number_of_entries_(number_of_entries), | 167 number_of_entries_(number_of_entries), |
168 cache_size_(cache_size) {} | 168 cache_size_(cache_size) {} |
169 | 169 |
170 void SimpleIndexFile::IndexMetadata::Serialize(base::Pickle* pickle) const { | 170 void SimpleIndexFile::IndexMetadata::Serialize(base::Pickle* pickle) const { |
171 DCHECK(pickle); | 171 DCHECK(pickle); |
172 pickle->WriteUInt64(magic_number_); | 172 pickle->WriteUInt64(magic_number_); |
173 pickle->WriteUInt32(version_); | 173 pickle->WriteUInt32(version_); |
174 pickle->WriteUInt64(number_of_entries_); | 174 pickle->WriteUInt64(number_of_entries_); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 const base::Closure& callback, | 270 const base::Closure& callback, |
271 SimpleIndexLoadResult* out_result) { | 271 SimpleIndexLoadResult* out_result) { |
272 base::Closure task = base::Bind(&SimpleIndexFile::SyncLoadIndexEntries, | 272 base::Closure task = base::Bind(&SimpleIndexFile::SyncLoadIndexEntries, |
273 cache_type_, | 273 cache_type_, |
274 cache_last_modified, cache_directory_, | 274 cache_last_modified, cache_directory_, |
275 index_file_, out_result); | 275 index_file_, out_result); |
276 worker_pool_->PostTaskAndReply(FROM_HERE, task, callback); | 276 worker_pool_->PostTaskAndReply(FROM_HERE, task, callback); |
277 } | 277 } |
278 | 278 |
279 void SimpleIndexFile::WriteToDisk(const SimpleIndex::EntrySet& entry_set, | 279 void SimpleIndexFile::WriteToDisk(const SimpleIndex::EntrySet& entry_set, |
280 uint64 cache_size, | 280 uint64_t cache_size, |
281 const base::TimeTicks& start, | 281 const base::TimeTicks& start, |
282 bool app_on_background, | 282 bool app_on_background, |
283 const base::Closure& callback) { | 283 const base::Closure& callback) { |
284 IndexMetadata index_metadata(entry_set.size(), cache_size); | 284 IndexMetadata index_metadata(entry_set.size(), cache_size); |
285 scoped_ptr<base::Pickle> pickle = Serialize(index_metadata, entry_set); | 285 scoped_ptr<base::Pickle> pickle = Serialize(index_metadata, entry_set); |
286 base::Closure task = | 286 base::Closure task = |
287 base::Bind(&SimpleIndexFile::SyncWriteToDisk, | 287 base::Bind(&SimpleIndexFile::SyncWriteToDisk, |
288 cache_type_, cache_directory_, index_file_, temp_index_file_, | 288 cache_type_, cache_directory_, index_file_, temp_index_file_, |
289 base::Passed(&pickle), start, app_on_background); | 289 base::Passed(&pickle), start, app_on_background); |
290 if (callback.is_null()) | 290 if (callback.is_null()) |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 | 396 |
397 base::Pickle pickle(data, data_len); | 397 base::Pickle pickle(data, data_len); |
398 if (!pickle.data()) { | 398 if (!pickle.data()) { |
399 LOG(WARNING) << "Corrupt Simple Index File."; | 399 LOG(WARNING) << "Corrupt Simple Index File."; |
400 return; | 400 return; |
401 } | 401 } |
402 | 402 |
403 base::PickleIterator pickle_it(pickle); | 403 base::PickleIterator pickle_it(pickle); |
404 SimpleIndexFile::PickleHeader* header_p = | 404 SimpleIndexFile::PickleHeader* header_p = |
405 pickle.headerT<SimpleIndexFile::PickleHeader>(); | 405 pickle.headerT<SimpleIndexFile::PickleHeader>(); |
406 const uint32 crc_read = header_p->crc; | 406 const uint32_t crc_read = header_p->crc; |
407 const uint32 crc_calculated = CalculatePickleCRC(pickle); | 407 const uint32_t crc_calculated = CalculatePickleCRC(pickle); |
408 | 408 |
409 if (crc_read != crc_calculated) { | 409 if (crc_read != crc_calculated) { |
410 LOG(WARNING) << "Invalid CRC in Simple Index file."; | 410 LOG(WARNING) << "Invalid CRC in Simple Index file."; |
411 return; | 411 return; |
412 } | 412 } |
413 | 413 |
414 SimpleIndexFile::IndexMetadata index_metadata; | 414 SimpleIndexFile::IndexMetadata index_metadata; |
415 if (!index_metadata.Deserialize(&pickle_it)) { | 415 if (!index_metadata.Deserialize(&pickle_it)) { |
416 LOG(ERROR) << "Invalid index_metadata on Simple Cache Index."; | 416 LOG(ERROR) << "Invalid index_metadata on Simple Cache Index."; |
417 return; | 417 return; |
418 } | 418 } |
419 | 419 |
420 if (!index_metadata.CheckIndexMetadata()) { | 420 if (!index_metadata.CheckIndexMetadata()) { |
421 LOG(ERROR) << "Invalid index_metadata on Simple Cache Index."; | 421 LOG(ERROR) << "Invalid index_metadata on Simple Cache Index."; |
422 return; | 422 return; |
423 } | 423 } |
424 | 424 |
425 #if !defined(OS_WIN) | 425 #if !defined(OS_WIN) |
426 // TODO(gavinp): Consider using std::unordered_map. | 426 // TODO(gavinp): Consider using std::unordered_map. |
427 entries->resize(index_metadata.GetNumberOfEntries() + kExtraSizeForMerge); | 427 entries->resize(index_metadata.GetNumberOfEntries() + kExtraSizeForMerge); |
428 #endif | 428 #endif |
429 while (entries->size() < index_metadata.GetNumberOfEntries()) { | 429 while (entries->size() < index_metadata.GetNumberOfEntries()) { |
430 uint64 hash_key; | 430 uint64_t hash_key; |
431 EntryMetadata entry_metadata; | 431 EntryMetadata entry_metadata; |
432 if (!pickle_it.ReadUInt64(&hash_key) || | 432 if (!pickle_it.ReadUInt64(&hash_key) || |
433 !entry_metadata.Deserialize(&pickle_it)) { | 433 !entry_metadata.Deserialize(&pickle_it)) { |
434 LOG(WARNING) << "Invalid EntryMetadata in Simple Index file."; | 434 LOG(WARNING) << "Invalid EntryMetadata in Simple Index file."; |
435 entries->clear(); | 435 entries->clear(); |
436 return; | 436 return; |
437 } | 437 } |
438 SimpleIndex::InsertInEntrySet(hash_key, entry_metadata, entries); | 438 SimpleIndex::InsertInEntrySet(hash_key, entry_metadata, entries); |
439 } | 439 } |
440 | 440 |
441 int64 cache_last_modified; | 441 int64_t cache_last_modified; |
442 if (!pickle_it.ReadInt64(&cache_last_modified)) { | 442 if (!pickle_it.ReadInt64(&cache_last_modified)) { |
443 entries->clear(); | 443 entries->clear(); |
444 return; | 444 return; |
445 } | 445 } |
446 DCHECK(out_cache_last_modified); | 446 DCHECK(out_cache_last_modified); |
447 *out_cache_last_modified = base::Time::FromInternalValue(cache_last_modified); | 447 *out_cache_last_modified = base::Time::FromInternalValue(cache_last_modified); |
448 | 448 |
449 out_result->did_load = true; | 449 out_result->did_load = true; |
450 } | 450 } |
451 | 451 |
(...skipping 23 matching lines...) Expand all Loading... |
475 bool SimpleIndexFile::LegacyIsIndexFileStale( | 475 bool SimpleIndexFile::LegacyIsIndexFileStale( |
476 base::Time cache_last_modified, | 476 base::Time cache_last_modified, |
477 const base::FilePath& index_file_path) { | 477 const base::FilePath& index_file_path) { |
478 base::Time index_mtime; | 478 base::Time index_mtime; |
479 if (!simple_util::GetMTime(index_file_path, &index_mtime)) | 479 if (!simple_util::GetMTime(index_file_path, &index_mtime)) |
480 return true; | 480 return true; |
481 return index_mtime < cache_last_modified; | 481 return index_mtime < cache_last_modified; |
482 } | 482 } |
483 | 483 |
484 } // namespace disk_cache | 484 } // namespace disk_cache |
OLD | NEW |