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 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/files/file_path.h" 13 #include "base/gtest_prod_util.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "net/base/net_export.h" 16 #include "net/disk_cache/addr.h" 17 #include "net/disk_cache/disk_format_base.h" 18 #include "net/disk_cache/mapped_file.h" 19 20 namespace base { 21 class ThreadChecker; 22 } 23 24 namespace disk_cache { 25 26 // An instance of this class represents the header of a block file in memory. 27 // Note that this class doesn't perform any file operation. 28 class NET_EXPORT_PRIVATE BlockHeader { 29 public: 30 BlockHeader(); 31 explicit BlockHeader(BlockFileHeader* header); 32 explicit BlockHeader(MappedFile* file); 33 BlockHeader(const BlockHeader& other); 34 ~BlockHeader(); 35 36 // Creates a new entry on the allocation map, updating the apropriate 37 // counters. |target| is the type of block to use (number of empty blocks), 38 // and |size| is the actual number of blocks to use. 39 bool CreateMapBlock(int target, int size, int* index); 40 41 // Deletes the block pointed by |index|. 42 void DeleteMapBlock(int index, int block_size); 43 44 // Returns true if the specified block is used. 45 bool UsedMapBlock(int index, int size); 46 47 // Restores the "empty counters" and allocation hints. 48 void FixAllocationCounters(); 49 50 // Returns true if the current block file should not be used as-is to store 51 // more records. |block_count| is the number of blocks to allocate. 52 bool NeedToGrowBlockFile(int block_count); 53 54 // Returns the number of empty blocks for this file. 55 int EmptyBlocks() const; 56 57 // Returns true if the counters look OK. 58 bool ValidateCounters() const; 59 60 // Returns the size of the wrapped structure (BlockFileHeader). 61 int Size() const; 62 63 BlockFileHeader* operator->() { return header_; } 64 void operator=(const BlockHeader& other) { header_ = other.header_; } 65 BlockFileHeader* Get() { return header_; } 66 67 private: 68 BlockFileHeader* header_; 69 }; 70 71 typedef std::vector<BlockHeader> BlockFilesBitmaps; 72 73 // This class handles the set of block-files open by the disk cache. 74 class NET_EXPORT_PRIVATE BlockFiles { 75 public: 76 explicit BlockFiles(const base::FilePath& path); 77 ~BlockFiles(); 78 79 // Performs the object initialization. create_files indicates if the backing 80 // files should be created or just open. 81 bool Init(bool create_files); 82 83 // Returns the file that stores a given address. 84 MappedFile* GetFile(Addr address); 85 86 // Creates a new entry on a block file. block_type indicates the size of block 87 // to be used (as defined on cache_addr.h), block_count is the number of 88 // blocks to allocate, and block_address is the address of the new entry. 89 bool CreateBlock(FileType block_type, int block_count, Addr* block_address); 90 91 // Removes an entry from the block files. If deep is true, the storage is zero 92 // filled; otherwise the entry is removed but the data is not altered (must be 93 // already zeroed). 94 void DeleteBlock(Addr address, bool deep); 95 96 // Close all the files and set the internal state to be initializad again. The 97 // cache is being purged. 98 void CloseFiles(); 99 100 // Sends UMA stats. 101 void ReportStats(); 102 103 // Returns true if the blocks pointed by a given address are currently used. 104 // This method is only intended for debugging. 105 bool IsValid(Addr address); 106 107 private: 108 // Set force to true to overwrite the file if it exists. 109 bool CreateBlockFile(int index, FileType file_type, bool force); 110 bool OpenBlockFile(int index); 111 112 // Attemp to grow this file. Fails if the file cannot be extended anymore. 113 bool GrowBlockFile(MappedFile* file, BlockFileHeader* header); 114 115 // Returns the appropriate file to use for a new block. 116 MappedFile* FileForNewBlock(FileType block_type, int block_count); 117 118 // Returns the next block file on this chain, creating new files if needed. 119 MappedFile* NextFile(MappedFile* file); 120 121 // Creates an empty block file and returns its index. 122 int CreateNextBlockFile(FileType block_type); 123 124 // Removes a chained block file that is now empty. 125 bool RemoveEmptyFile(FileType block_type); 126 127 // Restores the header of a potentially inconsistent file. 128 bool FixBlockFileHeader(MappedFile* file); 129 130 // Retrieves stats for the given file index. 131 void GetFileStats(int index, int* used_count, int* load); 132 133 // Returns the filename for a given file index. 134 base::FilePath Name(int index); 135 136 bool init_; 137 char* zero_buffer_; // Buffer to speed-up cleaning deleted entries. 138 base::FilePath path_; // Path to the backing folder. 139 std::vector<MappedFile*> block_files_; // The actual files. 140 scoped_ptr<base::ThreadChecker> thread_checker_; 141 142 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_ZeroSizeFile); 143 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_TruncatedFile); 144 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_InvalidFile); 145 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_Stats); 146 147 DISALLOW_COPY_AND_ASSIGN(BlockFiles); 148 }; 149 150 } // namespace disk_cache 151 152 #endif // NET_DISK_CACHE_BLOCK_FILES_H_ 153