1
0
Fork 0
forked from suyu/suyu

common: fs: file: Revert Flush to its previous behavior and add Commit

It became apparent that logging can continuously spam errors that trigger file flushing.
Since committing the files to disk is an expensive operation, this causes unnecessarily high disk usage.
As such, we will revert Flush() to the previous behavior and add a Commit() member function in the event that this behavior is needed.
This commit is contained in:
Morph 2021-07-05 12:32:14 -04:00
parent d299d5531f
commit 14ab50defb
2 changed files with 34 additions and 3 deletions

View file

@ -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;

View file

@ -396,12 +396,21 @@ public:
[[nodiscard]] size_t WriteString(std::span<const char> string) const;
/**
* Attempts to flush any unwritten buffered data into the file and flush the file into the disk.
* Attempts to flush any unwritten buffered data into the file.
*
* @returns True if the flush was successful, false otherwise.
*/
bool Flush() const;
/**
* Attempts to commit the file into the disk.
* Note that this is an expensive operation as this forces the operating system to write
* the contents of the file associated with the file descriptor into the disk.
*
* @returns True if the commit was successful, false otherwise.
*/
bool Commit() const;
/**
* Resizes the file to a given size.
* If the file is resized to a smaller size, the remainder of the file is discarded.