Speed up block loading/storing by issuing only one read/write syscall to the base file system
This commit is contained in:
parent
7348f7c64d
commit
0d860fa4f0
@ -99,56 +99,38 @@ void OnDiskBlock::RemoveFromDisk(const bf::path &rootdir, const Key &key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void OnDiskBlock::_storeToDisk() const {
|
void OnDiskBlock::_storeToDisk() const {
|
||||||
std::ofstream file(_filepath.c_str(), std::ios::binary | std::ios::trunc);
|
Data fileContent(formatVersionHeaderSize() + _data.size());
|
||||||
if (!file.good()) {
|
std::memcpy(fileContent.data(), FORMAT_VERSION_HEADER.c_str(), formatVersionHeaderSize());
|
||||||
throw std::runtime_error("Could not open file for writing");
|
std::memcpy(fileContent.dataOffset(formatVersionHeaderSize()), _data.data(), _data.size());
|
||||||
}
|
fileContent.StoreToFile(_filepath);
|
||||||
file.write(FORMAT_VERSION_HEADER.c_str(), formatVersionHeaderSize());
|
|
||||||
if (!file.good()) {
|
|
||||||
throw std::runtime_error("Error writing block header");
|
|
||||||
}
|
|
||||||
_data.StoreToStream(file);
|
|
||||||
if (!file.good()) {
|
|
||||||
throw std::runtime_error("Error writing block data");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
optional<Data> OnDiskBlock::_loadFromDisk(const bf::path &filepath) {
|
optional<Data> OnDiskBlock::_loadFromDisk(const bf::path &filepath) {
|
||||||
//If it isn't a file, ifstream::good() would return false. We still need this extra check
|
auto fileContent = Data::LoadFromFile(filepath);
|
||||||
//upfront, because ifstream::good() doesn't crash if we give it the path of a directory
|
if (fileContent == none) {
|
||||||
//instead the path of a file.
|
|
||||||
if(!bf::is_regular_file(filepath)) {
|
|
||||||
return none;
|
return none;
|
||||||
}
|
}
|
||||||
ifstream file(filepath.c_str(), ios::binary);
|
return _checkAndRemoveHeader(std::move(*fileContent));
|
||||||
if (!file.good()) {
|
|
||||||
return none;
|
|
||||||
}
|
|
||||||
_checkHeader(&file);
|
|
||||||
Data result = Data::LoadFromStream(file);
|
|
||||||
//TODO With newer compilers, "return result;" would be enough
|
|
||||||
return boost::optional<Data>(std::move(result));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDiskBlock::_checkHeader(istream *str) {
|
Data OnDiskBlock::_checkAndRemoveHeader(Data data) {
|
||||||
Data header(formatVersionHeaderSize());
|
if (!_isAcceptedCryfsHeader(data)) {
|
||||||
str->read(reinterpret_cast<char*>(header.data()), formatVersionHeaderSize());
|
if (_isOtherCryfsHeader(data)) {
|
||||||
if (!_isAcceptedCryfsHeader(header)) {
|
|
||||||
if (_isOtherCryfsHeader(header)) {
|
|
||||||
throw std::runtime_error("This block is not supported yet. Maybe it was created with a newer version of CryFS?");
|
throw std::runtime_error("This block is not supported yet. Maybe it was created with a newer version of CryFS?");
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error("This is not a valid block.");
|
throw std::runtime_error("This is not a valid block.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Data result(data.size() - formatVersionHeaderSize());
|
||||||
|
std::memcpy(result.data(), data.dataOffset(formatVersionHeaderSize()), result.size());
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OnDiskBlock::_isAcceptedCryfsHeader(const Data &data) {
|
bool OnDiskBlock::_isAcceptedCryfsHeader(const Data &data) {
|
||||||
ASSERT(data.size() == formatVersionHeaderSize(), "We extracted the wrong header size from the block.");
|
|
||||||
return 0 == std::memcmp(data.data(), FORMAT_VERSION_HEADER.c_str(), formatVersionHeaderSize());
|
return 0 == std::memcmp(data.data(), FORMAT_VERSION_HEADER.c_str(), formatVersionHeaderSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OnDiskBlock::_isOtherCryfsHeader(const Data &data) {
|
bool OnDiskBlock::_isOtherCryfsHeader(const Data &data) {
|
||||||
ASSERT(data.size() >= FORMAT_VERSION_HEADER_PREFIX.size(), "We extracted the wrong header size from the block.");
|
|
||||||
return 0 == std::memcmp(data.data(), FORMAT_VERSION_HEADER_PREFIX.c_str(), FORMAT_VERSION_HEADER_PREFIX.size());
|
return 0 == std::memcmp(data.data(), FORMAT_VERSION_HEADER_PREFIX.c_str(), FORMAT_VERSION_HEADER_PREFIX.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ private:
|
|||||||
|
|
||||||
static bool _isAcceptedCryfsHeader(const cpputils::Data &data);
|
static bool _isAcceptedCryfsHeader(const cpputils::Data &data);
|
||||||
static bool _isOtherCryfsHeader(const cpputils::Data &data);
|
static bool _isOtherCryfsHeader(const cpputils::Data &data);
|
||||||
static void _checkHeader(std::istream *str);
|
static cpputils::Data _checkAndRemoveHeader(cpputils::Data data);
|
||||||
static boost::filesystem::path _getFilepath(const boost::filesystem::path &rootdir, const Key &key);
|
static boost::filesystem::path _getFilepath(const boost::filesystem::path &rootdir, const Key &key);
|
||||||
|
|
||||||
const boost::filesystem::path _filepath;
|
const boost::filesystem::path _filepath;
|
||||||
|
Loading…
Reference in New Issue
Block a user