OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/block_files.h" | 5 #include "net/disk_cache/block_files.h" |
6 | 6 |
7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 } | 443 } |
444 | 444 |
445 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 445 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); |
446 if (kBlockMagic != header->magic || kCurrentVersion != header->version) { | 446 if (kBlockMagic != header->magic || kCurrentVersion != header->version) { |
447 LOG(ERROR) << "Invalid file version or magic " << name.value(); | 447 LOG(ERROR) << "Invalid file version or magic " << name.value(); |
448 return false; | 448 return false; |
449 } | 449 } |
450 | 450 |
451 if (header->updating || !ValidateCounters(header)) { | 451 if (header->updating || !ValidateCounters(header)) { |
452 // Last instance was not properly shutdown, or counters are out of sync. | 452 // Last instance was not properly shutdown, or counters are out of sync. |
453 if (!FixBlockFileHeader(file)) { | 453 if (!FixBlockFileHeader(file.get())) { |
454 LOG(ERROR) << "Unable to fix block file " << name.value(); | 454 LOG(ERROR) << "Unable to fix block file " << name.value(); |
455 return false; | 455 return false; |
456 } | 456 } |
457 } | 457 } |
458 | 458 |
459 if (static_cast<int>(file_len) < | 459 if (static_cast<int>(file_len) < |
460 header->max_entries * header->entry_size + kBlockHeaderSize) { | 460 header->max_entries * header->entry_size + kBlockHeaderSize) { |
461 LOG(ERROR) << "File too small " << name.value(); | 461 LOG(ERROR) << "File too small " << name.value(); |
462 return false; | 462 return false; |
463 } | 463 } |
464 | 464 |
465 if (index == 0) { | 465 if (index == 0) { |
466 // Load the links file into memory with a single read. | 466 // Load the links file into memory with a single read. |
467 scoped_ptr<char[]> buf(new char[file_len]); | 467 scoped_ptr<char[]> buf(new char[file_len]); |
468 if (!file->Read(buf.get(), file_len, 0)) | 468 if (!file->Read(buf.get(), file_len, 0)) |
469 return false; | 469 return false; |
470 } | 470 } |
471 | 471 |
472 ScopedFlush flush(file); | 472 ScopedFlush flush(file.get()); |
473 DCHECK(!block_files_[index]); | 473 DCHECK(!block_files_[index]); |
474 file.swap(&block_files_[index]); | 474 file.swap(&block_files_[index]); |
475 return true; | 475 return true; |
476 } | 476 } |
477 | 477 |
478 bool BlockFiles::GrowBlockFile(MappedFile* file, BlockFileHeader* header) { | 478 bool BlockFiles::GrowBlockFile(MappedFile* file, BlockFileHeader* header) { |
479 if (kMaxBlocks == header->max_entries) | 479 if (kMaxBlocks == header->max_entries) |
480 return false; | 480 return false; |
481 | 481 |
482 ScopedFlush flush(file); | 482 ScopedFlush flush(file); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 } | 678 } |
679 | 679 |
680 base::FilePath BlockFiles::Name(int index) { | 680 base::FilePath BlockFiles::Name(int index) { |
681 // The file format allows for 256 files. | 681 // The file format allows for 256 files. |
682 DCHECK(index < 256 || index >= 0); | 682 DCHECK(index < 256 || index >= 0); |
683 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); | 683 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); |
684 return path_.AppendASCII(tmp); | 684 return path_.AppendASCII(tmp); |
685 } | 685 } |
686 | 686 |
687 } // namespace disk_cache | 687 } // namespace disk_cache |
OLD | NEW |