Chromium Code Reviews| 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_synchronous_entry.h" | 5 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 #include <functional> | 9 #include <functional> |
| 10 #include <limits> | 10 #include <limits> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/hash.h" | 14 #include "base/hash.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/memory/ptr_util.h" | |
| 17 #include "base/metrics/field_trial_params.h" | |
| 16 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/numerics/safe_conversions.h" | 19 #include "base/numerics/safe_conversions.h" |
| 18 #include "base/sha1.h" | 20 #include "base/sha1.h" |
| 21 #include "base/strings/string_piece.h" | |
| 19 #include "base/timer/elapsed_timer.h" | 22 #include "base/timer/elapsed_timer.h" |
| 20 #include "crypto/secure_hash.h" | 23 #include "crypto/secure_hash.h" |
| 21 #include "net/base/hash_value.h" | 24 #include "net/base/hash_value.h" |
| 22 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
| 23 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 24 #include "net/disk_cache/simple/simple_backend_version.h" | 27 #include "net/disk_cache/simple/simple_backend_version.h" |
| 28 #include "net/disk_cache/simple/simple_experiment.h" | |
| 25 #include "net/disk_cache/simple/simple_histogram_enums.h" | 29 #include "net/disk_cache/simple/simple_histogram_enums.h" |
| 26 #include "net/disk_cache/simple/simple_histogram_macros.h" | 30 #include "net/disk_cache/simple/simple_histogram_macros.h" |
| 27 #include "net/disk_cache/simple/simple_util.h" | 31 #include "net/disk_cache/simple/simple_util.h" |
| 28 #include "third_party/zlib/zlib.h" | 32 #include "third_party/zlib/zlib.h" |
| 29 | 33 |
| 30 using base::File; | 34 using base::File; |
| 31 using base::FilePath; | 35 using base::FilePath; |
| 32 using base::Time; | 36 using base::Time; |
| 33 | 37 |
| 34 namespace disk_cache { | 38 namespace disk_cache { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 SIMPLE_CACHE_UMA(ENUMERATION, | 71 SIMPLE_CACHE_UMA(ENUMERATION, |
| 68 "SyncCloseResult", cache_type, result, CLOSE_RESULT_MAX); | 72 "SyncCloseResult", cache_type, result, CLOSE_RESULT_MAX); |
| 69 } | 73 } |
| 70 | 74 |
| 71 void RecordKeySHA256Result(net::CacheType cache_type, KeySHA256Result result) { | 75 void RecordKeySHA256Result(net::CacheType cache_type, KeySHA256Result result) { |
| 72 SIMPLE_CACHE_UMA(ENUMERATION, "SyncKeySHA256Result", cache_type, | 76 SIMPLE_CACHE_UMA(ENUMERATION, "SyncKeySHA256Result", cache_type, |
| 73 static_cast<int>(result), | 77 static_cast<int>(result), |
| 74 static_cast<int>(KeySHA256Result::MAX)); | 78 static_cast<int>(KeySHA256Result::MAX)); |
| 75 } | 79 } |
| 76 | 80 |
| 81 void RecordWhetherOpenDidPrefetch(net::CacheType cache_type, bool result) { | |
| 82 SIMPLE_CACHE_UMA(BOOLEAN, "SyncOpenDidPrefetch", cache_type, result); | |
| 83 } | |
| 84 | |
| 77 bool CanOmitEmptyFile(int file_index) { | 85 bool CanOmitEmptyFile(int file_index) { |
| 78 DCHECK_GE(file_index, 0); | 86 DCHECK_GE(file_index, 0); |
| 79 DCHECK_LT(file_index, kSimpleEntryFileCount); | 87 DCHECK_LT(file_index, kSimpleEntryFileCount); |
| 80 return file_index == simple_util::GetFileIndexFromStreamIndex(2); | 88 return file_index == simple_util::GetFileIndexFromStreamIndex(2); |
| 81 } | 89 } |
| 82 | 90 |
| 83 bool TruncatePath(const FilePath& filename_to_truncate) { | 91 bool TruncatePath(const FilePath& filename_to_truncate) { |
| 84 File file_to_truncate; | 92 File file_to_truncate; |
| 85 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE | | 93 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE | |
| 86 File::FLAG_SHARE_DELETE; | 94 File::FLAG_SHARE_DELETE; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 103 } // namespace | 111 } // namespace |
| 104 | 112 |
| 105 using simple_util::GetEntryHashKey; | 113 using simple_util::GetEntryHashKey; |
| 106 using simple_util::GetFilenameFromEntryHashAndFileIndex; | 114 using simple_util::GetFilenameFromEntryHashAndFileIndex; |
| 107 using simple_util::GetSparseFilenameFromEntryHash; | 115 using simple_util::GetSparseFilenameFromEntryHash; |
| 108 using simple_util::GetHeaderSize; | 116 using simple_util::GetHeaderSize; |
| 109 using simple_util::GetDataSizeFromFileSize; | 117 using simple_util::GetDataSizeFromFileSize; |
| 110 using simple_util::GetFileSizeFromDataSize; | 118 using simple_util::GetFileSizeFromDataSize; |
| 111 using simple_util::GetFileIndexFromStreamIndex; | 119 using simple_util::GetFileIndexFromStreamIndex; |
| 112 | 120 |
| 121 const base::Feature kSimpleCachePrefetchExperiment = { | |
| 122 "GetSimpleCachePrefetchExperiment", base::FEATURE_DISABLED_BY_DEFAULT}; | |
| 123 const char kSimplePrefetchBytesParam[] = "Bytes"; | |
| 124 | |
| 125 int GetSimpleCachePrefetchSize() { | |
| 126 return base::GetFieldTrialParamByFeatureAsInt(kSimpleCachePrefetchExperiment, | |
| 127 kSimplePrefetchBytesParam, 0); | |
| 128 } | |
| 129 | |
| 113 SimpleEntryStat::SimpleEntryStat(base::Time last_used, | 130 SimpleEntryStat::SimpleEntryStat(base::Time last_used, |
| 114 base::Time last_modified, | 131 base::Time last_modified, |
| 115 const int32_t data_size[], | 132 const int32_t data_size[], |
| 116 const int32_t sparse_data_size) | 133 const int32_t sparse_data_size) |
| 117 : last_used_(last_used), | 134 : last_used_(last_used), |
| 118 last_modified_(last_modified), | 135 last_modified_(last_modified), |
| 119 sparse_data_size_(sparse_data_size) { | 136 sparse_data_size_(sparse_data_size) { |
| 120 memcpy(data_size_, data_size, sizeof(data_size_)); | 137 memcpy(data_size_, data_size, sizeof(data_size_)); |
| 121 } | 138 } |
| 122 | 139 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 int32_t total_data_size; | 172 int32_t total_data_size; |
| 156 if (file_index == 0) { | 173 if (file_index == 0) { |
| 157 total_data_size = data_size_[0] + data_size_[1] + | 174 total_data_size = data_size_[0] + data_size_[1] + |
| 158 sizeof(net::SHA256HashValue) + sizeof(SimpleFileEOF); | 175 sizeof(net::SHA256HashValue) + sizeof(SimpleFileEOF); |
| 159 } else { | 176 } else { |
| 160 total_data_size = data_size_[2]; | 177 total_data_size = data_size_[2]; |
| 161 } | 178 } |
| 162 return GetFileSizeFromDataSize(key_length, total_data_size); | 179 return GetFileSizeFromDataSize(key_length, total_data_size); |
| 163 } | 180 } |
| 164 | 181 |
| 182 SimpleStreamPrefetchData::SimpleStreamPrefetchData() | |
| 183 : stream_crc32(crc32(0, Z_NULL, 0)) {} | |
| 184 | |
| 185 SimpleStreamPrefetchData::~SimpleStreamPrefetchData() {} | |
| 186 | |
| 165 SimpleEntryCreationResults::SimpleEntryCreationResults( | 187 SimpleEntryCreationResults::SimpleEntryCreationResults( |
| 166 SimpleEntryStat entry_stat) | 188 SimpleEntryStat entry_stat) |
| 167 : sync_entry(NULL), | 189 : sync_entry(NULL), entry_stat(entry_stat), result(net::OK) {} |
| 168 entry_stat(entry_stat), | |
| 169 stream_0_crc32(crc32(0, Z_NULL, 0)), | |
| 170 result(net::OK) { | |
| 171 } | |
| 172 | 190 |
| 173 SimpleEntryCreationResults::~SimpleEntryCreationResults() { | 191 SimpleEntryCreationResults::~SimpleEntryCreationResults() { |
| 174 } | 192 } |
| 175 | 193 |
| 176 SimpleSynchronousEntry::CRCRecord::CRCRecord() : index(-1), | 194 SimpleSynchronousEntry::CRCRecord::CRCRecord() : index(-1), |
| 177 has_crc32(false), | 195 has_crc32(false), |
| 178 data_crc32(0) { | 196 data_crc32(0) { |
| 179 } | 197 } |
| 180 | 198 |
| 181 SimpleSynchronousEntry::CRCRecord::CRCRecord(int index_p, | 199 SimpleSynchronousEntry::CRCRecord::CRCRecord(int index_p, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 const bool had_index, | 233 const bool had_index, |
| 216 const base::TimeTicks& time_enqueued, | 234 const base::TimeTicks& time_enqueued, |
| 217 SimpleEntryCreationResults* out_results) { | 235 SimpleEntryCreationResults* out_results) { |
| 218 base::TimeTicks start_sync_open_entry = base::TimeTicks::Now(); | 236 base::TimeTicks start_sync_open_entry = base::TimeTicks::Now(); |
| 219 SIMPLE_CACHE_UMA(TIMES, "QueueLatency.OpenEntry", cache_type, | 237 SIMPLE_CACHE_UMA(TIMES, "QueueLatency.OpenEntry", cache_type, |
| 220 (start_sync_open_entry - time_enqueued)); | 238 (start_sync_open_entry - time_enqueued)); |
| 221 | 239 |
| 222 SimpleSynchronousEntry* sync_entry = | 240 SimpleSynchronousEntry* sync_entry = |
| 223 new SimpleSynchronousEntry(cache_type, path, key, entry_hash, had_index); | 241 new SimpleSynchronousEntry(cache_type, path, key, entry_hash, had_index); |
| 224 out_results->result = sync_entry->InitializeForOpen( | 242 out_results->result = sync_entry->InitializeForOpen( |
| 225 &out_results->entry_stat, &out_results->stream_0_data, | 243 &out_results->entry_stat, out_results->stream_prefetch_data); |
| 226 &out_results->stream_0_crc32); | |
| 227 if (out_results->result != net::OK) { | 244 if (out_results->result != net::OK) { |
| 228 sync_entry->Doom(); | 245 sync_entry->Doom(); |
| 229 delete sync_entry; | 246 delete sync_entry; |
| 230 out_results->sync_entry = NULL; | 247 out_results->sync_entry = NULL; |
| 231 out_results->stream_0_data = NULL; | 248 out_results->stream_prefetch_data[0].data = nullptr; |
| 249 out_results->stream_prefetch_data[1].data = nullptr; | |
| 232 return; | 250 return; |
| 233 } | 251 } |
| 234 SIMPLE_CACHE_UMA(TIMES, "DiskOpenLatency", cache_type, | 252 SIMPLE_CACHE_UMA(TIMES, "DiskOpenLatency", cache_type, |
| 235 base::TimeTicks::Now() - start_sync_open_entry); | 253 base::TimeTicks::Now() - start_sync_open_entry); |
| 236 out_results->sync_entry = sync_entry; | 254 out_results->sync_entry = sync_entry; |
| 237 } | 255 } |
| 238 | 256 |
| 239 // static | 257 // static |
| 240 void SimpleSynchronousEntry::CreateEntry( | 258 void SimpleSynchronousEntry::CreateEntry( |
| 241 net::CacheType cache_type, | 259 net::CacheType cache_type, |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 637 it->second.offset == start + avail_so_far) { | 655 it->second.offset == start + avail_so_far) { |
| 638 avail_so_far += it->second.length; | 656 avail_so_far += it->second.length; |
| 639 ++it; | 657 ++it; |
| 640 } | 658 } |
| 641 | 659 |
| 642 int64_t len_from_start = len - (start - offset); | 660 int64_t len_from_start = len - (start - offset); |
| 643 *out_start = start; | 661 *out_start = start; |
| 644 *out_result = static_cast<int>(std::min(avail_so_far, len_from_start)); | 662 *out_result = static_cast<int>(std::min(avail_so_far, len_from_start)); |
| 645 } | 663 } |
| 646 | 664 |
| 647 int SimpleSynchronousEntry::CheckEOFRecord(int index, | 665 int SimpleSynchronousEntry::CheckEOFRecord(int stream_index, |
| 648 const SimpleEntryStat& entry_stat, | 666 const SimpleEntryStat& entry_stat, |
| 649 uint32_t expected_crc32) const { | 667 uint32_t expected_crc32) { |
| 650 DCHECK(initialized_); | 668 DCHECK(initialized_); |
| 651 uint32_t crc32; | 669 SimpleFileEOF eof_record; |
| 652 bool has_crc32; | 670 int file_offset = entry_stat.GetEOFOffsetInFile(key_.size(), stream_index); |
| 653 bool has_key_sha256; | 671 int file_index = GetFileIndexFromStreamIndex(stream_index); |
| 654 int32_t stream_size; | 672 int rv = GetEOFRecordData(base::StringPiece(), file_index, file_offset, |
| 655 int rv = GetEOFRecordData(index, entry_stat, &has_crc32, &has_key_sha256, | 673 &eof_record); |
| 656 &crc32, &stream_size); | 674 |
| 657 if (rv != net::OK) { | 675 if (rv != net::OK) { |
| 658 Doom(); | 676 Doom(); |
| 659 return rv; | 677 return rv; |
| 660 } | 678 } |
| 661 if (has_crc32 && crc32 != expected_crc32) { | 679 if ((eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) && |
| 680 eof_record.data_crc32 != expected_crc32) { | |
| 662 DVLOG(1) << "EOF record had bad crc."; | 681 DVLOG(1) << "EOF record had bad crc."; |
| 663 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | 682 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); |
| 664 Doom(); | 683 Doom(); |
| 665 return net::ERR_CACHE_CHECKSUM_MISMATCH; | 684 return net::ERR_CACHE_CHECKSUM_MISMATCH; |
| 666 } | 685 } |
| 667 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); | 686 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); |
| 668 return net::OK; | 687 return net::OK; |
| 669 } | 688 } |
| 670 | 689 |
| 690 int SimpleSynchronousEntry::PreReadStreamPayload( | |
| 691 base::StringPiece file_0_prefetch, | |
| 692 int stream_index, | |
| 693 int extra_size, | |
| 694 const SimpleEntryStat& entry_stat, | |
| 695 const SimpleFileEOF& eof_record, | |
| 696 SimpleStreamPrefetchData* out) { | |
| 697 DCHECK(stream_index == 0 || stream_index == 1); | |
| 698 | |
| 699 int stream_size = entry_stat.data_size(stream_index); | |
| 700 int read_size = stream_size + extra_size; | |
| 701 out->data = new net::GrowableIOBuffer(); | |
| 702 out->data->SetCapacity(read_size); | |
| 703 int file_offset = entry_stat.GetOffsetInFile(key_.size(), 0, stream_index); | |
| 704 if (!ReadFromFileOrPrefetched(file_0_prefetch, 0, file_offset, read_size, | |
| 705 out->data->data())) | |
| 706 return net::ERR_FAILED; | |
| 707 | |
| 708 // Check the CRC32. | |
| 709 uint32_t expected_crc32 = simple_util::Crc32(out->data->data(), stream_size); | |
| 710 if ((eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) && | |
| 711 eof_record.data_crc32 != expected_crc32) { | |
| 712 DVLOG(1) << "EOF record had bad crc."; | |
| 713 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | |
| 714 return net::ERR_CACHE_CHECKSUM_MISMATCH; | |
| 715 } | |
| 716 out->stream_crc32 = expected_crc32; | |
| 717 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); | |
| 718 return net::OK; | |
| 719 } | |
| 720 | |
| 671 void SimpleSynchronousEntry::Close( | 721 void SimpleSynchronousEntry::Close( |
| 672 const SimpleEntryStat& entry_stat, | 722 const SimpleEntryStat& entry_stat, |
| 673 std::unique_ptr<std::vector<CRCRecord>> crc32s_to_write, | 723 std::unique_ptr<std::vector<CRCRecord>> crc32s_to_write, |
| 674 net::GrowableIOBuffer* stream_0_data) { | 724 net::GrowableIOBuffer* stream_0_data) { |
| 675 base::ElapsedTimer close_time; | 725 base::ElapsedTimer close_time; |
| 676 DCHECK(stream_0_data); | 726 DCHECK(stream_0_data); |
| 677 | 727 |
| 678 for (std::vector<CRCRecord>::const_iterator it = crc32s_to_write->begin(); | 728 for (std::vector<CRCRecord>::const_iterator it = crc32s_to_write->begin(); |
| 679 it != crc32s_to_write->end(); ++it) { | 729 it != crc32s_to_write->end(); ++it) { |
| 680 const int stream_index = it->index; | 730 const int stream_index = it->index; |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 898 // 2) Stream 0 and stream 1 are in the same file, and the exact size for | 948 // 2) Stream 0 and stream 1 are in the same file, and the exact size for |
| 899 // each will only be known when reading the EOF record for stream 0. | 949 // each will only be known when reading the EOF record for stream 0. |
| 900 // | 950 // |
| 901 // The size for file 0 and 1 is temporarily kept in | 951 // The size for file 0 and 1 is temporarily kept in |
| 902 // |data_size(1)| and |data_size(2)| respectively. Reading the key in | 952 // |data_size(1)| and |data_size(2)| respectively. Reading the key in |
| 903 // InitializeForOpen yields the data size for each file. In the case of | 953 // InitializeForOpen yields the data size for each file. In the case of |
| 904 // file hash_1, this is the total size of stream 2, and is assigned to | 954 // file hash_1, this is the total size of stream 2, and is assigned to |
| 905 // data_size(2). In the case of file 0, it is the combined size of stream | 955 // data_size(2). In the case of file 0, it is the combined size of stream |
| 906 // 0, stream 1 and one EOF record. The exact distribution of sizes between | 956 // 0, stream 1 and one EOF record. The exact distribution of sizes between |
| 907 // stream 1 and stream 0 is only determined after reading the EOF record | 957 // stream 1 and stream 0 is only determined after reading the EOF record |
| 908 // for stream 0 in ReadAndValidateStream0. | 958 // for stream 0 in ReadAndValidateStream0AndMaybe1. |
| 909 if (!base::IsValueInRangeForNumericType<int>(file_info.size)) { | 959 if (!base::IsValueInRangeForNumericType<int>(file_info.size)) { |
| 910 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_INVALID_FILE_LENGTH, | 960 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_INVALID_FILE_LENGTH, |
| 911 had_index_); | 961 had_index_); |
| 912 return false; | 962 return false; |
| 913 } | 963 } |
| 914 out_entry_stat->set_data_size(i + 1, static_cast<int>(file_info.size)); | 964 out_entry_stat->set_data_size(i + 1, static_cast<int>(file_info.size)); |
| 915 } | 965 } |
| 916 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, | 966 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, |
| 917 "SyncOpenEntryAge", cache_type_, | 967 "SyncOpenEntryAge", cache_type_, |
| 918 entry_age.InHours(), 1, 1000, 50); | 968 entry_age.InHours(), 1, 1000, 50); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1042 return false; | 1092 return false; |
| 1043 } | 1093 } |
| 1044 } | 1094 } |
| 1045 | 1095 |
| 1046 header_and_key_check_needed_[file_index] = false; | 1096 header_and_key_check_needed_[file_index] = false; |
| 1047 return true; | 1097 return true; |
| 1048 } | 1098 } |
| 1049 | 1099 |
| 1050 int SimpleSynchronousEntry::InitializeForOpen( | 1100 int SimpleSynchronousEntry::InitializeForOpen( |
| 1051 SimpleEntryStat* out_entry_stat, | 1101 SimpleEntryStat* out_entry_stat, |
| 1052 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, | 1102 SimpleStreamPrefetchData stream_prefetch_data[2]) { |
| 1053 uint32_t* out_stream_0_crc32) { | |
| 1054 DCHECK(!initialized_); | 1103 DCHECK(!initialized_); |
| 1055 if (!OpenFiles(out_entry_stat)) { | 1104 if (!OpenFiles(out_entry_stat)) { |
| 1056 DLOG(WARNING) << "Could not open platform files for entry."; | 1105 DLOG(WARNING) << "Could not open platform files for entry."; |
| 1057 return net::ERR_FAILED; | 1106 return net::ERR_FAILED; |
| 1058 } | 1107 } |
| 1059 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 1108 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 1060 if (empty_file_omitted_[i]) | 1109 if (empty_file_omitted_[i]) |
| 1061 continue; | 1110 continue; |
| 1062 | 1111 |
| 1063 if (key_.empty()) { | 1112 if (key_.empty()) { |
| 1064 // If |key_| is empty, we were opened via the iterator interface, without | 1113 // If |key_| is empty, we were opened via the iterator interface, without |
| 1065 // knowing what our key is. We must therefore read the header immediately | 1114 // knowing what our key is. We must therefore read the header immediately |
| 1066 // to discover it, so SimpleEntryImpl can make it available to | 1115 // to discover it, so SimpleEntryImpl can make it available to |
| 1067 // disk_cache::Entry::GetKey(). | 1116 // disk_cache::Entry::GetKey(). |
| 1068 if (!CheckHeaderAndKey(i)) | 1117 if (!CheckHeaderAndKey(i)) |
| 1069 return net::ERR_FAILED; | 1118 return net::ERR_FAILED; |
| 1070 } else { | 1119 } else { |
| 1071 // If we do know which key were are looking for, we still need to | 1120 // If we do know which key were are looking for, we still need to |
| 1072 // check that the file actually has it (rather than just being a hash | 1121 // check that the file actually has it (rather than just being a hash |
| 1073 // collision or some sort of file system accident), but that can be put | 1122 // collision or some sort of file system accident), but that can be put |
| 1074 // off until opportune time: either the read of the footer, or when we | 1123 // off until opportune time: either the read of the footer, or when we |
| 1075 // start reading in the data, depending on stream # and format revision. | 1124 // start reading in the data, depending on stream # and format revision. |
| 1076 header_and_key_check_needed_[i] = true; | 1125 header_and_key_check_needed_[i] = true; |
| 1077 } | 1126 } |
| 1078 | 1127 |
| 1079 if (i == 0) { | 1128 if (i == 0) { |
| 1080 // File size for stream 0 has been stored temporarily in data_size[1]. | 1129 // File size for stream 0 has been stored temporarily in data_size[1]. |
| 1081 int ret_value_stream_0 = | 1130 int ret_value_stream_0 = ReadAndValidateStream0AndMaybe1( |
| 1082 ReadAndValidateStream0(out_entry_stat->data_size(1), out_entry_stat, | 1131 out_entry_stat->data_size(1), out_entry_stat, stream_prefetch_data); |
| 1083 stream_0_data, out_stream_0_crc32); | |
| 1084 if (ret_value_stream_0 != net::OK) | 1132 if (ret_value_stream_0 != net::OK) |
| 1085 return ret_value_stream_0; | 1133 return ret_value_stream_0; |
| 1086 } else { | 1134 } else { |
| 1087 out_entry_stat->set_data_size( | 1135 out_entry_stat->set_data_size( |
| 1088 2, | 1136 2, |
| 1089 GetDataSizeFromFileSize(key_.size(), out_entry_stat->data_size(2))); | 1137 GetDataSizeFromFileSize(key_.size(), out_entry_stat->data_size(2))); |
| 1090 if (out_entry_stat->data_size(2) < 0) { | 1138 if (out_entry_stat->data_size(2) < 0) { |
| 1091 DLOG(WARNING) << "Stream 2 file is too small."; | 1139 DLOG(WARNING) << "Stream 2 file is too small."; |
| 1092 return net::ERR_FAILED; | 1140 return net::ERR_FAILED; |
| 1093 } | 1141 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1164 if (!InitializeCreatedFile(i, &result)) { | 1212 if (!InitializeCreatedFile(i, &result)) { |
| 1165 RecordSyncCreateResult(result, had_index_); | 1213 RecordSyncCreateResult(result, had_index_); |
| 1166 return net::ERR_FAILED; | 1214 return net::ERR_FAILED; |
| 1167 } | 1215 } |
| 1168 } | 1216 } |
| 1169 RecordSyncCreateResult(CREATE_ENTRY_SUCCESS, had_index_); | 1217 RecordSyncCreateResult(CREATE_ENTRY_SUCCESS, had_index_); |
| 1170 initialized_ = true; | 1218 initialized_ = true; |
| 1171 return net::OK; | 1219 return net::OK; |
| 1172 } | 1220 } |
| 1173 | 1221 |
| 1174 int SimpleSynchronousEntry::ReadAndValidateStream0( | 1222 int SimpleSynchronousEntry::ReadAndValidateStream0AndMaybe1( |
| 1175 int file_size, | 1223 int file_size, |
| 1176 SimpleEntryStat* out_entry_stat, | 1224 SimpleEntryStat* out_entry_stat, |
| 1177 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, | 1225 SimpleStreamPrefetchData stream_prefetch_data[2]) { |
| 1178 uint32_t* out_stream_0_crc32) { | 1226 // If the file is sufficiently small, we will prefetch everything -- |
| 1179 // Pretend this file has a null stream zero, and contains the optional key | 1227 // in which case |prefetch_buf| will be non-null, and we should look at it |
| 1180 // SHA256. This is good enough to read the EOF record on the file, which gives | 1228 // rather than call ::Read for the bits. |
| 1181 // the actual size of stream 0. | 1229 std::unique_ptr<char[]> prefetch_buf; |
| 1182 int temp_data_size = GetDataSizeFromFileSize(key_.size(), file_size); | 1230 base::StringPiece file_0_prefetch; |
| 1183 out_entry_stat->set_data_size(0, 0); | |
| 1184 out_entry_stat->set_data_size( | |
| 1185 1, temp_data_size - sizeof(net::SHA256HashValue) - sizeof(SimpleFileEOF)); | |
| 1186 | 1231 |
| 1187 bool has_crc32; | 1232 if (file_size > GetSimpleCachePrefetchSize()) { |
| 1188 bool has_key_sha256; | 1233 RecordWhetherOpenDidPrefetch(cache_type_, false); |
| 1189 uint32_t read_crc32; | 1234 } else { |
| 1190 int32_t stream_0_size; | 1235 RecordWhetherOpenDidPrefetch(cache_type_, true); |
| 1191 int ret_value_crc32 = | 1236 prefetch_buf = base::MakeUnique<char[]>(file_size); |
| 1192 GetEOFRecordData(0, *out_entry_stat, &has_crc32, &has_key_sha256, | 1237 if (files_[0].Read(0, prefetch_buf.get(), file_size) != file_size) |
| 1193 &read_crc32, &stream_0_size); | 1238 return net::ERR_FAILED; |
| 1194 if (ret_value_crc32 != net::OK) | 1239 file_0_prefetch.set(prefetch_buf.get(), file_size); |
| 1195 return ret_value_crc32; | 1240 } |
| 1196 | 1241 |
| 1197 // Calculate and set the real values for the two streams. | 1242 // Read stream 0 footer first --- it has size/feature info required to figure |
| 1198 int32_t total_size = out_entry_stat->data_size(1); | 1243 // out file 0's layout. |
| 1199 if (!has_key_sha256) | 1244 SimpleFileEOF stream_0_eof; |
| 1200 total_size += sizeof(net::SHA256HashValue); | 1245 int rv = GetEOFRecordData( |
| 1201 if (stream_0_size > total_size) | 1246 file_0_prefetch, /* file_index = */ 0, |
| 1247 /* file_offset = */ file_size - sizeof(SimpleFileEOF), &stream_0_eof); | |
| 1248 if (rv != net::OK) | |
| 1249 return rv; | |
| 1250 | |
| 1251 int32_t stream_0_size = stream_0_eof.stream_size; | |
| 1252 if (stream_0_size < 0 || stream_0_size > file_size) | |
| 1202 return net::ERR_FAILED; | 1253 return net::ERR_FAILED; |
| 1203 out_entry_stat->set_data_size(0, stream_0_size); | 1254 out_entry_stat->set_data_size(0, stream_0_size); |
| 1204 out_entry_stat->set_data_size(1, total_size - stream_0_size); | |
| 1205 | 1255 |
| 1206 // Put stream 0 data in memory. | 1256 // Calculate size for stream 1, now we know stream 0's. |
| 1207 *stream_0_data = new net::GrowableIOBuffer(); | 1257 // See comments in simple_entry_format.h for background. |
| 1208 (*stream_0_data)->SetCapacity(stream_0_size + sizeof(net::SHA256HashValue)); | 1258 bool has_key_sha256 = |
| 1209 int file_offset = out_entry_stat->GetOffsetInFile(key_.size(), 0, 0); | 1259 (stream_0_eof.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) == |
| 1210 int read_size = stream_0_size; | 1260 SimpleFileEOF::FLAG_HAS_KEY_SHA256; |
| 1261 int extra_post_stream_0_read = 0; | |
| 1211 if (has_key_sha256) | 1262 if (has_key_sha256) |
| 1212 read_size += sizeof(net::SHA256HashValue); | 1263 extra_post_stream_0_read += sizeof(net::SHA256HashValue); |
| 1213 if (files_[0].Read(file_offset, (*stream_0_data)->data(), read_size) != | 1264 |
| 1214 read_size) | 1265 int32_t stream1_size = file_size - 2 * sizeof(SimpleFileEOF) - stream_0_size - |
| 1266 sizeof(SimpleFileHeader) - key_.size() - | |
| 1267 extra_post_stream_0_read; | |
| 1268 if (stream1_size < 0 || stream1_size > file_size) | |
| 1215 return net::ERR_FAILED; | 1269 return net::ERR_FAILED; |
| 1216 | 1270 |
| 1217 // Check the CRC32. | 1271 out_entry_stat->set_data_size(1, stream1_size); |
| 1218 uint32_t expected_crc32 = | 1272 |
| 1219 simple_util::Crc32((*stream_0_data)->data(), stream_0_size); | 1273 // Put stream 0 data in memory --- plus maybe the sha256(key) footer. |
| 1220 if (has_crc32 && read_crc32 != expected_crc32) { | 1274 rv = PreReadStreamPayload(file_0_prefetch, /* stream_index = */ 0, |
| 1221 DVLOG(1) << "EOF record had bad crc."; | 1275 extra_post_stream_0_read, *out_entry_stat, |
| 1222 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | 1276 stream_0_eof, &stream_prefetch_data[0]); |
| 1223 return net::ERR_FAILED; | 1277 if (rv != net::OK) |
| 1278 return rv; | |
| 1279 | |
| 1280 // If prefetch buffer is available, and we have sha256(key) (so we don't need | |
| 1281 // to look at the header), extract out stream 1 info as well. | |
| 1282 if (prefetch_buf && has_key_sha256) { | |
| 1283 SimpleFileEOF stream_1_eof; | |
| 1284 rv = GetEOFRecordData( | |
| 1285 file_0_prefetch, /* file_index = */ 0, | |
| 1286 out_entry_stat->GetEOFOffsetInFile(key_.size(), /* stream_index = */ 1), | |
| 1287 &stream_1_eof); | |
| 1288 if (rv != net::OK) | |
| 1289 return rv; | |
| 1290 | |
| 1291 rv = PreReadStreamPayload(file_0_prefetch, /* stream_index = */ 1, | |
| 1292 /* extra_size = */ 0, *out_entry_stat, | |
| 1293 stream_1_eof, &stream_prefetch_data[1]); | |
| 1294 if (rv != net::OK) | |
| 1295 return rv; | |
| 1224 } | 1296 } |
| 1225 *out_stream_0_crc32 = expected_crc32; | |
| 1226 | 1297 |
| 1227 // If present, check the key SHA256. | 1298 // If present, check the key SHA256. |
| 1228 if (has_key_sha256) { | 1299 if (has_key_sha256) { |
| 1229 net::SHA256HashValue hash_value; | 1300 net::SHA256HashValue hash_value; |
| 1230 CalculateSHA256OfKey(key_, &hash_value); | 1301 CalculateSHA256OfKey(key_, &hash_value); |
| 1231 bool matched = | 1302 bool matched = |
| 1232 std::memcmp(&hash_value, (*stream_0_data)->data() + stream_0_size, | 1303 std::memcmp(&hash_value, |
| 1304 stream_prefetch_data[0].data->data() + stream_0_size, | |
| 1233 sizeof(hash_value)) == 0; | 1305 sizeof(hash_value)) == 0; |
| 1234 if (!matched) { | 1306 if (!matched) { |
| 1235 RecordKeySHA256Result(cache_type_, KeySHA256Result::NO_MATCH); | 1307 RecordKeySHA256Result(cache_type_, KeySHA256Result::NO_MATCH); |
| 1236 return net::ERR_FAILED; | 1308 return net::ERR_FAILED; |
| 1237 } | 1309 } |
| 1238 // Elide header check if we verified sha256(key) via footer. | 1310 // Elide header check if we verified sha256(key) via footer. |
| 1239 header_and_key_check_needed_[0] = false; | 1311 header_and_key_check_needed_[0] = false; |
| 1240 RecordKeySHA256Result(cache_type_, KeySHA256Result::MATCHED); | 1312 RecordKeySHA256Result(cache_type_, KeySHA256Result::MATCHED); |
| 1241 } else { | 1313 } else { |
| 1242 RecordKeySHA256Result(cache_type_, KeySHA256Result::NOT_PRESENT); | 1314 RecordKeySHA256Result(cache_type_, KeySHA256Result::NOT_PRESENT); |
| 1243 } | 1315 } |
| 1244 | 1316 |
| 1245 // Ensure the key is validated before completion. | 1317 // Ensure the key is validated before completion. |
| 1246 if (!has_key_sha256 && header_and_key_check_needed_[0]) | 1318 if (!has_key_sha256 && header_and_key_check_needed_[0]) |
| 1247 CheckHeaderAndKey(0); | 1319 CheckHeaderAndKey(0); |
| 1248 | 1320 |
| 1249 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); | |
| 1250 return net::OK; | 1321 return net::OK; |
| 1251 } | 1322 } |
| 1252 | 1323 |
| 1253 int SimpleSynchronousEntry::GetEOFRecordData(int index, | 1324 bool SimpleSynchronousEntry::ReadFromFileOrPrefetched( |
| 1254 const SimpleEntryStat& entry_stat, | 1325 base::StringPiece file_0_prefetch, |
| 1255 bool* out_has_crc32, | 1326 int file_index, |
| 1256 bool* out_has_key_sha256, | 1327 int offset, |
| 1257 uint32_t* out_crc32, | 1328 int size, |
| 1258 int32_t* out_data_size) const { | 1329 char* dest) { |
| 1259 SimpleFileEOF eof_record; | 1330 if (file_0_prefetch.empty() || file_index != 0) { |
| 1260 int file_offset = entry_stat.GetEOFOffsetInFile(key_.size(), index); | 1331 return files_[file_index].Read(offset, dest, size) == size; |
| 1261 int file_index = GetFileIndexFromStreamIndex(index); | 1332 } else { |
| 1262 File* file = const_cast<File*>(&files_[file_index]); | 1333 if (offset < 0 || size < 0) |
| 1263 if (file->Read(file_offset, reinterpret_cast<char*>(&eof_record), | 1334 return false; |
| 1264 sizeof(eof_record)) != | 1335 if (size == 0) |
| 1265 sizeof(eof_record)) { | 1336 return true; |
| 1337 | |
| 1338 base::CheckedNumeric<size_t> start(offset); | |
| 1339 size_t start_numeric; | |
| 1340 if (!start.AssignIfValid(&start_numeric) || | |
| 1341 start_numeric >= file_0_prefetch.size()) | |
| 1342 return false; | |
| 1343 | |
| 1344 base::CheckedNumeric<size_t> end = start + size - 1; | |
| 1345 size_t end_numeric; | |
| 1346 if (!end.AssignIfValid(&end_numeric) || | |
| 1347 end_numeric >= file_0_prefetch.size()) | |
| 1348 return false; | |
| 1349 | |
| 1350 memcpy(dest, file_0_prefetch.data() + offset, size); | |
| 1351 return true; | |
| 1352 } | |
| 1353 } | |
| 1354 | |
| 1355 int SimpleSynchronousEntry::GetEOFRecordData(base::StringPiece file_0_prefetch, | |
| 1356 int file_index, | |
| 1357 int file_offset, | |
| 1358 SimpleFileEOF* eof_record) { | |
| 1359 bool ok = ReadFromFileOrPrefetched(file_0_prefetch, file_index, file_offset, | |
| 1360 sizeof(SimpleFileEOF), | |
| 1361 reinterpret_cast<char*>(eof_record)); | |
| 1362 if (!ok) { | |
|
gavinp
2017/08/25 17:46:46
Could we just move the expression initializing ok
Maks Orlovich
2017/08/25 18:19:54
Done.
| |
| 1266 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); | 1363 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); |
| 1267 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; | 1364 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; |
| 1268 } | 1365 } |
| 1269 | 1366 |
| 1270 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { | 1367 if (eof_record->final_magic_number != kSimpleFinalMagicNumber) { |
| 1271 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); | 1368 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); |
| 1272 DVLOG(1) << "EOF record had bad magic number."; | 1369 DVLOG(1) << "EOF record had bad magic number."; |
| 1273 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; | 1370 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; |
| 1274 } | 1371 } |
| 1275 | 1372 |
| 1276 if (!base::IsValueInRangeForNumericType<int32_t>(eof_record.stream_size)) | 1373 if (!base::IsValueInRangeForNumericType<int32_t>(eof_record->stream_size)) |
| 1277 return net::ERR_FAILED; | 1374 return net::ERR_FAILED; |
| 1278 | 1375 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, |
| 1279 *out_has_crc32 = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) == | 1376 (eof_record->flags & SimpleFileEOF::FLAG_HAS_CRC32) == |
| 1280 SimpleFileEOF::FLAG_HAS_CRC32; | 1377 SimpleFileEOF::FLAG_HAS_CRC32); |
| 1281 *out_has_key_sha256 = | |
| 1282 (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) == | |
| 1283 SimpleFileEOF::FLAG_HAS_KEY_SHA256; | |
| 1284 *out_crc32 = eof_record.data_crc32; | |
| 1285 *out_data_size = eof_record.stream_size; | |
| 1286 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, *out_has_crc32); | |
| 1287 return net::OK; | 1378 return net::OK; |
| 1288 } | 1379 } |
| 1289 | 1380 |
| 1290 void SimpleSynchronousEntry::Doom() const { | 1381 void SimpleSynchronousEntry::Doom() const { |
| 1291 DeleteFilesForEntryHash(path_, entry_hash_); | 1382 DeleteFilesForEntryHash(path_, entry_hash_); |
| 1292 } | 1383 } |
| 1293 | 1384 |
| 1294 // static | 1385 // static |
| 1295 bool SimpleSynchronousEntry::DeleteFileForEntryHash(const FilePath& path, | 1386 bool SimpleSynchronousEntry::DeleteFileForEntryHash(const FilePath& path, |
| 1296 const uint64_t entry_hash, | 1387 const uint64_t entry_hash, |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1596 range.offset = offset; | 1687 range.offset = offset; |
| 1597 range.length = len; | 1688 range.length = len; |
| 1598 range.data_crc32 = data_crc32; | 1689 range.data_crc32 = data_crc32; |
| 1599 range.file_offset = data_file_offset; | 1690 range.file_offset = data_file_offset; |
| 1600 sparse_ranges_.insert(std::make_pair(offset, range)); | 1691 sparse_ranges_.insert(std::make_pair(offset, range)); |
| 1601 | 1692 |
| 1602 return true; | 1693 return true; |
| 1603 } | 1694 } |
| 1604 | 1695 |
| 1605 } // namespace disk_cache | 1696 } // namespace disk_cache |
| OLD | NEW |