Repository navigation
Added the ability to write files to disk more reliably #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,9 @@ | |
| */ | ||
| #include "filesystem.h" | ||
|
|
||
| #include <cerrno> | ||
| #include <cstring> | ||
|
|
||
| #include <QDir> | ||
| #include <QDirIterator> | ||
| #include <QFileInfo> | ||
|
|
@@ -29,11 +32,21 @@ | |
|
|
||
| #ifdef Q_OS_WIN | ||
| #include <windows.h> | ||
| #include <io.h> | ||
| #else | ||
| #include <unistd.h> | ||
| #endif | ||
|
|
||
| #include "../ioretcodes.h" | ||
| #include "log.h" | ||
|
|
||
| #ifdef Q_OS_WIN | ||
| //Use _commit instead of FlushFileBuffers since it takes a file descriptor like fsync. | ||
| #define OS_SYNC_FILE(handle) _commit((handle)) | ||
| #else | ||
| #define OS_SYNC_FILE(handle) fsync((handle)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a stronger sync operation on macOS. On macOS, 🧰 Tools
|
||
| #endif | ||
|
|
||
| using namespace muse; | ||
| using namespace muse::io; | ||
|
|
||
|
|
@@ -198,7 +211,7 @@ Ret FileSystem::readFile(const io::path_t& filePath, ByteArray& data) const | |
| return ret; | ||
| } | ||
|
|
||
| Ret FileSystem::writeFile(const io::path_t& filePath, const ByteArray& data) | ||
| Ret FileSystem::writeFile(const io::path_t& filePath, const ByteArray& data, bool syncToDisk) | ||
| { | ||
| Ret ret = muse::make_ok(); | ||
|
|
||
|
|
@@ -223,6 +236,21 @@ Ret FileSystem::writeFile(const io::path_t& filePath, const ByteArray& data) | |
| return ret; | ||
| } | ||
|
|
||
| if (syncToDisk) { | ||
| // Flush user-space buffer (Qt/fflush), not a disk sync. | ||
| // Must be called before OS_SYNC_FILE. | ||
| file.flush(); | ||
|
|
||
| // Ask the OS to push data to disk. | ||
| // If it fails, we log a warning and proceed — | ||
| // a failed sync does not necessarily mean the data wasn't written. | ||
| // At least we tried. | ||
| if (OS_SYNC_FILE(file.handle()) == -1) { | ||
| const int errorCode = errno; | ||
| LOGW() << "Failed to sync file to disk:" << std::strerror(errorCode); | ||
| } | ||
| } | ||
|
|
||
| file.close(); | ||
|
|
||
| return ret; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔎 Supported by static analysis
Script executed:
Repository: musescore/muse_framework
Length of output: 20575
Script executed:
Repository: musescore/muse_framework
Length of output: 827
Update the
MemFileSystemoverride.IFileSystem::writeFilenow has a third parameter, butMemFileSystemstill declares and defines a two-parameteroverride. The declaration causes a compile error in targets that compile this class.Suggested fix