jsaak
jsaak

Reputation: 645

Does QSaveFile::commit() fsync() the file to the filesystem?

QSaveFile is recommended for atomic writes to the filesystem. How does it work?

Does QSaveFile::commit() fsync() the file to the filesystem?

Upvotes: 1

Views: 464

Answers (1)

jsaak
jsaak

Reputation: 645

Yes, and no.

It writes to a temporary file, fsync()-it, and then renames it to the desired filename. But the rename is not fsync()-ed. So in case of a power loss, the new file will not take place until the OS decides to write.

My solution is to call this after commit(): (linux only)

#include <unistd.h>
#include <fcntl.h>

saveFile.write(saveDoc.toJson());
saveFile.commit();

QFileInfo fi(saveFile.fileName());
int fd = ::open(fi.absolutePath().toLocal8Bit().data(), O_RDONLY);
::fsync(fd);
::close(fd);

Upvotes: -1

Related Questions