1 // Copyright (c) 2011 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 #pragma once 10 11 #include <vector> 12 13 #include "base/file_path.h" 14 #include "base/gtest_prod_util.h" 15 #include "base/memory/scoped_ptr.h" 16 #include "net/disk_cache/addr.h" 17 #include "net/disk_cache/mapped_file.h" 18 19 namespace base { 20 class ThreadChecker; 21 } 22 23 namespace disk_cache { 24 25 // This class handles the set of block-files open by the disk cache. 26 class BlockFiles { 27 public: 28 explicit BlockFiles(const FilePath& path); 29 ~BlockFiles(); 30 31 // Performs the object initialization. create_files indicates if the backing 32 // files should be created or just open. 33 bool Init(bool create_files); 34 35 // Returns the file that stores a given address. 36 MappedFile* GetFile(Addr address); 37 38 // Creates a new entry on a block file. block_type indicates the size of block 39 // to be used (as defined on cache_addr.h), block_count is the number of 40 // blocks to allocate, and block_address is the address of the new entry. 41 bool CreateBlock(FileType block_type, int block_count, Addr* block_address); 42 43 // Removes an entry from the block files. If deep is true, the storage is zero 44 // filled; otherwise the entry is removed but the data is not altered (must be 45 // already zeroed). 46 void DeleteBlock(Addr address, bool deep); 47 48 // Close all the files and set the internal state to be initializad again. The 49 // cache is being purged. 50 void CloseFiles(); 51 52 // Sends UMA stats. 53 void ReportStats(); 54 55 // Returns true if the blocks pointed by a given address are currently used. 56 // This method is only intended for debugging. 57 bool IsValid(Addr address); 58 59 private: 60 // Set force to true to overwrite the file if it exists. 61 bool CreateBlockFile(int index, FileType file_type, bool force); 62 bool OpenBlockFile(int index); 63 64 // Attemp to grow this file. Fails if the file cannot be extended anymore. 65 bool GrowBlockFile(MappedFile* file, BlockFileHeader* header); 66 67 // Returns the appropriate file to use for a new block. 68 MappedFile* FileForNewBlock(FileType block_type, int block_count); 69 70 // Returns the next block file on this chain, creating new files if needed. 71 MappedFile* NextFile(const MappedFile* file); 72 73 // Creates an empty block file and returns its index. 74 int CreateNextBlockFile(FileType block_type); 75 76 // Removes a chained block file that is now empty. 77 void RemoveEmptyFile(FileType block_type); 78 79 // Restores the header of a potentially inconsistent file. 80 bool FixBlockFileHeader(MappedFile* file); 81 82 // Retrieves stats for the given file index. 83 void GetFileStats(int index, int* used_count, int* load); 84 85 // Returns the filename for a given file index. 86 FilePath Name(int index); 87 88 bool init_; 89 char* zero_buffer_; // Buffer to speed-up cleaning deleted entries. 90 FilePath path_; // Path to the backing folder. 91 std::vector<MappedFile*> block_files_; // The actual files. 92 scoped_ptr<base::ThreadChecker> thread_checker_; 93 94 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_ZeroSizeFile); 95 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_TruncatedFile); 96 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_InvalidFile); 97 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_Stats); 98 99 DISALLOW_COPY_AND_ASSIGN(BlockFiles); 100 }; 101 102 } // namespace disk_cache 103 104 #endif // NET_DISK_CACHE_BLOCK_FILES_H__ 105