mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 03:48:44 +02:00
Update FileCache to use QSaveFile for atomic writes
This commit is contained in:
parent
d86071d783
commit
542001b14f
1 changed files with 7 additions and 12 deletions
|
@ -17,6 +17,7 @@
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
#include <QSaveFile>
|
||||||
|
|
||||||
#include <PathUtils.h>
|
#include <PathUtils.h>
|
||||||
|
|
||||||
|
@ -110,19 +111,13 @@ FilePointer FileCache::writeFile(const char* data, File::Metadata&& metadata) {
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the data to a temporary file
|
QSaveFile saveFile(QString::fromStdString(filepath));
|
||||||
std::string tmpFilepath = filepath + "_tmp";
|
saveFile.open(QIODevice::WriteOnly);
|
||||||
FILE* saveFile = fopen(tmpFilepath.c_str(), "wb");
|
saveFile.write(data, metadata.length);
|
||||||
if (saveFile != nullptr && fwrite(data, metadata.length, 1, saveFile) && fclose(saveFile) == 0) {
|
if (saveFile.commit()) {
|
||||||
int result = rename(tmpFilepath.c_str(), filepath.c_str());
|
file = addFile(std::move(metadata), filepath);
|
||||||
if (result == 0) {
|
|
||||||
file = addFile(std::move(metadata), filepath);
|
|
||||||
} else {
|
|
||||||
qCWarning(file_cache, "[%s] Failed to rename %s to %s (%s)", tmpFilepath.c_str(), filepath.c_str(), strerror(errno));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
qCWarning(file_cache, "[%s] Failed to write %s (%s)", _dirname.c_str(), metadata.key.c_str(), strerror(errno));
|
qCWarning(file_cache, "[%s] Failed to write %s", _dirname.c_str(), metadata.key.c_str());
|
||||||
errno = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return file;
|
return file;
|
||||||
|
|
Loading…
Reference in a new issue