#ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_
#define NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_
#include <stdint.h>
#include <algorithm>
#include <array>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/feature_list.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "net/base/cache_type.h"
#include "net/base/net_errors.h"
#include "net/base/net_export.h"
#include "net/disk_cache/simple/simple_entry_format.h"
#include "net/disk_cache/simple/simple_file_tracker.h"
#include "net/disk_cache/simple/simple_histogram_enums.h"
namespace net {
class GrowableIOBuffer;
class IOBuffer;
}
FORWARD_DECLARE_TEST(DiskCacheBackendTest, SimpleCacheEnumerationLongKeys);
namespace disk_cache {
class BackendFileOperations;
class UnboundBackendFileOperations;
NET_EXPORT_PRIVATE BASE_DECLARE_FEATURE(kSimpleCachePrefetchExperiment);
NET_EXPORT_PRIVATE extern const char kSimpleCacheFullPrefetchBytesParam[];
NET_EXPORT_PRIVATE extern const char
kSimpleCacheTrailerPrefetchSpeculativeBytesParam[];
NET_EXPORT_PRIVATE int GetSimpleCachePrefetchSize();
class SimpleSynchronousEntry;
struct RangeResult;
class NET_EXPORT_PRIVATE SimpleEntryStat {
public:
SimpleEntryStat(base::Time last_used,
const std::array<int64_t, kSimpleEntryStreamCount>& data_size,
const uint64_t sparse_data_size);
int64_t GetOffsetInFile(size_t key_length,
int64_t offset,
int stream_index) const;
int64_t GetEOFOffsetInFile(size_t key_length, int stream_index) const;
int64_t GetLastEOFOffsetInFile(size_t key_length, int file_index) const;
int64_t GetFileSize(size_t key_length, int file_index) const;
base::Time last_used() const { return last_used_; }
void set_last_used(base::Time last_used) { last_used_ = last_used; }
int64_t data_size(int stream_index) const { return data_size_[stream_index]; }
void set_data_size(int stream_index, int64_t data_size) {
data_size_[stream_index] = data_size;
}
uint64_t sparse_data_size() const { return sparse_data_size_; }
void set_sparse_data_size(uint64_t sparse_data_size) {
sparse_data_size_ = sparse_data_size;
}
private:
base::Time last_used_;
std::array<int64_t, kSimpleEntryStreamCount> data_size_;
uint64_t sparse_data_size_;
};
struct SimpleStreamPrefetchData {
SimpleStreamPrefetchData();
~SimpleStreamPrefetchData();
scoped_refptr<net::GrowableIOBuffer> data;
uint32_t stream_crc32;
};
struct SimpleEntryCreationResults {
explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat);
~SimpleEntryCreationResults();
raw_ptr<SimpleSynchronousEntry> sync_entry;
std::unique_ptr<UnboundBackendFileOperations> unbound_file_operations;
std::array<SimpleStreamPrefetchData, 2> stream_prefetch_data;
SimpleEntryStat entry_stat;
uint32_t computed_trailer_prefetch_size = 0;
int result = net::OK;
bool created = false;
};
struct SimpleEntryCloseResults {
int32_t estimated_trailer_prefetch_size = -1;
};
class SimpleSynchronousEntry {
public:
struct CRCRecord {
CRCRecord();
CRCRecord(int index_p, bool has_crc32_p, uint32_t data_crc32_p);
int index;
bool has_crc32;
uint32_t data_crc32;
};
struct ReadRequest {
ReadRequest(int index_p, int64_t offset_p, int buf_len_p);
int index;
int64_t offset;
int buf_len;
uint32_t previous_crc32;
bool request_update_crc = false;
bool request_verify_crc;
};
struct ReadResult {
ReadResult() = default;
int result;
uint32_t updated_crc32;
bool crc_updated = false;
};
struct WriteRequest {
WriteRequest(int index_p,
int64_t offset_p,
int buf_len_p,
uint32_t previous_crc32_p,
bool truncate_p,
bool doomed_p,
bool request_update_crc_p);
int index;
int64_t offset;
int buf_len;
uint32_t previous_crc32;
bool truncate;
bool doomed;
bool request_update_crc;
};
struct WriteResult {
WriteResult() = default;
int result;
uint32_t updated_crc32;
bool crc_updated = false;
};
struct SparseRequest {
SparseRequest(uint64_t sparse_offset_p, size_t buf_len_p);
uint64_t sparse_offset;
size_t buf_len;
};
NET_EXPORT_PRIVATE SimpleSynchronousEntry(
net::CacheType cache_type,
const base::FilePath& path,
const std::optional<std::string>& key,
uint64_t entry_hash,
SimpleFileTracker* simple_file_tracker,
std::unique_ptr<UnboundBackendFileOperations> file_operations,
uint32_t trailer_prefetch_size);
NET_EXPORT_PRIVATE ~SimpleSynchronousEntry();
static void OpenEntry(
net::CacheType cache_type,
const base::FilePath& path,
const std::optional<std::string>& key,
uint64_t entry_hash,
SimpleFileTracker* file_tracker,
std::unique_ptr<UnboundBackendFileOperations> file_operations,
uint32_t trailer_prefetch_size,
SimpleEntryCreationResults* out_results);
static void CreateEntry(
net::CacheType cache_type,
const base::FilePath& path,
const std::string& key,
uint64_t entry_hash,
SimpleFileTracker* file_tracker,
std::unique_ptr<UnboundBackendFileOperations> file_operations,
SimpleEntryCreationResults* out_results);
static void OpenOrCreateEntry(
net::CacheType cache_type,
const base::FilePath& path,
const std::string& key,
uint64_t entry_hash,
OpenEntryIndexEnum index_state,
bool optimistic_create,
SimpleFileTracker* file_tracker,
std::unique_ptr<UnboundBackendFileOperations> file_operations,
uint32_t trailer_prefetch_size,
SimpleEntryCreationResults* out_results);
int Doom();
static int DeleteEntryFiles(
const base::FilePath& path,
net::CacheType cache_type,
uint64_t entry_hash,
std::unique_ptr<UnboundBackendFileOperations> unbound_file_operations);
static int TruncateEntryFiles(
const base::FilePath& path,
uint64_t entry_hash,
std::unique_ptr<UnboundBackendFileOperations> file_operations);
static int DeleteEntrySetFiles(
const std::vector<uint64_t>* key_hashes,
const base::FilePath& path,
std::unique_ptr<UnboundBackendFileOperations> unbound_file_operations);
void ReadData(const ReadRequest& in_entry_op,
SimpleEntryStat* entry_stat,
net::IOBuffer* out_buf,
ReadResult* out_result);
void WriteData(const WriteRequest& in_entry_op,
net::IOBuffer* in_buf,
SimpleEntryStat* out_entry_stat,
WriteResult* out_write_result);
int CheckEOFRecord(BackendFileOperations* file_operations,
base::File* file,
int stream_index,
const SimpleEntryStat& entry_stat,
uint32_t expected_crc32);
void ReadSparseData(const SparseRequest& in_entry_op,
net::IOBuffer* out_buf,
base::Time* out_last_used,
int* out_result);
void WriteSparseData(const SparseRequest& in_entry_op,
net::IOBuffer* in_buf,
uint64_t max_sparse_data_size,
SimpleEntryStat* out_entry_stat,
int* out_result);
void GetAvailableRange(const SparseRequest& in_entry_op,
RangeResult* out_result);
void Close(const SimpleEntryStat& entry_stat,
std::unique_ptr<std::vector<CRCRecord>> crc32s_to_write,
net::GrowableIOBuffer* stream_0_data,
SimpleEntryCloseResults* out_results);
const base::FilePath& path() const { return path_; }
std::optional<std::string> key() const { return key_; }
const SimpleFileTracker::EntryFileKey& entry_file_key() const {
return entry_file_key_;
}
NET_EXPORT_PRIVATE base::FilePath GetFilenameForSubfile(
SimpleFileTracker::SubFile sub_file) const;
uint32_t computed_trailer_prefetch_size() const {
return computed_trailer_prefetch_size_;
}
private:
FRIEND_TEST_ALL_PREFIXES(::DiskCacheBackendTest,
SimpleCacheEnumerationLongKeys);
friend class SimpleFileTrackerTest;
class PrefetchData;
class ScopedFileOperationsBinding;
enum FileRequired {
FILE_NOT_REQUIRED,
FILE_REQUIRED
};
struct SparseRange {
uint64_t offset;
size_t length;
uint32_t data_crc32;
int64_t file_offset;
bool operator<(const SparseRange& other) const {
return offset < other.offset;
}
};
static const size_t kInitialHeaderRead = 64 * 1024;
bool MaybeOpenFile(BackendFileOperations* file_operations,
int file_index,
base::File::Error* out_error);
bool MaybeCreateFile(BackendFileOperations* file_operations,
int file_index,
FileRequired file_required,
base::File::Error* out_error);
bool OpenFiles(BackendFileOperations* file_operations,
SimpleEntryStat* out_entry_stat);
bool CreateFiles(BackendFileOperations* file_operations,
SimpleEntryStat* out_entry_stat);
void CloseFile(BackendFileOperations* file_operations, int index);
void CloseFiles();
bool CheckHeaderAndKey(base::File* file, int file_index);
int InitializeForOpen(
BackendFileOperations* file_operations,
SimpleEntryStat* out_entry_stat,
std::array<SimpleStreamPrefetchData, 2>& stream_prefetch_data);
bool InitializeCreatedFile(BackendFileOperations* file_operations, int index);
int InitializeForCreate(BackendFileOperations* file_operations,
SimpleEntryStat* out_entry_stat);
int ReadAndValidateStream0AndMaybe1(
BackendFileOperations* file_operations,
int64_t file_size,
SimpleEntryStat* out_entry_stat,
std::array<SimpleStreamPrefetchData, 2>& stream_prefetch_data);
int GetEOFRecordData(base::File* file,
PrefetchData* prefetch_data,
int file_index,
int64_t file_offset,
SimpleFileEOF* eof_record);
bool ReadFromFileOrPrefetched(base::File* file,
PrefetchData* prefetch_data,
int file_index,
int64_t offset,
size_t size,
base::span<uint8_t> dest);
int PreReadStreamPayload(base::File* file,
PrefetchData* prefetch_data,
int stream_index,
int extra_size,
const SimpleEntryStat& entry_stat,
const SimpleFileEOF& eof_record,
SimpleStreamPrefetchData* out);
bool OpenSparseFileIfExists(BackendFileOperations* file_operations,
uint64_t* out_sparse_data_size);
bool CreateSparseFile(BackendFileOperations* file_operations);
void CloseSparseFile(BackendFileOperations* file_operations);
bool InitializeSparseFile(base::File* file);
bool TruncateSparseFile(base::File* sparse_file);
bool ScanSparseFile(base::File* sparse_file, uint64_t* out_sparse_data_size);
bool ReadSparseRange(base::File* sparse_file,
const SparseRange* range,
size_t offset_in_range,
size_t len,
base::span<uint8_t> buf);
bool WriteSparseRange(base::File* sparse_file,
SparseRange* range,
size_t offset_in_range,
size_t len,
base::span<const uint8_t> buf);
bool AppendSparseRange(base::File* sparse_file,
uint64_t offset,
size_t len,
base::span<const uint8_t> buf);
static int DeleteEntryFilesInternal(const base::FilePath& path,
net::CacheType cache_type,
uint64_t entry_hash,
BackendFileOperations* file_operations);
static bool DeleteFileForEntryHash(const base::FilePath& path,
uint64_t entry_hash,
int file_index,
BackendFileOperations* file_operations);
static bool DeleteFilesForEntryHash(const base::FilePath& path,
uint64_t entry_hash,
BackendFileOperations* file_operations);
static bool TruncateFilesForEntryHash(const base::FilePath& path,
uint64_t entry_hash,
BackendFileOperations* file_operations);
int DoomInternal(BackendFileOperations* file_operations);
base::FilePath GetFilenameFromFileIndex(int file_index) const;
bool sparse_file_open() const { return sparse_file_open_; }
const net::CacheType cache_type_;
const base::FilePath path_;
SimpleFileTracker::EntryFileKey entry_file_key_;
std::optional<std::string> key_;
bool have_open_files_ = false;
bool initialized_ = false;
std::array<bool, kSimpleEntryNormalFileCount> header_and_key_check_needed_ =
std::to_array({false, false});
raw_ptr<SimpleFileTracker> file_tracker_;
std::unique_ptr<UnboundBackendFileOperations> unbound_file_operations_;
uint32_t trailer_prefetch_size_;
uint32_t computed_trailer_prefetch_size_ = 0;
std::array<bool, kSimpleEntryNormalFileCount> empty_file_omitted_;
typedef std::map<uint64_t, SparseRange> SparseRangeOffsetMap;
typedef SparseRangeOffsetMap::iterator SparseRangeIterator;
SparseRangeOffsetMap sparse_ranges_;
bool sparse_file_open_ = false;
uint64_t sparse_tail_offset_;
};
}
#endif