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" |
11 #include "net/disk_cache/cache_util.h" | 11 #include "net/disk_cache/cache_util.h" |
12 #include "net/disk_cache/file_lock.h" | 12 #include "net/disk_cache/file_lock.h" |
13 | 13 |
14 using base::Time; | 14 using base::Time; |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const wchar_t* kBlockName = L"data_"; | 18 const char* kBlockName = "data_"; |
19 | 19 |
20 // This array is used to perform a fast lookup of the nibble bit pattern to the | 20 // This array is used to perform a fast lookup of the nibble bit pattern to the |
21 // type of entry that can be stored there (number of consecutive blocks). | 21 // type of entry that can be stored there (number of consecutive blocks). |
22 const char s_types[16] = {4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}; | 22 const char s_types[16] = {4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}; |
23 | 23 |
24 // Returns the type of block (number of consecutive blocks that can be stored) | 24 // Returns the type of block (number of consecutive blocks that can be stored) |
25 // for a given nibble of the bitmap. | 25 // for a given nibble of the bitmap. |
26 inline int GetMapBlockType(uint8 value) { | 26 inline int GetMapBlockType(uint8 value) { |
27 value &= 0xf; | 27 value &= 0xf; |
28 return s_types[value]; | 28 return s_types[value]; |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 init_ = false; | 193 init_ = false; |
194 for (unsigned int i = 0; i < block_files_.size(); i++) { | 194 for (unsigned int i = 0; i < block_files_.size(); i++) { |
195 if (block_files_[i]) { | 195 if (block_files_[i]) { |
196 block_files_[i]->Release(); | 196 block_files_[i]->Release(); |
197 block_files_[i] = NULL; | 197 block_files_[i] = NULL; |
198 } | 198 } |
199 } | 199 } |
200 block_files_.clear(); | 200 block_files_.clear(); |
201 } | 201 } |
202 | 202 |
203 std::wstring BlockFiles::Name(int index) { | 203 FilePath BlockFiles::Name(int index) { |
204 // The file format allows for 256 files. | 204 // The file format allows for 256 files. |
205 DCHECK(index < 256 || index >= 0); | 205 DCHECK(index < 256 || index >= 0); |
206 std::wstring name(path_); | 206 std::string tmp = StringPrintf("%s%d", kBlockName, index); |
207 std::wstring tmp = StringPrintf(L"%ls%d", kBlockName, index); | 207 return path_.AppendASCII(tmp); |
208 file_util::AppendToPath(&name, tmp); | |
209 | |
210 return name; | |
211 } | 208 } |
212 | 209 |
213 bool BlockFiles::CreateBlockFile(int index, FileType file_type, bool force) { | 210 bool BlockFiles::CreateBlockFile(int index, FileType file_type, bool force) { |
214 std::wstring name = Name(index); | 211 FilePath name = Name(index); |
215 int flags = | 212 int flags = |
216 force ? base::PLATFORM_FILE_CREATE_ALWAYS : base::PLATFORM_FILE_CREATE; | 213 force ? base::PLATFORM_FILE_CREATE_ALWAYS : base::PLATFORM_FILE_CREATE; |
217 flags |= base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_WRITE; | 214 flags |= base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_WRITE; |
218 | 215 |
219 scoped_refptr<File> file(new File( | 216 scoped_refptr<File> file(new File( |
220 base::CreatePlatformFile(name.c_str(), flags, NULL))); | 217 base::CreatePlatformFile(name, flags, NULL))); |
221 if (!file->IsValid()) | 218 if (!file->IsValid()) |
222 return false; | 219 return false; |
223 | 220 |
224 BlockFileHeader header; | 221 BlockFileHeader header; |
225 header.entry_size = Addr::BlockSizeForFileType(file_type); | 222 header.entry_size = Addr::BlockSizeForFileType(file_type); |
226 header.this_file = static_cast<int16>(index); | 223 header.this_file = static_cast<int16>(index); |
227 DCHECK(index <= kint16max && index >= 0); | 224 DCHECK(index <= kint16max && index >= 0); |
228 | 225 |
229 return file->Write(&header, sizeof(header), 0); | 226 return file->Write(&header, sizeof(header), 0); |
230 } | 227 } |
231 | 228 |
232 bool BlockFiles::OpenBlockFile(int index) { | 229 bool BlockFiles::OpenBlockFile(int index) { |
233 if (block_files_.size() - 1 < static_cast<unsigned int>(index)) { | 230 if (block_files_.size() - 1 < static_cast<unsigned int>(index)) { |
234 DCHECK(index > 0); | 231 DCHECK(index > 0); |
235 int to_add = index - static_cast<int>(block_files_.size()) + 1; | 232 int to_add = index - static_cast<int>(block_files_.size()) + 1; |
236 block_files_.resize(block_files_.size() + to_add); | 233 block_files_.resize(block_files_.size() + to_add); |
237 } | 234 } |
238 | 235 |
239 std::wstring name = Name(index); | 236 FilePath name = Name(index); |
240 scoped_refptr<MappedFile> file(new MappedFile()); | 237 scoped_refptr<MappedFile> file(new MappedFile()); |
241 | 238 |
242 if (!file->Init(name, kBlockHeaderSize)) { | 239 if (!file->Init(name.ToWStringHack(), kBlockHeaderSize)) { |
243 LOG(ERROR) << "Failed to open " << name; | 240 LOG(ERROR) << "Failed to open " << name.value(); |
244 return false; | 241 return false; |
245 } | 242 } |
246 | 243 |
247 if (file->GetLength() < static_cast<size_t>(kBlockHeaderSize)) { | 244 if (file->GetLength() < static_cast<size_t>(kBlockHeaderSize)) { |
248 LOG(ERROR) << "File too small " << name; | 245 LOG(ERROR) << "File too small " << name.value(); |
249 return false; | 246 return false; |
250 } | 247 } |
251 | 248 |
252 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 249 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); |
253 if (kBlockMagic != header->magic || kCurrentVersion != header->version) { | 250 if (kBlockMagic != header->magic || kCurrentVersion != header->version) { |
254 LOG(ERROR) << "Invalid file version or magic"; | 251 LOG(ERROR) << "Invalid file version or magic"; |
255 return false; | 252 return false; |
256 } | 253 } |
257 | 254 |
258 if (header->updating) { | 255 if (header->updating) { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 reinterpret_cast<BlockFileHeader*>(next_file->buffer()); | 380 reinterpret_cast<BlockFileHeader*>(next_file->buffer()); |
384 if (!next_header->num_entries) { | 381 if (!next_header->num_entries) { |
385 DCHECK_EQ(next_header->entry_size, header->entry_size); | 382 DCHECK_EQ(next_header->entry_size, header->entry_size); |
386 // Delete next_file and remove it from the chain. | 383 // Delete next_file and remove it from the chain. |
387 int file_index = header->next_file; | 384 int file_index = header->next_file; |
388 header->next_file = next_header->next_file; | 385 header->next_file = next_header->next_file; |
389 DCHECK(block_files_.size() >= static_cast<unsigned int>(file_index)); | 386 DCHECK(block_files_.size() >= static_cast<unsigned int>(file_index)); |
390 block_files_[file_index]->Release(); | 387 block_files_[file_index]->Release(); |
391 block_files_[file_index] = NULL; | 388 block_files_[file_index] = NULL; |
392 | 389 |
393 std::wstring name = Name(file_index); | 390 FilePath name = Name(file_index); |
394 int failure = DeleteCacheFile(name) ? 0 : 1; | 391 int failure = DeleteCacheFile(name) ? 0 : 1; |
395 UMA_HISTOGRAM_COUNTS("DiskCache.DeleteFailed2", failure); | 392 UMA_HISTOGRAM_COUNTS("DiskCache.DeleteFailed2", failure); |
396 if (failure) | 393 if (failure) |
397 LOG(ERROR) << "Failed to delete " << name << " from the cache."; | 394 LOG(ERROR) << "Failed to delete " << name.value() << " from the cache."; |
398 continue; | 395 continue; |
399 } | 396 } |
400 | 397 |
401 header = next_header; | 398 header = next_header; |
402 file = next_file; | 399 file = next_file; |
403 } | 400 } |
404 } | 401 } |
405 | 402 |
406 bool BlockFiles::CreateBlock(FileType block_type, int block_count, | 403 bool BlockFiles::CreateBlock(FileType block_type, int block_count, |
407 Addr* block_address) { | 404 Addr* block_address) { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 int num_entries = (file_size - sizeof(*header)) / header->entry_size; | 479 int num_entries = (file_size - sizeof(*header)) / header->entry_size; |
483 header->max_entries = num_entries; | 480 header->max_entries = num_entries; |
484 } | 481 } |
485 | 482 |
486 FixAllocationCounters(header); | 483 FixAllocationCounters(header); |
487 header->updating = 0; | 484 header->updating = 0; |
488 return true; | 485 return true; |
489 } | 486 } |
490 | 487 |
491 } // namespace disk_cache | 488 } // namespace disk_cache |
OLD | NEW |