OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/histogram.h" | 8 #include "base/histogram.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 } | 230 } |
231 | 231 |
232 bool BlockFiles::OpenBlockFile(int index) { | 232 bool BlockFiles::OpenBlockFile(int index) { |
233 if (block_files_.size() - 1 < static_cast<unsigned int>(index)) { | 233 if (block_files_.size() - 1 < static_cast<unsigned int>(index)) { |
234 DCHECK(index > 0); | 234 DCHECK(index > 0); |
235 int to_add = index - static_cast<int>(block_files_.size()) + 1; | 235 int to_add = index - static_cast<int>(block_files_.size()) + 1; |
236 block_files_.resize(block_files_.size() + to_add); | 236 block_files_.resize(block_files_.size() + to_add); |
237 } | 237 } |
238 | 238 |
239 std::wstring name = Name(index); | 239 std::wstring name = Name(index); |
240 MappedFile* file = new MappedFile(); | 240 scoped_refptr<MappedFile> file(new MappedFile()); |
241 file->AddRef(); | |
242 | 241 |
243 if (!file->Init(name, kBlockHeaderSize)) { | 242 if (!file->Init(name, kBlockHeaderSize)) { |
244 NOTREACHED(); | |
245 LOG(ERROR) << "Failed to open " << name; | 243 LOG(ERROR) << "Failed to open " << name; |
246 file->Release(); | |
247 return false; | 244 return false; |
248 } | 245 } |
249 | 246 |
250 if (file->GetLength() < static_cast<size_t>(kBlockHeaderSize)) { | 247 if (file->GetLength() < static_cast<size_t>(kBlockHeaderSize)) { |
251 LOG(ERROR) << "File too small " << name; | 248 LOG(ERROR) << "File too small " << name; |
252 file->Release(); | |
253 return false; | 249 return false; |
254 } | 250 } |
255 | 251 |
256 block_files_[index] = file; | |
257 | |
258 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 252 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); |
259 if (kBlockMagic != header->magic || kCurrentVersion != header->version) { | 253 if (kBlockMagic != header->magic || kCurrentVersion != header->version) { |
260 LOG(ERROR) << "Invalid file version or magic"; | 254 LOG(ERROR) << "Invalid file version or magic"; |
261 return false; | 255 return false; |
262 } | 256 } |
263 | 257 |
264 if (header->updating) { | 258 if (header->updating) { |
265 // Last instance was not properly shutdown. | 259 // Last instance was not properly shutdown. |
266 if (!FixBlockFileHeader(file)) | 260 if (!FixBlockFileHeader(file)) |
267 return false; | 261 return false; |
268 } | 262 } |
| 263 |
| 264 DCHECK(!block_files_[index]); |
| 265 file.swap(&block_files_[index]); |
269 return true; | 266 return true; |
270 } | 267 } |
271 | 268 |
272 MappedFile* BlockFiles::GetFile(Addr address) { | 269 MappedFile* BlockFiles::GetFile(Addr address) { |
273 DCHECK(block_files_.size() >= 4); | 270 DCHECK(block_files_.size() >= 4); |
274 DCHECK(address.is_block_file() || !address.is_initialized()); | 271 DCHECK(address.is_block_file() || !address.is_initialized()); |
275 if (!address.is_initialized()) | 272 if (!address.is_initialized()) |
276 return NULL; | 273 return NULL; |
277 | 274 |
278 int file_index = address.FileNumber(); | 275 int file_index = address.FileNumber(); |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 int num_entries = (file_size - sizeof(*header)) / header->entry_size; | 482 int num_entries = (file_size - sizeof(*header)) / header->entry_size; |
486 header->max_entries = num_entries; | 483 header->max_entries = num_entries; |
487 } | 484 } |
488 | 485 |
489 FixAllocationCounters(header); | 486 FixAllocationCounters(header); |
490 header->updating = 0; | 487 header->updating = 0; |
491 return true; | 488 return true; |
492 } | 489 } |
493 | 490 |
494 } // namespace disk_cache | 491 } // namespace disk_cache |
OLD | NEW |