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

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

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 | Annotate | Revision Log
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>
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 // Used in histograms, please only add entries at the end. 67 // Used in histograms, please only add entries at the end.
68 enum CloseResult { 68 enum CloseResult {
69 CLOSE_RESULT_SUCCESS, 69 CLOSE_RESULT_SUCCESS,
70 CLOSE_RESULT_WRITE_FAILURE, 70 CLOSE_RESULT_WRITE_FAILURE,
71 }; 71 };
72 72
73 void RecordSyncOpenResult(net::CacheType cache_type, 73 void RecordSyncOpenResult(net::CacheType cache_type,
74 OpenEntryResult result, 74 OpenEntryResult result,
75 bool had_index) { 75 bool had_index) {
76 DCHECK_GT(OPEN_ENTRY_MAX, result); 76 DCHECK_GT(OPEN_ENTRY_MAX, result);
77 SIMPLE_CACHE_UMA(ENUMERATION, 77 SIMPLE_CACHE_UMA(
78 "SyncOpenResult", cache_type, result, OPEN_ENTRY_MAX); 78 ENUMERATION, "SyncOpenResult", cache_type, result, OPEN_ENTRY_MAX);
79 if (had_index) { 79 if (had_index) {
80 SIMPLE_CACHE_UMA(ENUMERATION, 80 SIMPLE_CACHE_UMA(ENUMERATION,
81 "SyncOpenResult_WithIndex", cache_type, 81 "SyncOpenResult_WithIndex",
82 result, OPEN_ENTRY_MAX); 82 cache_type,
83 result,
84 OPEN_ENTRY_MAX);
83 } else { 85 } else {
84 SIMPLE_CACHE_UMA(ENUMERATION, 86 SIMPLE_CACHE_UMA(ENUMERATION,
85 "SyncOpenResult_WithoutIndex", cache_type, 87 "SyncOpenResult_WithoutIndex",
86 result, OPEN_ENTRY_MAX); 88 cache_type,
89 result,
90 OPEN_ENTRY_MAX);
87 } 91 }
88 } 92 }
89 93
90 void RecordWriteResult(net::CacheType cache_type, WriteResult result) { 94 void RecordWriteResult(net::CacheType cache_type, WriteResult result) {
91 SIMPLE_CACHE_UMA(ENUMERATION, 95 SIMPLE_CACHE_UMA(
92 "SyncWriteResult", cache_type, result, WRITE_RESULT_MAX); 96 ENUMERATION, "SyncWriteResult", cache_type, result, WRITE_RESULT_MAX);
93 } 97 }
94 98
95 void RecordCheckEOFResult(net::CacheType cache_type, CheckEOFResult result) { 99 void RecordCheckEOFResult(net::CacheType cache_type, CheckEOFResult result) {
96 SIMPLE_CACHE_UMA(ENUMERATION, 100 SIMPLE_CACHE_UMA(ENUMERATION,
97 "SyncCheckEOFResult", cache_type, 101 "SyncCheckEOFResult",
98 result, CHECK_EOF_RESULT_MAX); 102 cache_type,
103 result,
104 CHECK_EOF_RESULT_MAX);
99 } 105 }
100 106
101 void RecordCloseResult(net::CacheType cache_type, CloseResult result) { 107 void RecordCloseResult(net::CacheType cache_type, CloseResult result) {
102 SIMPLE_CACHE_UMA(ENUMERATION, 108 SIMPLE_CACHE_UMA(
103 "SyncCloseResult", cache_type, result, WRITE_RESULT_MAX); 109 ENUMERATION, "SyncCloseResult", cache_type, result, WRITE_RESULT_MAX);
104 } 110 }
105 111
106 bool CanOmitEmptyFile(int file_index) { 112 bool CanOmitEmptyFile(int file_index) {
107 DCHECK_LE(0, file_index); 113 DCHECK_LE(0, file_index);
108 DCHECK_GT(disk_cache::kSimpleEntryFileCount, file_index); 114 DCHECK_GT(disk_cache::kSimpleEntryFileCount, file_index);
109 return file_index == disk_cache::simple_util::GetFileIndexFromStreamIndex(2); 115 return file_index == disk_cache::simple_util::GetFileIndexFromStreamIndex(2);
110 } 116 }
111 117
112 } // namespace 118 } // namespace
113 119
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 SimpleEntryStat entry_stat) 170 SimpleEntryStat entry_stat)
165 : sync_entry(NULL), 171 : sync_entry(NULL),
166 entry_stat(entry_stat), 172 entry_stat(entry_stat),
167 stream_0_crc32(crc32(0, Z_NULL, 0)), 173 stream_0_crc32(crc32(0, Z_NULL, 0)),
168 result(net::OK) { 174 result(net::OK) {
169 } 175 }
170 176
171 SimpleEntryCreationResults::~SimpleEntryCreationResults() { 177 SimpleEntryCreationResults::~SimpleEntryCreationResults() {
172 } 178 }
173 179
174 SimpleSynchronousEntry::CRCRecord::CRCRecord() : index(-1), 180 SimpleSynchronousEntry::CRCRecord::CRCRecord()
175 has_crc32(false), 181 : index(-1), has_crc32(false), data_crc32(0) {
176 data_crc32(0) {
177 } 182 }
178 183
179 SimpleSynchronousEntry::CRCRecord::CRCRecord(int index_p, 184 SimpleSynchronousEntry::CRCRecord::CRCRecord(int index_p,
180 bool has_crc32_p, 185 bool has_crc32_p,
181 uint32 data_crc32_p) 186 uint32 data_crc32_p)
182 : index(index_p), 187 : index(index_p), has_crc32(has_crc32_p), data_crc32(data_crc32_p) {
183 has_crc32(has_crc32_p), 188 }
184 data_crc32(data_crc32_p) {}
185 189
186 SimpleSynchronousEntry::EntryOperationData::EntryOperationData(int index_p, 190 SimpleSynchronousEntry::EntryOperationData::EntryOperationData(int index_p,
187 int offset_p, 191 int offset_p,
188 int buf_len_p) 192 int buf_len_p)
189 : index(index_p), 193 : index(index_p), offset(offset_p), buf_len(buf_len_p) {
190 offset(offset_p), 194 }
191 buf_len(buf_len_p) {}
192 195
193 SimpleSynchronousEntry::EntryOperationData::EntryOperationData(int index_p, 196 SimpleSynchronousEntry::EntryOperationData::EntryOperationData(int index_p,
194 int offset_p, 197 int offset_p,
195 int buf_len_p, 198 int buf_len_p,
196 bool truncate_p, 199 bool truncate_p,
197 bool doomed_p) 200 bool doomed_p)
198 : index(index_p), 201 : index(index_p),
199 offset(offset_p), 202 offset(offset_p),
200 buf_len(buf_len_p), 203 buf_len(buf_len_p),
201 truncate(truncate_p), 204 truncate(truncate_p),
202 doomed(doomed_p) {} 205 doomed(doomed_p) {
206 }
203 207
204 SimpleSynchronousEntry::EntryOperationData::EntryOperationData( 208 SimpleSynchronousEntry::EntryOperationData::EntryOperationData(
205 int64 sparse_offset_p, 209 int64 sparse_offset_p,
206 int buf_len_p) 210 int buf_len_p)
207 : sparse_offset(sparse_offset_p), 211 : sparse_offset(sparse_offset_p), buf_len(buf_len_p) {
208 buf_len(buf_len_p) {} 212 }
209 213
210 // static 214 // static
211 void SimpleSynchronousEntry::OpenEntry( 215 void SimpleSynchronousEntry::OpenEntry(
212 net::CacheType cache_type, 216 net::CacheType cache_type,
213 const FilePath& path, 217 const FilePath& path,
214 const uint64 entry_hash, 218 const uint64 entry_hash,
215 bool had_index, 219 bool had_index,
216 SimpleEntryCreationResults *out_results) { 220 SimpleEntryCreationResults* out_results) {
217 SimpleSynchronousEntry* sync_entry = 221 SimpleSynchronousEntry* sync_entry =
218 new SimpleSynchronousEntry(cache_type, path, "", entry_hash); 222 new SimpleSynchronousEntry(cache_type, path, "", entry_hash);
219 out_results->result = 223 out_results->result =
220 sync_entry->InitializeForOpen(had_index, 224 sync_entry->InitializeForOpen(had_index,
221 &out_results->entry_stat, 225 &out_results->entry_stat,
222 &out_results->stream_0_data, 226 &out_results->stream_0_data,
223 &out_results->stream_0_crc32); 227 &out_results->stream_0_crc32);
224 if (out_results->result != net::OK) { 228 if (out_results->result != net::OK) {
225 sync_entry->Doom(); 229 sync_entry->Doom();
226 delete sync_entry; 230 delete sync_entry;
227 out_results->sync_entry = NULL; 231 out_results->sync_entry = NULL;
228 out_results->stream_0_data = NULL; 232 out_results->stream_0_data = NULL;
229 return; 233 return;
230 } 234 }
231 out_results->sync_entry = sync_entry; 235 out_results->sync_entry = sync_entry;
232 } 236 }
233 237
234 // static 238 // static
235 void SimpleSynchronousEntry::CreateEntry( 239 void SimpleSynchronousEntry::CreateEntry(
236 net::CacheType cache_type, 240 net::CacheType cache_type,
237 const FilePath& path, 241 const FilePath& path,
238 const std::string& key, 242 const std::string& key,
239 const uint64 entry_hash, 243 const uint64 entry_hash,
240 bool had_index, 244 bool had_index,
241 SimpleEntryCreationResults *out_results) { 245 SimpleEntryCreationResults* out_results) {
242 DCHECK_EQ(entry_hash, GetEntryHashKey(key)); 246 DCHECK_EQ(entry_hash, GetEntryHashKey(key));
243 SimpleSynchronousEntry* sync_entry = 247 SimpleSynchronousEntry* sync_entry =
244 new SimpleSynchronousEntry(cache_type, path, key, entry_hash); 248 new SimpleSynchronousEntry(cache_type, path, key, entry_hash);
245 out_results->result = sync_entry->InitializeForCreate( 249 out_results->result =
246 had_index, &out_results->entry_stat); 250 sync_entry->InitializeForCreate(had_index, &out_results->entry_stat);
247 if (out_results->result != net::OK) { 251 if (out_results->result != net::OK) {
248 if (out_results->result != net::ERR_FILE_EXISTS) 252 if (out_results->result != net::ERR_FILE_EXISTS)
249 sync_entry->Doom(); 253 sync_entry->Doom();
250 delete sync_entry; 254 delete sync_entry;
251 out_results->sync_entry = NULL; 255 out_results->sync_entry = NULL;
252 return; 256 return;
253 } 257 }
254 out_results->sync_entry = sync_entry; 258 out_results->sync_entry = sync_entry;
255 } 259 }
256 260
257 // static 261 // static
258 int SimpleSynchronousEntry::DoomEntry( 262 int SimpleSynchronousEntry::DoomEntry(const FilePath& path, uint64 entry_hash) {
259 const FilePath& path,
260 uint64 entry_hash) {
261 const bool deleted_well = DeleteFilesForEntryHash(path, entry_hash); 263 const bool deleted_well = DeleteFilesForEntryHash(path, entry_hash);
262 return deleted_well ? net::OK : net::ERR_FAILED; 264 return deleted_well ? net::OK : net::ERR_FAILED;
263 } 265 }
264 266
265 // static 267 // static
266 int SimpleSynchronousEntry::DoomEntrySet( 268 int SimpleSynchronousEntry::DoomEntrySet(const std::vector<uint64>* key_hashes,
267 const std::vector<uint64>* key_hashes, 269 const FilePath& path) {
268 const FilePath& path) {
269 const size_t did_delete_count = std::count_if( 270 const size_t did_delete_count = std::count_if(
270 key_hashes->begin(), key_hashes->end(), std::bind1st( 271 key_hashes->begin(),
272 key_hashes->end(),
273 std::bind1st(
271 std::ptr_fun(SimpleSynchronousEntry::DeleteFilesForEntryHash), path)); 274 std::ptr_fun(SimpleSynchronousEntry::DeleteFilesForEntryHash), path));
272 return (did_delete_count == key_hashes->size()) ? net::OK : net::ERR_FAILED; 275 return (did_delete_count == key_hashes->size()) ? net::OK : net::ERR_FAILED;
273 } 276 }
274 277
275 void SimpleSynchronousEntry::ReadData(const EntryOperationData& in_entry_op, 278 void SimpleSynchronousEntry::ReadData(const EntryOperationData& in_entry_op,
276 net::IOBuffer* out_buf, 279 net::IOBuffer* out_buf,
277 uint32* out_crc32, 280 uint32* out_crc32,
278 SimpleEntryStat* entry_stat, 281 SimpleEntryStat* entry_stat,
279 int* out_result) const { 282 int* out_result) const {
280 DCHECK(initialized_); 283 DCHECK(initialized_);
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 if (it != sparse_ranges_.begin()) { 408 if (it != sparse_ranges_.begin()) {
406 // Hop back one range and read the one overlapping with the start. 409 // Hop back one range and read the one overlapping with the start.
407 --it; 410 --it;
408 SparseRange* found_range = &it->second; 411 SparseRange* found_range = &it->second;
409 DCHECK_EQ(it->first, found_range->offset); 412 DCHECK_EQ(it->first, found_range->offset);
410 if (found_range->offset + found_range->length > offset) { 413 if (found_range->offset + found_range->length > offset) {
411 DCHECK_LE(0, found_range->length); 414 DCHECK_LE(0, found_range->length);
412 DCHECK_GE(kint32max, found_range->length); 415 DCHECK_GE(kint32max, found_range->length);
413 DCHECK_LE(0, offset - found_range->offset); 416 DCHECK_LE(0, offset - found_range->offset);
414 DCHECK_GE(kint32max, offset - found_range->offset); 417 DCHECK_GE(kint32max, offset - found_range->offset);
415 int range_len_after_offset = found_range->length - 418 int range_len_after_offset =
416 (offset - found_range->offset); 419 found_range->length - (offset - found_range->offset);
417 DCHECK_LE(0, range_len_after_offset); 420 DCHECK_LE(0, range_len_after_offset);
418 421
419 int len_to_read = std::min(buf_len, range_len_after_offset); 422 int len_to_read = std::min(buf_len, range_len_after_offset);
420 if (!ReadSparseRange(found_range, 423 if (!ReadSparseRange(
421 offset - found_range->offset, 424 found_range, offset - found_range->offset, len_to_read, buf)) {
422 len_to_read,
423 buf)) {
424 *out_result = net::ERR_CACHE_READ_FAILURE; 425 *out_result = net::ERR_CACHE_READ_FAILURE;
425 return; 426 return;
426 } 427 }
427 read_so_far += len_to_read; 428 read_so_far += len_to_read;
428 } 429 }
429 ++it; 430 ++it;
430 } 431 }
431 432
432 // Keep reading until the buffer is full or there is not another contiguous 433 // Keep reading until the buffer is full or there is not another contiguous
433 // range. 434 // range.
434 while (read_so_far < buf_len && 435 while (read_so_far < buf_len && it != sparse_ranges_.end() &&
435 it != sparse_ranges_.end() &&
436 it->second.offset == offset + read_so_far) { 436 it->second.offset == offset + read_so_far) {
437 SparseRange* found_range = &it->second; 437 SparseRange* found_range = &it->second;
438 DCHECK_EQ(it->first, found_range->offset); 438 DCHECK_EQ(it->first, found_range->offset);
439 int range_len = (found_range->length > kint32max) ? 439 int range_len =
440 kint32max : found_range->length; 440 (found_range->length > kint32max) ? kint32max : found_range->length;
441 int len_to_read = std::min(buf_len - read_so_far, range_len); 441 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)) { 442 if (!ReadSparseRange(found_range, 0, len_to_read, buf + read_so_far)) {
443 *out_result = net::ERR_CACHE_READ_FAILURE; 443 *out_result = net::ERR_CACHE_READ_FAILURE;
444 return; 444 return;
445 } 445 }
446 read_so_far += len_to_read; 446 read_so_far += len_to_read;
447 ++it; 447 ++it;
448 } 448 }
449 449
450 *out_result = read_so_far; 450 *out_result = read_so_far;
(...skipping 30 matching lines...) Expand all
481 SparseRangeIterator it = sparse_ranges_.lower_bound(offset); 481 SparseRangeIterator it = sparse_ranges_.lower_bound(offset);
482 482
483 if (it != sparse_ranges_.begin()) { 483 if (it != sparse_ranges_.begin()) {
484 --it; 484 --it;
485 SparseRange* found_range = &it->second; 485 SparseRange* found_range = &it->second;
486 if (found_range->offset + found_range->length > offset) { 486 if (found_range->offset + found_range->length > offset) {
487 DCHECK_LE(0, found_range->length); 487 DCHECK_LE(0, found_range->length);
488 DCHECK_GE(kint32max, found_range->length); 488 DCHECK_GE(kint32max, found_range->length);
489 DCHECK_LE(0, offset - found_range->offset); 489 DCHECK_LE(0, offset - found_range->offset);
490 DCHECK_GE(kint32max, offset - found_range->offset); 490 DCHECK_GE(kint32max, offset - found_range->offset);
491 int range_len_after_offset = found_range->length - 491 int range_len_after_offset =
492 (offset - found_range->offset); 492 found_range->length - (offset - found_range->offset);
493 DCHECK_LE(0, range_len_after_offset); 493 DCHECK_LE(0, range_len_after_offset);
494 494
495 int len_to_write = std::min(buf_len, range_len_after_offset); 495 int len_to_write = std::min(buf_len, range_len_after_offset);
496 if (!WriteSparseRange(found_range, 496 if (!WriteSparseRange(
497 offset - found_range->offset, 497 found_range, offset - found_range->offset, len_to_write, buf)) {
498 len_to_write,
499 buf)) {
500 *out_result = net::ERR_CACHE_WRITE_FAILURE; 498 *out_result = net::ERR_CACHE_WRITE_FAILURE;
501 return; 499 return;
502 } 500 }
503 written_so_far += len_to_write; 501 written_so_far += len_to_write;
504 } 502 }
505 ++it; 503 ++it;
506 } 504 }
507 505
508 while (written_so_far < buf_len && 506 while (written_so_far < buf_len && it != sparse_ranges_.end() &&
509 it != sparse_ranges_.end() &&
510 it->second.offset < offset + buf_len) { 507 it->second.offset < offset + buf_len) {
511 SparseRange* found_range = &it->second; 508 SparseRange* found_range = &it->second;
512 if (offset + written_so_far < found_range->offset) { 509 if (offset + written_so_far < found_range->offset) {
513 int len_to_append = found_range->offset - (offset + written_so_far); 510 int len_to_append = found_range->offset - (offset + written_so_far);
514 if (!AppendSparseRange(offset + written_so_far, 511 if (!AppendSparseRange(
515 len_to_append, 512 offset + written_so_far, len_to_append, buf + written_so_far)) {
516 buf + written_so_far)) {
517 *out_result = net::ERR_CACHE_WRITE_FAILURE; 513 *out_result = net::ERR_CACHE_WRITE_FAILURE;
518 return; 514 return;
519 } 515 }
520 written_so_far += len_to_append; 516 written_so_far += len_to_append;
521 appended_so_far += len_to_append; 517 appended_so_far += len_to_append;
522 } 518 }
523 int range_len = (found_range->length > kint32max) ? 519 int range_len =
524 kint32max : found_range->length; 520 (found_range->length > kint32max) ? 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, 0, len_to_write, buf + written_so_far)) {
527 0,
528 len_to_write,
529 buf + written_so_far)) {
530 *out_result = net::ERR_CACHE_WRITE_FAILURE; 523 *out_result = net::ERR_CACHE_WRITE_FAILURE;
531 return; 524 return;
532 } 525 }
533 written_so_far += len_to_write; 526 written_so_far += len_to_write;
534 ++it; 527 ++it;
535 } 528 }
536 529
537 if (written_so_far < buf_len) { 530 if (written_so_far < buf_len) {
538 int len_to_append = buf_len - written_so_far; 531 int len_to_append = buf_len - written_so_far;
539 if (!AppendSparseRange(offset + written_so_far, 532 if (!AppendSparseRange(
540 len_to_append, 533 offset + written_so_far, len_to_append, buf + written_so_far)) {
541 buf + written_so_far)) {
542 *out_result = net::ERR_CACHE_WRITE_FAILURE; 534 *out_result = net::ERR_CACHE_WRITE_FAILURE;
543 return; 535 return;
544 } 536 }
545 written_so_far += len_to_append; 537 written_so_far += len_to_append;
546 appended_so_far += len_to_append; 538 appended_so_far += len_to_append;
547 } 539 }
548 540
549 DCHECK_EQ(buf_len, written_so_far); 541 DCHECK_EQ(buf_len, written_so_far);
550 542
551 base::Time modification_time = Time::Now(); 543 base::Time modification_time = Time::Now();
(...skipping 23 matching lines...) Expand all
575 if ((it == sparse_ranges_.end() || it->second.offset > offset) && 567 if ((it == sparse_ranges_.end() || it->second.offset > offset) &&
576 it != sparse_ranges_.begin()) { 568 it != sparse_ranges_.begin()) {
577 --it; 569 --it;
578 if (it->second.offset + it->second.length > offset) { 570 if (it->second.offset + it->second.length > offset) {
579 start = offset; 571 start = offset;
580 avail_so_far = (it->second.offset + it->second.length) - offset; 572 avail_so_far = (it->second.offset + it->second.length) - offset;
581 } 573 }
582 ++it; 574 ++it;
583 } 575 }
584 576
585 while (start + avail_so_far < offset + len && 577 while (start + avail_so_far < offset + len && it != sparse_ranges_.end() &&
586 it != sparse_ranges_.end() &&
587 it->second.offset == start + avail_so_far) { 578 it->second.offset == start + avail_so_far) {
588 avail_so_far += it->second.length; 579 avail_so_far += it->second.length;
589 ++it; 580 ++it;
590 } 581 }
591 582
592 int len_from_start = len - (start - offset); 583 int len_from_start = len - (start - offset);
593 *out_start = start; 584 *out_start = start;
594 *out_result = std::min(avail_so_far, len_from_start); 585 *out_result = std::min(avail_so_far, len_from_start);
595 } 586 }
596 587
(...skipping 21 matching lines...) Expand all
618 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); 609 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS);
619 } 610 }
620 611
621 void SimpleSynchronousEntry::Close( 612 void SimpleSynchronousEntry::Close(
622 const SimpleEntryStat& entry_stat, 613 const SimpleEntryStat& entry_stat,
623 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write, 614 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write,
624 net::GrowableIOBuffer* stream_0_data) { 615 net::GrowableIOBuffer* stream_0_data) {
625 DCHECK(stream_0_data); 616 DCHECK(stream_0_data);
626 // Write stream 0 data. 617 // Write stream 0 data.
627 int stream_0_offset = entry_stat.GetOffsetInFile(key_, 0, 0); 618 int stream_0_offset = entry_stat.GetOffsetInFile(key_, 0, 0);
628 if (files_[0].Write(stream_0_offset, stream_0_data->data(), 619 if (files_[0].Write(stream_0_offset,
629 entry_stat.data_size(0)) != 620 stream_0_data->data(),
630 entry_stat.data_size(0)) { 621 entry_stat.data_size(0)) != entry_stat.data_size(0)) {
631 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); 622 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE);
632 DVLOG(1) << "Could not write stream 0 data."; 623 DVLOG(1) << "Could not write stream 0 data.";
633 Doom(); 624 Doom();
634 } 625 }
635 626
636 for (std::vector<CRCRecord>::const_iterator it = crc32s_to_write->begin(); 627 for (std::vector<CRCRecord>::const_iterator it = crc32s_to_write->begin();
637 it != crc32s_to_write->end(); ++it) { 628 it != crc32s_to_write->end();
629 ++it) {
638 const int stream_index = it->index; 630 const int stream_index = it->index;
639 const int file_index = GetFileIndexFromStreamIndex(stream_index); 631 const int file_index = GetFileIndexFromStreamIndex(stream_index);
640 if (empty_file_omitted_[file_index]) 632 if (empty_file_omitted_[file_index])
641 continue; 633 continue;
642 634
643 SimpleFileEOF eof_record; 635 SimpleFileEOF eof_record;
644 eof_record.stream_size = entry_stat.data_size(stream_index); 636 eof_record.stream_size = entry_stat.data_size(stream_index);
645 eof_record.final_magic_number = kSimpleFinalMagicNumber; 637 eof_record.final_magic_number = kSimpleFinalMagicNumber;
646 eof_record.flags = 0; 638 eof_record.flags = 0;
647 if (it->has_crc32) 639 if (it->has_crc32)
648 eof_record.flags |= SimpleFileEOF::FLAG_HAS_CRC32; 640 eof_record.flags |= SimpleFileEOF::FLAG_HAS_CRC32;
649 eof_record.data_crc32 = it->data_crc32; 641 eof_record.data_crc32 = it->data_crc32;
650 int eof_offset = entry_stat.GetEOFOffsetInFile(key_, stream_index); 642 int eof_offset = entry_stat.GetEOFOffsetInFile(key_, stream_index);
651 // If stream 0 changed size, the file needs to be resized, otherwise the 643 // If stream 0 changed size, the file needs to be resized, otherwise the
652 // next open will yield wrong stream sizes. On stream 1 and stream 2 proper 644 // next open will yield wrong stream sizes. On stream 1 and stream 2 proper
653 // resizing of the file is handled in SimpleSynchronousEntry::WriteData(). 645 // resizing of the file is handled in SimpleSynchronousEntry::WriteData().
654 if (stream_index == 0 && 646 if (stream_index == 0 && !files_[file_index].SetLength(eof_offset)) {
655 !files_[file_index].SetLength(eof_offset)) {
656 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); 647 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE);
657 DVLOG(1) << "Could not truncate stream 0 file."; 648 DVLOG(1) << "Could not truncate stream 0 file.";
658 Doom(); 649 Doom();
659 break; 650 break;
660 } 651 }
661 if (files_[file_index].Write(eof_offset, 652 if (files_[file_index].Write(eof_offset,
662 reinterpret_cast<const char*>(&eof_record), 653 reinterpret_cast<const char*>(&eof_record),
663 sizeof(eof_record)) != 654 sizeof(eof_record)) != sizeof(eof_record)) {
664 sizeof(eof_record)) {
665 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); 655 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE);
666 DVLOG(1) << "Could not write eof record."; 656 DVLOG(1) << "Could not write eof record.";
667 Doom(); 657 Doom();
668 break; 658 break;
669 } 659 }
670 } 660 }
671 for (int i = 0; i < kSimpleEntryFileCount; ++i) { 661 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
672 if (empty_file_omitted_[i]) 662 if (empty_file_omitted_[i])
673 continue; 663 continue;
674 664
675 files_[i].Close(); 665 files_[i].Close();
676 const int64 file_size = entry_stat.GetFileSize(key_, i); 666 const int64 file_size = entry_stat.GetFileSize(key_, i);
677 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, 667 SIMPLE_CACHE_UMA(CUSTOM_COUNTS,
678 "LastClusterSize", cache_type_, 668 "LastClusterSize",
679 file_size % 4096, 0, 4097, 50); 669 cache_type_,
670 file_size % 4096,
671 0,
672 4097,
673 50);
680 const int64 cluster_loss = file_size % 4096 ? 4096 - file_size % 4096 : 0; 674 const int64 cluster_loss = file_size % 4096 ? 4096 - file_size % 4096 : 0;
681 SIMPLE_CACHE_UMA(PERCENTAGE, 675 SIMPLE_CACHE_UMA(PERCENTAGE,
682 "LastClusterLossPercent", cache_type_, 676 "LastClusterLossPercent",
677 cache_type_,
683 cluster_loss * 100 / (cluster_loss + file_size)); 678 cluster_loss * 100 / (cluster_loss + file_size));
684 } 679 }
685 680
686 if (sparse_file_open()) 681 if (sparse_file_open())
687 sparse_file_.Close(); 682 sparse_file_.Close();
688 683
689 if (files_created_) { 684 if (files_created_) {
690 const int stream2_file_index = GetFileIndexFromStreamIndex(2); 685 const int stream2_file_index = GetFileIndexFromStreamIndex(2);
691 SIMPLE_CACHE_UMA(BOOLEAN, "EntryCreatedAndStream2Omitted", cache_type_, 686 SIMPLE_CACHE_UMA(BOOLEAN,
687 "EntryCreatedAndStream2Omitted",
688 cache_type_,
692 empty_file_omitted_[stream2_file_index]); 689 empty_file_omitted_[stream2_file_index]);
693 } 690 }
694 RecordCloseResult(cache_type_, CLOSE_RESULT_SUCCESS); 691 RecordCloseResult(cache_type_, CLOSE_RESULT_SUCCESS);
695 have_open_files_ = false; 692 have_open_files_ = false;
696 delete this; 693 delete this;
697 } 694 }
698 695
699 SimpleSynchronousEntry::SimpleSynchronousEntry(net::CacheType cache_type, 696 SimpleSynchronousEntry::SimpleSynchronousEntry(net::CacheType cache_type,
700 const FilePath& path, 697 const FilePath& path,
701 const std::string& key, 698 const std::string& key,
702 const uint64 entry_hash) 699 const uint64 entry_hash)
703 : cache_type_(cache_type), 700 : cache_type_(cache_type),
704 path_(path), 701 path_(path),
705 entry_hash_(entry_hash), 702 entry_hash_(entry_hash),
706 key_(key), 703 key_(key),
707 have_open_files_(false), 704 have_open_files_(false),
708 initialized_(false) { 705 initialized_(false) {
709 for (int i = 0; i < kSimpleEntryFileCount; ++i) 706 for (int i = 0; i < kSimpleEntryFileCount; ++i)
710 empty_file_omitted_[i] = false; 707 empty_file_omitted_[i] = false;
711 } 708 }
712 709
713 SimpleSynchronousEntry::~SimpleSynchronousEntry() { 710 SimpleSynchronousEntry::~SimpleSynchronousEntry() {
714 DCHECK(!(have_open_files_ && initialized_)); 711 DCHECK(!(have_open_files_ && initialized_));
715 if (have_open_files_) 712 if (have_open_files_)
716 CloseFiles(); 713 CloseFiles();
717 } 714 }
718 715
719 bool SimpleSynchronousEntry::MaybeOpenFile( 716 bool SimpleSynchronousEntry::MaybeOpenFile(int file_index,
720 int file_index, 717 File::Error* out_error) {
721 File::Error* out_error) {
722 DCHECK(out_error); 718 DCHECK(out_error);
723 719
724 FilePath filename = GetFilenameFromFileIndex(file_index); 720 FilePath filename = GetFilenameFromFileIndex(file_index);
725 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; 721 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
726 files_[file_index].Initialize(filename, flags); 722 files_[file_index].Initialize(filename, flags);
727 *out_error = files_[file_index].error_details(); 723 *out_error = files_[file_index].error_details();
728 724
729 if (CanOmitEmptyFile(file_index) && !files_[file_index].IsValid() && 725 if (CanOmitEmptyFile(file_index) && !files_[file_index].IsValid() &&
730 *out_error == File::FILE_ERROR_NOT_FOUND) { 726 *out_error == File::FILE_ERROR_NOT_FOUND) {
731 empty_file_omitted_[file_index] = true; 727 empty_file_omitted_[file_index] = true;
732 return true; 728 return true;
733 } 729 }
734 730
735 return files_[file_index].IsValid(); 731 return files_[file_index].IsValid();
736 } 732 }
737 733
738 bool SimpleSynchronousEntry::MaybeCreateFile( 734 bool SimpleSynchronousEntry::MaybeCreateFile(int file_index,
739 int file_index, 735 FileRequired file_required,
740 FileRequired file_required, 736 File::Error* out_error) {
741 File::Error* out_error) {
742 DCHECK(out_error); 737 DCHECK(out_error);
743 738
744 if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) { 739 if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) {
745 empty_file_omitted_[file_index] = true; 740 empty_file_omitted_[file_index] = true;
746 return true; 741 return true;
747 } 742 }
748 743
749 FilePath filename = GetFilenameFromFileIndex(file_index); 744 FilePath filename = GetFilenameFromFileIndex(file_index);
750 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE; 745 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE;
751 files_[file_index].Initialize(filename, flags); 746 files_[file_index].Initialize(filename, flags);
752 *out_error = files_[file_index].error_details(); 747 *out_error = files_[file_index].error_details();
753 748
754 empty_file_omitted_[file_index] = false; 749 empty_file_omitted_[file_index] = false;
755 750
756 return files_[file_index].IsValid(); 751 return files_[file_index].IsValid();
757 } 752 }
758 753
759 bool SimpleSynchronousEntry::OpenFiles( 754 bool SimpleSynchronousEntry::OpenFiles(bool had_index,
760 bool had_index, 755 SimpleEntryStat* out_entry_stat) {
761 SimpleEntryStat* out_entry_stat) {
762 for (int i = 0; i < kSimpleEntryFileCount; ++i) { 756 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
763 File::Error error; 757 File::Error error;
764 if (!MaybeOpenFile(i, &error)) { 758 if (!MaybeOpenFile(i, &error)) {
765 // TODO(ttuttle,gavinp): Remove one each of these triplets of histograms. 759 // TODO(ttuttle,gavinp): Remove one each of these triplets of histograms.
766 // We can calculate the third as the sum or difference of the other two. 760 // We can calculate the third as the sum or difference of the other two.
767 RecordSyncOpenResult( 761 RecordSyncOpenResult(
768 cache_type_, OPEN_ENTRY_PLATFORM_FILE_ERROR, had_index); 762 cache_type_, OPEN_ENTRY_PLATFORM_FILE_ERROR, had_index);
769 SIMPLE_CACHE_UMA(ENUMERATION, 763 SIMPLE_CACHE_UMA(ENUMERATION,
770 "SyncOpenPlatformFileError", cache_type_, 764 "SyncOpenPlatformFileError",
771 -error, -base::File::FILE_ERROR_MAX); 765 cache_type_,
766 -error,
767 -base::File::FILE_ERROR_MAX);
772 if (had_index) { 768 if (had_index) {
773 SIMPLE_CACHE_UMA(ENUMERATION, 769 SIMPLE_CACHE_UMA(ENUMERATION,
774 "SyncOpenPlatformFileError_WithIndex", cache_type_, 770 "SyncOpenPlatformFileError_WithIndex",
775 -error, -base::File::FILE_ERROR_MAX); 771 cache_type_,
772 -error,
773 -base::File::FILE_ERROR_MAX);
776 } else { 774 } else {
777 SIMPLE_CACHE_UMA(ENUMERATION, 775 SIMPLE_CACHE_UMA(ENUMERATION,
778 "SyncOpenPlatformFileError_WithoutIndex", 776 "SyncOpenPlatformFileError_WithoutIndex",
779 cache_type_, 777 cache_type_,
780 -error, -base::File::FILE_ERROR_MAX); 778 -error,
779 -base::File::FILE_ERROR_MAX);
781 } 780 }
782 while (--i >= 0) 781 while (--i >= 0)
783 CloseFile(i); 782 CloseFile(i);
784 return false; 783 return false;
785 } 784 }
786 } 785 }
787 786
788 have_open_files_ = true; 787 have_open_files_ = true;
789 788
790 base::TimeDelta entry_age = base::Time::Now() - base::Time::UnixEpoch(); 789 base::TimeDelta entry_age = base::Time::Now() - base::Time::UnixEpoch();
(...skipping 30 matching lines...) Expand all
821 // |data_size(1)| and |data_size(2)| respectively. Reading the key in 820 // |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 821 // 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 822 // 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 823 // 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 824 // 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 825 // stream 1 and stream 0 is only determined after reading the EOF record
827 // for stream 0 in ReadAndValidateStream0. 826 // for stream 0 in ReadAndValidateStream0.
828 out_entry_stat->set_data_size(i + 1, file_info.size); 827 out_entry_stat->set_data_size(i + 1, file_info.size);
829 } 828 }
830 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, 829 SIMPLE_CACHE_UMA(CUSTOM_COUNTS,
831 "SyncOpenEntryAge", cache_type_, 830 "SyncOpenEntryAge",
832 entry_age.InHours(), 1, 1000, 50); 831 cache_type_,
832 entry_age.InHours(),
833 1,
834 1000,
835 50);
833 836
834 files_created_ = false; 837 files_created_ = false;
835 838
836 return true; 839 return true;
837 } 840 }
838 841
839 bool SimpleSynchronousEntry::CreateFiles( 842 bool SimpleSynchronousEntry::CreateFiles(bool had_index,
840 bool had_index, 843 SimpleEntryStat* out_entry_stat) {
841 SimpleEntryStat* out_entry_stat) {
842 for (int i = 0; i < kSimpleEntryFileCount; ++i) { 844 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
843 File::Error error; 845 File::Error error;
844 if (!MaybeCreateFile(i, FILE_NOT_REQUIRED, &error)) { 846 if (!MaybeCreateFile(i, FILE_NOT_REQUIRED, &error)) {
845 // TODO(ttuttle,gavinp): Remove one each of these triplets of histograms. 847 // TODO(ttuttle,gavinp): Remove one each of these triplets of histograms.
846 // We can calculate the third as the sum or difference of the other two. 848 // We can calculate the third as the sum or difference of the other two.
847 RecordSyncCreateResult(CREATE_ENTRY_PLATFORM_FILE_ERROR, had_index); 849 RecordSyncCreateResult(CREATE_ENTRY_PLATFORM_FILE_ERROR, had_index);
848 SIMPLE_CACHE_UMA(ENUMERATION, 850 SIMPLE_CACHE_UMA(ENUMERATION,
849 "SyncCreatePlatformFileError", cache_type_, 851 "SyncCreatePlatformFileError",
850 -error, -base::File::FILE_ERROR_MAX); 852 cache_type_,
853 -error,
854 -base::File::FILE_ERROR_MAX);
851 if (had_index) { 855 if (had_index) {
852 SIMPLE_CACHE_UMA(ENUMERATION, 856 SIMPLE_CACHE_UMA(ENUMERATION,
853 "SyncCreatePlatformFileError_WithIndex", cache_type_, 857 "SyncCreatePlatformFileError_WithIndex",
854 -error, -base::File::FILE_ERROR_MAX); 858 cache_type_,
859 -error,
860 -base::File::FILE_ERROR_MAX);
855 } else { 861 } else {
856 SIMPLE_CACHE_UMA(ENUMERATION, 862 SIMPLE_CACHE_UMA(ENUMERATION,
857 "SyncCreatePlatformFileError_WithoutIndex", 863 "SyncCreatePlatformFileError_WithoutIndex",
858 cache_type_, 864 cache_type_,
859 -error, -base::File::FILE_ERROR_MAX); 865 -error,
866 -base::File::FILE_ERROR_MAX);
860 } 867 }
861 while (--i >= 0) 868 while (--i >= 0)
862 CloseFile(i); 869 CloseFile(i);
863 return false; 870 return false;
864 } 871 }
865 } 872 }
866 873
867 have_open_files_ = true; 874 have_open_files_ = true;
868 875
869 base::Time creation_time = Time::Now(); 876 base::Time creation_time = Time::Now();
870 out_entry_stat->set_last_modified(creation_time); 877 out_entry_stat->set_last_modified(creation_time);
871 out_entry_stat->set_last_used(creation_time); 878 out_entry_stat->set_last_used(creation_time);
872 for (int i = 0; i < kSimpleEntryStreamCount; ++i) 879 for (int i = 0; i < kSimpleEntryStreamCount; ++i)
873 out_entry_stat->set_data_size(i, 0); 880 out_entry_stat->set_data_size(i, 0);
874 881
875 files_created_ = true; 882 files_created_ = true;
876 883
877 return true; 884 return true;
878 } 885 }
879 886
880 void SimpleSynchronousEntry::CloseFile(int index) { 887 void SimpleSynchronousEntry::CloseFile(int index) {
881 if (empty_file_omitted_[index]) { 888 if (empty_file_omitted_[index]) {
882 empty_file_omitted_[index] = false; 889 empty_file_omitted_[index] = false;
883 } else { 890 } else {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 return net::ERR_FAILED; 933 return net::ERR_FAILED;
927 } 934 }
928 935
929 if (header.version != kSimpleEntryVersionOnDisk) { 936 if (header.version != kSimpleEntryVersionOnDisk) {
930 DLOG(WARNING) << "Unreadable version."; 937 DLOG(WARNING) << "Unreadable version.";
931 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_BAD_VERSION, had_index); 938 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_BAD_VERSION, had_index);
932 return net::ERR_FAILED; 939 return net::ERR_FAILED;
933 } 940 }
934 941
935 scoped_ptr<char[]> key(new char[header.key_length]); 942 scoped_ptr<char[]> key(new char[header.key_length]);
936 int key_read_result = files_[i].Read(sizeof(header), key.get(), 943 int key_read_result =
937 header.key_length); 944 files_[i].Read(sizeof(header), key.get(), header.key_length);
938 if (key_read_result != implicit_cast<int>(header.key_length)) { 945 if (key_read_result != implicit_cast<int>(header.key_length)) {
939 DLOG(WARNING) << "Cannot read key from entry."; 946 DLOG(WARNING) << "Cannot read key from entry.";
940 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_KEY, had_index); 947 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_KEY, had_index);
941 return net::ERR_FAILED; 948 return net::ERR_FAILED;
942 } 949 }
943 950
944 key_ = std::string(key.get(), header.key_length); 951 key_ = std::string(key.get(), header.key_length);
945 if (i == 0) { 952 if (i == 0) {
946 // File size for stream 0 has been stored temporarily in data_size[1]. 953 // File size for stream 0 has been stored temporarily in data_size[1].
947 int total_data_size = 954 int total_data_size =
(...skipping 14 matching lines...) Expand all
962 if (base::Hash(key.get(), header.key_length) != header.key_hash) { 969 if (base::Hash(key.get(), header.key_length) != header.key_hash) {
963 DLOG(WARNING) << "Hash mismatch on key."; 970 DLOG(WARNING) << "Hash mismatch on key.";
964 RecordSyncOpenResult( 971 RecordSyncOpenResult(
965 cache_type_, OPEN_ENTRY_KEY_HASH_MISMATCH, had_index); 972 cache_type_, OPEN_ENTRY_KEY_HASH_MISMATCH, had_index);
966 return net::ERR_FAILED; 973 return net::ERR_FAILED;
967 } 974 }
968 } 975 }
969 976
970 int32 sparse_data_size = 0; 977 int32 sparse_data_size = 0;
971 if (!OpenSparseFileIfExists(&sparse_data_size)) { 978 if (!OpenSparseFileIfExists(&sparse_data_size)) {
972 RecordSyncOpenResult( 979 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_SPARSE_OPEN_FAILED, had_index);
973 cache_type_, OPEN_ENTRY_SPARSE_OPEN_FAILED, had_index);
974 return net::ERR_FAILED; 980 return net::ERR_FAILED;
975 } 981 }
976 out_entry_stat->set_sparse_data_size(sparse_data_size); 982 out_entry_stat->set_sparse_data_size(sparse_data_size);
977 983
978 bool removed_stream2 = false; 984 bool removed_stream2 = false;
979 const int stream2_file_index = GetFileIndexFromStreamIndex(2); 985 const int stream2_file_index = GetFileIndexFromStreamIndex(2);
980 DCHECK(CanOmitEmptyFile(stream2_file_index)); 986 DCHECK(CanOmitEmptyFile(stream2_file_index));
981 if (!empty_file_omitted_[stream2_file_index] && 987 if (!empty_file_omitted_[stream2_file_index] &&
982 out_entry_stat->data_size(2) == 0) { 988 out_entry_stat->data_size(2) == 0) {
983 DVLOG(1) << "Removing empty stream 2 file."; 989 DVLOG(1) << "Removing empty stream 2 file.";
984 CloseFile(stream2_file_index); 990 CloseFile(stream2_file_index);
985 DeleteFileForEntryHash(path_, entry_hash_, stream2_file_index); 991 DeleteFileForEntryHash(path_, entry_hash_, stream2_file_index);
986 empty_file_omitted_[stream2_file_index] = true; 992 empty_file_omitted_[stream2_file_index] = true;
987 removed_stream2 = true; 993 removed_stream2 = true;
988 } 994 }
989 995
990 SIMPLE_CACHE_UMA(BOOLEAN, "EntryOpenedAndStream2Removed", cache_type_, 996 SIMPLE_CACHE_UMA(
991 removed_stream2); 997 BOOLEAN, "EntryOpenedAndStream2Removed", cache_type_, removed_stream2);
992 998
993 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_SUCCESS, had_index); 999 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_SUCCESS, had_index);
994 initialized_ = true; 1000 initialized_ = true;
995 return net::OK; 1001 return net::OK;
996 } 1002 }
997 1003
998 bool SimpleSynchronousEntry::InitializeCreatedFile( 1004 bool SimpleSynchronousEntry::InitializeCreatedFile(
999 int file_index, 1005 int file_index,
1000 CreateEntryResult* out_result) { 1006 CreateEntryResult* out_result) {
1001 SimpleFileHeader header; 1007 SimpleFileHeader header;
1002 header.initial_magic_number = kSimpleInitialMagicNumber; 1008 header.initial_magic_number = kSimpleInitialMagicNumber;
1003 header.version = kSimpleEntryVersionOnDisk; 1009 header.version = kSimpleEntryVersionOnDisk;
1004 1010
1005 header.key_length = key_.size(); 1011 header.key_length = key_.size();
1006 header.key_hash = base::Hash(key_); 1012 header.key_hash = base::Hash(key_);
1007 1013
1008 int bytes_written = files_[file_index].Write( 1014 int bytes_written = files_[file_index].Write(
1009 0, reinterpret_cast<char*>(&header), sizeof(header)); 1015 0, reinterpret_cast<char*>(&header), sizeof(header));
1010 if (bytes_written != sizeof(header)) { 1016 if (bytes_written != sizeof(header)) {
1011 *out_result = CREATE_ENTRY_CANT_WRITE_HEADER; 1017 *out_result = CREATE_ENTRY_CANT_WRITE_HEADER;
1012 return false; 1018 return false;
1013 } 1019 }
1014 1020
1015 bytes_written = files_[file_index].Write(sizeof(header), key_.data(), 1021 bytes_written =
1016 key_.size()); 1022 files_[file_index].Write(sizeof(header), key_.data(), key_.size());
1017 if (bytes_written != implicit_cast<int>(key_.size())) { 1023 if (bytes_written != implicit_cast<int>(key_.size())) {
1018 *out_result = CREATE_ENTRY_CANT_WRITE_KEY; 1024 *out_result = CREATE_ENTRY_CANT_WRITE_KEY;
1019 return false; 1025 return false;
1020 } 1026 }
1021 1027
1022 return true; 1028 return true;
1023 } 1029 }
1024 1030
1025 int SimpleSynchronousEntry::InitializeForCreate( 1031 int SimpleSynchronousEntry::InitializeForCreate(
1026 bool had_index, 1032 bool had_index,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 int ret_value_crc32 = GetEOFRecordData( 1067 int ret_value_crc32 = GetEOFRecordData(
1062 0, *out_entry_stat, &has_crc32, &read_crc32, &stream_0_size); 1068 0, *out_entry_stat, &has_crc32, &read_crc32, &stream_0_size);
1063 if (ret_value_crc32 != net::OK) 1069 if (ret_value_crc32 != net::OK)
1064 return ret_value_crc32; 1070 return ret_value_crc32;
1065 1071
1066 if (stream_0_size > out_entry_stat->data_size(1)) 1072 if (stream_0_size > out_entry_stat->data_size(1))
1067 return net::ERR_FAILED; 1073 return net::ERR_FAILED;
1068 1074
1069 // These are the real values of data size. 1075 // These are the real values of data size.
1070 out_entry_stat->set_data_size(0, stream_0_size); 1076 out_entry_stat->set_data_size(0, stream_0_size);
1071 out_entry_stat->set_data_size( 1077 out_entry_stat->set_data_size(1,
1072 1, out_entry_stat->data_size(1) - stream_0_size); 1078 out_entry_stat->data_size(1) - stream_0_size);
1073 1079
1074 // Put stream 0 data in memory. 1080 // Put stream 0 data in memory.
1075 *stream_0_data = new net::GrowableIOBuffer(); 1081 *stream_0_data = new net::GrowableIOBuffer();
1076 (*stream_0_data)->SetCapacity(stream_0_size); 1082 (*stream_0_data)->SetCapacity(stream_0_size);
1077 int file_offset = out_entry_stat->GetOffsetInFile(key_, 0, 0); 1083 int file_offset = out_entry_stat->GetOffsetInFile(key_, 0, 0);
1078 File* file = const_cast<File*>(&files_[0]); 1084 File* file = const_cast<File*>(&files_[0]);
1079 int bytes_read = 1085 int bytes_read =
1080 file->Read(file_offset, (*stream_0_data)->data(), stream_0_size); 1086 file->Read(file_offset, (*stream_0_data)->data(), stream_0_size);
1081 if (bytes_read != stream_0_size) 1087 if (bytes_read != stream_0_size)
1082 return net::ERR_FAILED; 1088 return net::ERR_FAILED;
(...skipping 17 matching lines...) Expand all
1100 1106
1101 int SimpleSynchronousEntry::GetEOFRecordData(int index, 1107 int SimpleSynchronousEntry::GetEOFRecordData(int index,
1102 const SimpleEntryStat& entry_stat, 1108 const SimpleEntryStat& entry_stat,
1103 bool* out_has_crc32, 1109 bool* out_has_crc32,
1104 uint32* out_crc32, 1110 uint32* out_crc32,
1105 int* out_data_size) const { 1111 int* out_data_size) const {
1106 SimpleFileEOF eof_record; 1112 SimpleFileEOF eof_record;
1107 int file_offset = entry_stat.GetEOFOffsetInFile(key_, index); 1113 int file_offset = entry_stat.GetEOFOffsetInFile(key_, index);
1108 int file_index = GetFileIndexFromStreamIndex(index); 1114 int file_index = GetFileIndexFromStreamIndex(index);
1109 File* file = const_cast<File*>(&files_[file_index]); 1115 File* file = const_cast<File*>(&files_[file_index]);
1110 if (file->Read(file_offset, reinterpret_cast<char*>(&eof_record), 1116 if (file->Read(file_offset,
1111 sizeof(eof_record)) != 1117 reinterpret_cast<char*>(&eof_record),
1112 sizeof(eof_record)) { 1118 sizeof(eof_record)) != sizeof(eof_record)) {
1113 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); 1119 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE);
1114 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; 1120 return net::ERR_CACHE_CHECKSUM_READ_FAILURE;
1115 } 1121 }
1116 1122
1117 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { 1123 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) {
1118 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); 1124 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH);
1119 DVLOG(1) << "EOF record had bad magic number."; 1125 DVLOG(1) << "EOF record had bad magic number.";
1120 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; 1126 return net::ERR_CACHE_CHECKSUM_READ_FAILURE;
1121 } 1127 }
1122 1128
1123 *out_has_crc32 = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) == 1129 *out_has_crc32 = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) ==
1124 SimpleFileEOF::FLAG_HAS_CRC32; 1130 SimpleFileEOF::FLAG_HAS_CRC32;
1125 *out_crc32 = eof_record.data_crc32; 1131 *out_crc32 = eof_record.data_crc32;
1126 *out_data_size = eof_record.stream_size; 1132 *out_data_size = eof_record.stream_size;
1127 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, *out_has_crc32); 1133 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, *out_has_crc32);
1128 return net::OK; 1134 return net::OK;
1129 } 1135 }
1130 1136
1131 void SimpleSynchronousEntry::Doom() const { 1137 void SimpleSynchronousEntry::Doom() const {
1132 DeleteFilesForEntryHash(path_, entry_hash_); 1138 DeleteFilesForEntryHash(path_, entry_hash_);
1133 } 1139 }
1134 1140
1135 // static 1141 // static
1136 bool SimpleSynchronousEntry::DeleteFileForEntryHash( 1142 bool SimpleSynchronousEntry::DeleteFileForEntryHash(const FilePath& path,
1137 const FilePath& path, 1143 const uint64 entry_hash,
1138 const uint64 entry_hash, 1144 const int file_index) {
1139 const int file_index) {
1140 FilePath to_delete = path.AppendASCII( 1145 FilePath to_delete = path.AppendASCII(
1141 GetFilenameFromEntryHashAndFileIndex(entry_hash, file_index)); 1146 GetFilenameFromEntryHashAndFileIndex(entry_hash, file_index));
1142 return base::DeleteFile(to_delete, false); 1147 return base::DeleteFile(to_delete, false);
1143 } 1148 }
1144 1149
1145 // static 1150 // static
1146 bool SimpleSynchronousEntry::DeleteFilesForEntryHash( 1151 bool SimpleSynchronousEntry::DeleteFilesForEntryHash(const FilePath& path,
1147 const FilePath& path, 1152 const uint64 entry_hash) {
1148 const uint64 entry_hash) {
1149 bool result = true; 1153 bool result = true;
1150 for (int i = 0; i < kSimpleEntryFileCount; ++i) { 1154 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
1151 if (!DeleteFileForEntryHash(path, entry_hash, i) && !CanOmitEmptyFile(i)) 1155 if (!DeleteFileForEntryHash(path, entry_hash, i) && !CanOmitEmptyFile(i))
1152 result = false; 1156 result = false;
1153 } 1157 }
1154 FilePath to_delete = path.AppendASCII( 1158 FilePath to_delete =
1155 GetSparseFilenameFromEntryHash(entry_hash)); 1159 path.AppendASCII(GetSparseFilenameFromEntryHash(entry_hash));
1156 base::DeleteFile(to_delete, false); 1160 base::DeleteFile(to_delete, false);
1157 return result; 1161 return result;
1158 } 1162 }
1159 1163
1160 void SimpleSynchronousEntry::RecordSyncCreateResult(CreateEntryResult result, 1164 void SimpleSynchronousEntry::RecordSyncCreateResult(CreateEntryResult result,
1161 bool had_index) { 1165 bool had_index) {
1162 DCHECK_GT(CREATE_ENTRY_MAX, result); 1166 DCHECK_GT(CREATE_ENTRY_MAX, result);
1163 SIMPLE_CACHE_UMA(ENUMERATION, 1167 SIMPLE_CACHE_UMA(
1164 "SyncCreateResult", cache_type_, result, CREATE_ENTRY_MAX); 1168 ENUMERATION, "SyncCreateResult", cache_type_, result, CREATE_ENTRY_MAX);
1165 if (had_index) { 1169 if (had_index) {
1166 SIMPLE_CACHE_UMA(ENUMERATION, 1170 SIMPLE_CACHE_UMA(ENUMERATION,
1167 "SyncCreateResult_WithIndex", cache_type_, 1171 "SyncCreateResult_WithIndex",
1168 result, CREATE_ENTRY_MAX); 1172 cache_type_,
1173 result,
1174 CREATE_ENTRY_MAX);
1169 } else { 1175 } else {
1170 SIMPLE_CACHE_UMA(ENUMERATION, 1176 SIMPLE_CACHE_UMA(ENUMERATION,
1171 "SyncCreateResult_WithoutIndex", cache_type_, 1177 "SyncCreateResult_WithoutIndex",
1172 result, CREATE_ENTRY_MAX); 1178 cache_type_,
1179 result,
1180 CREATE_ENTRY_MAX);
1173 } 1181 }
1174 } 1182 }
1175 1183
1176 FilePath SimpleSynchronousEntry::GetFilenameFromFileIndex(int file_index) { 1184 FilePath SimpleSynchronousEntry::GetFilenameFromFileIndex(int file_index) {
1177 return path_.AppendASCII( 1185 return path_.AppendASCII(
1178 GetFilenameFromEntryHashAndFileIndex(entry_hash_, file_index)); 1186 GetFilenameFromEntryHashAndFileIndex(entry_hash_, file_index));
1179 } 1187 }
1180 1188
1181 bool SimpleSynchronousEntry::OpenSparseFileIfExists( 1189 bool SimpleSynchronousEntry::OpenSparseFileIfExists(
1182 int32* out_sparse_data_size) { 1190 int32* out_sparse_data_size) {
1183 DCHECK(!sparse_file_open()); 1191 DCHECK(!sparse_file_open());
1184 1192
1185 FilePath filename = path_.AppendASCII( 1193 FilePath filename =
1186 GetSparseFilenameFromEntryHash(entry_hash_)); 1194 path_.AppendASCII(GetSparseFilenameFromEntryHash(entry_hash_));
1187 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; 1195 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
1188 sparse_file_.Initialize(filename, flags); 1196 sparse_file_.Initialize(filename, flags);
1189 if (sparse_file_.IsValid()) 1197 if (sparse_file_.IsValid())
1190 return ScanSparseFile(out_sparse_data_size); 1198 return ScanSparseFile(out_sparse_data_size);
1191 1199
1192 return sparse_file_.error_details() == File::FILE_ERROR_NOT_FOUND; 1200 return sparse_file_.error_details() == File::FILE_ERROR_NOT_FOUND;
1193 } 1201 }
1194 1202
1195 bool SimpleSynchronousEntry::CreateSparseFile() { 1203 bool SimpleSynchronousEntry::CreateSparseFile() {
1196 DCHECK(!sparse_file_open()); 1204 DCHECK(!sparse_file_open());
1197 1205
1198 FilePath filename = path_.AppendASCII( 1206 FilePath filename =
1199 GetSparseFilenameFromEntryHash(entry_hash_)); 1207 path_.AppendASCII(GetSparseFilenameFromEntryHash(entry_hash_));
1200 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE; 1208 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE;
1201 sparse_file_.Initialize(filename, flags); 1209 sparse_file_.Initialize(filename, flags);
1202 if (!sparse_file_.IsValid()) 1210 if (!sparse_file_.IsValid())
1203 return false; 1211 return false;
1204 1212
1205 return InitializeSparseFile(); 1213 return InitializeSparseFile();
1206 } 1214 }
1207 1215
1208 void SimpleSynchronousEntry::CloseSparseFile() { 1216 void SimpleSynchronousEntry::CloseSparseFile() {
1209 DCHECK(sparse_file_open()); 1217 DCHECK(sparse_file_open());
(...skipping 23 matching lines...) Expand all
1233 header.key_length = key_.size(); 1241 header.key_length = key_.size();
1234 header.key_hash = base::Hash(key_); 1242 header.key_hash = base::Hash(key_);
1235 1243
1236 int header_write_result = 1244 int header_write_result =
1237 sparse_file_.Write(0, reinterpret_cast<char*>(&header), sizeof(header)); 1245 sparse_file_.Write(0, reinterpret_cast<char*>(&header), sizeof(header));
1238 if (header_write_result != sizeof(header)) { 1246 if (header_write_result != sizeof(header)) {
1239 DLOG(WARNING) << "Could not write sparse file header"; 1247 DLOG(WARNING) << "Could not write sparse file header";
1240 return false; 1248 return false;
1241 } 1249 }
1242 1250
1243 int key_write_result = sparse_file_.Write(sizeof(header), key_.data(), 1251 int key_write_result =
1244 key_.size()); 1252 sparse_file_.Write(sizeof(header), key_.data(), key_.size());
1245 if (key_write_result != implicit_cast<int>(key_.size())) { 1253 if (key_write_result != implicit_cast<int>(key_.size())) {
1246 DLOG(WARNING) << "Could not write sparse file key"; 1254 DLOG(WARNING) << "Could not write sparse file key";
1247 return false; 1255 return false;
1248 } 1256 }
1249 1257
1250 sparse_ranges_.clear(); 1258 sparse_ranges_.clear();
1251 sparse_tail_offset_ = sizeof(header) + key_.size(); 1259 sparse_tail_offset_ = sizeof(header) + key_.size();
1252 1260
1253 return true; 1261 return true;
1254 } 1262 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1311 sparse_data_size += range.length; 1319 sparse_data_size += range.length;
1312 } 1320 }
1313 1321
1314 *out_sparse_data_size = sparse_data_size; 1322 *out_sparse_data_size = sparse_data_size;
1315 sparse_tail_offset_ = range_header_offset; 1323 sparse_tail_offset_ = range_header_offset;
1316 1324
1317 return true; 1325 return true;
1318 } 1326 }
1319 1327
1320 bool SimpleSynchronousEntry::ReadSparseRange(const SparseRange* range, 1328 bool SimpleSynchronousEntry::ReadSparseRange(const SparseRange* range,
1321 int offset, int len, char* buf) { 1329 int offset,
1330 int len,
1331 char* buf) {
1322 DCHECK(range); 1332 DCHECK(range);
1323 DCHECK(buf); 1333 DCHECK(buf);
1324 DCHECK_GE(range->length, offset); 1334 DCHECK_GE(range->length, offset);
1325 DCHECK_GE(range->length, offset + len); 1335 DCHECK_GE(range->length, offset + len);
1326 1336
1327 int bytes_read = sparse_file_.Read(range->file_offset + offset, buf, len); 1337 int bytes_read = sparse_file_.Read(range->file_offset + offset, buf, len);
1328 if (bytes_read < len) { 1338 if (bytes_read < len) {
1329 DLOG(WARNING) << "Could not read sparse range."; 1339 DLOG(WARNING) << "Could not read sparse range.";
1330 return false; 1340 return false;
1331 } 1341 }
1332 1342
1333 // If we read the whole range and we have a crc32, check it. 1343 // If we read the whole range and we have a crc32, check it.
1334 if (offset == 0 && len == range->length && range->data_crc32 != 0) { 1344 if (offset == 0 && len == range->length && range->data_crc32 != 0) {
1335 uint32 actual_crc32 = crc32(crc32(0L, Z_NULL, 0), 1345 uint32 actual_crc32 =
1336 reinterpret_cast<const Bytef*>(buf), 1346 crc32(crc32(0L, Z_NULL, 0), reinterpret_cast<const Bytef*>(buf), len);
1337 len);
1338 if (actual_crc32 != range->data_crc32) { 1347 if (actual_crc32 != range->data_crc32) {
1339 DLOG(WARNING) << "Sparse range crc32 mismatch."; 1348 DLOG(WARNING) << "Sparse range crc32 mismatch.";
1340 return false; 1349 return false;
1341 } 1350 }
1342 } 1351 }
1343 // TODO(ttuttle): Incremental crc32 calculation? 1352 // TODO(ttuttle): Incremental crc32 calculation?
1344 1353
1345 return true; 1354 return true;
1346 } 1355 }
1347 1356
1348 bool SimpleSynchronousEntry::WriteSparseRange(SparseRange* range, 1357 bool SimpleSynchronousEntry::WriteSparseRange(SparseRange* range,
1349 int offset, int len, 1358 int offset,
1359 int len,
1350 const char* buf) { 1360 const char* buf) {
1351 DCHECK(range); 1361 DCHECK(range);
1352 DCHECK(buf); 1362 DCHECK(buf);
1353 DCHECK_GE(range->length, offset); 1363 DCHECK_GE(range->length, offset);
1354 DCHECK_GE(range->length, offset + len); 1364 DCHECK_GE(range->length, offset + len);
1355 1365
1356 uint32 new_crc32 = 0; 1366 uint32 new_crc32 = 0;
1357 if (offset == 0 && len == range->length) { 1367 if (offset == 0 && len == range->length) {
1358 new_crc32 = crc32(crc32(0L, Z_NULL, 0), 1368 new_crc32 =
1359 reinterpret_cast<const Bytef*>(buf), 1369 crc32(crc32(0L, Z_NULL, 0), reinterpret_cast<const Bytef*>(buf), len);
1360 len);
1361 } 1370 }
1362 1371
1363 if (new_crc32 != range->data_crc32) { 1372 if (new_crc32 != range->data_crc32) {
1364 range->data_crc32 = new_crc32; 1373 range->data_crc32 = new_crc32;
1365 1374
1366 SimpleFileSparseRangeHeader header; 1375 SimpleFileSparseRangeHeader header;
1367 header.sparse_range_magic_number = kSimpleSparseRangeMagicNumber; 1376 header.sparse_range_magic_number = kSimpleSparseRangeMagicNumber;
1368 header.offset = range->offset; 1377 header.offset = range->offset;
1369 header.length = range->length; 1378 header.length = range->length;
1370 header.data_crc32 = range->data_crc32; 1379 header.data_crc32 = range->data_crc32;
(...skipping 16 matching lines...) Expand all
1387 return true; 1396 return true;
1388 } 1397 }
1389 1398
1390 bool SimpleSynchronousEntry::AppendSparseRange(int64 offset, 1399 bool SimpleSynchronousEntry::AppendSparseRange(int64 offset,
1391 int len, 1400 int len,
1392 const char* buf) { 1401 const char* buf) {
1393 DCHECK_LE(0, offset); 1402 DCHECK_LE(0, offset);
1394 DCHECK_LT(0, len); 1403 DCHECK_LT(0, len);
1395 DCHECK(buf); 1404 DCHECK(buf);
1396 1405
1397 uint32 data_crc32 = crc32(crc32(0L, Z_NULL, 0), 1406 uint32 data_crc32 =
1398 reinterpret_cast<const Bytef*>(buf), 1407 crc32(crc32(0L, Z_NULL, 0), reinterpret_cast<const Bytef*>(buf), len);
1399 len);
1400 1408
1401 SimpleFileSparseRangeHeader header; 1409 SimpleFileSparseRangeHeader header;
1402 header.sparse_range_magic_number = kSimpleSparseRangeMagicNumber; 1410 header.sparse_range_magic_number = kSimpleSparseRangeMagicNumber;
1403 header.offset = offset; 1411 header.offset = offset;
1404 header.length = len; 1412 header.length = len;
1405 header.data_crc32 = data_crc32; 1413 header.data_crc32 = data_crc32;
1406 1414
1407 int bytes_written = sparse_file_.Write(sparse_tail_offset_, 1415 int bytes_written = sparse_file_.Write(
1408 reinterpret_cast<char*>(&header), 1416 sparse_tail_offset_, reinterpret_cast<char*>(&header), sizeof(header));
1409 sizeof(header));
1410 if (bytes_written != implicit_cast<int>(sizeof(header))) { 1417 if (bytes_written != implicit_cast<int>(sizeof(header))) {
1411 DLOG(WARNING) << "Could not append sparse range header."; 1418 DLOG(WARNING) << "Could not append sparse range header.";
1412 return false; 1419 return false;
1413 } 1420 }
1414 sparse_tail_offset_ += bytes_written; 1421 sparse_tail_offset_ += bytes_written;
1415 1422
1416 bytes_written = sparse_file_.Write(sparse_tail_offset_, buf, len); 1423 bytes_written = sparse_file_.Write(sparse_tail_offset_, buf, len);
1417 if (bytes_written < len) { 1424 if (bytes_written < len) {
1418 DLOG(WARNING) << "Could not append sparse range data."; 1425 DLOG(WARNING) << "Could not append sparse range data.";
1419 return false; 1426 return false;
1420 } 1427 }
1421 int64 data_file_offset = sparse_tail_offset_; 1428 int64 data_file_offset = sparse_tail_offset_;
1422 sparse_tail_offset_ += bytes_written; 1429 sparse_tail_offset_ += bytes_written;
1423 1430
1424 SparseRange range; 1431 SparseRange range;
1425 range.offset = offset; 1432 range.offset = offset;
1426 range.length = len; 1433 range.length = len;
1427 range.data_crc32 = data_crc32; 1434 range.data_crc32 = data_crc32;
1428 range.file_offset = data_file_offset; 1435 range.file_offset = data_file_offset;
1429 sparse_ranges_.insert(std::make_pair(offset, range)); 1436 sparse_ranges_.insert(std::make_pair(offset, range));
1430 1437
1431 return true; 1438 return true;
1432 } 1439 }
1433 1440
1434 } // namespace disk_cache 1441 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698