| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | |
| 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | |
| 7 | |
| 8 #include <algorithm> | |
| 9 #include <map> | |
| 10 #include <string> | |
| 11 #include <utility> | |
| 12 #include <vector> | |
| 13 | |
| 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" | |
| 22 | |
| 23 namespace net { | |
| 24 class GrowableIOBuffer; | |
| 25 class IOBuffer; | |
| 26 } | |
| 27 | |
| 28 namespace disk_cache { | |
| 29 | |
| 30 class SimpleSynchronousEntry; | |
| 31 | |
| 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 { | |
| 36 public: | |
| 37 SimpleEntryStat(base::Time last_used, | |
| 38 base::Time last_modified, | |
| 39 const int32 data_size[], | |
| 40 const int32 sparse_data_size); | |
| 41 | |
| 42 int GetOffsetInFile(const std::string& key, | |
| 43 int offset, | |
| 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; | |
| 48 | |
| 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; | |
| 54 } | |
| 55 | |
| 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; | |
| 59 } | |
| 60 | |
| 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; | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 base::Time last_used_; | |
| 68 base::Time last_modified_; | |
| 69 int32 data_size_[kSimpleEntryStreamCount]; | |
| 70 int32 sparse_data_size_; | |
| 71 }; | |
| 72 | |
| 73 struct SimpleEntryCreationResults { | |
| 74 explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat); | |
| 75 ~SimpleEntryCreationResults(); | |
| 76 | |
| 77 SimpleSynchronousEntry* sync_entry; | |
| 78 scoped_refptr<net::GrowableIOBuffer> stream_0_data; | |
| 79 SimpleEntryStat entry_stat; | |
| 80 uint32 stream_0_crc32; | |
| 81 int result; | |
| 82 }; | |
| 83 | |
| 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 { | |
| 88 public: | |
| 89 struct CRCRecord { | |
| 90 CRCRecord(); | |
| 91 CRCRecord(int index_p, bool has_crc32_p, uint32 data_crc32_p); | |
| 92 | |
| 93 int index; | |
| 94 bool has_crc32; | |
| 95 uint32 data_crc32; | |
| 96 }; | |
| 97 | |
| 98 struct EntryOperationData { | |
| 99 EntryOperationData(int index_p, int offset_p, int buf_len_p); | |
| 100 EntryOperationData(int index_p, | |
| 101 int offset_p, | |
| 102 int buf_len_p, | |
| 103 bool truncate_p, | |
| 104 bool doomed_p); | |
| 105 EntryOperationData(int64 sparse_offset_p, int buf_len_p); | |
| 106 | |
| 107 int index; | |
| 108 int offset; | |
| 109 int64 sparse_offset; | |
| 110 int buf_len; | |
| 111 bool truncate; | |
| 112 bool doomed; | |
| 113 }; | |
| 114 | |
| 115 static void OpenEntry(net::CacheType cache_type, | |
| 116 const base::FilePath& path, | |
| 117 uint64 entry_hash, | |
| 118 bool had_index, | |
| 119 SimpleEntryCreationResults* out_results); | |
| 120 | |
| 121 static void CreateEntry(net::CacheType cache_type, | |
| 122 const base::FilePath& path, | |
| 123 const std::string& key, | |
| 124 uint64 entry_hash, | |
| 125 bool had_index, | |
| 126 SimpleEntryCreationResults* out_results); | |
| 127 | |
| 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, | |
| 132 uint64 entry_hash); | |
| 133 | |
| 134 // Like |DoomEntry()| above. Deletes all entries corresponding to the | |
| 135 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net | |
| 136 // error code. | |
| 137 static int DoomEntrySet(const std::vector<uint64>* key_hashes, | |
| 138 const base::FilePath& path); | |
| 139 | |
| 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, | |
| 143 uint32* out_crc32, | |
| 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, | |
| 149 int* out_result); | |
| 150 void CheckEOFRecord(int index, | |
| 151 const SimpleEntryStat& entry_stat, | |
| 152 uint32 expected_crc32, | |
| 153 int* out_result) const; | |
| 154 | |
| 155 void ReadSparseData(const EntryOperationData& in_entry_op, | |
| 156 net::IOBuffer* out_buf, | |
| 157 base::Time* out_last_used, | |
| 158 int* out_result); | |
| 159 void WriteSparseData(const EntryOperationData& in_entry_op, | |
| 160 net::IOBuffer* in_buf, | |
| 161 uint64 max_sparse_data_size, | |
| 162 SimpleEntryStat* out_entry_stat, | |
| 163 int* out_result); | |
| 164 void GetAvailableRange(const EntryOperationData& in_entry_op, | |
| 165 int64* out_start, | |
| 166 int* out_result); | |
| 167 | |
| 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); | |
| 173 | |
| 174 const base::FilePath& path() const { return path_; } | |
| 175 std::string key() const { return key_; } | |
| 176 | |
| 177 private: | |
| 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, | |
| 184 }; | |
| 185 | |
| 186 enum FileRequired { | |
| 187 FILE_NOT_REQUIRED, | |
| 188 FILE_REQUIRED | |
| 189 }; | |
| 190 | |
| 191 struct SparseRange { | |
| 192 int64 offset; | |
| 193 int64 length; | |
| 194 uint32 data_crc32; | |
| 195 int64 file_offset; | |
| 196 | |
| 197 bool operator<(const SparseRange& other) const { | |
| 198 return offset < other.offset; | |
| 199 } | |
| 200 }; | |
| 201 | |
| 202 SimpleSynchronousEntry( | |
| 203 net::CacheType cache_type, | |
| 204 const base::FilePath& path, | |
| 205 const std::string& key, | |
| 206 uint64 entry_hash); | |
| 207 | |
| 208 // Like Entry, the SimpleSynchronousEntry self releases when Close() is | |
| 209 // called. | |
| 210 ~SimpleSynchronousEntry(); | |
| 211 | |
| 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); | |
| 228 void CloseFiles(); | |
| 229 | |
| 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); | |
| 237 | |
| 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); | |
| 242 | |
| 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); | |
| 248 | |
| 249 // Allocates and fills a buffer with stream 0 data in |stream_0_data|, then | |
| 250 // checks its crc32. | |
| 251 int ReadAndValidateStream0( | |
| 252 int total_data_size, | |
| 253 SimpleEntryStat* out_entry_stat, | |
| 254 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, | |
| 255 uint32* out_stream_0_crc32) const; | |
| 256 | |
| 257 int GetEOFRecordData(int index, | |
| 258 const SimpleEntryStat& entry_stat, | |
| 259 bool* out_has_crc32, | |
| 260 uint32* out_crc32, | |
| 261 int* out_data_size) const; | |
| 262 void Doom() const; | |
| 263 | |
| 264 // Opens the sparse data file and scans it if it exists. | |
| 265 bool OpenSparseFileIfExists(int32* out_sparse_data_size); | |
| 266 | |
| 267 // Creates and initializes the sparse data file. | |
| 268 bool CreateSparseFile(); | |
| 269 | |
| 270 // Closes the sparse data file. | |
| 271 void CloseSparseFile(); | |
| 272 | |
| 273 // Writes the header to the (newly-created) sparse file. | |
| 274 bool InitializeSparseFile(); | |
| 275 | |
| 276 // Removes all but the header of the sparse file. | |
| 277 bool TruncateSparseFile(); | |
| 278 | |
| 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); | |
| 283 | |
| 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); | |
| 288 | |
| 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); | |
| 293 | |
| 294 // Appends a new sparse range to the sparse data file. | |
| 295 bool AppendSparseRange(int64 offset, int len, const char* buf); | |
| 296 | |
| 297 static bool DeleteFileForEntryHash(const base::FilePath& path, | |
| 298 uint64 entry_hash, | |
| 299 int file_index); | |
| 300 static bool DeleteFilesForEntryHash(const base::FilePath& path, | |
| 301 uint64 entry_hash); | |
| 302 | |
| 303 void RecordSyncCreateResult(CreateEntryResult result, bool had_index); | |
| 304 | |
| 305 base::FilePath GetFilenameFromFileIndex(int file_index); | |
| 306 | |
| 307 bool sparse_file_open() const { | |
| 308 return sparse_file_.IsValid(); | |
| 309 } | |
| 310 | |
| 311 const net::CacheType cache_type_; | |
| 312 const base::FilePath path_; | |
| 313 const uint64 entry_hash_; | |
| 314 std::string key_; | |
| 315 | |
| 316 bool have_open_files_; | |
| 317 bool initialized_; | |
| 318 | |
| 319 base::File files_[kSimpleEntryFileCount]; | |
| 320 | |
| 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]; | |
| 324 | |
| 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 | |
| 330 // written). | |
| 331 int64 sparse_tail_offset_; | |
| 332 | |
| 333 // True if the entry was created, or false if it was opened. Used to log | |
| 334 // SimpleCache.*.EntryCreatedWithStream2Omitted only for created entries. | |
| 335 bool files_created_; | |
| 336 }; | |
| 337 | |
| 338 } // namespace disk_cache | |
| 339 | |
| 340 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | |
| OLD | NEW |