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/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/hash.h" | 15 #include "base/hash.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/sha1.h" | 17 #include "base/sha1.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.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_histogram_macros.h" | 22 #include "net/disk_cache/simple/simple_histogram_macros.h" |
23 #include "net/disk_cache/simple/simple_util.h" | 23 #include "net/disk_cache/simple/simple_util.h" |
24 #include "third_party/zlib/zlib.h" | 24 #include "third_party/zlib/zlib.h" |
25 | 25 |
26 using base::kInvalidPlatformFileValue; | 26 using base::File; |
27 using base::ClosePlatformFile; | |
28 using base::FilePath; | 27 using base::FilePath; |
29 using base::GetPlatformFileInfo; | |
30 using base::PlatformFileError; | |
31 using base::PlatformFileInfo; | |
32 using base::PLATFORM_FILE_CREATE; | |
33 using base::PLATFORM_FILE_ERROR_EXISTS; | |
34 using base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
35 using base::PLATFORM_FILE_OK; | |
36 using base::PLATFORM_FILE_OPEN; | |
37 using base::PLATFORM_FILE_OPEN_ALWAYS; | |
38 using base::PLATFORM_FILE_READ; | |
39 using base::PLATFORM_FILE_WRITE; | |
40 using base::ReadPlatformFile; | |
41 using base::Time; | 28 using base::Time; |
42 using base::TruncatePlatformFile; | |
43 using base::WritePlatformFile; | |
44 | 29 |
45 namespace { | 30 namespace { |
46 | 31 |
47 // Used in histograms, please only add entries at the end. | 32 // Used in histograms, please only add entries at the end. |
48 enum OpenEntryResult { | 33 enum OpenEntryResult { |
49 OPEN_ENTRY_SUCCESS = 0, | 34 OPEN_ENTRY_SUCCESS = 0, |
50 OPEN_ENTRY_PLATFORM_FILE_ERROR = 1, | 35 OPEN_ENTRY_PLATFORM_FILE_ERROR = 1, |
51 OPEN_ENTRY_CANT_READ_HEADER = 2, | 36 OPEN_ENTRY_CANT_READ_HEADER = 2, |
52 OPEN_ENTRY_BAD_MAGIC_NUMBER = 3, | 37 OPEN_ENTRY_BAD_MAGIC_NUMBER = 3, |
53 OPEN_ENTRY_BAD_VERSION = 4, | 38 OPEN_ENTRY_BAD_VERSION = 4, |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 int* out_result) const { | 279 int* out_result) const { |
295 DCHECK(initialized_); | 280 DCHECK(initialized_); |
296 DCHECK_NE(0, in_entry_op.index); | 281 DCHECK_NE(0, in_entry_op.index); |
297 const int64 file_offset = | 282 const int64 file_offset = |
298 entry_stat->GetOffsetInFile(key_, in_entry_op.offset, in_entry_op.index); | 283 entry_stat->GetOffsetInFile(key_, in_entry_op.offset, in_entry_op.index); |
299 int file_index = GetFileIndexFromStreamIndex(in_entry_op.index); | 284 int file_index = GetFileIndexFromStreamIndex(in_entry_op.index); |
300 // Zero-length reads and reads to the empty streams of omitted files should | 285 // Zero-length reads and reads to the empty streams of omitted files should |
301 // be handled in the SimpleEntryImpl. | 286 // be handled in the SimpleEntryImpl. |
302 DCHECK_LT(0, in_entry_op.buf_len); | 287 DCHECK_LT(0, in_entry_op.buf_len); |
303 DCHECK(!empty_file_omitted_[file_index]); | 288 DCHECK(!empty_file_omitted_[file_index]); |
304 int bytes_read = ReadPlatformFile( | 289 File* file = const_cast<File*>(&files_[file_index]); |
305 files_[file_index], file_offset, out_buf->data(), in_entry_op.buf_len); | 290 int bytes_read = |
291 file->Read(file_offset, out_buf->data(), in_entry_op.buf_len); | |
306 if (bytes_read > 0) { | 292 if (bytes_read > 0) { |
307 entry_stat->set_last_used(Time::Now()); | 293 entry_stat->set_last_used(Time::Now()); |
308 *out_crc32 = crc32(crc32(0L, Z_NULL, 0), | 294 *out_crc32 = crc32(crc32(0L, Z_NULL, 0), |
309 reinterpret_cast<const Bytef*>(out_buf->data()), | 295 reinterpret_cast<const Bytef*>(out_buf->data()), |
310 bytes_read); | 296 bytes_read); |
311 } | 297 } |
312 if (bytes_read >= 0) { | 298 if (bytes_read >= 0) { |
313 *out_result = bytes_read; | 299 *out_result = bytes_read; |
314 } else { | 300 } else { |
315 *out_result = net::ERR_CACHE_READ_FAILURE; | 301 *out_result = net::ERR_CACHE_READ_FAILURE; |
(...skipping 20 matching lines...) Expand all Loading... | |
336 if (empty_file_omitted_[file_index]) { | 322 if (empty_file_omitted_[file_index]) { |
337 // Don't create a new file if the entry has been doomed, to avoid it being | 323 // Don't create a new file if the entry has been doomed, to avoid it being |
338 // mixed up with a newly-created entry with the same key. | 324 // mixed up with a newly-created entry with the same key. |
339 if (doomed) { | 325 if (doomed) { |
340 DLOG(WARNING) << "Rejecting write to lazily omitted stream " | 326 DLOG(WARNING) << "Rejecting write to lazily omitted stream " |
341 << in_entry_op.index << " of doomed cache entry."; | 327 << in_entry_op.index << " of doomed cache entry."; |
342 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_STREAM_ENTRY_DOOMED); | 328 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_STREAM_ENTRY_DOOMED); |
343 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 329 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
344 return; | 330 return; |
345 } | 331 } |
346 PlatformFileError error; | 332 File::Error error; |
347 if (!MaybeCreateFile(file_index, FILE_REQUIRED, &error)) { | 333 if (!MaybeCreateFile(file_index, FILE_REQUIRED, &error)) { |
348 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_CREATE_FAILURE); | 334 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_CREATE_FAILURE); |
349 Doom(); | 335 Doom(); |
350 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 336 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
351 return; | 337 return; |
352 } | 338 } |
353 CreateEntryResult result; | 339 CreateEntryResult result; |
354 if (!InitializeCreatedFile(file_index, &result)) { | 340 if (!InitializeCreatedFile(file_index, &result)) { |
355 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_INITIALIZE_FAILURE); | 341 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_INITIALIZE_FAILURE); |
356 Doom(); | 342 Doom(); |
357 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 343 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
358 return; | 344 return; |
359 } | 345 } |
360 } | 346 } |
361 DCHECK(!empty_file_omitted_[file_index]); | 347 DCHECK(!empty_file_omitted_[file_index]); |
362 | 348 |
363 if (extending_by_write) { | 349 if (extending_by_write) { |
364 // The EOF record and the eventual stream afterward need to be zeroed out. | 350 // The EOF record and the eventual stream afterward need to be zeroed out. |
365 const int64 file_eof_offset = | 351 const int64 file_eof_offset = |
366 out_entry_stat->GetEOFOffsetInFile(key_, index); | 352 out_entry_stat->GetEOFOffsetInFile(key_, index); |
367 if (!TruncatePlatformFile(files_[file_index], file_eof_offset)) { | 353 if (!files_[file_index].SetLength(file_eof_offset)) { |
368 RecordWriteResult(cache_type_, WRITE_RESULT_PRETRUNCATE_FAILURE); | 354 RecordWriteResult(cache_type_, WRITE_RESULT_PRETRUNCATE_FAILURE); |
369 Doom(); | 355 Doom(); |
370 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 356 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
371 return; | 357 return; |
372 } | 358 } |
373 } | 359 } |
374 if (buf_len > 0) { | 360 if (buf_len > 0) { |
375 if (WritePlatformFile( | 361 if (files_[file_index].Write(file_offset, in_buf->data(), buf_len) != |
376 files_[file_index], file_offset, in_buf->data(), buf_len) != | |
377 buf_len) { | 362 buf_len) { |
378 RecordWriteResult(cache_type_, WRITE_RESULT_WRITE_FAILURE); | 363 RecordWriteResult(cache_type_, WRITE_RESULT_WRITE_FAILURE); |
379 Doom(); | 364 Doom(); |
380 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 365 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
381 return; | 366 return; |
382 } | 367 } |
383 } | 368 } |
384 if (!truncate && (buf_len > 0 || !extending_by_write)) { | 369 if (!truncate && (buf_len > 0 || !extending_by_write)) { |
385 out_entry_stat->set_data_size( | 370 out_entry_stat->set_data_size( |
386 index, std::max(out_entry_stat->data_size(index), offset + buf_len)); | 371 index, std::max(out_entry_stat->data_size(index), offset + buf_len)); |
387 } else { | 372 } else { |
388 out_entry_stat->set_data_size(index, offset + buf_len); | 373 out_entry_stat->set_data_size(index, offset + buf_len); |
389 int file_eof_offset = out_entry_stat->GetLastEOFOffsetInFile(key_, index); | 374 int file_eof_offset = out_entry_stat->GetLastEOFOffsetInFile(key_, index); |
390 if (!TruncatePlatformFile(files_[file_index], file_eof_offset)) { | 375 if (!files_[file_index].SetLength(file_eof_offset)) { |
391 RecordWriteResult(cache_type_, WRITE_RESULT_TRUNCATE_FAILURE); | 376 RecordWriteResult(cache_type_, WRITE_RESULT_TRUNCATE_FAILURE); |
392 Doom(); | 377 Doom(); |
393 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 378 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
394 return; | 379 return; |
395 } | 380 } |
396 } | 381 } |
397 | 382 |
398 RecordWriteResult(cache_type_, WRITE_RESULT_SUCCESS); | 383 RecordWriteResult(cache_type_, WRITE_RESULT_SUCCESS); |
399 base::Time modification_time = Time::Now(); | 384 base::Time modification_time = Time::Now(); |
400 out_entry_stat->set_last_used(modification_time); | 385 out_entry_stat->set_last_used(modification_time); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
481 | 466 |
482 if (!sparse_file_open() && !CreateSparseFile()) { | 467 if (!sparse_file_open() && !CreateSparseFile()) { |
483 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 468 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
484 return; | 469 return; |
485 } | 470 } |
486 | 471 |
487 int64 sparse_data_size = out_entry_stat->sparse_data_size(); | 472 int64 sparse_data_size = out_entry_stat->sparse_data_size(); |
488 // This is a pessimistic estimate; it assumes the entire buffer is going to | 473 // 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. | 474 // be appended as a new range, not written over existing ranges. |
490 if (sparse_data_size + buf_len > max_sparse_data_size) { | 475 if (sparse_data_size + buf_len > max_sparse_data_size) { |
491 DLOG(INFO) << "Truncating sparse data file (" << sparse_data_size << " + " | 476 DVLOG(1) << "Truncating sparse data file (" << sparse_data_size << " + " |
492 << buf_len << " > " << max_sparse_data_size << ")"; | 477 << buf_len << " > " << max_sparse_data_size << ")"; |
493 TruncateSparseFile(); | 478 TruncateSparseFile(); |
494 } | 479 } |
495 | 480 |
496 SparseRangeIterator it = sparse_ranges_.lower_bound(offset); | 481 SparseRangeIterator it = sparse_ranges_.lower_bound(offset); |
497 | 482 |
498 if (it != sparse_ranges_.begin()) { | 483 if (it != sparse_ranges_.begin()) { |
499 --it; | 484 --it; |
500 SparseRange* found_range = &it->second; | 485 SparseRange* found_range = &it->second; |
501 if (found_range->offset + found_range->length > offset) { | 486 if (found_range->offset + found_range->length > offset) { |
502 DCHECK_LE(0, found_range->length); | 487 DCHECK_LE(0, found_range->length); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
617 uint32 crc32; | 602 uint32 crc32; |
618 bool has_crc32; | 603 bool has_crc32; |
619 int stream_size; | 604 int stream_size; |
620 *out_result = | 605 *out_result = |
621 GetEOFRecordData(index, entry_stat, &has_crc32, &crc32, &stream_size); | 606 GetEOFRecordData(index, entry_stat, &has_crc32, &crc32, &stream_size); |
622 if (*out_result != net::OK) { | 607 if (*out_result != net::OK) { |
623 Doom(); | 608 Doom(); |
624 return; | 609 return; |
625 } | 610 } |
626 if (has_crc32 && crc32 != expected_crc32) { | 611 if (has_crc32 && crc32 != expected_crc32) { |
627 DLOG(INFO) << "EOF record had bad crc."; | 612 DVLOG(1) << "EOF record had bad crc."; |
Nico
2014/01/08 23:40:16
Is this ever interesting? Can all these log statem
rvargas (doing something else)
2014/01/09 04:11:46
I don't think they are interesting. That's a good
| |
628 *out_result = net::ERR_CACHE_CHECKSUM_MISMATCH; | 613 *out_result = net::ERR_CACHE_CHECKSUM_MISMATCH; |
629 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | 614 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); |
630 Doom(); | 615 Doom(); |
631 return; | 616 return; |
632 } | 617 } |
633 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); | 618 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); |
634 } | 619 } |
635 | 620 |
636 void SimpleSynchronousEntry::Close( | 621 void SimpleSynchronousEntry::Close( |
637 const SimpleEntryStat& entry_stat, | 622 const SimpleEntryStat& entry_stat, |
638 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write, | 623 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write, |
639 net::GrowableIOBuffer* stream_0_data) { | 624 net::GrowableIOBuffer* stream_0_data) { |
640 DCHECK(stream_0_data); | 625 DCHECK(stream_0_data); |
641 // Write stream 0 data. | 626 // Write stream 0 data. |
642 int stream_0_offset = entry_stat.GetOffsetInFile(key_, 0, 0); | 627 int stream_0_offset = entry_stat.GetOffsetInFile(key_, 0, 0); |
643 if (WritePlatformFile(files_[0], | 628 if (files_[0].Write(stream_0_offset, stream_0_data->data(), |
644 stream_0_offset, | 629 entry_stat.data_size(0)) != |
645 stream_0_data->data(), | 630 entry_stat.data_size(0)) { |
646 entry_stat.data_size(0)) != entry_stat.data_size(0)) { | |
647 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); | 631 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); |
648 DLOG(INFO) << "Could not write stream 0 data."; | 632 DVLOG(1) << "Could not write stream 0 data."; |
649 Doom(); | 633 Doom(); |
650 } | 634 } |
651 | 635 |
652 for (std::vector<CRCRecord>::const_iterator it = crc32s_to_write->begin(); | 636 for (std::vector<CRCRecord>::const_iterator it = crc32s_to_write->begin(); |
653 it != crc32s_to_write->end(); ++it) { | 637 it != crc32s_to_write->end(); ++it) { |
654 const int stream_index = it->index; | 638 const int stream_index = it->index; |
655 const int file_index = GetFileIndexFromStreamIndex(stream_index); | 639 const int file_index = GetFileIndexFromStreamIndex(stream_index); |
656 if (empty_file_omitted_[file_index]) | 640 if (empty_file_omitted_[file_index]) |
657 continue; | 641 continue; |
658 | 642 |
659 SimpleFileEOF eof_record; | 643 SimpleFileEOF eof_record; |
660 eof_record.stream_size = entry_stat.data_size(stream_index); | 644 eof_record.stream_size = entry_stat.data_size(stream_index); |
661 eof_record.final_magic_number = kSimpleFinalMagicNumber; | 645 eof_record.final_magic_number = kSimpleFinalMagicNumber; |
662 eof_record.flags = 0; | 646 eof_record.flags = 0; |
663 if (it->has_crc32) | 647 if (it->has_crc32) |
664 eof_record.flags |= SimpleFileEOF::FLAG_HAS_CRC32; | 648 eof_record.flags |= SimpleFileEOF::FLAG_HAS_CRC32; |
665 eof_record.data_crc32 = it->data_crc32; | 649 eof_record.data_crc32 = it->data_crc32; |
666 int eof_offset = entry_stat.GetEOFOffsetInFile(key_, stream_index); | 650 int eof_offset = entry_stat.GetEOFOffsetInFile(key_, stream_index); |
667 // If stream 0 changed size, the file needs to be resized, otherwise the | 651 // 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 | 652 // next open will yield wrong stream sizes. On stream 1 and stream 2 proper |
669 // resizing of the file is handled in SimpleSynchronousEntry::WriteData(). | 653 // resizing of the file is handled in SimpleSynchronousEntry::WriteData(). |
670 if (stream_index == 0 && | 654 if (stream_index == 0 && |
671 !TruncatePlatformFile(files_[file_index], eof_offset)) { | 655 !files_[file_index].SetLength(eof_offset)) { |
672 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); | 656 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); |
673 DLOG(INFO) << "Could not truncate stream 0 file."; | 657 DVLOG(1) << "Could not truncate stream 0 file."; |
674 Doom(); | 658 Doom(); |
675 break; | 659 break; |
676 } | 660 } |
677 if (WritePlatformFile(files_[file_index], | 661 if (files_[file_index].Write(eof_offset, |
678 eof_offset, | 662 reinterpret_cast<const char*>(&eof_record), |
679 reinterpret_cast<const char*>(&eof_record), | 663 sizeof(eof_record)) != |
680 sizeof(eof_record)) != sizeof(eof_record)) { | 664 sizeof(eof_record)) { |
681 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); | 665 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); |
682 DLOG(INFO) << "Could not write eof record."; | 666 DVLOG(1) << "Could not write eof record."; |
683 Doom(); | 667 Doom(); |
684 break; | 668 break; |
685 } | 669 } |
686 } | 670 } |
687 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 671 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
688 if (empty_file_omitted_[i]) | 672 if (empty_file_omitted_[i]) |
689 continue; | 673 continue; |
690 | 674 |
691 bool did_close_file = ClosePlatformFile(files_[i]); | 675 files_[i].Close(); |
692 DCHECK(did_close_file); | |
693 const int64 file_size = entry_stat.GetFileSize(key_, i); | 676 const int64 file_size = entry_stat.GetFileSize(key_, i); |
694 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, | 677 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, |
695 "LastClusterSize", cache_type_, | 678 "LastClusterSize", cache_type_, |
696 file_size % 4096, 0, 4097, 50); | 679 file_size % 4096, 0, 4097, 50); |
697 const int64 cluster_loss = file_size % 4096 ? 4096 - file_size % 4096 : 0; | 680 const int64 cluster_loss = file_size % 4096 ? 4096 - file_size % 4096 : 0; |
698 SIMPLE_CACHE_UMA(PERCENTAGE, | 681 SIMPLE_CACHE_UMA(PERCENTAGE, |
699 "LastClusterLossPercent", cache_type_, | 682 "LastClusterLossPercent", cache_type_, |
700 cluster_loss * 100 / (cluster_loss + file_size)); | 683 cluster_loss * 100 / (cluster_loss + file_size)); |
701 } | 684 } |
702 | 685 |
703 if (sparse_file_open()) { | 686 if (sparse_file_open()) |
704 bool did_close_file = ClosePlatformFile(sparse_file_); | 687 sparse_file_.Close(); |
705 CHECK(did_close_file); | |
706 } | |
707 | 688 |
708 if (files_created_) { | 689 if (files_created_) { |
709 const int stream2_file_index = GetFileIndexFromStreamIndex(2); | 690 const int stream2_file_index = GetFileIndexFromStreamIndex(2); |
710 SIMPLE_CACHE_UMA(BOOLEAN, "EntryCreatedAndStream2Omitted", cache_type_, | 691 SIMPLE_CACHE_UMA(BOOLEAN, "EntryCreatedAndStream2Omitted", cache_type_, |
711 empty_file_omitted_[stream2_file_index]); | 692 empty_file_omitted_[stream2_file_index]); |
712 } | 693 } |
713 RecordCloseResult(cache_type_, CLOSE_RESULT_SUCCESS); | 694 RecordCloseResult(cache_type_, CLOSE_RESULT_SUCCESS); |
714 have_open_files_ = false; | 695 have_open_files_ = false; |
715 delete this; | 696 delete this; |
716 } | 697 } |
717 | 698 |
718 SimpleSynchronousEntry::SimpleSynchronousEntry(net::CacheType cache_type, | 699 SimpleSynchronousEntry::SimpleSynchronousEntry(net::CacheType cache_type, |
719 const FilePath& path, | 700 const FilePath& path, |
720 const std::string& key, | 701 const std::string& key, |
721 const uint64 entry_hash) | 702 const uint64 entry_hash) |
722 : cache_type_(cache_type), | 703 : cache_type_(cache_type), |
723 path_(path), | 704 path_(path), |
724 entry_hash_(entry_hash), | 705 entry_hash_(entry_hash), |
725 key_(key), | 706 key_(key), |
726 have_open_files_(false), | 707 have_open_files_(false), |
727 initialized_(false), | 708 initialized_(false) { |
728 sparse_file_(kInvalidPlatformFileValue) { | 709 for (int i = 0; i < kSimpleEntryFileCount; ++i) |
729 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | |
730 files_[i] = kInvalidPlatformFileValue; | |
731 empty_file_omitted_[i] = false; | 710 empty_file_omitted_[i] = false; |
732 } | |
733 } | 711 } |
734 | 712 |
735 SimpleSynchronousEntry::~SimpleSynchronousEntry() { | 713 SimpleSynchronousEntry::~SimpleSynchronousEntry() { |
736 DCHECK(!(have_open_files_ && initialized_)); | 714 DCHECK(!(have_open_files_ && initialized_)); |
737 if (have_open_files_) | 715 if (have_open_files_) |
738 CloseFiles(); | 716 CloseFiles(); |
739 } | 717 } |
740 | 718 |
741 bool SimpleSynchronousEntry::MaybeOpenFile( | 719 bool SimpleSynchronousEntry::MaybeOpenFile( |
742 int file_index, | 720 int file_index, |
743 PlatformFileError* out_error) { | 721 File::Error* out_error) { |
744 DCHECK(out_error); | 722 DCHECK(out_error); |
745 | 723 |
746 FilePath filename = GetFilenameFromFileIndex(file_index); | 724 FilePath filename = GetFilenameFromFileIndex(file_index); |
747 int flags = PLATFORM_FILE_OPEN | PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; | 725 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; |
748 files_[file_index] = CreatePlatformFile(filename, flags, NULL, out_error); | 726 files_[file_index].Initialize(filename, flags); |
727 *out_error = files_[file_index].error_details(); | |
749 | 728 |
750 if (CanOmitEmptyFile(file_index) && | 729 if (CanOmitEmptyFile(file_index) && !files_[file_index].IsValid() && |
751 *out_error == PLATFORM_FILE_ERROR_NOT_FOUND) { | 730 *out_error == File::FILE_ERROR_NOT_FOUND) { |
752 empty_file_omitted_[file_index] = true; | 731 empty_file_omitted_[file_index] = true; |
753 return true; | 732 return true; |
754 } | 733 } |
755 | 734 |
756 return *out_error == PLATFORM_FILE_OK; | 735 return files_[file_index].IsValid(); |
757 } | 736 } |
758 | 737 |
759 bool SimpleSynchronousEntry::MaybeCreateFile( | 738 bool SimpleSynchronousEntry::MaybeCreateFile( |
760 int file_index, | 739 int file_index, |
761 FileRequired file_required, | 740 FileRequired file_required, |
762 PlatformFileError* out_error) { | 741 File::Error* out_error) { |
763 DCHECK(out_error); | 742 DCHECK(out_error); |
764 | 743 |
765 if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) { | 744 if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) { |
766 empty_file_omitted_[file_index] = true; | 745 empty_file_omitted_[file_index] = true; |
767 return true; | 746 return true; |
768 } | 747 } |
769 | 748 |
770 FilePath filename = GetFilenameFromFileIndex(file_index); | 749 FilePath filename = GetFilenameFromFileIndex(file_index); |
771 int flags = PLATFORM_FILE_CREATE | PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; | 750 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE; |
772 files_[file_index] = CreatePlatformFile(filename, flags, NULL, out_error); | 751 files_[file_index].Initialize(filename, flags); |
752 *out_error = files_[file_index].error_details(); | |
773 | 753 |
774 empty_file_omitted_[file_index] = false; | 754 empty_file_omitted_[file_index] = false; |
775 | 755 |
776 return *out_error == PLATFORM_FILE_OK; | 756 return files_[file_index].IsValid(); |
777 } | 757 } |
778 | 758 |
779 bool SimpleSynchronousEntry::OpenFiles( | 759 bool SimpleSynchronousEntry::OpenFiles( |
780 bool had_index, | 760 bool had_index, |
781 SimpleEntryStat* out_entry_stat) { | 761 SimpleEntryStat* out_entry_stat) { |
782 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 762 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
783 PlatformFileError error; | 763 File::Error error; |
784 if (!MaybeOpenFile(i, &error)) { | 764 if (!MaybeOpenFile(i, &error)) { |
785 // TODO(ttuttle,gavinp): Remove one each of these triplets of histograms. | 765 // TODO(ttuttle,gavinp): Remove one each of these triplets of histograms. |
786 // We can calculate the third as the sum or difference of the other two. | 766 // We can calculate the third as the sum or difference of the other two. |
787 RecordSyncOpenResult( | 767 RecordSyncOpenResult( |
788 cache_type_, OPEN_ENTRY_PLATFORM_FILE_ERROR, had_index); | 768 cache_type_, OPEN_ENTRY_PLATFORM_FILE_ERROR, had_index); |
789 SIMPLE_CACHE_UMA(ENUMERATION, | 769 SIMPLE_CACHE_UMA(ENUMERATION, |
790 "SyncOpenPlatformFileError", cache_type_, | 770 "SyncOpenPlatformFileError", cache_type_, |
791 -error, -base::PLATFORM_FILE_ERROR_MAX); | 771 -error, -base::PLATFORM_FILE_ERROR_MAX); |
792 if (had_index) { | 772 if (had_index) { |
793 SIMPLE_CACHE_UMA(ENUMERATION, | 773 SIMPLE_CACHE_UMA(ENUMERATION, |
(...skipping 13 matching lines...) Expand all Loading... | |
807 | 787 |
808 have_open_files_ = true; | 788 have_open_files_ = true; |
809 | 789 |
810 base::TimeDelta entry_age = base::Time::Now() - base::Time::UnixEpoch(); | 790 base::TimeDelta entry_age = base::Time::Now() - base::Time::UnixEpoch(); |
811 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 791 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
812 if (empty_file_omitted_[i]) { | 792 if (empty_file_omitted_[i]) { |
813 out_entry_stat->set_data_size(i + 1, 0); | 793 out_entry_stat->set_data_size(i + 1, 0); |
814 continue; | 794 continue; |
815 } | 795 } |
816 | 796 |
817 PlatformFileInfo file_info; | 797 File::Info file_info; |
818 bool success = GetPlatformFileInfo(files_[i], &file_info); | 798 bool success = files_[i].GetInfo(&file_info); |
819 base::Time file_last_modified; | 799 base::Time file_last_modified; |
820 if (!success) { | 800 if (!success) { |
821 DLOG(WARNING) << "Could not get platform file info."; | 801 DLOG(WARNING) << "Could not get platform file info."; |
822 continue; | 802 continue; |
823 } | 803 } |
824 out_entry_stat->set_last_used(file_info.last_accessed); | 804 out_entry_stat->set_last_used(file_info.last_accessed); |
825 if (simple_util::GetMTime(path_, &file_last_modified)) | 805 if (simple_util::GetMTime(path_, &file_last_modified)) |
826 out_entry_stat->set_last_modified(file_last_modified); | 806 out_entry_stat->set_last_modified(file_last_modified); |
827 else | 807 else |
828 out_entry_stat->set_last_modified(file_info.last_modified); | 808 out_entry_stat->set_last_modified(file_info.last_modified); |
(...skipping 24 matching lines...) Expand all Loading... | |
853 | 833 |
854 files_created_ = false; | 834 files_created_ = false; |
855 | 835 |
856 return true; | 836 return true; |
857 } | 837 } |
858 | 838 |
859 bool SimpleSynchronousEntry::CreateFiles( | 839 bool SimpleSynchronousEntry::CreateFiles( |
860 bool had_index, | 840 bool had_index, |
861 SimpleEntryStat* out_entry_stat) { | 841 SimpleEntryStat* out_entry_stat) { |
862 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 842 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
863 PlatformFileError error; | 843 File::Error error; |
864 if (!MaybeCreateFile(i, FILE_NOT_REQUIRED, &error)) { | 844 if (!MaybeCreateFile(i, FILE_NOT_REQUIRED, &error)) { |
865 // TODO(ttuttle,gavinp): Remove one each of these triplets of histograms. | 845 // TODO(ttuttle,gavinp): Remove one each of these triplets of histograms. |
866 // We can calculate the third as the sum or difference of the other two. | 846 // We can calculate the third as the sum or difference of the other two. |
867 RecordSyncCreateResult(CREATE_ENTRY_PLATFORM_FILE_ERROR, had_index); | 847 RecordSyncCreateResult(CREATE_ENTRY_PLATFORM_FILE_ERROR, had_index); |
868 SIMPLE_CACHE_UMA(ENUMERATION, | 848 SIMPLE_CACHE_UMA(ENUMERATION, |
869 "SyncCreatePlatformFileError", cache_type_, | 849 "SyncCreatePlatformFileError", cache_type_, |
870 -error, -base::PLATFORM_FILE_ERROR_MAX); | 850 -error, -base::PLATFORM_FILE_ERROR_MAX); |
871 if (had_index) { | 851 if (had_index) { |
872 SIMPLE_CACHE_UMA(ENUMERATION, | 852 SIMPLE_CACHE_UMA(ENUMERATION, |
873 "SyncCreatePlatformFileError_WithIndex", cache_type_, | 853 "SyncCreatePlatformFileError_WithIndex", cache_type_, |
(...skipping 20 matching lines...) Expand all Loading... | |
894 | 874 |
895 files_created_ = true; | 875 files_created_ = true; |
896 | 876 |
897 return true; | 877 return true; |
898 } | 878 } |
899 | 879 |
900 void SimpleSynchronousEntry::CloseFile(int index) { | 880 void SimpleSynchronousEntry::CloseFile(int index) { |
901 if (empty_file_omitted_[index]) { | 881 if (empty_file_omitted_[index]) { |
902 empty_file_omitted_[index] = false; | 882 empty_file_omitted_[index] = false; |
903 } else { | 883 } else { |
904 DCHECK_NE(kInvalidPlatformFileValue, files_[index]); | 884 DCHECK(files_[index].IsValid()); |
905 bool did_close = ClosePlatformFile(files_[index]); | 885 files_[index].Close(); |
906 DCHECK(did_close); | |
907 files_[index] = kInvalidPlatformFileValue; | |
908 } | 886 } |
909 | 887 |
910 if (sparse_file_open()) { | 888 if (sparse_file_open()) |
911 bool did_close = CloseSparseFile(); | 889 CloseSparseFile(); |
912 DCHECK(did_close); | |
913 } | |
914 } | 890 } |
915 | 891 |
916 void SimpleSynchronousEntry::CloseFiles() { | 892 void SimpleSynchronousEntry::CloseFiles() { |
917 for (int i = 0; i < kSimpleEntryFileCount; ++i) | 893 for (int i = 0; i < kSimpleEntryFileCount; ++i) |
918 CloseFile(i); | 894 CloseFile(i); |
919 } | 895 } |
920 | 896 |
921 int SimpleSynchronousEntry::InitializeForOpen( | 897 int SimpleSynchronousEntry::InitializeForOpen( |
922 bool had_index, | 898 bool had_index, |
923 SimpleEntryStat* out_entry_stat, | 899 SimpleEntryStat* out_entry_stat, |
924 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, | 900 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, |
925 uint32* out_stream_0_crc32) { | 901 uint32* out_stream_0_crc32) { |
926 DCHECK(!initialized_); | 902 DCHECK(!initialized_); |
927 if (!OpenFiles(had_index, out_entry_stat)) { | 903 if (!OpenFiles(had_index, out_entry_stat)) { |
928 DLOG(WARNING) << "Could not open platform files for entry."; | 904 DLOG(WARNING) << "Could not open platform files for entry."; |
929 return net::ERR_FAILED; | 905 return net::ERR_FAILED; |
930 } | 906 } |
931 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 907 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
932 if (empty_file_omitted_[i]) | 908 if (empty_file_omitted_[i]) |
933 continue; | 909 continue; |
934 | 910 |
935 SimpleFileHeader header; | 911 SimpleFileHeader header; |
936 int header_read_result = | 912 int header_read_result = |
937 ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), | 913 files_[i].Read(0, reinterpret_cast<char*>(&header), sizeof(header)); |
938 sizeof(header)); | |
939 if (header_read_result != sizeof(header)) { | 914 if (header_read_result != sizeof(header)) { |
940 DLOG(WARNING) << "Cannot read header from entry."; | 915 DLOG(WARNING) << "Cannot read header from entry."; |
941 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_HEADER, had_index); | 916 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_HEADER, had_index); |
942 return net::ERR_FAILED; | 917 return net::ERR_FAILED; |
943 } | 918 } |
944 | 919 |
945 if (header.initial_magic_number != kSimpleInitialMagicNumber) { | 920 if (header.initial_magic_number != kSimpleInitialMagicNumber) { |
946 // TODO(gavinp): This seems very bad; for now we log at WARNING, but we | 921 // TODO(gavinp): This seems very bad; for now we log at WARNING, but we |
947 // should give consideration to not saturating the log with these if that | 922 // should give consideration to not saturating the log with these if that |
948 // becomes a problem. | 923 // becomes a problem. |
949 DLOG(WARNING) << "Magic number did not match."; | 924 DLOG(WARNING) << "Magic number did not match."; |
950 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_BAD_MAGIC_NUMBER, had_index); | 925 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_BAD_MAGIC_NUMBER, had_index); |
951 return net::ERR_FAILED; | 926 return net::ERR_FAILED; |
952 } | 927 } |
953 | 928 |
954 if (header.version != kSimpleEntryVersionOnDisk) { | 929 if (header.version != kSimpleEntryVersionOnDisk) { |
955 DLOG(WARNING) << "Unreadable version."; | 930 DLOG(WARNING) << "Unreadable version."; |
956 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_BAD_VERSION, had_index); | 931 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_BAD_VERSION, had_index); |
957 return net::ERR_FAILED; | 932 return net::ERR_FAILED; |
958 } | 933 } |
959 | 934 |
960 scoped_ptr<char[]> key(new char[header.key_length]); | 935 scoped_ptr<char[]> key(new char[header.key_length]); |
961 int key_read_result = ReadPlatformFile(files_[i], sizeof(header), | 936 int key_read_result = files_[i].Read(sizeof(header), key.get(), |
962 key.get(), header.key_length); | 937 header.key_length); |
963 if (key_read_result != implicit_cast<int>(header.key_length)) { | 938 if (key_read_result != implicit_cast<int>(header.key_length)) { |
964 DLOG(WARNING) << "Cannot read key from entry."; | 939 DLOG(WARNING) << "Cannot read key from entry."; |
965 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_KEY, had_index); | 940 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_KEY, had_index); |
966 return net::ERR_FAILED; | 941 return net::ERR_FAILED; |
967 } | 942 } |
968 | 943 |
969 key_ = std::string(key.get(), header.key_length); | 944 key_ = std::string(key.get(), header.key_length); |
970 if (i == 0) { | 945 if (i == 0) { |
971 // File size for stream 0 has been stored temporarily in data_size[1]. | 946 // File size for stream 0 has been stored temporarily in data_size[1]. |
972 int total_data_size = | 947 int total_data_size = |
(...skipping 25 matching lines...) Expand all Loading... | |
998 cache_type_, OPEN_ENTRY_SPARSE_OPEN_FAILED, had_index); | 973 cache_type_, OPEN_ENTRY_SPARSE_OPEN_FAILED, had_index); |
999 return net::ERR_FAILED; | 974 return net::ERR_FAILED; |
1000 } | 975 } |
1001 out_entry_stat->set_sparse_data_size(sparse_data_size); | 976 out_entry_stat->set_sparse_data_size(sparse_data_size); |
1002 | 977 |
1003 bool removed_stream2 = false; | 978 bool removed_stream2 = false; |
1004 const int stream2_file_index = GetFileIndexFromStreamIndex(2); | 979 const int stream2_file_index = GetFileIndexFromStreamIndex(2); |
1005 DCHECK(CanOmitEmptyFile(stream2_file_index)); | 980 DCHECK(CanOmitEmptyFile(stream2_file_index)); |
1006 if (!empty_file_omitted_[stream2_file_index] && | 981 if (!empty_file_omitted_[stream2_file_index] && |
1007 out_entry_stat->data_size(2) == 0) { | 982 out_entry_stat->data_size(2) == 0) { |
1008 DLOG(INFO) << "Removing empty stream 2 file."; | 983 DVLOG(1) << "Removing empty stream 2 file."; |
1009 CloseFile(stream2_file_index); | 984 CloseFile(stream2_file_index); |
1010 DeleteFileForEntryHash(path_, entry_hash_, stream2_file_index); | 985 DeleteFileForEntryHash(path_, entry_hash_, stream2_file_index); |
1011 empty_file_omitted_[stream2_file_index] = true; | 986 empty_file_omitted_[stream2_file_index] = true; |
1012 removed_stream2 = true; | 987 removed_stream2 = true; |
1013 } | 988 } |
1014 | 989 |
1015 SIMPLE_CACHE_UMA(BOOLEAN, "EntryOpenedAndStream2Removed", cache_type_, | 990 SIMPLE_CACHE_UMA(BOOLEAN, "EntryOpenedAndStream2Removed", cache_type_, |
1016 removed_stream2); | 991 removed_stream2); |
1017 | 992 |
1018 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_SUCCESS, had_index); | 993 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_SUCCESS, had_index); |
1019 initialized_ = true; | 994 initialized_ = true; |
1020 return net::OK; | 995 return net::OK; |
1021 } | 996 } |
1022 | 997 |
1023 bool SimpleSynchronousEntry::InitializeCreatedFile( | 998 bool SimpleSynchronousEntry::InitializeCreatedFile( |
1024 int file_index, | 999 int file_index, |
1025 CreateEntryResult* out_result) { | 1000 CreateEntryResult* out_result) { |
1026 SimpleFileHeader header; | 1001 SimpleFileHeader header; |
1027 header.initial_magic_number = kSimpleInitialMagicNumber; | 1002 header.initial_magic_number = kSimpleInitialMagicNumber; |
1028 header.version = kSimpleEntryVersionOnDisk; | 1003 header.version = kSimpleEntryVersionOnDisk; |
1029 | 1004 |
1030 header.key_length = key_.size(); | 1005 header.key_length = key_.size(); |
1031 header.key_hash = base::Hash(key_); | 1006 header.key_hash = base::Hash(key_); |
1032 | 1007 |
1033 int bytes_written = WritePlatformFile( | 1008 int bytes_written = files_[file_index].Write( |
1034 files_[file_index], 0, reinterpret_cast<char*>(&header), sizeof(header)); | 1009 0, reinterpret_cast<char*>(&header), sizeof(header)); |
1035 if (bytes_written != sizeof(header)) { | 1010 if (bytes_written != sizeof(header)) { |
1036 *out_result = CREATE_ENTRY_CANT_WRITE_HEADER; | 1011 *out_result = CREATE_ENTRY_CANT_WRITE_HEADER; |
1037 return false; | 1012 return false; |
1038 } | 1013 } |
1039 | 1014 |
1040 bytes_written = WritePlatformFile( | 1015 bytes_written = files_[file_index].Write(sizeof(header), key_.data(), |
1041 files_[file_index], sizeof(header), key_.data(), key_.size()); | 1016 key_.size()); |
1042 if (bytes_written != implicit_cast<int>(key_.size())) { | 1017 if (bytes_written != implicit_cast<int>(key_.size())) { |
1043 *out_result = CREATE_ENTRY_CANT_WRITE_KEY; | 1018 *out_result = CREATE_ENTRY_CANT_WRITE_KEY; |
1044 return false; | 1019 return false; |
1045 } | 1020 } |
1046 | 1021 |
1047 return true; | 1022 return true; |
1048 } | 1023 } |
1049 | 1024 |
1050 int SimpleSynchronousEntry::InitializeForCreate( | 1025 int SimpleSynchronousEntry::InitializeForCreate( |
1051 bool had_index, | 1026 bool had_index, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1093 | 1068 |
1094 // These are the real values of data size. | 1069 // These are the real values of data size. |
1095 out_entry_stat->set_data_size(0, stream_0_size); | 1070 out_entry_stat->set_data_size(0, stream_0_size); |
1096 out_entry_stat->set_data_size( | 1071 out_entry_stat->set_data_size( |
1097 1, out_entry_stat->data_size(1) - stream_0_size); | 1072 1, out_entry_stat->data_size(1) - stream_0_size); |
1098 | 1073 |
1099 // Put stream 0 data in memory. | 1074 // Put stream 0 data in memory. |
1100 *stream_0_data = new net::GrowableIOBuffer(); | 1075 *stream_0_data = new net::GrowableIOBuffer(); |
1101 (*stream_0_data)->SetCapacity(stream_0_size); | 1076 (*stream_0_data)->SetCapacity(stream_0_size); |
1102 int file_offset = out_entry_stat->GetOffsetInFile(key_, 0, 0); | 1077 int file_offset = out_entry_stat->GetOffsetInFile(key_, 0, 0); |
1103 int bytes_read = ReadPlatformFile( | 1078 File* file = const_cast<File*>(&files_[0]); |
1104 files_[0], file_offset, (*stream_0_data)->data(), stream_0_size); | 1079 int bytes_read = |
1080 file->Read(file_offset, (*stream_0_data)->data(), stream_0_size); | |
1105 if (bytes_read != stream_0_size) | 1081 if (bytes_read != stream_0_size) |
1106 return net::ERR_FAILED; | 1082 return net::ERR_FAILED; |
1107 | 1083 |
1108 // Check the CRC32. | 1084 // Check the CRC32. |
1109 uint32 expected_crc32 = | 1085 uint32 expected_crc32 = |
1110 stream_0_size == 0 | 1086 stream_0_size == 0 |
1111 ? crc32(0, Z_NULL, 0) | 1087 ? crc32(0, Z_NULL, 0) |
1112 : crc32(crc32(0, Z_NULL, 0), | 1088 : crc32(crc32(0, Z_NULL, 0), |
1113 reinterpret_cast<const Bytef*>((*stream_0_data)->data()), | 1089 reinterpret_cast<const Bytef*>((*stream_0_data)->data()), |
1114 stream_0_size); | 1090 stream_0_size); |
1115 if (has_crc32 && read_crc32 != expected_crc32) { | 1091 if (has_crc32 && read_crc32 != expected_crc32) { |
1116 DLOG(INFO) << "EOF record had bad crc."; | 1092 DVLOG(1) << "EOF record had bad crc."; |
1117 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | 1093 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); |
1118 return net::ERR_FAILED; | 1094 return net::ERR_FAILED; |
1119 } | 1095 } |
1120 *out_stream_0_crc32 = expected_crc32; | 1096 *out_stream_0_crc32 = expected_crc32; |
1121 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); | 1097 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); |
1122 return net::OK; | 1098 return net::OK; |
1123 } | 1099 } |
1124 | 1100 |
1125 int SimpleSynchronousEntry::GetEOFRecordData(int index, | 1101 int SimpleSynchronousEntry::GetEOFRecordData(int index, |
1126 const SimpleEntryStat& entry_stat, | 1102 const SimpleEntryStat& entry_stat, |
1127 bool* out_has_crc32, | 1103 bool* out_has_crc32, |
1128 uint32* out_crc32, | 1104 uint32* out_crc32, |
1129 int* out_data_size) const { | 1105 int* out_data_size) const { |
1130 SimpleFileEOF eof_record; | 1106 SimpleFileEOF eof_record; |
1131 int file_offset = entry_stat.GetEOFOffsetInFile(key_, index); | 1107 int file_offset = entry_stat.GetEOFOffsetInFile(key_, index); |
1132 int file_index = GetFileIndexFromStreamIndex(index); | 1108 int file_index = GetFileIndexFromStreamIndex(index); |
1133 if (ReadPlatformFile(files_[file_index], | 1109 File* file = const_cast<File*>(&files_[file_index]); |
1134 file_offset, | 1110 if (file->Read(file_offset, reinterpret_cast<char*>(&eof_record), |
1135 reinterpret_cast<char*>(&eof_record), | 1111 sizeof(eof_record)) != |
1136 sizeof(eof_record)) != sizeof(eof_record)) { | 1112 sizeof(eof_record)) { |
1137 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); | 1113 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); |
1138 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; | 1114 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; |
1139 } | 1115 } |
1140 | 1116 |
1141 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { | 1117 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { |
1142 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); | 1118 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); |
1143 DLOG(INFO) << "EOF record had bad magic number."; | 1119 DVLOG(1) << "EOF record had bad magic number."; |
1144 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; | 1120 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; |
1145 } | 1121 } |
1146 | 1122 |
1147 *out_has_crc32 = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) == | 1123 *out_has_crc32 = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) == |
1148 SimpleFileEOF::FLAG_HAS_CRC32; | 1124 SimpleFileEOF::FLAG_HAS_CRC32; |
1149 *out_crc32 = eof_record.data_crc32; | 1125 *out_crc32 = eof_record.data_crc32; |
1150 *out_data_size = eof_record.stream_size; | 1126 *out_data_size = eof_record.stream_size; |
1151 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, *out_has_crc32); | 1127 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, *out_has_crc32); |
1152 return net::OK; | 1128 return net::OK; |
1153 } | 1129 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1201 return path_.AppendASCII( | 1177 return path_.AppendASCII( |
1202 GetFilenameFromEntryHashAndFileIndex(entry_hash_, file_index)); | 1178 GetFilenameFromEntryHashAndFileIndex(entry_hash_, file_index)); |
1203 } | 1179 } |
1204 | 1180 |
1205 bool SimpleSynchronousEntry::OpenSparseFileIfExists( | 1181 bool SimpleSynchronousEntry::OpenSparseFileIfExists( |
1206 int32* out_sparse_data_size) { | 1182 int32* out_sparse_data_size) { |
1207 DCHECK(!sparse_file_open()); | 1183 DCHECK(!sparse_file_open()); |
1208 | 1184 |
1209 FilePath filename = path_.AppendASCII( | 1185 FilePath filename = path_.AppendASCII( |
1210 GetSparseFilenameFromEntryHash(entry_hash_)); | 1186 GetSparseFilenameFromEntryHash(entry_hash_)); |
1211 int flags = PLATFORM_FILE_OPEN | PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; | 1187 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; |
1212 bool created; | 1188 sparse_file_.Initialize(filename, flags); |
1213 PlatformFileError error; | 1189 if (sparse_file_.IsValid()) |
1214 sparse_file_ = CreatePlatformFile(filename, flags, &created, &error); | 1190 return ScanSparseFile(out_sparse_data_size); |
1215 if (error == PLATFORM_FILE_ERROR_NOT_FOUND) | |
1216 return true; | |
1217 | 1191 |
1218 return ScanSparseFile(out_sparse_data_size); | 1192 return sparse_file_.error_details() == File::FILE_ERROR_NOT_FOUND; |
1219 } | 1193 } |
1220 | 1194 |
1221 bool SimpleSynchronousEntry::CreateSparseFile() { | 1195 bool SimpleSynchronousEntry::CreateSparseFile() { |
1222 DCHECK(!sparse_file_open()); | 1196 DCHECK(!sparse_file_open()); |
1223 | 1197 |
1224 FilePath filename = path_.AppendASCII( | 1198 FilePath filename = path_.AppendASCII( |
1225 GetSparseFilenameFromEntryHash(entry_hash_)); | 1199 GetSparseFilenameFromEntryHash(entry_hash_)); |
1226 int flags = PLATFORM_FILE_CREATE | PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; | 1200 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE; |
1227 bool created; | 1201 sparse_file_.Initialize(filename, flags); |
1228 PlatformFileError error; | 1202 if (!sparse_file_.IsValid()) |
1229 sparse_file_ = CreatePlatformFile(filename, flags, &created, &error); | |
1230 if (error != PLATFORM_FILE_OK) | |
1231 return false; | 1203 return false; |
1232 | 1204 |
1233 return InitializeSparseFile(); | 1205 return InitializeSparseFile(); |
1234 } | 1206 } |
1235 | 1207 |
1236 bool SimpleSynchronousEntry::CloseSparseFile() { | 1208 void SimpleSynchronousEntry::CloseSparseFile() { |
1237 DCHECK(sparse_file_open()); | 1209 DCHECK(sparse_file_open()); |
1238 | 1210 sparse_file_.Close(); |
1239 bool did_close = ClosePlatformFile(sparse_file_); | |
1240 if (did_close) | |
1241 sparse_file_ = kInvalidPlatformFileValue; | |
1242 return did_close; | |
1243 } | 1211 } |
1244 | 1212 |
1245 bool SimpleSynchronousEntry::TruncateSparseFile() { | 1213 bool SimpleSynchronousEntry::TruncateSparseFile() { |
1246 DCHECK(sparse_file_open()); | 1214 DCHECK(sparse_file_open()); |
1247 | 1215 |
1248 int64 header_and_key_length = sizeof(SimpleFileHeader) + key_.size(); | 1216 int64 header_and_key_length = sizeof(SimpleFileHeader) + key_.size(); |
1249 if (!TruncatePlatformFile(sparse_file_, header_and_key_length)) { | 1217 if (!sparse_file_.SetLength(header_and_key_length)) { |
1250 DLOG(WARNING) << "Could not truncate sparse file"; | 1218 DLOG(WARNING) << "Could not truncate sparse file"; |
1251 return false; | 1219 return false; |
1252 } | 1220 } |
1253 | 1221 |
1254 sparse_ranges_.clear(); | 1222 sparse_ranges_.clear(); |
1255 | 1223 |
1256 return true; | 1224 return true; |
1257 } | 1225 } |
1258 | 1226 |
1259 bool SimpleSynchronousEntry::InitializeSparseFile() { | 1227 bool SimpleSynchronousEntry::InitializeSparseFile() { |
1260 DCHECK(sparse_file_open()); | 1228 DCHECK(sparse_file_open()); |
1261 | 1229 |
1262 SimpleFileHeader header; | 1230 SimpleFileHeader header; |
1263 header.initial_magic_number = kSimpleInitialMagicNumber; | 1231 header.initial_magic_number = kSimpleInitialMagicNumber; |
1264 header.version = kSimpleVersion; | 1232 header.version = kSimpleVersion; |
1265 header.key_length = key_.size(); | 1233 header.key_length = key_.size(); |
1266 header.key_hash = base::Hash(key_); | 1234 header.key_hash = base::Hash(key_); |
1267 | 1235 |
1268 int header_write_result = | 1236 int header_write_result = |
1269 WritePlatformFile(sparse_file_, 0, reinterpret_cast<char*>(&header), | 1237 sparse_file_.Write(0, reinterpret_cast<char*>(&header), sizeof(header)); |
1270 sizeof(header)); | |
1271 if (header_write_result != sizeof(header)) { | 1238 if (header_write_result != sizeof(header)) { |
1272 DLOG(WARNING) << "Could not write sparse file header"; | 1239 DLOG(WARNING) << "Could not write sparse file header"; |
1273 return false; | 1240 return false; |
1274 } | 1241 } |
1275 | 1242 |
1276 int key_write_result = WritePlatformFile(sparse_file_, sizeof(header), | 1243 int key_write_result = sparse_file_.Write(sizeof(header), key_.data(), |
1277 key_.data(), key_.size()); | 1244 key_.size()); |
1278 if (key_write_result != implicit_cast<int>(key_.size())) { | 1245 if (key_write_result != implicit_cast<int>(key_.size())) { |
1279 DLOG(WARNING) << "Could not write sparse file key"; | 1246 DLOG(WARNING) << "Could not write sparse file key"; |
1280 return false; | 1247 return false; |
1281 } | 1248 } |
1282 | 1249 |
1283 sparse_ranges_.clear(); | 1250 sparse_ranges_.clear(); |
1284 sparse_tail_offset_ = sizeof(header) + key_.size(); | 1251 sparse_tail_offset_ = sizeof(header) + key_.size(); |
1285 | 1252 |
1286 return true; | 1253 return true; |
1287 } | 1254 } |
1288 | 1255 |
1289 bool SimpleSynchronousEntry::ScanSparseFile(int32* out_sparse_data_size) { | 1256 bool SimpleSynchronousEntry::ScanSparseFile(int32* out_sparse_data_size) { |
1290 DCHECK(sparse_file_open()); | 1257 DCHECK(sparse_file_open()); |
1291 | 1258 |
1292 int32 sparse_data_size = 0; | 1259 int32 sparse_data_size = 0; |
1293 | 1260 |
1294 SimpleFileHeader header; | 1261 SimpleFileHeader header; |
1295 int header_read_result = | 1262 int header_read_result = |
1296 ReadPlatformFile(sparse_file_, 0, reinterpret_cast<char*>(&header), | 1263 sparse_file_.Read(0, reinterpret_cast<char*>(&header), sizeof(header)); |
1297 sizeof(header)); | |
1298 if (header_read_result != sizeof(header)) { | 1264 if (header_read_result != sizeof(header)) { |
1299 DLOG(WARNING) << "Could not read header from sparse file."; | 1265 DLOG(WARNING) << "Could not read header from sparse file."; |
1300 return false; | 1266 return false; |
1301 } | 1267 } |
1302 | 1268 |
1303 if (header.initial_magic_number != kSimpleInitialMagicNumber) { | 1269 if (header.initial_magic_number != kSimpleInitialMagicNumber) { |
1304 DLOG(WARNING) << "Sparse file magic number did not match."; | 1270 DLOG(WARNING) << "Sparse file magic number did not match."; |
1305 return false; | 1271 return false; |
1306 } | 1272 } |
1307 | 1273 |
1308 if (header.version != kSimpleVersion) { | 1274 if (header.version != kSimpleVersion) { |
1309 DLOG(WARNING) << "Sparse file unreadable version."; | 1275 DLOG(WARNING) << "Sparse file unreadable version."; |
1310 return false; | 1276 return false; |
1311 } | 1277 } |
1312 | 1278 |
1313 sparse_ranges_.clear(); | 1279 sparse_ranges_.clear(); |
1314 | 1280 |
1315 int64 range_header_offset = sizeof(header) + key_.size(); | 1281 int64 range_header_offset = sizeof(header) + key_.size(); |
1316 while (1) { | 1282 while (1) { |
1317 SimpleFileSparseRangeHeader range_header; | 1283 SimpleFileSparseRangeHeader range_header; |
1318 int range_header_read_result = | 1284 int range_header_read_result = |
1319 ReadPlatformFile(sparse_file_, | 1285 sparse_file_.Read(range_header_offset, |
1320 range_header_offset, | 1286 reinterpret_cast<char*>(&range_header), |
1321 reinterpret_cast<char*>(&range_header), | 1287 sizeof(range_header)); |
1322 sizeof(range_header)); | |
1323 if (range_header_read_result == 0) | 1288 if (range_header_read_result == 0) |
1324 break; | 1289 break; |
1325 if (range_header_read_result != sizeof(range_header)) { | 1290 if (range_header_read_result != sizeof(range_header)) { |
1326 DLOG(WARNING) << "Could not read sparse range header."; | 1291 DLOG(WARNING) << "Could not read sparse range header."; |
1327 return false; | 1292 return false; |
1328 } | 1293 } |
1329 | 1294 |
1330 if (range_header.sparse_range_magic_number != | 1295 if (range_header.sparse_range_magic_number != |
1331 kSimpleSparseRangeMagicNumber) { | 1296 kSimpleSparseRangeMagicNumber) { |
1332 DLOG(WARNING) << "Invalid sparse range header magic number."; | 1297 DLOG(WARNING) << "Invalid sparse range header magic number."; |
(...skipping 19 matching lines...) Expand all Loading... | |
1352 return true; | 1317 return true; |
1353 } | 1318 } |
1354 | 1319 |
1355 bool SimpleSynchronousEntry::ReadSparseRange(const SparseRange* range, | 1320 bool SimpleSynchronousEntry::ReadSparseRange(const SparseRange* range, |
1356 int offset, int len, char* buf) { | 1321 int offset, int len, char* buf) { |
1357 DCHECK(range); | 1322 DCHECK(range); |
1358 DCHECK(buf); | 1323 DCHECK(buf); |
1359 DCHECK_GE(range->length, offset); | 1324 DCHECK_GE(range->length, offset); |
1360 DCHECK_GE(range->length, offset + len); | 1325 DCHECK_GE(range->length, offset + len); |
1361 | 1326 |
1362 int bytes_read = ReadPlatformFile(sparse_file_, | 1327 int bytes_read = sparse_file_.Read(range->file_offset + offset, buf, len); |
1363 range->file_offset + offset, | |
1364 buf, len); | |
1365 if (bytes_read < len) { | 1328 if (bytes_read < len) { |
1366 DLOG(WARNING) << "Could not read sparse range."; | 1329 DLOG(WARNING) << "Could not read sparse range."; |
1367 return false; | 1330 return false; |
1368 } | 1331 } |
1369 | 1332 |
1370 // If we read the whole range and we have a crc32, check it. | 1333 // If we read the whole range and we have a crc32, check it. |
1371 if (offset == 0 && len == range->length && range->data_crc32 != 0) { | 1334 if (offset == 0 && len == range->length && range->data_crc32 != 0) { |
1372 uint32 actual_crc32 = crc32(crc32(0L, Z_NULL, 0), | 1335 uint32 actual_crc32 = crc32(crc32(0L, Z_NULL, 0), |
1373 reinterpret_cast<const Bytef*>(buf), | 1336 reinterpret_cast<const Bytef*>(buf), |
1374 len); | 1337 len); |
(...skipping 24 matching lines...) Expand all Loading... | |
1399 | 1362 |
1400 if (new_crc32 != range->data_crc32) { | 1363 if (new_crc32 != range->data_crc32) { |
1401 range->data_crc32 = new_crc32; | 1364 range->data_crc32 = new_crc32; |
1402 | 1365 |
1403 SimpleFileSparseRangeHeader header; | 1366 SimpleFileSparseRangeHeader header; |
1404 header.sparse_range_magic_number = kSimpleSparseRangeMagicNumber; | 1367 header.sparse_range_magic_number = kSimpleSparseRangeMagicNumber; |
1405 header.offset = range->offset; | 1368 header.offset = range->offset; |
1406 header.length = range->length; | 1369 header.length = range->length; |
1407 header.data_crc32 = range->data_crc32; | 1370 header.data_crc32 = range->data_crc32; |
1408 | 1371 |
1409 int bytes_written = WritePlatformFile(sparse_file_, | 1372 int bytes_written = sparse_file_.Write(range->file_offset - sizeof(header), |
1410 range->file_offset - sizeof(header), | 1373 reinterpret_cast<char*>(&header), |
1411 reinterpret_cast<char*>(&header), | 1374 sizeof(header)); |
1412 sizeof(header)); | |
1413 if (bytes_written != implicit_cast<int>(sizeof(header))) { | 1375 if (bytes_written != implicit_cast<int>(sizeof(header))) { |
1414 DLOG(WARNING) << "Could not rewrite sparse range header."; | 1376 DLOG(WARNING) << "Could not rewrite sparse range header."; |
1415 return false; | 1377 return false; |
1416 } | 1378 } |
1417 } | 1379 } |
1418 | 1380 |
1419 int bytes_written = WritePlatformFile(sparse_file_, | 1381 int bytes_written = sparse_file_.Write(range->file_offset + offset, buf, len); |
1420 range->file_offset + offset, | |
1421 buf, len); | |
1422 if (bytes_written < len) { | 1382 if (bytes_written < len) { |
1423 DLOG(WARNING) << "Could not write sparse range."; | 1383 DLOG(WARNING) << "Could not write sparse range."; |
1424 return false; | 1384 return false; |
1425 } | 1385 } |
1426 | 1386 |
1427 return true; | 1387 return true; |
1428 } | 1388 } |
1429 | 1389 |
1430 bool SimpleSynchronousEntry::AppendSparseRange(int64 offset, | 1390 bool SimpleSynchronousEntry::AppendSparseRange(int64 offset, |
1431 int len, | 1391 int len, |
1432 const char* buf) { | 1392 const char* buf) { |
1433 DCHECK_LE(0, offset); | 1393 DCHECK_LE(0, offset); |
1434 DCHECK_LT(0, len); | 1394 DCHECK_LT(0, len); |
1435 DCHECK(buf); | 1395 DCHECK(buf); |
1436 | 1396 |
1437 uint32 data_crc32 = crc32(crc32(0L, Z_NULL, 0), | 1397 uint32 data_crc32 = crc32(crc32(0L, Z_NULL, 0), |
1438 reinterpret_cast<const Bytef*>(buf), | 1398 reinterpret_cast<const Bytef*>(buf), |
1439 len); | 1399 len); |
1440 | 1400 |
1441 SimpleFileSparseRangeHeader header; | 1401 SimpleFileSparseRangeHeader header; |
1442 header.sparse_range_magic_number = kSimpleSparseRangeMagicNumber; | 1402 header.sparse_range_magic_number = kSimpleSparseRangeMagicNumber; |
1443 header.offset = offset; | 1403 header.offset = offset; |
1444 header.length = len; | 1404 header.length = len; |
1445 header.data_crc32 = data_crc32; | 1405 header.data_crc32 = data_crc32; |
1446 | 1406 |
1447 int bytes_written = WritePlatformFile(sparse_file_, | 1407 int bytes_written = sparse_file_.Write(sparse_tail_offset_, |
1448 sparse_tail_offset_, | 1408 reinterpret_cast<char*>(&header), |
1449 reinterpret_cast<char*>(&header), | 1409 sizeof(header)); |
1450 sizeof(header)); | |
1451 if (bytes_written != implicit_cast<int>(sizeof(header))) { | 1410 if (bytes_written != implicit_cast<int>(sizeof(header))) { |
1452 DLOG(WARNING) << "Could not append sparse range header."; | 1411 DLOG(WARNING) << "Could not append sparse range header."; |
1453 return false; | 1412 return false; |
1454 } | 1413 } |
1455 sparse_tail_offset_ += bytes_written; | 1414 sparse_tail_offset_ += bytes_written; |
1456 | 1415 |
1457 bytes_written = WritePlatformFile(sparse_file_, | 1416 bytes_written = sparse_file_.Write(sparse_tail_offset_, buf, len); |
1458 sparse_tail_offset_, | |
1459 buf, | |
1460 len); | |
1461 if (bytes_written < len) { | 1417 if (bytes_written < len) { |
1462 DLOG(WARNING) << "Could not append sparse range data."; | 1418 DLOG(WARNING) << "Could not append sparse range data."; |
1463 return false; | 1419 return false; |
1464 } | 1420 } |
1465 int64 data_file_offset = sparse_tail_offset_; | 1421 int64 data_file_offset = sparse_tail_offset_; |
1466 sparse_tail_offset_ += bytes_written; | 1422 sparse_tail_offset_ += bytes_written; |
1467 | 1423 |
1468 SparseRange range; | 1424 SparseRange range; |
1469 range.offset = offset; | 1425 range.offset = offset; |
1470 range.length = len; | 1426 range.length = len; |
1471 range.data_crc32 = data_crc32; | 1427 range.data_crc32 = data_crc32; |
1472 range.file_offset = data_file_offset; | 1428 range.file_offset = data_file_offset; |
1473 sparse_ranges_.insert(std::make_pair(offset, range)); | 1429 sparse_ranges_.insert(std::make_pair(offset, range)); |
1474 | 1430 |
1475 return true; | 1431 return true; |
1476 } | 1432 } |
1477 | 1433 |
1478 } // namespace disk_cache | 1434 } // namespace disk_cache |
OLD | NEW |