• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 // See net/disk_cache/disk_cache.h for the public interface.
6 
7 #ifndef NET_DISK_CACHE_BLOCK_FILES_H__
8 #define NET_DISK_CACHE_BLOCK_FILES_H__
9 
10 #include <vector>
11 
12 #include "base/file_path.h"
13 #include "net/disk_cache/addr.h"
14 #include "net/disk_cache/mapped_file.h"
15 #include "testing/gtest/include/gtest/gtest_prod.h"
16 
17 namespace disk_cache {
18 
19 class EntryImpl;
20 
21 // This class handles the set of block-files open by the disk cache.
22 class BlockFiles {
23  public:
BlockFiles(const FilePath & path)24   explicit BlockFiles(const FilePath& path)
25       : init_(false), zero_buffer_(NULL), path_(path) {}
26   ~BlockFiles();
27 
28   // Performs the object initialization. create_files indicates if the backing
29   // files should be created or just open.
30   bool Init(bool create_files);
31 
32   // Returns the file that stores a given address.
33   MappedFile* GetFile(Addr address);
34 
35   // Creates a new entry on a block file. block_type indicates the size of block
36   // to be used (as defined on cache_addr.h), block_count is the number of
37   // blocks to allocate, and block_address is the address of the new entry.
38   bool CreateBlock(FileType block_type, int block_count, Addr* block_address);
39 
40   // Removes an entry from the block files. If deep is true, the storage is zero
41   // filled; otherwise the entry is removed but the data is not altered (must be
42   // already zeroed).
43   void DeleteBlock(Addr address, bool deep);
44 
45   // Close all the files and set the internal state to be initializad again. The
46   // cache is being purged.
47   void CloseFiles();
48 
49  private:
50   // Set force to true to overwrite the file if it exists.
51   bool CreateBlockFile(int index, FileType file_type, bool force);
52   bool OpenBlockFile(int index);
53 
54   // Attemp to grow this file. Fails if the file cannot be extended anymore.
55   bool GrowBlockFile(MappedFile* file, BlockFileHeader* header);
56 
57   // Returns the appropriate file to use for a new block.
58   MappedFile* FileForNewBlock(FileType block_type, int block_count);
59 
60   // Returns the next block file on this chain, creating new files if needed.
61   MappedFile* NextFile(const MappedFile* file);
62 
63   // Creates an empty block file and returns its index.
64   int CreateNextBlockFile(FileType block_type);
65 
66   // Removes a chained block file that is now empty.
67   void RemoveEmptyFile(FileType block_type);
68 
69   // Restores the header of a potentially inconsistent file.
70   bool FixBlockFileHeader(MappedFile* file);
71 
72   // Returns the filename for a given file index.
73   FilePath Name(int index);
74 
75   bool init_;
76   char* zero_buffer_;  // Buffer to speed-up cleaning deleted entries.
77   FilePath path_;  // Path to the backing folder.
78   std::vector<MappedFile*> block_files_;  // The actual files.
79 
80   FRIEND_TEST(DiskCacheTest, BlockFiles_ZeroSizeFile);
81   FRIEND_TEST(DiskCacheTest, BlockFiles_InvalidFile);
82 
83   DISALLOW_EVIL_CONSTRUCTORS(BlockFiles);
84 };
85 
86 }  // namespace disk_cache
87 
88 #endif  // NET_DISK_CACHE_BLOCK_FILES_H__
89