| 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" | 
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" | 
| 12 #include "base/files/memory_mapped_file.h" | 12 #include "base/files/memory_mapped_file.h" | 
| 13 #include "base/hash.h" | 13 #include "base/hash.h" | 
| 14 #include "base/logging.h" | 14 #include "base/logging.h" | 
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" | 
| 16 #include "base/numerics/safe_conversions.h" | 16 #include "base/numerics/safe_conversions.h" | 
| 17 #include "base/pickle.h" | 17 #include "base/pickle.h" | 
| 18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" | 
| 19 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" | 
| 20 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" | 
| 21 #include "net/disk_cache/simple/simple_backend_version.h" | 21 #include "net/disk_cache/simple/simple_backend_version.h" | 
| 22 #include "net/disk_cache/simple/simple_entry_format.h" | 22 #include "net/disk_cache/simple/simple_entry_format.h" | 
| 23 #include "net/disk_cache/simple/simple_histogram_macros.h" | 23 #include "net/disk_cache/simple/simple_histogram_macros.h" | 
| 24 #include "net/disk_cache/simple/simple_index.h" | 24 #include "net/disk_cache/simple/simple_index.h" | 
| 25 #include "net/disk_cache/simple/simple_synchronous_entry.h" | 25 #include "net/disk_cache/simple/simple_synchronous_entry.h" | 
| 26 #include "net/disk_cache/simple/simple_util.h" | 26 #include "net/disk_cache/simple/simple_util.h" | 
| 27 | 27 | 
| 28 using base::File; |  | 
| 29 |  | 
| 30 namespace disk_cache { | 28 namespace disk_cache { | 
| 31 namespace { | 29 namespace { | 
| 32 | 30 | 
| 33 const int kEntryFilesHashLength = 16; | 31 const int kEntryFilesHashLength = 16; | 
| 34 const int kEntryFilesSuffixLength = 2; | 32 const int kEntryFilesSuffixLength = 2; | 
| 35 | 33 | 
| 36 // Limit on how big a file we are willing to work with, to avoid crashes | 34 // Limit on how big a file we are willing to work with, to avoid crashes | 
| 37 // when its corrupt. | 35 // when its corrupt. | 
| 38 const int kMaxEntriesInIndex = 1000000; | 36 const int kMaxEntriesInIndex = 1000000; | 
| 39 | 37 | 
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 100     quality = STALE_INDEX_MISSED_ENTRIES; | 98     quality = STALE_INDEX_MISSED_ENTRIES; | 
| 101   else if (extra_entry_count > 0) | 99   else if (extra_entry_count > 0) | 
| 102     quality = STALE_INDEX_EXTRA_ENTRIES; | 100     quality = STALE_INDEX_EXTRA_ENTRIES; | 
| 103   else | 101   else | 
| 104     quality = STALE_INDEX_OK; | 102     quality = STALE_INDEX_OK; | 
| 105   SIMPLE_CACHE_UMA(ENUMERATION, "StaleIndexQuality", cache_type, quality, | 103   SIMPLE_CACHE_UMA(ENUMERATION, "StaleIndexQuality", cache_type, quality, | 
| 106                    STALE_INDEX_MAX); | 104                    STALE_INDEX_MAX); | 
| 107 } | 105 } | 
| 108 | 106 | 
| 109 bool WritePickleFile(base::Pickle* pickle, const base::FilePath& file_name) { | 107 bool WritePickleFile(base::Pickle* pickle, const base::FilePath& file_name) { | 
| 110   File file( | 108   base::File file( | 
| 111       file_name, | 109       file_name, | 
| 112       File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE | File::FLAG_SHARE_DELETE); | 110       base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | base::File::FLAG
     _SHARE_DELETE); | 
| 113   if (!file.IsValid()) | 111   if (!file.IsValid()) | 
| 114     return false; | 112     return false; | 
| 115 | 113 | 
| 116   int bytes_written = | 114   int bytes_written = | 
| 117       file.Write(0, static_cast<const char*>(pickle->data()), pickle->size()); | 115       file.Write(0, static_cast<const char*>(pickle->data()), pickle->size()); | 
| 118   if (bytes_written != base::checked_cast<int>(pickle->size())) { | 116   if (bytes_written != base::checked_cast<int>(pickle->size())) { | 
| 119     simple_util::SimpleCacheDeleteFile(file_name); | 117     simple_util::SimpleCacheDeleteFile(file_name); | 
| 120     return false; | 118     return false; | 
| 121   } | 119   } | 
| 122   return true; | 120   return true; | 
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 460   } | 458   } | 
| 461   UmaRecordIndexInitMethod(out_result->init_method, cache_type); | 459   UmaRecordIndexInitMethod(out_result->init_method, cache_type); | 
| 462 } | 460 } | 
| 463 | 461 | 
| 464 // static | 462 // static | 
| 465 void SimpleIndexFile::SyncLoadFromDisk(const base::FilePath& index_filename, | 463 void SimpleIndexFile::SyncLoadFromDisk(const base::FilePath& index_filename, | 
| 466                                        base::Time* out_last_cache_seen_by_index, | 464                                        base::Time* out_last_cache_seen_by_index, | 
| 467                                        SimpleIndexLoadResult* out_result) { | 465                                        SimpleIndexLoadResult* out_result) { | 
| 468   out_result->Reset(); | 466   out_result->Reset(); | 
| 469 | 467 | 
| 470   File file(index_filename, File::FLAG_OPEN | File::FLAG_READ | | 468   base::File file(index_filename, base::File::FLAG_OPEN | base::File::FLAG_READ 
     | | 
| 471                                 File::FLAG_SHARE_DELETE | | 469                                 base::File::FLAG_SHARE_DELETE | | 
| 472                                 File::FLAG_SEQUENTIAL_SCAN); | 470                                 base::File::FLAG_SEQUENTIAL_SCAN); | 
| 473   if (!file.IsValid()) | 471   if (!file.IsValid()) | 
| 474     return; | 472     return; | 
| 475 | 473 | 
| 476   // Sanity-check the length. We don't want to crash trying to read some corrupt | 474   // Sanity-check the length. We don't want to crash trying to read some corrupt | 
| 477   // 10GiB file or such. | 475   // 10GiB file or such. | 
| 478   int64_t file_length = file.GetLength(); | 476   int64_t file_length = file.GetLength(); | 
| 479   if (file_length < 0 || file_length > kMaxIndexFileSizeBytes) { | 477   if (file_length < 0 || file_length > kMaxIndexFileSizeBytes) { | 
| 480     simple_util::SimpleCacheDeleteFile(index_filename); | 478     simple_util::SimpleCacheDeleteFile(index_filename); | 
| 481     return; | 479     return; | 
| 482   } | 480   } | 
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 603 bool SimpleIndexFile::LegacyIsIndexFileStale( | 601 bool SimpleIndexFile::LegacyIsIndexFileStale( | 
| 604     base::Time cache_last_modified, | 602     base::Time cache_last_modified, | 
| 605     const base::FilePath& index_file_path) { | 603     const base::FilePath& index_file_path) { | 
| 606   base::Time index_mtime; | 604   base::Time index_mtime; | 
| 607   if (!simple_util::GetMTime(index_file_path, &index_mtime)) | 605   if (!simple_util::GetMTime(index_file_path, &index_mtime)) | 
| 608     return true; | 606     return true; | 
| 609   return index_mtime < cache_last_modified; | 607   return index_mtime < cache_last_modified; | 
| 610 } | 608 } | 
| 611 | 609 | 
| 612 }  // namespace disk_cache | 610 }  // namespace disk_cache | 
| OLD | NEW | 
|---|