Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: net/disk_cache/simple/simple_synchronous_entry.cc

Issue 637023002: Misc. cleanup, primarily removing unused locals. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove macros.h change Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/files/file_util.h" 14 #include "base/files/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/numerics/safe_conversions.h"
17 #include "base/sha1.h" 18 #include "base/sha1.h"
18 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
19 #include "net/base/io_buffer.h" 20 #include "net/base/io_buffer.h"
20 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
21 #include "net/disk_cache/simple/simple_backend_version.h" 22 #include "net/disk_cache/simple/simple_backend_version.h"
22 #include "net/disk_cache/simple/simple_histogram_macros.h" 23 #include "net/disk_cache/simple/simple_histogram_macros.h"
23 #include "net/disk_cache/simple/simple_util.h" 24 #include "net/disk_cache/simple/simple_util.h"
24 #include "third_party/zlib/zlib.h" 25 #include "third_party/zlib/zlib.h"
25 26
26 using base::File; 27 using base::File;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 67
67 // Used in histograms, please only add entries at the end. 68 // Used in histograms, please only add entries at the end.
68 enum CloseResult { 69 enum CloseResult {
69 CLOSE_RESULT_SUCCESS, 70 CLOSE_RESULT_SUCCESS,
70 CLOSE_RESULT_WRITE_FAILURE, 71 CLOSE_RESULT_WRITE_FAILURE,
71 }; 72 };
72 73
73 void RecordSyncOpenResult(net::CacheType cache_type, 74 void RecordSyncOpenResult(net::CacheType cache_type,
74 OpenEntryResult result, 75 OpenEntryResult result,
75 bool had_index) { 76 bool had_index) {
76 DCHECK_GT(OPEN_ENTRY_MAX, result); 77 DCHECK_LT(result, OPEN_ENTRY_MAX);
77 SIMPLE_CACHE_UMA(ENUMERATION, 78 SIMPLE_CACHE_UMA(ENUMERATION,
78 "SyncOpenResult", cache_type, result, OPEN_ENTRY_MAX); 79 "SyncOpenResult", cache_type, result, OPEN_ENTRY_MAX);
79 if (had_index) { 80 if (had_index) {
80 SIMPLE_CACHE_UMA(ENUMERATION, 81 SIMPLE_CACHE_UMA(ENUMERATION,
81 "SyncOpenResult_WithIndex", cache_type, 82 "SyncOpenResult_WithIndex", cache_type,
82 result, OPEN_ENTRY_MAX); 83 result, OPEN_ENTRY_MAX);
83 } else { 84 } else {
84 SIMPLE_CACHE_UMA(ENUMERATION, 85 SIMPLE_CACHE_UMA(ENUMERATION,
85 "SyncOpenResult_WithoutIndex", cache_type, 86 "SyncOpenResult_WithoutIndex", cache_type,
86 result, OPEN_ENTRY_MAX); 87 result, OPEN_ENTRY_MAX);
(...skipping 10 matching lines...) Expand all
97 "SyncCheckEOFResult", cache_type, 98 "SyncCheckEOFResult", cache_type,
98 result, CHECK_EOF_RESULT_MAX); 99 result, CHECK_EOF_RESULT_MAX);
99 } 100 }
100 101
101 void RecordCloseResult(net::CacheType cache_type, CloseResult result) { 102 void RecordCloseResult(net::CacheType cache_type, CloseResult result) {
102 SIMPLE_CACHE_UMA(ENUMERATION, 103 SIMPLE_CACHE_UMA(ENUMERATION,
103 "SyncCloseResult", cache_type, result, WRITE_RESULT_MAX); 104 "SyncCloseResult", cache_type, result, WRITE_RESULT_MAX);
104 } 105 }
105 106
106 bool CanOmitEmptyFile(int file_index) { 107 bool CanOmitEmptyFile(int file_index) {
107 DCHECK_LE(0, file_index); 108 DCHECK_GE(file_index, 0);
rvargas (doing something else) 2014/10/08 20:01:14 Thanks for fixing all of these.
108 DCHECK_GT(disk_cache::kSimpleEntryFileCount, file_index); 109 DCHECK_LT(file_index, disk_cache::kSimpleEntryFileCount);
109 return file_index == disk_cache::simple_util::GetFileIndexFromStreamIndex(2); 110 return file_index == disk_cache::simple_util::GetFileIndexFromStreamIndex(2);
110 } 111 }
111 112
112 } // namespace 113 } // namespace
113 114
114 namespace disk_cache { 115 namespace disk_cache {
115 116
116 using simple_util::GetEntryHashKey; 117 using simple_util::GetEntryHashKey;
117 using simple_util::GetFilenameFromEntryHashAndFileIndex; 118 using simple_util::GetFilenameFromEntryHashAndFileIndex;
118 using simple_util::GetSparseFilenameFromEntryHash; 119 using simple_util::GetSparseFilenameFromEntryHash;
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 uint32* out_crc32, 278 uint32* out_crc32,
278 SimpleEntryStat* entry_stat, 279 SimpleEntryStat* entry_stat,
279 int* out_result) const { 280 int* out_result) const {
280 DCHECK(initialized_); 281 DCHECK(initialized_);
281 DCHECK_NE(0, in_entry_op.index); 282 DCHECK_NE(0, in_entry_op.index);
282 const int64 file_offset = 283 const int64 file_offset =
283 entry_stat->GetOffsetInFile(key_, in_entry_op.offset, in_entry_op.index); 284 entry_stat->GetOffsetInFile(key_, in_entry_op.offset, in_entry_op.index);
284 int file_index = GetFileIndexFromStreamIndex(in_entry_op.index); 285 int file_index = GetFileIndexFromStreamIndex(in_entry_op.index);
285 // Zero-length reads and reads to the empty streams of omitted files should 286 // Zero-length reads and reads to the empty streams of omitted files should
286 // be handled in the SimpleEntryImpl. 287 // be handled in the SimpleEntryImpl.
287 DCHECK_LT(0, in_entry_op.buf_len); 288 DCHECK_GT(in_entry_op.buf_len, 0);
288 DCHECK(!empty_file_omitted_[file_index]); 289 DCHECK(!empty_file_omitted_[file_index]);
289 File* file = const_cast<File*>(&files_[file_index]); 290 File* file = const_cast<File*>(&files_[file_index]);
290 int bytes_read = 291 int bytes_read =
291 file->Read(file_offset, out_buf->data(), in_entry_op.buf_len); 292 file->Read(file_offset, out_buf->data(), in_entry_op.buf_len);
292 if (bytes_read > 0) { 293 if (bytes_read > 0) {
293 entry_stat->set_last_used(Time::Now()); 294 entry_stat->set_last_used(Time::Now());
294 *out_crc32 = crc32(crc32(0L, Z_NULL, 0), 295 *out_crc32 = crc32(crc32(0L, Z_NULL, 0),
295 reinterpret_cast<const Bytef*>(out_buf->data()), 296 reinterpret_cast<const Bytef*>(out_buf->data()),
296 bytes_read); 297 bytes_read);
297 } 298 }
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 402
402 // Find the first sparse range at or after the requested offset. 403 // Find the first sparse range at or after the requested offset.
403 SparseRangeIterator it = sparse_ranges_.lower_bound(offset); 404 SparseRangeIterator it = sparse_ranges_.lower_bound(offset);
404 405
405 if (it != sparse_ranges_.begin()) { 406 if (it != sparse_ranges_.begin()) {
406 // Hop back one range and read the one overlapping with the start. 407 // Hop back one range and read the one overlapping with the start.
407 --it; 408 --it;
408 SparseRange* found_range = &it->second; 409 SparseRange* found_range = &it->second;
409 DCHECK_EQ(it->first, found_range->offset); 410 DCHECK_EQ(it->first, found_range->offset);
410 if (found_range->offset + found_range->length > offset) { 411 if (found_range->offset + found_range->length > offset) {
411 DCHECK_LE(0, found_range->length); 412 DCHECK_GE(found_range->length, 0);
412 DCHECK_GE(kint32max, found_range->length); 413 DCHECK_LE(found_range->length, kint32max);
413 DCHECK_LE(0, offset - found_range->offset); 414 DCHECK_GE(offset - found_range->offset, 0);
414 DCHECK_GE(kint32max, offset - found_range->offset); 415 DCHECK_LE(offset - found_range->offset, kint32max);
415 int range_len_after_offset = found_range->length - 416 int net_offset = static_cast<int>(offset - found_range->offset);
416 (offset - found_range->offset); 417 int range_len_after_offset =
417 DCHECK_LE(0, range_len_after_offset); 418 static_cast<int>(found_range->length - net_offset);
419 DCHECK_GE(range_len_after_offset, 0);
418 420
419 int len_to_read = std::min(buf_len, range_len_after_offset); 421 int len_to_read = std::min(buf_len, range_len_after_offset);
420 if (!ReadSparseRange(found_range, 422 if (!ReadSparseRange(found_range, net_offset, len_to_read, buf)) {
421 offset - found_range->offset,
422 len_to_read,
423 buf)) {
424 *out_result = net::ERR_CACHE_READ_FAILURE; 423 *out_result = net::ERR_CACHE_READ_FAILURE;
425 return; 424 return;
426 } 425 }
427 read_so_far += len_to_read; 426 read_so_far += len_to_read;
428 } 427 }
429 ++it; 428 ++it;
430 } 429 }
431 430
432 // Keep reading until the buffer is full or there is not another contiguous 431 // Keep reading until the buffer is full or there is not another contiguous
433 // range. 432 // range.
434 while (read_so_far < buf_len && 433 while (read_so_far < buf_len &&
435 it != sparse_ranges_.end() && 434 it != sparse_ranges_.end() &&
436 it->second.offset == offset + read_so_far) { 435 it->second.offset == offset + read_so_far) {
437 SparseRange* found_range = &it->second; 436 SparseRange* found_range = &it->second;
438 DCHECK_EQ(it->first, found_range->offset); 437 DCHECK_EQ(it->first, found_range->offset);
439 int range_len = (found_range->length > kint32max) ? 438 int range_len = base::saturated_cast<int>(found_range->length);
pasko 2015/12/09 18:41:06 Is this actually right? I am not very familiar wi
Peter Kasting 2015/12/09 22:07:23 saturated_cast is not checked_cast. This change d
440 kint32max : found_range->length;
441 int len_to_read = std::min(buf_len - read_so_far, range_len); 439 int len_to_read = std::min(buf_len - read_so_far, range_len);
442 if (!ReadSparseRange(found_range, 0, len_to_read, buf + read_so_far)) { 440 if (!ReadSparseRange(found_range, 0, len_to_read, buf + read_so_far)) {
443 *out_result = net::ERR_CACHE_READ_FAILURE; 441 *out_result = net::ERR_CACHE_READ_FAILURE;
444 return; 442 return;
445 } 443 }
446 read_so_far += len_to_read; 444 read_so_far += len_to_read;
447 ++it; 445 ++it;
448 } 446 }
449 447
450 *out_result = read_so_far; 448 *out_result = read_so_far;
(...skipping 26 matching lines...) Expand all
477 << buf_len << " > " << max_sparse_data_size << ")"; 475 << buf_len << " > " << max_sparse_data_size << ")";
478 TruncateSparseFile(); 476 TruncateSparseFile();
479 } 477 }
480 478
481 SparseRangeIterator it = sparse_ranges_.lower_bound(offset); 479 SparseRangeIterator it = sparse_ranges_.lower_bound(offset);
482 480
483 if (it != sparse_ranges_.begin()) { 481 if (it != sparse_ranges_.begin()) {
484 --it; 482 --it;
485 SparseRange* found_range = &it->second; 483 SparseRange* found_range = &it->second;
486 if (found_range->offset + found_range->length > offset) { 484 if (found_range->offset + found_range->length > offset) {
487 DCHECK_LE(0, found_range->length); 485 DCHECK_GE(found_range->length, 0);
488 DCHECK_GE(kint32max, found_range->length); 486 DCHECK_LE(found_range->length, kint32max);
489 DCHECK_LE(0, offset - found_range->offset); 487 DCHECK_GE(offset - found_range->offset, 0);
490 DCHECK_GE(kint32max, offset - found_range->offset); 488 DCHECK_LE(offset - found_range->offset, kint32max);
491 int range_len_after_offset = found_range->length - 489 int net_offset = static_cast<int>(offset - found_range->offset);
492 (offset - found_range->offset); 490 int range_len_after_offset =
493 DCHECK_LE(0, range_len_after_offset); 491 static_cast<int>(found_range->length - net_offset);
492 DCHECK_GE(range_len_after_offset, 0);
494 493
495 int len_to_write = std::min(buf_len, range_len_after_offset); 494 int len_to_write = std::min(buf_len, range_len_after_offset);
496 if (!WriteSparseRange(found_range, 495 if (!WriteSparseRange(found_range, net_offset, len_to_write, buf)) {
497 offset - found_range->offset,
498 len_to_write,
499 buf)) {
500 *out_result = net::ERR_CACHE_WRITE_FAILURE; 496 *out_result = net::ERR_CACHE_WRITE_FAILURE;
501 return; 497 return;
502 } 498 }
503 written_so_far += len_to_write; 499 written_so_far += len_to_write;
504 } 500 }
505 ++it; 501 ++it;
506 } 502 }
507 503
508 while (written_so_far < buf_len && 504 while (written_so_far < buf_len &&
509 it != sparse_ranges_.end() && 505 it != sparse_ranges_.end() &&
510 it->second.offset < offset + buf_len) { 506 it->second.offset < offset + buf_len) {
511 SparseRange* found_range = &it->second; 507 SparseRange* found_range = &it->second;
512 if (offset + written_so_far < found_range->offset) { 508 if (offset + written_so_far < found_range->offset) {
513 int len_to_append = found_range->offset - (offset + written_so_far); 509 int len_to_append =
510 static_cast<int>(found_range->offset - (offset + written_so_far));
514 if (!AppendSparseRange(offset + written_so_far, 511 if (!AppendSparseRange(offset + written_so_far,
515 len_to_append, 512 len_to_append,
516 buf + written_so_far)) { 513 buf + written_so_far)) {
517 *out_result = net::ERR_CACHE_WRITE_FAILURE; 514 *out_result = net::ERR_CACHE_WRITE_FAILURE;
518 return; 515 return;
519 } 516 }
520 written_so_far += len_to_append; 517 written_so_far += len_to_append;
521 appended_so_far += len_to_append; 518 appended_so_far += len_to_append;
522 } 519 }
523 int range_len = (found_range->length > kint32max) ? 520 int range_len = base::saturated_cast<int>(found_range->length);
524 kint32max : found_range->length;
525 int len_to_write = std::min(buf_len - written_so_far, range_len); 521 int len_to_write = std::min(buf_len - written_so_far, range_len);
526 if (!WriteSparseRange(found_range, 522 if (!WriteSparseRange(found_range,
527 0, 523 0,
528 len_to_write, 524 len_to_write,
529 buf + written_so_far)) { 525 buf + written_so_far)) {
530 *out_result = net::ERR_CACHE_WRITE_FAILURE; 526 *out_result = net::ERR_CACHE_WRITE_FAILURE;
531 return; 527 return;
532 } 528 }
533 written_so_far += len_to_write; 529 written_so_far += len_to_write;
534 ++it; 530 ++it;
(...skipping 25 matching lines...) Expand all
560 const EntryOperationData& in_entry_op, 556 const EntryOperationData& in_entry_op,
561 int64* out_start, 557 int64* out_start,
562 int* out_result) { 558 int* out_result) {
563 DCHECK(initialized_); 559 DCHECK(initialized_);
564 int64 offset = in_entry_op.sparse_offset; 560 int64 offset = in_entry_op.sparse_offset;
565 int len = in_entry_op.buf_len; 561 int len = in_entry_op.buf_len;
566 562
567 SparseRangeIterator it = sparse_ranges_.lower_bound(offset); 563 SparseRangeIterator it = sparse_ranges_.lower_bound(offset);
568 564
569 int64 start = offset; 565 int64 start = offset;
570 int avail_so_far = 0; 566 int64 avail_so_far = 0;
Peter Kasting 2014/10/08 19:21:56 The type changes and typecasts inserted in this fi
571 567
572 if (it != sparse_ranges_.end() && it->second.offset < offset + len) 568 if (it != sparse_ranges_.end() && it->second.offset < offset + len)
573 start = it->second.offset; 569 start = it->second.offset;
574 570
575 if ((it == sparse_ranges_.end() || it->second.offset > offset) && 571 if ((it == sparse_ranges_.end() || it->second.offset > offset) &&
576 it != sparse_ranges_.begin()) { 572 it != sparse_ranges_.begin()) {
577 --it; 573 --it;
578 if (it->second.offset + it->second.length > offset) { 574 if (it->second.offset + it->second.length > offset) {
579 start = offset; 575 start = offset;
580 avail_so_far = (it->second.offset + it->second.length) - offset; 576 avail_so_far = (it->second.offset + it->second.length) - offset;
581 } 577 }
582 ++it; 578 ++it;
583 } 579 }
584 580
585 while (start + avail_so_far < offset + len && 581 while (start + avail_so_far < offset + len &&
586 it != sparse_ranges_.end() && 582 it != sparse_ranges_.end() &&
587 it->second.offset == start + avail_so_far) { 583 it->second.offset == start + avail_so_far) {
588 avail_so_far += it->second.length; 584 avail_so_far += it->second.length;
589 ++it; 585 ++it;
590 } 586 }
591 587
592 int len_from_start = len - (start - offset); 588 int64 len_from_start = len - (start - offset);
593 *out_start = start; 589 *out_start = start;
594 *out_result = std::min(avail_so_far, len_from_start); 590 *out_result = static_cast<int>(std::min(avail_so_far, len_from_start));
595 } 591 }
596 592
597 void SimpleSynchronousEntry::CheckEOFRecord(int index, 593 void SimpleSynchronousEntry::CheckEOFRecord(int index,
598 const SimpleEntryStat& entry_stat, 594 const SimpleEntryStat& entry_stat,
599 uint32 expected_crc32, 595 uint32 expected_crc32,
600 int* out_result) const { 596 int* out_result) const {
601 DCHECK(initialized_); 597 DCHECK(initialized_);
602 uint32 crc32; 598 uint32 crc32;
603 bool has_crc32; 599 bool has_crc32;
604 int stream_size; 600 int stream_size;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 continue; 669 continue;
674 670
675 files_[i].Close(); 671 files_[i].Close();
676 const int64 file_size = entry_stat.GetFileSize(key_, i); 672 const int64 file_size = entry_stat.GetFileSize(key_, i);
677 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, 673 SIMPLE_CACHE_UMA(CUSTOM_COUNTS,
678 "LastClusterSize", cache_type_, 674 "LastClusterSize", cache_type_,
679 file_size % 4096, 0, 4097, 50); 675 file_size % 4096, 0, 4097, 50);
680 const int64 cluster_loss = file_size % 4096 ? 4096 - file_size % 4096 : 0; 676 const int64 cluster_loss = file_size % 4096 ? 4096 - file_size % 4096 : 0;
681 SIMPLE_CACHE_UMA(PERCENTAGE, 677 SIMPLE_CACHE_UMA(PERCENTAGE,
682 "LastClusterLossPercent", cache_type_, 678 "LastClusterLossPercent", cache_type_,
683 cluster_loss * 100 / (cluster_loss + file_size)); 679 static_cast<base::HistogramBase::Sample>(
680 cluster_loss * 100 / (cluster_loss + file_size)));
684 } 681 }
685 682
686 if (sparse_file_open()) 683 if (sparse_file_open())
687 sparse_file_.Close(); 684 sparse_file_.Close();
688 685
689 if (files_created_) { 686 if (files_created_) {
690 const int stream2_file_index = GetFileIndexFromStreamIndex(2); 687 const int stream2_file_index = GetFileIndexFromStreamIndex(2);
691 SIMPLE_CACHE_UMA(BOOLEAN, "EntryCreatedAndStream2Omitted", cache_type_, 688 SIMPLE_CACHE_UMA(BOOLEAN, "EntryCreatedAndStream2Omitted", cache_type_,
692 empty_file_omitted_[stream2_file_index]); 689 empty_file_omitted_[stream2_file_index]);
693 } 690 }
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
818 // each will only be known when reading the EOF record for stream 0. 815 // each will only be known when reading the EOF record for stream 0.
819 // 816 //
820 // The size for file 0 and 1 is temporarily kept in 817 // The size for file 0 and 1 is temporarily kept in
821 // |data_size(1)| and |data_size(2)| respectively. Reading the key in 818 // |data_size(1)| and |data_size(2)| respectively. Reading the key in
822 // InitializeForOpen yields the data size for each file. In the case of 819 // InitializeForOpen yields the data size for each file. In the case of
823 // file hash_1, this is the total size of stream 2, and is assigned to 820 // file hash_1, this is the total size of stream 2, and is assigned to
824 // data_size(2). In the case of file 0, it is the combined size of stream 821 // data_size(2). In the case of file 0, it is the combined size of stream
825 // 0, stream 1 and one EOF record. The exact distribution of sizes between 822 // 0, stream 1 and one EOF record. The exact distribution of sizes between
826 // stream 1 and stream 0 is only determined after reading the EOF record 823 // stream 1 and stream 0 is only determined after reading the EOF record
827 // for stream 0 in ReadAndValidateStream0. 824 // for stream 0 in ReadAndValidateStream0.
828 out_entry_stat->set_data_size(i + 1, file_info.size); 825 out_entry_stat->set_data_size(i + 1, static_cast<int>(file_info.size));
829 } 826 }
830 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, 827 SIMPLE_CACHE_UMA(CUSTOM_COUNTS,
831 "SyncOpenEntryAge", cache_type_, 828 "SyncOpenEntryAge", cache_type_,
832 entry_age.InHours(), 1, 1000, 50); 829 entry_age.InHours(), 1, 1000, 50);
833 830
834 files_created_ = false; 831 files_created_ = false;
835 832
836 return true; 833 return true;
837 } 834 }
838 835
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
1152 result = false; 1149 result = false;
1153 } 1150 }
1154 FilePath to_delete = path.AppendASCII( 1151 FilePath to_delete = path.AppendASCII(
1155 GetSparseFilenameFromEntryHash(entry_hash)); 1152 GetSparseFilenameFromEntryHash(entry_hash));
1156 base::DeleteFile(to_delete, false); 1153 base::DeleteFile(to_delete, false);
1157 return result; 1154 return result;
1158 } 1155 }
1159 1156
1160 void SimpleSynchronousEntry::RecordSyncCreateResult(CreateEntryResult result, 1157 void SimpleSynchronousEntry::RecordSyncCreateResult(CreateEntryResult result,
1161 bool had_index) { 1158 bool had_index) {
1162 DCHECK_GT(CREATE_ENTRY_MAX, result); 1159 DCHECK_LT(result, CREATE_ENTRY_MAX);
1163 SIMPLE_CACHE_UMA(ENUMERATION, 1160 SIMPLE_CACHE_UMA(ENUMERATION,
1164 "SyncCreateResult", cache_type_, result, CREATE_ENTRY_MAX); 1161 "SyncCreateResult", cache_type_, result, CREATE_ENTRY_MAX);
1165 if (had_index) { 1162 if (had_index) {
1166 SIMPLE_CACHE_UMA(ENUMERATION, 1163 SIMPLE_CACHE_UMA(ENUMERATION,
1167 "SyncCreateResult_WithIndex", cache_type_, 1164 "SyncCreateResult_WithIndex", cache_type_,
1168 result, CREATE_ENTRY_MAX); 1165 result, CREATE_ENTRY_MAX);
1169 } else { 1166 } else {
1170 SIMPLE_CACHE_UMA(ENUMERATION, 1167 SIMPLE_CACHE_UMA(ENUMERATION,
1171 "SyncCreateResult_WithoutIndex", cache_type_, 1168 "SyncCreateResult_WithoutIndex", cache_type_,
1172 result, CREATE_ENTRY_MAX); 1169 result, CREATE_ENTRY_MAX);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1249 1246
1250 sparse_ranges_.clear(); 1247 sparse_ranges_.clear();
1251 sparse_tail_offset_ = sizeof(header) + key_.size(); 1248 sparse_tail_offset_ = sizeof(header) + key_.size();
1252 1249
1253 return true; 1250 return true;
1254 } 1251 }
1255 1252
1256 bool SimpleSynchronousEntry::ScanSparseFile(int32* out_sparse_data_size) { 1253 bool SimpleSynchronousEntry::ScanSparseFile(int32* out_sparse_data_size) {
1257 DCHECK(sparse_file_open()); 1254 DCHECK(sparse_file_open());
1258 1255
1259 int32 sparse_data_size = 0; 1256 int64 sparse_data_size = 0;
1260 1257
1261 SimpleFileHeader header; 1258 SimpleFileHeader header;
1262 int header_read_result = 1259 int header_read_result =
1263 sparse_file_.Read(0, reinterpret_cast<char*>(&header), sizeof(header)); 1260 sparse_file_.Read(0, reinterpret_cast<char*>(&header), sizeof(header));
1264 if (header_read_result != sizeof(header)) { 1261 if (header_read_result != sizeof(header)) {
1265 DLOG(WARNING) << "Could not read header from sparse file."; 1262 DLOG(WARNING) << "Could not read header from sparse file.";
1266 return false; 1263 return false;
1267 } 1264 }
1268 1265
1269 if (header.initial_magic_number != kSimpleInitialMagicNumber) { 1266 if (header.initial_magic_number != kSimpleInitialMagicNumber) {
(...skipping 30 matching lines...) Expand all
1300 1297
1301 SparseRange range; 1298 SparseRange range;
1302 range.offset = range_header.offset; 1299 range.offset = range_header.offset;
1303 range.length = range_header.length; 1300 range.length = range_header.length;
1304 range.data_crc32 = range_header.data_crc32; 1301 range.data_crc32 = range_header.data_crc32;
1305 range.file_offset = range_header_offset + sizeof(range_header); 1302 range.file_offset = range_header_offset + sizeof(range_header);
1306 sparse_ranges_.insert(std::make_pair(range.offset, range)); 1303 sparse_ranges_.insert(std::make_pair(range.offset, range));
1307 1304
1308 range_header_offset += sizeof(range_header) + range.length; 1305 range_header_offset += sizeof(range_header) + range.length;
1309 1306
1310 DCHECK_LE(sparse_data_size, sparse_data_size + range.length); 1307 DCHECK_GE(sparse_data_size + range.length, sparse_data_size);
1311 sparse_data_size += range.length; 1308 sparse_data_size += range.length;
1312 } 1309 }
1313 1310
1314 *out_sparse_data_size = sparse_data_size; 1311 *out_sparse_data_size = static_cast<int32>(sparse_data_size);
1315 sparse_tail_offset_ = range_header_offset; 1312 sparse_tail_offset_ = range_header_offset;
1316 1313
1317 return true; 1314 return true;
1318 } 1315 }
1319 1316
1320 bool SimpleSynchronousEntry::ReadSparseRange(const SparseRange* range, 1317 bool SimpleSynchronousEntry::ReadSparseRange(const SparseRange* range,
1321 int offset, int len, char* buf) { 1318 int offset, int len, char* buf) {
1322 DCHECK(range); 1319 DCHECK(range);
1323 DCHECK(buf); 1320 DCHECK(buf);
1324 DCHECK_GE(range->length, offset); 1321 DCHECK_LE(offset, range->length);
1325 DCHECK_GE(range->length, offset + len); 1322 DCHECK_LE(offset + len, range->length);
1326 1323
1327 int bytes_read = sparse_file_.Read(range->file_offset + offset, buf, len); 1324 int bytes_read = sparse_file_.Read(range->file_offset + offset, buf, len);
1328 if (bytes_read < len) { 1325 if (bytes_read < len) {
1329 DLOG(WARNING) << "Could not read sparse range."; 1326 DLOG(WARNING) << "Could not read sparse range.";
1330 return false; 1327 return false;
1331 } 1328 }
1332 1329
1333 // If we read the whole range and we have a crc32, check it. 1330 // If we read the whole range and we have a crc32, check it.
1334 if (offset == 0 && len == range->length && range->data_crc32 != 0) { 1331 if (offset == 0 && len == range->length && range->data_crc32 != 0) {
1335 uint32 actual_crc32 = crc32(crc32(0L, Z_NULL, 0), 1332 uint32 actual_crc32 = crc32(crc32(0L, Z_NULL, 0),
1336 reinterpret_cast<const Bytef*>(buf), 1333 reinterpret_cast<const Bytef*>(buf),
1337 len); 1334 len);
1338 if (actual_crc32 != range->data_crc32) { 1335 if (actual_crc32 != range->data_crc32) {
1339 DLOG(WARNING) << "Sparse range crc32 mismatch."; 1336 DLOG(WARNING) << "Sparse range crc32 mismatch.";
1340 return false; 1337 return false;
1341 } 1338 }
1342 } 1339 }
1343 // TODO(ttuttle): Incremental crc32 calculation? 1340 // TODO(ttuttle): Incremental crc32 calculation?
1344 1341
1345 return true; 1342 return true;
1346 } 1343 }
1347 1344
1348 bool SimpleSynchronousEntry::WriteSparseRange(SparseRange* range, 1345 bool SimpleSynchronousEntry::WriteSparseRange(SparseRange* range,
1349 int offset, int len, 1346 int offset, int len,
1350 const char* buf) { 1347 const char* buf) {
1351 DCHECK(range); 1348 DCHECK(range);
1352 DCHECK(buf); 1349 DCHECK(buf);
1353 DCHECK_GE(range->length, offset); 1350 DCHECK_LE(offset, range->length);
1354 DCHECK_GE(range->length, offset + len); 1351 DCHECK_LE(offset + len, range->length);
1355 1352
1356 uint32 new_crc32 = 0; 1353 uint32 new_crc32 = 0;
1357 if (offset == 0 && len == range->length) { 1354 if (offset == 0 && len == range->length) {
1358 new_crc32 = crc32(crc32(0L, Z_NULL, 0), 1355 new_crc32 = crc32(crc32(0L, Z_NULL, 0),
1359 reinterpret_cast<const Bytef*>(buf), 1356 reinterpret_cast<const Bytef*>(buf),
1360 len); 1357 len);
1361 } 1358 }
1362 1359
1363 if (new_crc32 != range->data_crc32) { 1360 if (new_crc32 != range->data_crc32) {
1364 range->data_crc32 = new_crc32; 1361 range->data_crc32 = new_crc32;
(...skipping 18 matching lines...) Expand all
1383 DLOG(WARNING) << "Could not write sparse range."; 1380 DLOG(WARNING) << "Could not write sparse range.";
1384 return false; 1381 return false;
1385 } 1382 }
1386 1383
1387 return true; 1384 return true;
1388 } 1385 }
1389 1386
1390 bool SimpleSynchronousEntry::AppendSparseRange(int64 offset, 1387 bool SimpleSynchronousEntry::AppendSparseRange(int64 offset,
1391 int len, 1388 int len,
1392 const char* buf) { 1389 const char* buf) {
1393 DCHECK_LE(0, offset); 1390 DCHECK_GE(offset, 0);
1394 DCHECK_LT(0, len); 1391 DCHECK_GT(len, 0);
1395 DCHECK(buf); 1392 DCHECK(buf);
1396 1393
1397 uint32 data_crc32 = crc32(crc32(0L, Z_NULL, 0), 1394 uint32 data_crc32 = crc32(crc32(0L, Z_NULL, 0),
1398 reinterpret_cast<const Bytef*>(buf), 1395 reinterpret_cast<const Bytef*>(buf),
1399 len); 1396 len);
1400 1397
1401 SimpleFileSparseRangeHeader header; 1398 SimpleFileSparseRangeHeader header;
1402 header.sparse_range_magic_number = kSimpleSparseRangeMagicNumber; 1399 header.sparse_range_magic_number = kSimpleSparseRangeMagicNumber;
1403 header.offset = offset; 1400 header.offset = offset;
1404 header.length = len; 1401 header.length = len;
(...skipping 20 matching lines...) Expand all
1425 range.offset = offset; 1422 range.offset = offset;
1426 range.length = len; 1423 range.length = len;
1427 range.data_crc32 = data_crc32; 1424 range.data_crc32 = data_crc32;
1428 range.file_offset = data_file_offset; 1425 range.file_offset = data_file_offset;
1429 sparse_ranges_.insert(std::make_pair(offset, range)); 1426 sparse_ranges_.insert(std::make_pair(offset, range));
1430 1427
1431 return true; 1428 return true;
1432 } 1429 }
1433 1430
1434 } // namespace disk_cache 1431 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698