diff options
author | bunnei <bunneidev@gmail.com> | 2021-07-07 00:40:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-07 00:40:31 -0700 |
commit | 2eb018c80fd2a83686ee11a80c7427a98ea44354 (patch) | |
tree | a062eae0798d08a88411642490c2fef96b082dc6 /src/common/fs/file.cpp | |
parent | eb3cb3af353dd1d62b11a6032e35656153ecb505 (diff) | |
parent | a59ae5e702a9fed1626e8815b2208acc7f373e22 (diff) |
Merge pull request #6562 from Morph1984/flush-behavior
common: fs: More misc. changes
Diffstat (limited to 'src/common/fs/file.cpp')
-rw-r--r-- | src/common/fs/file.cpp | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/common/fs/file.cpp b/src/common/fs/file.cpp index 077f34995..274f57659 100644 --- a/src/common/fs/file.cpp +++ b/src/common/fs/file.cpp @@ -306,9 +306,9 @@ bool IOFile::Flush() const { errno = 0; #ifdef _WIN32 - const auto flush_result = std::fflush(file) == 0 && _commit(fileno(file)) == 0; + const auto flush_result = std::fflush(file) == 0; #else - const auto flush_result = std::fflush(file) == 0 && fsync(fileno(file)) == 0; + const auto flush_result = std::fflush(file) == 0; #endif if (!flush_result) { @@ -320,6 +320,28 @@ bool IOFile::Flush() const { return flush_result; } +bool IOFile::Commit() const { + if (!IsOpen()) { + return false; + } + + errno = 0; + +#ifdef _WIN32 + const auto commit_result = std::fflush(file) == 0 && _commit(fileno(file)) == 0; +#else + const auto commit_result = std::fflush(file) == 0 && fsync(fileno(file)) == 0; +#endif + + if (!commit_result) { + const auto ec = std::error_code{errno, std::generic_category()}; + LOG_ERROR(Common_Filesystem, "Failed to commit the file at path={}, ec_message={}", + PathToUTF8String(file_path), ec.message()); + } + + return commit_result; +} + bool IOFile::SetSize(u64 size) const { if (!IsOpen()) { return false; @@ -347,6 +369,9 @@ u64 IOFile::GetSize() const { return 0; } + // Flush any unwritten buffered data into the file prior to retrieving the file size. + std::fflush(file); + std::error_code ec; const auto file_size = fs::file_size(file_path, ec); |