mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 03:44:02 +02:00
Only open files for write if necessary
This commit is contained in:
parent
79d53d92a1
commit
e0863aa50f
2 changed files with 33 additions and 2 deletions
|
@ -68,7 +68,7 @@ StoragePointer FileStorage::create(const QString& filename, size_t size, const u
|
|||
}
|
||||
|
||||
FileStorage::FileStorage(const QString& filename) : _file(filename) {
|
||||
if (_file.open(QFile::ReadWrite)) {
|
||||
if (_file.open(QFile::ReadOnly)) {
|
||||
_mapped = _file.map(0, _file.size());
|
||||
if (_mapped) {
|
||||
_valid = true;
|
||||
|
@ -90,3 +90,31 @@ FileStorage::~FileStorage() {
|
|||
_file.close();
|
||||
}
|
||||
}
|
||||
|
||||
void FileStorage::ensureWriteAccess() {
|
||||
if (_hasWriteAccess) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_mapped) {
|
||||
if (!_file.unmap(_mapped)) {
|
||||
throw std::runtime_error("Unable to unmap file");
|
||||
}
|
||||
}
|
||||
if (_file.isOpen()) {
|
||||
_file.close();
|
||||
}
|
||||
_valid = false;
|
||||
|
||||
if (_file.open(QFile::ReadWrite)) {
|
||||
_mapped = _file.map(0, _file.size());
|
||||
if (_mapped) {
|
||||
_valid = true;
|
||||
_hasWriteAccess = true;
|
||||
} else {
|
||||
qCWarning(storagelogging) << "Failed to map file " << _file.fileName();
|
||||
}
|
||||
} else {
|
||||
qCWarning(storagelogging) << "Failed to open file " << _file.fileName();
|
||||
}
|
||||
}
|
|
@ -60,11 +60,14 @@ namespace storage {
|
|||
FileStorage& operator=(const FileStorage& other) = delete;
|
||||
|
||||
const uint8_t* data() const override { return _mapped; }
|
||||
uint8_t* mutableData() override { return _mapped; }
|
||||
uint8_t* mutableData() override { ensureWriteAccess(); return _mapped; }
|
||||
size_t size() const override { return _file.size(); }
|
||||
operator bool() const override { return _valid; }
|
||||
private:
|
||||
void ensureWriteAccess();
|
||||
|
||||
bool _valid { false };
|
||||
bool _hasWriteAccess { false };
|
||||
QFile _file;
|
||||
uint8_t* _mapped { nullptr };
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue