• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/disk_cache/blockfile/block_files.h"
6 
7 #include <atomic>
8 #include <limits>
9 #include <memory>
10 
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/metrics/histogram_macros.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/threading/thread_checker.h"
17 #include "base/time/time.h"
18 #include "net/disk_cache/blockfile/file_lock.h"
19 #include "net/disk_cache/blockfile/stress_support.h"
20 #include "net/disk_cache/cache_util.h"
21 #include "third_party/abseil-cpp/absl/types/optional.h"
22 
23 using base::TimeTicks;
24 
25 namespace {
26 
27 const char kBlockName[] = "data_";
28 
29 // This array is used to perform a fast lookup of the nibble bit pattern to the
30 // type of entry that can be stored there (number of consecutive blocks).
31 const char s_types[16] = {4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
32 
33 // Returns the type of block (number of consecutive blocks that can be stored)
34 // for a given nibble of the bitmap.
GetMapBlockType(uint32_t value)35 inline int GetMapBlockType(uint32_t value) {
36   value &= 0xf;
37   return s_types[value];
38 }
39 
40 }  // namespace
41 
42 namespace disk_cache {
43 
BlockHeader()44 BlockHeader::BlockHeader() : header_(nullptr) {}
45 
BlockHeader(BlockFileHeader * header)46 BlockHeader::BlockHeader(BlockFileHeader* header) : header_(header) {
47 }
48 
BlockHeader(MappedFile * file)49 BlockHeader::BlockHeader(MappedFile* file)
50     : header_(reinterpret_cast<BlockFileHeader*>(file->buffer())) {
51 }
52 
53 BlockHeader::BlockHeader(const BlockHeader& other) = default;
54 
55 BlockHeader::~BlockHeader() = default;
56 
CreateMapBlock(int size,int * index)57 bool BlockHeader::CreateMapBlock(int size, int* index) {
58   DCHECK(size > 0 && size <= kMaxNumBlocks);
59   int target = 0;
60   for (int i = size; i <= kMaxNumBlocks; i++) {
61     if (header_->empty[i - 1]) {
62       target = i;
63       break;
64     }
65   }
66 
67   if (!target) {
68     STRESS_NOTREACHED();
69     return false;
70   }
71 
72   TimeTicks start = TimeTicks::Now();
73   // We are going to process the map on 32-block chunks (32 bits), and on every
74   // chunk, iterate through the 8 nibbles where the new block can be located.
75   int current = header_->hints[target - 1];
76   for (int i = 0; i < header_->max_entries / 32; i++, current++) {
77     if (current == header_->max_entries / 32)
78       current = 0;
79     uint32_t map_block = header_->allocation_map[current];
80 
81     for (int j = 0; j < 8; j++, map_block >>= 4) {
82       if (GetMapBlockType(map_block) != target)
83         continue;
84 
85       disk_cache::FileLock lock(header_);
86       int index_offset = j * 4 + 4 - target;
87       *index = current * 32 + index_offset;
88       STRESS_DCHECK(*index / 4 == (*index + size - 1) / 4);
89       uint32_t to_add = ((1 << size) - 1) << index_offset;
90       header_->num_entries++;
91 
92       // Note that there is no race in the normal sense here, but if we enforce
93       // the order of memory accesses between num_entries and allocation_map, we
94       // can assert that even if we crash here, num_entries will never be less
95       // than the actual number of used blocks.
96       std::atomic_thread_fence(std::memory_order_seq_cst);
97       header_->allocation_map[current] |= to_add;
98 
99       header_->hints[target - 1] = current;
100       header_->empty[target - 1]--;
101       STRESS_DCHECK(header_->empty[target - 1] >= 0);
102       if (target != size) {
103         header_->empty[target - size - 1]++;
104       }
105       LOCAL_HISTOGRAM_TIMES("DiskCache.CreateBlock", TimeTicks::Now() - start);
106       return true;
107     }
108   }
109 
110   // It is possible to have an undetected corruption (for example when the OS
111   // crashes), fix it here.
112   LOG(ERROR) << "Failing CreateMapBlock";
113   FixAllocationCounters();
114   return false;
115 }
116 
DeleteMapBlock(int index,int size)117 void BlockHeader::DeleteMapBlock(int index, int size) {
118   if (size < 0 || size > kMaxNumBlocks) {
119     NOTREACHED();
120     return;
121   }
122   TimeTicks start = TimeTicks::Now();
123   int byte_index = index / 8;
124   uint8_t* byte_map = reinterpret_cast<uint8_t*>(header_->allocation_map);
125   uint8_t map_block = byte_map[byte_index];
126 
127   if (index % 8 >= 4)
128     map_block >>= 4;
129 
130   // See what type of block will be available after we delete this one.
131   int bits_at_end = 4 - size - index % 4;
132   uint8_t end_mask = (0xf << (4 - bits_at_end)) & 0xf;
133   bool update_counters = (map_block & end_mask) == 0;
134   uint8_t new_value = map_block & ~(((1 << size) - 1) << (index % 4));
135   int new_type = GetMapBlockType(new_value);
136 
137   disk_cache::FileLock lock(header_);
138   STRESS_DCHECK((((1 << size) - 1) << (index % 8)) < 0x100);
139   uint8_t to_clear = ((1 << size) - 1) << (index % 8);
140   STRESS_DCHECK((byte_map[byte_index] & to_clear) == to_clear);
141   byte_map[byte_index] &= ~to_clear;
142 
143   if (update_counters) {
144     if (bits_at_end)
145       header_->empty[bits_at_end - 1]--;
146     header_->empty[new_type - 1]++;
147     STRESS_DCHECK(header_->empty[bits_at_end - 1] >= 0);
148   }
149   std::atomic_thread_fence(std::memory_order_seq_cst);
150   header_->num_entries--;
151   STRESS_DCHECK(header_->num_entries >= 0);
152   LOCAL_HISTOGRAM_TIMES("DiskCache.DeleteBlock", TimeTicks::Now() - start);
153 }
154 
155 // Note that this is a simplified version of DeleteMapBlock().
UsedMapBlock(int index,int size)156 bool BlockHeader::UsedMapBlock(int index, int size) {
157   if (size < 0 || size > kMaxNumBlocks)
158     return false;
159 
160   int byte_index = index / 8;
161   uint8_t* byte_map = reinterpret_cast<uint8_t*>(header_->allocation_map);
162 
163   STRESS_DCHECK((((1 << size) - 1) << (index % 8)) < 0x100);
164   uint8_t to_clear = ((1 << size) - 1) << (index % 8);
165   return ((byte_map[byte_index] & to_clear) == to_clear);
166 }
167 
FixAllocationCounters()168 void BlockHeader::FixAllocationCounters() {
169   for (int i = 0; i < kMaxNumBlocks; i++) {
170     header_->hints[i] = 0;
171     header_->empty[i] = 0;
172   }
173 
174   for (int i = 0; i < header_->max_entries / 32; i++) {
175     uint32_t map_block = header_->allocation_map[i];
176 
177     for (int j = 0; j < 8; j++, map_block >>= 4) {
178       int type = GetMapBlockType(map_block);
179       if (type)
180         header_->empty[type -1]++;
181     }
182   }
183 }
184 
NeedToGrowBlockFile(int block_count) const185 bool BlockHeader::NeedToGrowBlockFile(int block_count) const {
186   bool have_space = false;
187   int empty_blocks = 0;
188   for (int i = 0; i < kMaxNumBlocks; i++) {
189     empty_blocks += header_->empty[i] * (i + 1);
190     if (i >= block_count - 1 && header_->empty[i])
191       have_space = true;
192   }
193 
194   if (header_->next_file && (empty_blocks < kMaxBlocks / 10)) {
195     // This file is almost full but we already created another one, don't use
196     // this file yet so that it is easier to find empty blocks when we start
197     // using this file again.
198     return true;
199   }
200   return !have_space;
201 }
202 
CanAllocate(int block_count) const203 bool BlockHeader::CanAllocate(int block_count) const {
204   DCHECK_GT(block_count, 0);
205   for (int i = block_count - 1; i < kMaxNumBlocks; i++) {
206     if (header_->empty[i])
207       return true;
208   }
209 
210   return false;
211 }
212 
EmptyBlocks() const213 int BlockHeader::EmptyBlocks() const {
214   int empty_blocks = 0;
215   for (int i = 0; i < kMaxNumBlocks; i++) {
216     empty_blocks += header_->empty[i] * (i + 1);
217     if (header_->empty[i] < 0)
218       return 0;
219   }
220   return empty_blocks;
221 }
222 
MinimumAllocations() const223 int BlockHeader::MinimumAllocations() const {
224   return header_->empty[kMaxNumBlocks - 1];
225 }
226 
Capacity() const227 int BlockHeader::Capacity() const {
228   return header_->max_entries;
229 }
230 
ValidateCounters() const231 bool BlockHeader::ValidateCounters() const {
232   if (header_->max_entries < 0 || header_->max_entries > kMaxBlocks ||
233       header_->num_entries < 0)
234     return false;
235 
236   int empty_blocks = EmptyBlocks();
237   if (empty_blocks + header_->num_entries > header_->max_entries)
238     return false;
239 
240   return true;
241 }
242 
FileId() const243 int BlockHeader::FileId() const {
244   return header_->this_file;
245 }
246 
NextFileId() const247 int BlockHeader::NextFileId() const {
248   return header_->next_file;
249 }
250 
Size() const251 int BlockHeader::Size() const {
252   return static_cast<int>(sizeof(*header_));
253 }
254 
Header()255 BlockFileHeader* BlockHeader::Header() {
256   return header_;
257 }
258 
259 // ------------------------------------------------------------------------
260 
BlockFiles(const base::FilePath & path)261 BlockFiles::BlockFiles(const base::FilePath& path) : path_(path) {}
262 
~BlockFiles()263 BlockFiles::~BlockFiles() {
264   CloseFiles();
265 }
266 
Init(bool create_files)267 bool BlockFiles::Init(bool create_files) {
268   DCHECK(!init_);
269   if (init_)
270     return false;
271 
272   thread_checker_ = std::make_unique<base::ThreadChecker>();
273 
274   block_files_.resize(kFirstAdditionalBlockFile);
275   for (int16_t i = 0; i < kFirstAdditionalBlockFile; i++) {
276     if (create_files)
277       if (!CreateBlockFile(i, static_cast<FileType>(i + 1), true))
278         return false;
279 
280     if (!OpenBlockFile(i))
281       return false;
282 
283     // Walk this chain of files removing empty ones.
284     if (!RemoveEmptyFile(static_cast<FileType>(i + 1)))
285       return false;
286   }
287 
288   init_ = true;
289   return true;
290 }
291 
GetFile(Addr address)292 MappedFile* BlockFiles::GetFile(Addr address) {
293   DCHECK(thread_checker_->CalledOnValidThread());
294   DCHECK_GE(block_files_.size(),
295             static_cast<size_t>(kFirstAdditionalBlockFile));
296   DCHECK(address.is_block_file() || !address.is_initialized());
297   if (!address.is_initialized())
298     return nullptr;
299 
300   int file_index = address.FileNumber();
301   if (static_cast<unsigned int>(file_index) >= block_files_.size() ||
302       !block_files_[file_index]) {
303     // We need to open the file
304     if (!OpenBlockFile(file_index))
305       return nullptr;
306   }
307   DCHECK_GE(block_files_.size(), static_cast<unsigned int>(file_index));
308   return block_files_[file_index].get();
309 }
310 
CreateBlock(FileType block_type,int block_count,Addr * block_address)311 bool BlockFiles::CreateBlock(FileType block_type, int block_count,
312                              Addr* block_address) {
313   DCHECK(thread_checker_->CalledOnValidThread());
314   DCHECK_NE(block_type, EXTERNAL);
315   DCHECK_NE(block_type, BLOCK_FILES);
316   DCHECK_NE(block_type, BLOCK_ENTRIES);
317   DCHECK_NE(block_type, BLOCK_EVICTED);
318   if (block_count < 1 || block_count > kMaxNumBlocks)
319     return false;
320 
321   if (!init_)
322     return false;
323 
324   MappedFile* file = FileForNewBlock(block_type, block_count);
325   if (!file)
326     return false;
327 
328   ScopedFlush flush(file);
329   BlockHeader file_header(file);
330 
331   int index;
332   if (!file_header.CreateMapBlock(block_count, &index))
333     return false;
334 
335   Addr address(block_type, block_count, file_header.FileId(), index);
336   block_address->set_value(address.value());
337   return true;
338 }
339 
DeleteBlock(Addr address,bool deep)340 void BlockFiles::DeleteBlock(Addr address, bool deep) {
341   DCHECK(thread_checker_->CalledOnValidThread());
342   if (!address.is_initialized() || address.is_separate_file())
343     return;
344 
345   MappedFile* file = GetFile(address);
346   if (!file)
347     return;
348 
349   if (zero_buffer_.empty())
350     zero_buffer_.resize(Addr::BlockSizeForFileType(BLOCK_4K) * 4, 0);
351 
352   size_t size = address.BlockSize() * address.num_blocks();
353   size_t offset = address.start_block() * address.BlockSize() +
354                   kBlockHeaderSize;
355   if (deep)
356     file->Write(zero_buffer_.data(), size, offset);
357 
358   absl::optional<FileType> type_to_delete;
359   {
360     // Block Header can't outlive file's buffer.
361     BlockHeader file_header(file);
362     file_header.DeleteMapBlock(address.start_block(), address.num_blocks());
363     file->Flush();
364 
365     if (!file_header.Header()->num_entries) {
366       // This file is now empty. Let's try to delete it.
367       type_to_delete = Addr::RequiredFileType(file_header.Header()->entry_size);
368       if (Addr::BlockSizeForFileType(RANKINGS) ==
369           file_header.Header()->entry_size) {
370         type_to_delete = RANKINGS;
371       }
372     }
373   }
374   if (type_to_delete.has_value()) {
375     RemoveEmptyFile(type_to_delete.value());  // Ignore failures.
376   }
377 }
378 
CloseFiles()379 void BlockFiles::CloseFiles() {
380   if (init_) {
381     DCHECK(thread_checker_->CalledOnValidThread());
382   }
383   init_ = false;
384   block_files_.clear();
385 }
386 
IsValid(Addr address)387 bool BlockFiles::IsValid(Addr address) {
388 #ifdef NDEBUG
389   return true;
390 #else
391   if (!address.is_initialized() || address.is_separate_file())
392     return false;
393 
394   MappedFile* file = GetFile(address);
395   if (!file)
396     return false;
397 
398   BlockHeader header(file);
399   bool rv = header.UsedMapBlock(address.start_block(), address.num_blocks());
400   DCHECK(rv);
401 
402   static bool read_contents = false;
403   if (read_contents) {
404     auto buffer =
405         std::make_unique<char[]>(Addr::BlockSizeForFileType(BLOCK_4K) * 4);
406     size_t size = address.BlockSize() * address.num_blocks();
407     size_t offset = address.start_block() * address.BlockSize() +
408                     kBlockHeaderSize;
409     bool ok = file->Read(buffer.get(), size, offset);
410     DCHECK(ok);
411   }
412 
413   return rv;
414 #endif
415 }
416 
CreateBlockFile(int index,FileType file_type,bool force)417 bool BlockFiles::CreateBlockFile(int index, FileType file_type, bool force) {
418   base::FilePath name = Name(index);
419   int flags = force ? base::File::FLAG_CREATE_ALWAYS : base::File::FLAG_CREATE;
420   flags |= base::File::FLAG_WRITE | base::File::FLAG_WIN_EXCLUSIVE_WRITE;
421 
422   auto file = base::MakeRefCounted<File>(base::File(name, flags));
423   if (!file->IsValid())
424     return false;
425 
426   BlockFileHeader header;
427   memset(&header, 0, sizeof(header));
428   header.magic = kBlockMagic;
429   header.version = kBlockVersion2;
430   header.entry_size = Addr::BlockSizeForFileType(file_type);
431   header.this_file = static_cast<int16_t>(index);
432   DCHECK(index <= std::numeric_limits<int16_t>::max() && index >= 0);
433 
434   return file->Write(&header, sizeof(header), 0);
435 }
436 
OpenBlockFile(int index)437 bool BlockFiles::OpenBlockFile(int index) {
438   if (block_files_.size() - 1 < static_cast<unsigned int>(index)) {
439     DCHECK(index > 0);
440     int to_add = index - static_cast<int>(block_files_.size()) + 1;
441     block_files_.resize(block_files_.size() + to_add);
442   }
443 
444   base::FilePath name = Name(index);
445   auto file = base::MakeRefCounted<MappedFile>();
446 
447   if (!file->Init(name, kBlockHeaderSize)) {
448     LOG(ERROR) << "Failed to open " << name.value();
449     return false;
450   }
451 
452   size_t file_len = file->GetLength();
453   if (file_len < static_cast<size_t>(kBlockHeaderSize)) {
454     LOG(ERROR) << "File too small " << name.value();
455     return false;
456   }
457 
458   BlockHeader file_header(file.get());
459   BlockFileHeader* header = file_header.Header();
460   if (kBlockMagic != header->magic || kBlockVersion2 != header->version) {
461     LOG(ERROR) << "Invalid file version or magic " << name.value();
462     return false;
463   }
464 
465   if (header->updating || !file_header.ValidateCounters()) {
466     // Last instance was not properly shutdown, or counters are out of sync.
467     if (!FixBlockFileHeader(file.get())) {
468       LOG(ERROR) << "Unable to fix block file " << name.value();
469       return false;
470     }
471   }
472 
473   if (static_cast<int>(file_len) <
474       header->max_entries * header->entry_size + kBlockHeaderSize) {
475     LOG(ERROR) << "File too small " << name.value();
476     return false;
477   }
478 
479   if (index == 0) {
480     // Load the links file into memory.
481     if (!file->Preload())
482       return false;
483   }
484 
485   ScopedFlush flush(file.get());
486   DCHECK(!block_files_[index]);
487   block_files_[index] = std::move(file);
488   return true;
489 }
490 
GrowBlockFile(MappedFile * file,BlockFileHeader * header)491 bool BlockFiles::GrowBlockFile(MappedFile* file, BlockFileHeader* header) {
492   if (kMaxBlocks == header->max_entries)
493     return false;
494 
495   ScopedFlush flush(file);
496   DCHECK(!header->empty[3]);
497   int new_size = header->max_entries + 1024;
498   if (new_size > kMaxBlocks)
499     new_size = kMaxBlocks;
500 
501   int new_size_bytes = new_size * header->entry_size + sizeof(*header);
502 
503   if (!file->SetLength(new_size_bytes)) {
504     // Most likely we are trying to truncate the file, so the header is wrong.
505     if (header->updating < 10 && !FixBlockFileHeader(file)) {
506       // If we can't fix the file increase the lock guard so we'll pick it on
507       // the next start and replace it.
508       header->updating = 100;
509       return false;
510     }
511     return (header->max_entries >= new_size);
512   }
513 
514   FileLock lock(header);
515   header->empty[3] = (new_size - header->max_entries) / 4;  // 4 blocks entries
516   header->max_entries = new_size;
517 
518   return true;
519 }
520 
FileForNewBlock(FileType block_type,int block_count)521 MappedFile* BlockFiles::FileForNewBlock(FileType block_type, int block_count) {
522   static_assert(RANKINGS == 1, "invalid file type");
523   MappedFile* file = block_files_[block_type - 1].get();
524   BlockHeader file_header(file);
525 
526   TimeTicks start = TimeTicks::Now();
527   while (file_header.NeedToGrowBlockFile(block_count)) {
528     if (kMaxBlocks == file_header.Header()->max_entries) {
529       file = NextFile(file);
530       if (!file)
531         return nullptr;
532       file_header = BlockHeader(file);
533       continue;
534     }
535 
536     if (!GrowBlockFile(file, file_header.Header()))
537       return nullptr;
538     break;
539   }
540   LOCAL_HISTOGRAM_TIMES("DiskCache.GetFileForNewBlock",
541                         TimeTicks::Now() - start);
542   return file;
543 }
544 
NextFile(MappedFile * file)545 MappedFile* BlockFiles::NextFile(MappedFile* file) {
546   ScopedFlush flush(file);
547   BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer());
548   int16_t new_file = header->next_file;
549   if (!new_file) {
550     // RANKINGS is not reported as a type for small entries, but we may be
551     // extending the rankings block file.
552     FileType type = Addr::RequiredFileType(header->entry_size);
553     if (header->entry_size == Addr::BlockSizeForFileType(RANKINGS))
554       type = RANKINGS;
555 
556     new_file = CreateNextBlockFile(type);
557     if (!new_file)
558       return nullptr;
559 
560     FileLock lock(header);
561     header->next_file = new_file;
562   }
563 
564   // Only the block_file argument is relevant for what we want.
565   Addr address(BLOCK_256, 1, new_file, 0);
566   return GetFile(address);
567 }
568 
CreateNextBlockFile(FileType block_type)569 int16_t BlockFiles::CreateNextBlockFile(FileType block_type) {
570   for (int16_t i = kFirstAdditionalBlockFile; i <= kMaxBlockFile; i++) {
571     if (CreateBlockFile(i, block_type, false))
572       return i;
573   }
574   return 0;
575 }
576 
577 // We walk the list of files for this particular block type, deleting the ones
578 // that are empty.
RemoveEmptyFile(FileType block_type)579 bool BlockFiles::RemoveEmptyFile(FileType block_type) {
580   MappedFile* file = block_files_[block_type - 1].get();
581   BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer());
582 
583   while (header->next_file) {
584     // Only the block_file argument is relevant for what we want.
585     Addr address(BLOCK_256, 1, header->next_file, 0);
586     MappedFile* next_file = GetFile(address);
587     if (!next_file)
588       return false;
589 
590     BlockFileHeader* next_header =
591         reinterpret_cast<BlockFileHeader*>(next_file->buffer());
592     if (!next_header->num_entries) {
593       DCHECK_EQ(next_header->entry_size, header->entry_size);
594       // Delete next_file and remove it from the chain.
595       int file_index = header->next_file;
596       header->next_file = next_header->next_file;
597       DCHECK(block_files_.size() >= static_cast<unsigned int>(file_index));
598       file->Flush();
599 
600       // We get a new handle to the file and release the old one so that the
601       // file gets unmmaped... so we can delete it.
602       base::FilePath name = Name(file_index);
603       auto this_file = base::MakeRefCounted<File>(false);
604       this_file->Init(name);
605       block_files_[file_index] = nullptr;
606 
607       int failure = base::DeleteFile(name) ? 0 : 1;
608       UMA_HISTOGRAM_COUNTS_1M("DiskCache.DeleteFailed2", failure);
609       if (failure)
610         LOG(ERROR) << "Failed to delete " << name.value() << " from the cache.";
611       continue;
612     }
613 
614     header = next_header;
615     file = next_file;
616   }
617   return true;
618 }
619 
620 // Note that we expect to be called outside of a FileLock... however, we cannot
621 // DCHECK on header->updating because we may be fixing a crash.
FixBlockFileHeader(MappedFile * file)622 bool BlockFiles::FixBlockFileHeader(MappedFile* file) {
623   ScopedFlush flush(file);
624   BlockHeader file_header(file);
625   int file_size = static_cast<int>(file->GetLength());
626   if (file_size < file_header.Size())
627     return false;  // file_size > 2GB is also an error.
628 
629   const int kMinHeaderBlockSize = 36;
630   const int kMaxHeaderBlockSize = 4096;
631   BlockFileHeader* header = file_header.Header();
632   if (header->entry_size < kMinHeaderBlockSize ||
633       header->entry_size > kMaxHeaderBlockSize || header->num_entries < 0)
634     return false;
635 
636   // Make sure that we survive crashes.
637   header->updating = 1;
638   int expected = header->entry_size * header->max_entries + file_header.Size();
639   if (file_size != expected) {
640     int max_expected = header->entry_size * kMaxBlocks + file_header.Size();
641     if (file_size < expected || header->empty[3] || file_size > max_expected) {
642       LOG(ERROR) << "Unexpected file size";
643       return false;
644     }
645     // We were in the middle of growing the file.
646     int num_entries = (file_size - file_header.Size()) / header->entry_size;
647     header->max_entries = num_entries;
648   }
649 
650   file_header.FixAllocationCounters();
651   int empty_blocks = file_header.EmptyBlocks();
652   if (empty_blocks + header->num_entries > header->max_entries)
653     header->num_entries = header->max_entries - empty_blocks;
654 
655   if (!file_header.ValidateCounters())
656     return false;
657 
658   header->updating = 0;
659   return true;
660 }
661 
Name(int index)662 base::FilePath BlockFiles::Name(int index) {
663   // The file format allows for 256 files.
664   DCHECK(index < 256 && index >= 0);
665   std::string tmp = base::StringPrintf("%s%d", kBlockName, index);
666   return path_.AppendASCII(tmp);
667 }
668 
669 }  // namespace disk_cache
670