1 // Copyright (c) 2013 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 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_
14 #include "base/files/file.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/time/time.h"
19 #include "net/base/cache_type.h"
20 #include "net/base/net_export.h"
21 #include "net/disk_cache/simple/simple_entry_format.h"
24 class GrowableIOBuffer
;
28 namespace disk_cache
{
30 class SimpleSynchronousEntry
;
32 // This class handles the passing of data about the entry between
33 // SimpleEntryImplementation and SimpleSynchronousEntry and the computation of
34 // file offsets based on the data size for all streams.
35 class NET_EXPORT_PRIVATE SimpleEntryStat
{
37 SimpleEntryStat(base::Time last_used
,
38 base::Time last_modified
,
39 const int32 data_size
[],
40 const int32 sparse_data_size
);
42 int GetOffsetInFile(const std::string
& key
,
44 int stream_index
) const;
45 int GetEOFOffsetInFile(const std::string
& key
, int stream_index
) const;
46 int GetLastEOFOffsetInFile(const std::string
& key
, int file_index
) const;
47 int64
GetFileSize(const std::string
& key
, int file_index
) const;
49 base::Time
last_used() const { return last_used_
; }
50 base::Time
last_modified() const { return last_modified_
; }
51 void set_last_used(base::Time last_used
) { last_used_
= last_used
; }
52 void set_last_modified(base::Time last_modified
) {
53 last_modified_
= last_modified
;
56 int32
data_size(int stream_index
) const { return data_size_
[stream_index
]; }
57 void set_data_size(int stream_index
, int data_size
) {
58 data_size_
[stream_index
] = data_size
;
61 int32
sparse_data_size() const { return sparse_data_size_
; }
62 void set_sparse_data_size(int32 sparse_data_size
) {
63 sparse_data_size_
= sparse_data_size
;
67 base::Time last_used_
;
68 base::Time last_modified_
;
69 int32 data_size_
[kSimpleEntryStreamCount
];
70 int32 sparse_data_size_
;
73 struct SimpleEntryCreationResults
{
74 explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat
);
75 ~SimpleEntryCreationResults();
77 SimpleSynchronousEntry
* sync_entry
;
78 scoped_refptr
<net::GrowableIOBuffer
> stream_0_data
;
79 SimpleEntryStat entry_stat
;
80 uint32 stream_0_crc32
;
84 // Worker thread interface to the very simple cache. This interface is not
85 // thread safe, and callers must ensure that it is only ever accessed from
86 // a single thread between synchronization points.
87 class SimpleSynchronousEntry
{
91 CRCRecord(int index_p
, bool has_crc32_p
, uint32 data_crc32_p
);
98 struct EntryOperationData
{
99 EntryOperationData(int index_p
, int offset_p
, int buf_len_p
);
100 EntryOperationData(int index_p
,
105 EntryOperationData(int64 sparse_offset_p
, int buf_len_p
);
115 static void OpenEntry(net::CacheType cache_type
,
116 const base::FilePath
& path
,
119 SimpleEntryCreationResults
* out_results
);
121 static void CreateEntry(net::CacheType cache_type
,
122 const base::FilePath
& path
,
123 const std::string
& key
,
126 SimpleEntryCreationResults
* out_results
);
128 // Deletes an entry from the file system without affecting the state of the
129 // corresponding instance, if any (allowing operations to continue to be
130 // executed through that instance). Returns a net error code.
131 static int DoomEntry(const base::FilePath
& path
,
134 // Like |DoomEntry()| above. Deletes all entries corresponding to the
135 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net
137 static int DoomEntrySet(const std::vector
<uint64
>* key_hashes
,
138 const base::FilePath
& path
);
140 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO.
141 void ReadData(const EntryOperationData
& in_entry_op
,
142 net::IOBuffer
* out_buf
,
144 SimpleEntryStat
* entry_stat
,
145 int* out_result
) const;
146 void WriteData(const EntryOperationData
& in_entry_op
,
147 net::IOBuffer
* in_buf
,
148 SimpleEntryStat
* out_entry_stat
,
150 void CheckEOFRecord(int index
,
151 const SimpleEntryStat
& entry_stat
,
152 uint32 expected_crc32
,
153 int* out_result
) const;
155 void ReadSparseData(const EntryOperationData
& in_entry_op
,
156 net::IOBuffer
* out_buf
,
157 base::Time
* out_last_used
,
159 void WriteSparseData(const EntryOperationData
& in_entry_op
,
160 net::IOBuffer
* in_buf
,
161 uint64 max_sparse_data_size
,
162 SimpleEntryStat
* out_entry_stat
,
164 void GetAvailableRange(const EntryOperationData
& in_entry_op
,
168 // Close all streams, and add write EOF records to streams indicated by the
169 // CRCRecord entries in |crc32s_to_write|.
170 void Close(const SimpleEntryStat
& entry_stat
,
171 scoped_ptr
<std::vector
<CRCRecord
> > crc32s_to_write
,
172 net::GrowableIOBuffer
* stream_0_data
);
174 const base::FilePath
& path() const { return path_
; }
175 std::string
key() const { return key_
; }
178 enum CreateEntryResult
{
179 CREATE_ENTRY_SUCCESS
= 0,
180 CREATE_ENTRY_PLATFORM_FILE_ERROR
= 1,
181 CREATE_ENTRY_CANT_WRITE_HEADER
= 2,
182 CREATE_ENTRY_CANT_WRITE_KEY
= 3,
183 CREATE_ENTRY_MAX
= 4,
197 bool operator<(const SparseRange
& other
) const {
198 return offset
< other
.offset
;
202 SimpleSynchronousEntry(
203 net::CacheType cache_type
,
204 const base::FilePath
& path
,
205 const std::string
& key
,
208 // Like Entry, the SimpleSynchronousEntry self releases when Close() is
210 ~SimpleSynchronousEntry();
212 // Tries to open one of the cache entry files. Succeeds if the open succeeds
213 // or if the file was not found and is allowed to be omitted if the
214 // corresponding stream is empty.
215 bool MaybeOpenFile(int file_index
,
216 base::File::Error
* out_error
);
217 // Creates one of the cache entry files if necessary. If the file is allowed
218 // to be omitted if the corresponding stream is empty, and if |file_required|
219 // is FILE_NOT_REQUIRED, then the file is not created; otherwise, it is.
220 bool MaybeCreateFile(int file_index
,
221 FileRequired file_required
,
222 base::File::Error
* out_error
);
223 bool OpenFiles(bool had_index
,
224 SimpleEntryStat
* out_entry_stat
);
225 bool CreateFiles(bool had_index
,
226 SimpleEntryStat
* out_entry_stat
);
227 void CloseFile(int index
);
230 // Returns a net error, i.e. net::OK on success. |had_index| is passed
231 // from the main entry for metrics purposes, and is true if the index was
232 // initialized when the open operation began.
233 int InitializeForOpen(bool had_index
,
234 SimpleEntryStat
* out_entry_stat
,
235 scoped_refptr
<net::GrowableIOBuffer
>* stream_0_data
,
236 uint32
* out_stream_0_crc32
);
238 // Writes the header and key to a newly-created stream file. |index| is the
239 // index of the stream. Returns true on success; returns false and sets
240 // |*out_result| on failure.
241 bool InitializeCreatedFile(int index
, CreateEntryResult
* out_result
);
243 // Returns a net error, including net::OK on success and net::FILE_EXISTS
244 // when the entry already exists. |had_index| is passed from the main entry
245 // for metrics purposes, and is true if the index was initialized when the
246 // create operation began.
247 int InitializeForCreate(bool had_index
, SimpleEntryStat
* out_entry_stat
);
249 // Allocates and fills a buffer with stream 0 data in |stream_0_data|, then
251 int ReadAndValidateStream0(
253 SimpleEntryStat
* out_entry_stat
,
254 scoped_refptr
<net::GrowableIOBuffer
>* stream_0_data
,
255 uint32
* out_stream_0_crc32
) const;
257 int GetEOFRecordData(int index
,
258 const SimpleEntryStat
& entry_stat
,
261 int* out_data_size
) const;
264 // Opens the sparse data file and scans it if it exists.
265 bool OpenSparseFileIfExists(int32
* out_sparse_data_size
);
267 // Creates and initializes the sparse data file.
268 bool CreateSparseFile();
270 // Closes the sparse data file.
271 void CloseSparseFile();
273 // Writes the header to the (newly-created) sparse file.
274 bool InitializeSparseFile();
276 // Removes all but the header of the sparse file.
277 bool TruncateSparseFile();
279 // Scans the existing ranges in the sparse file. Populates |sparse_ranges_|
280 // and sets |*out_sparse_data_size| to the total size of all the ranges (not
281 // including headers).
282 bool ScanSparseFile(int32
* out_sparse_data_size
);
284 // Reads from a single sparse range. If asked to read the entire range, also
285 // verifies the CRC32.
286 bool ReadSparseRange(const SparseRange
* range
,
287 int offset
, int len
, char* buf
);
289 // Writes to a single (existing) sparse range. If asked to write the entire
290 // range, also updates the CRC32; otherwise, invalidates it.
291 bool WriteSparseRange(SparseRange
* range
,
292 int offset
, int len
, const char* buf
);
294 // Appends a new sparse range to the sparse data file.
295 bool AppendSparseRange(int64 offset
, int len
, const char* buf
);
297 static bool DeleteFileForEntryHash(const base::FilePath
& path
,
300 static bool DeleteFilesForEntryHash(const base::FilePath
& path
,
303 void RecordSyncCreateResult(CreateEntryResult result
, bool had_index
);
305 base::FilePath
GetFilenameFromFileIndex(int file_index
);
307 bool sparse_file_open() const {
308 return sparse_file_
.IsValid();
311 const net::CacheType cache_type_
;
312 const base::FilePath path_
;
313 const uint64 entry_hash_
;
316 bool have_open_files_
;
319 base::File files_
[kSimpleEntryFileCount
];
321 // True if the corresponding stream is empty and therefore no on-disk file
322 // was created to store it.
323 bool empty_file_omitted_
[kSimpleEntryFileCount
];
325 typedef std::map
<int64
, SparseRange
> SparseRangeOffsetMap
;
326 typedef SparseRangeOffsetMap::iterator SparseRangeIterator
;
327 SparseRangeOffsetMap sparse_ranges_
;
328 base::File sparse_file_
;
329 // Offset of the end of the sparse file (where the next sparse range will be
331 int64 sparse_tail_offset_
;
333 // True if the entry was created, or false if it was opened. Used to log
334 // SimpleCache.*.EntryCreatedWithStream2Omitted only for created entries.
338 } // namespace disk_cache
340 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_