| 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> |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 | 481 |
| 482 if (!sparse_file_open() && !CreateSparseFile()) { | 482 if (!sparse_file_open() && !CreateSparseFile()) { |
| 483 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 483 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
| 484 return; | 484 return; |
| 485 } | 485 } |
| 486 | 486 |
| 487 int64 sparse_data_size = out_entry_stat->sparse_data_size(); | 487 int64 sparse_data_size = out_entry_stat->sparse_data_size(); |
| 488 // This is a pessimistic estimate; it assumes the entire buffer is going to | 488 // This is a pessimistic estimate; it assumes the entire buffer is going to |
| 489 // be appended as a new range, not written over existing ranges. | 489 // be appended as a new range, not written over existing ranges. |
| 490 if (sparse_data_size + buf_len > max_sparse_data_size) { | 490 if (sparse_data_size + buf_len > max_sparse_data_size) { |
| 491 DLOG(INFO) << "Truncating sparse data file (" << sparse_data_size << " + " | 491 DVLOG(0) << "Truncating sparse data file (" << sparse_data_size << " + " |
| 492 << buf_len << " > " << max_sparse_data_size << ")"; | 492 << buf_len << " > " << max_sparse_data_size << ")"; |
| 493 TruncateSparseFile(); | 493 TruncateSparseFile(); |
| 494 } | 494 } |
| 495 | 495 |
| 496 SparseRangeIterator it = sparse_ranges_.lower_bound(offset); | 496 SparseRangeIterator it = sparse_ranges_.lower_bound(offset); |
| 497 | 497 |
| 498 if (it != sparse_ranges_.begin()) { | 498 if (it != sparse_ranges_.begin()) { |
| 499 --it; | 499 --it; |
| 500 SparseRange* found_range = &it->second; | 500 SparseRange* found_range = &it->second; |
| 501 if (found_range->offset + found_range->length > offset) { | 501 if (found_range->offset + found_range->length > offset) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 uint32 crc32; | 617 uint32 crc32; |
| 618 bool has_crc32; | 618 bool has_crc32; |
| 619 int stream_size; | 619 int stream_size; |
| 620 *out_result = | 620 *out_result = |
| 621 GetEOFRecordData(index, entry_stat, &has_crc32, &crc32, &stream_size); | 621 GetEOFRecordData(index, entry_stat, &has_crc32, &crc32, &stream_size); |
| 622 if (*out_result != net::OK) { | 622 if (*out_result != net::OK) { |
| 623 Doom(); | 623 Doom(); |
| 624 return; | 624 return; |
| 625 } | 625 } |
| 626 if (has_crc32 && crc32 != expected_crc32) { | 626 if (has_crc32 && crc32 != expected_crc32) { |
| 627 DLOG(INFO) << "EOF record had bad crc."; | 627 DVLOG(0) << "EOF record had bad crc."; |
| 628 *out_result = net::ERR_CACHE_CHECKSUM_MISMATCH; | 628 *out_result = net::ERR_CACHE_CHECKSUM_MISMATCH; |
| 629 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | 629 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); |
| 630 Doom(); | 630 Doom(); |
| 631 return; | 631 return; |
| 632 } | 632 } |
| 633 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); | 633 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); |
| 634 } | 634 } |
| 635 | 635 |
| 636 void SimpleSynchronousEntry::Close( | 636 void SimpleSynchronousEntry::Close( |
| 637 const SimpleEntryStat& entry_stat, | 637 const SimpleEntryStat& entry_stat, |
| 638 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write, | 638 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write, |
| 639 net::GrowableIOBuffer* stream_0_data) { | 639 net::GrowableIOBuffer* stream_0_data) { |
| 640 DCHECK(stream_0_data); | 640 DCHECK(stream_0_data); |
| 641 // Write stream 0 data. | 641 // Write stream 0 data. |
| 642 int stream_0_offset = entry_stat.GetOffsetInFile(key_, 0, 0); | 642 int stream_0_offset = entry_stat.GetOffsetInFile(key_, 0, 0); |
| 643 if (WritePlatformFile(files_[0], | 643 if (WritePlatformFile(files_[0], |
| 644 stream_0_offset, | 644 stream_0_offset, |
| 645 stream_0_data->data(), | 645 stream_0_data->data(), |
| 646 entry_stat.data_size(0)) != entry_stat.data_size(0)) { | 646 entry_stat.data_size(0)) != entry_stat.data_size(0)) { |
| 647 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); | 647 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); |
| 648 DLOG(INFO) << "Could not write stream 0 data."; | 648 DVLOG(0) << "Could not write stream 0 data."; |
| 649 Doom(); | 649 Doom(); |
| 650 } | 650 } |
| 651 | 651 |
| 652 for (std::vector<CRCRecord>::const_iterator it = crc32s_to_write->begin(); | 652 for (std::vector<CRCRecord>::const_iterator it = crc32s_to_write->begin(); |
| 653 it != crc32s_to_write->end(); ++it) { | 653 it != crc32s_to_write->end(); ++it) { |
| 654 const int stream_index = it->index; | 654 const int stream_index = it->index; |
| 655 const int file_index = GetFileIndexFromStreamIndex(stream_index); | 655 const int file_index = GetFileIndexFromStreamIndex(stream_index); |
| 656 if (empty_file_omitted_[file_index]) | 656 if (empty_file_omitted_[file_index]) |
| 657 continue; | 657 continue; |
| 658 | 658 |
| 659 SimpleFileEOF eof_record; | 659 SimpleFileEOF eof_record; |
| 660 eof_record.stream_size = entry_stat.data_size(stream_index); | 660 eof_record.stream_size = entry_stat.data_size(stream_index); |
| 661 eof_record.final_magic_number = kSimpleFinalMagicNumber; | 661 eof_record.final_magic_number = kSimpleFinalMagicNumber; |
| 662 eof_record.flags = 0; | 662 eof_record.flags = 0; |
| 663 if (it->has_crc32) | 663 if (it->has_crc32) |
| 664 eof_record.flags |= SimpleFileEOF::FLAG_HAS_CRC32; | 664 eof_record.flags |= SimpleFileEOF::FLAG_HAS_CRC32; |
| 665 eof_record.data_crc32 = it->data_crc32; | 665 eof_record.data_crc32 = it->data_crc32; |
| 666 int eof_offset = entry_stat.GetEOFOffsetInFile(key_, stream_index); | 666 int eof_offset = entry_stat.GetEOFOffsetInFile(key_, stream_index); |
| 667 // If stream 0 changed size, the file needs to be resized, otherwise the | 667 // If stream 0 changed size, the file needs to be resized, otherwise the |
| 668 // next open will yield wrong stream sizes. On stream 1 and stream 2 proper | 668 // next open will yield wrong stream sizes. On stream 1 and stream 2 proper |
| 669 // resizing of the file is handled in SimpleSynchronousEntry::WriteData(). | 669 // resizing of the file is handled in SimpleSynchronousEntry::WriteData(). |
| 670 if (stream_index == 0 && | 670 if (stream_index == 0 && |
| 671 !TruncatePlatformFile(files_[file_index], eof_offset)) { | 671 !TruncatePlatformFile(files_[file_index], eof_offset)) { |
| 672 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); | 672 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); |
| 673 DLOG(INFO) << "Could not truncate stream 0 file."; | 673 DVLOG(0) << "Could not truncate stream 0 file."; |
| 674 Doom(); | 674 Doom(); |
| 675 break; | 675 break; |
| 676 } | 676 } |
| 677 if (WritePlatformFile(files_[file_index], | 677 if (WritePlatformFile(files_[file_index], |
| 678 eof_offset, | 678 eof_offset, |
| 679 reinterpret_cast<const char*>(&eof_record), | 679 reinterpret_cast<const char*>(&eof_record), |
| 680 sizeof(eof_record)) != sizeof(eof_record)) { | 680 sizeof(eof_record)) != sizeof(eof_record)) { |
| 681 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); | 681 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); |
| 682 DLOG(INFO) << "Could not write eof record."; | 682 DVLOG(0) << "Could not write eof record."; |
| 683 Doom(); | 683 Doom(); |
| 684 break; | 684 break; |
| 685 } | 685 } |
| 686 } | 686 } |
| 687 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 687 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 688 if (empty_file_omitted_[i]) | 688 if (empty_file_omitted_[i]) |
| 689 continue; | 689 continue; |
| 690 | 690 |
| 691 bool did_close_file = ClosePlatformFile(files_[i]); | 691 bool did_close_file = ClosePlatformFile(files_[i]); |
| 692 DCHECK(did_close_file); | 692 DCHECK(did_close_file); |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 998 cache_type_, OPEN_ENTRY_SPARSE_OPEN_FAILED, had_index); | 998 cache_type_, OPEN_ENTRY_SPARSE_OPEN_FAILED, had_index); |
| 999 return net::ERR_FAILED; | 999 return net::ERR_FAILED; |
| 1000 } | 1000 } |
| 1001 out_entry_stat->set_sparse_data_size(sparse_data_size); | 1001 out_entry_stat->set_sparse_data_size(sparse_data_size); |
| 1002 | 1002 |
| 1003 bool removed_stream2 = false; | 1003 bool removed_stream2 = false; |
| 1004 const int stream2_file_index = GetFileIndexFromStreamIndex(2); | 1004 const int stream2_file_index = GetFileIndexFromStreamIndex(2); |
| 1005 DCHECK(CanOmitEmptyFile(stream2_file_index)); | 1005 DCHECK(CanOmitEmptyFile(stream2_file_index)); |
| 1006 if (!empty_file_omitted_[stream2_file_index] && | 1006 if (!empty_file_omitted_[stream2_file_index] && |
| 1007 out_entry_stat->data_size(2) == 0) { | 1007 out_entry_stat->data_size(2) == 0) { |
| 1008 DLOG(INFO) << "Removing empty stream 2 file."; | 1008 DVLOG(0) << "Removing empty stream 2 file."; |
| 1009 CloseFile(stream2_file_index); | 1009 CloseFile(stream2_file_index); |
| 1010 DeleteFileForEntryHash(path_, entry_hash_, stream2_file_index); | 1010 DeleteFileForEntryHash(path_, entry_hash_, stream2_file_index); |
| 1011 empty_file_omitted_[stream2_file_index] = true; | 1011 empty_file_omitted_[stream2_file_index] = true; |
| 1012 removed_stream2 = true; | 1012 removed_stream2 = true; |
| 1013 } | 1013 } |
| 1014 | 1014 |
| 1015 SIMPLE_CACHE_UMA(BOOLEAN, "EntryOpenedAndStream2Removed", cache_type_, | 1015 SIMPLE_CACHE_UMA(BOOLEAN, "EntryOpenedAndStream2Removed", cache_type_, |
| 1016 removed_stream2); | 1016 removed_stream2); |
| 1017 | 1017 |
| 1018 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_SUCCESS, had_index); | 1018 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_SUCCESS, had_index); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1106 return net::ERR_FAILED; | 1106 return net::ERR_FAILED; |
| 1107 | 1107 |
| 1108 // Check the CRC32. | 1108 // Check the CRC32. |
| 1109 uint32 expected_crc32 = | 1109 uint32 expected_crc32 = |
| 1110 stream_0_size == 0 | 1110 stream_0_size == 0 |
| 1111 ? crc32(0, Z_NULL, 0) | 1111 ? crc32(0, Z_NULL, 0) |
| 1112 : crc32(crc32(0, Z_NULL, 0), | 1112 : crc32(crc32(0, Z_NULL, 0), |
| 1113 reinterpret_cast<const Bytef*>((*stream_0_data)->data()), | 1113 reinterpret_cast<const Bytef*>((*stream_0_data)->data()), |
| 1114 stream_0_size); | 1114 stream_0_size); |
| 1115 if (has_crc32 && read_crc32 != expected_crc32) { | 1115 if (has_crc32 && read_crc32 != expected_crc32) { |
| 1116 DLOG(INFO) << "EOF record had bad crc."; | 1116 DVLOG(0) << "EOF record had bad crc."; |
| 1117 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | 1117 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); |
| 1118 return net::ERR_FAILED; | 1118 return net::ERR_FAILED; |
| 1119 } | 1119 } |
| 1120 *out_stream_0_crc32 = expected_crc32; | 1120 *out_stream_0_crc32 = expected_crc32; |
| 1121 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); | 1121 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); |
| 1122 return net::OK; | 1122 return net::OK; |
| 1123 } | 1123 } |
| 1124 | 1124 |
| 1125 int SimpleSynchronousEntry::GetEOFRecordData(int index, | 1125 int SimpleSynchronousEntry::GetEOFRecordData(int index, |
| 1126 const SimpleEntryStat& entry_stat, | 1126 const SimpleEntryStat& entry_stat, |
| 1127 bool* out_has_crc32, | 1127 bool* out_has_crc32, |
| 1128 uint32* out_crc32, | 1128 uint32* out_crc32, |
| 1129 int* out_data_size) const { | 1129 int* out_data_size) const { |
| 1130 SimpleFileEOF eof_record; | 1130 SimpleFileEOF eof_record; |
| 1131 int file_offset = entry_stat.GetEOFOffsetInFile(key_, index); | 1131 int file_offset = entry_stat.GetEOFOffsetInFile(key_, index); |
| 1132 int file_index = GetFileIndexFromStreamIndex(index); | 1132 int file_index = GetFileIndexFromStreamIndex(index); |
| 1133 if (ReadPlatformFile(files_[file_index], | 1133 if (ReadPlatformFile(files_[file_index], |
| 1134 file_offset, | 1134 file_offset, |
| 1135 reinterpret_cast<char*>(&eof_record), | 1135 reinterpret_cast<char*>(&eof_record), |
| 1136 sizeof(eof_record)) != sizeof(eof_record)) { | 1136 sizeof(eof_record)) != sizeof(eof_record)) { |
| 1137 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); | 1137 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); |
| 1138 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; | 1138 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; |
| 1139 } | 1139 } |
| 1140 | 1140 |
| 1141 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { | 1141 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { |
| 1142 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); | 1142 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); |
| 1143 DLOG(INFO) << "EOF record had bad magic number."; | 1143 DVLOG(0) << "EOF record had bad magic number."; |
| 1144 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; | 1144 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; |
| 1145 } | 1145 } |
| 1146 | 1146 |
| 1147 *out_has_crc32 = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) == | 1147 *out_has_crc32 = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) == |
| 1148 SimpleFileEOF::FLAG_HAS_CRC32; | 1148 SimpleFileEOF::FLAG_HAS_CRC32; |
| 1149 *out_crc32 = eof_record.data_crc32; | 1149 *out_crc32 = eof_record.data_crc32; |
| 1150 *out_data_size = eof_record.stream_size; | 1150 *out_data_size = eof_record.stream_size; |
| 1151 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, *out_has_crc32); | 1151 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, *out_has_crc32); |
| 1152 return net::OK; | 1152 return net::OK; |
| 1153 } | 1153 } |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1469 range.offset = offset; | 1469 range.offset = offset; |
| 1470 range.length = len; | 1470 range.length = len; |
| 1471 range.data_crc32 = data_crc32; | 1471 range.data_crc32 = data_crc32; |
| 1472 range.file_offset = data_file_offset; | 1472 range.file_offset = data_file_offset; |
| 1473 sparse_ranges_.insert(std::make_pair(offset, range)); | 1473 sparse_ranges_.insert(std::make_pair(offset, range)); |
| 1474 | 1474 |
| 1475 return true; | 1475 return true; |
| 1476 } | 1476 } |
| 1477 | 1477 |
| 1478 } // namespace disk_cache | 1478 } // namespace disk_cache |
| OLD | NEW |