Using QSaveFile in some cases not enough to prevent loosing content of a file.

This commit is contained in:
Roman Telezhynskyi 2023-06-22 18:32:13 +03:00
parent 81141acf6d
commit c78a51e9a0

View File

@ -54,6 +54,7 @@
#include <QIODevice> #include <QIODevice>
#include <QMessageLogger> #include <QMessageLogger>
#include <QObject> #include <QObject>
#include <QRegularExpression>
#include <QStringList> #include <QStringList>
#include <QTemporaryFile> #include <QTemporaryFile>
#include <QTextDocument> #include <QTextDocument>
@ -65,6 +66,11 @@
#include <QtConcurrentRun> #include <QtConcurrentRun>
#include <QtDebug> #include <QtDebug>
#ifdef Q_OS_UNIX
#include <fcntl.h>
#include <unistd.h>
#endif
namespace namespace
{ {
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -793,6 +799,20 @@ auto VDomDocument::SaveDocument(const QString &fileName, QString &error) -> bool
// save(out, indent); // save(out, indent);
success = file.commit(); success = file.commit();
#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
if (success)
{
// https://stackoverflow.com/questions/74051505/does-qsavefilecommit-fsync-the-file-to-the-filesystem
QString directoryPath = QFileInfo(file.fileName()).absoluteDir().path();
int dirFd = ::open(directoryPath.toLocal8Bit().data(), O_RDONLY | O_DIRECTORY);
if (dirFd != -1)
{
::fsync(dirFd);
::close(dirFd);
}
}
#endif
} }
if (not success) if (not success)