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.
5 // See net/disk_cache/disk_cache.h for the public interface.
7 #ifndef NET_DISK_CACHE_BLOCK_FILES_H_
8 #define NET_DISK_CACHE_BLOCK_FILES_H_
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"
24 namespace disk_cache
{
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
{
31 explicit BlockHeader(BlockFileHeader
* header
);
32 explicit BlockHeader(MappedFile
* file
);
33 BlockHeader(const BlockHeader
& other
);
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
);
41 // Deletes the block pointed by |index|.
42 void DeleteMapBlock(int index
, int block_size
);
44 // Returns true if the specified block is used.
45 bool UsedMapBlock(int index
, int size
);
47 // Restores the "empty counters" and allocation hints.
48 void FixAllocationCounters();
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
);
54 // Returns the number of empty blocks for this file.
55 int EmptyBlocks() const;
57 // Returns true if the counters look OK.
58 bool ValidateCounters() const;
60 // Returns the size of the wrapped structure (BlockFileHeader).
63 BlockFileHeader
* operator->() { return header_
; }
64 void operator=(const BlockHeader
& other
) { header_
= other
.header_
; }
65 BlockFileHeader
* Get() { return header_
; }
68 BlockFileHeader
* header_
;
71 typedef std::vector
<BlockHeader
> BlockFilesBitmaps
;
73 // This class handles the set of block-files open by the disk cache.
74 class NET_EXPORT_PRIVATE BlockFiles
{
76 explicit BlockFiles(const base::FilePath
& path
);
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
);
83 // Returns the file that stores a given address.
84 MappedFile
* GetFile(Addr address
);
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
);
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
94 void DeleteBlock(Addr address
, bool deep
);
96 // Close all the files and set the internal state to be initializad again. The
97 // cache is being purged.
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
);
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
);
112 // Attemp to grow this file. Fails if the file cannot be extended anymore.
113 bool GrowBlockFile(MappedFile
* file
, BlockFileHeader
* header
);
115 // Returns the appropriate file to use for a new block.
116 MappedFile
* FileForNewBlock(FileType block_type
, int block_count
);
118 // Returns the next block file on this chain, creating new files if needed.
119 MappedFile
* NextFile(MappedFile
* file
);
121 // Creates an empty block file and returns its index.
122 int CreateNextBlockFile(FileType block_type
);
124 // Removes a chained block file that is now empty.
125 bool RemoveEmptyFile(FileType block_type
);
127 // Restores the header of a potentially inconsistent file.
128 bool FixBlockFileHeader(MappedFile
* file
);
130 // Retrieves stats for the given file index.
131 void GetFileStats(int index
, int* used_count
, int* load
);
133 // Returns the filename for a given file index.
134 base::FilePath
Name(int index
);
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_
;
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
);
147 DISALLOW_COPY_AND_ASSIGN(BlockFiles
);
150 } // namespace disk_cache
152 #endif // NET_DISK_CACHE_BLOCK_FILES_H_