summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Morph2021-07-05 12:32:14 -0400
committerGravatar Morph2021-07-06 05:59:47 -0400
commit14ab50defb2c6b06aec9a1401154fc57e662fc9d (patch)
tree8fd09e89becca011840721de00e385318d597d96
parentcommon: fs: file: Flush the file in GetSize (diff)
downloadyuzu-14ab50defb2c6b06aec9a1401154fc57e662fc9d.tar.gz
yuzu-14ab50defb2c6b06aec9a1401154fc57e662fc9d.tar.xz
yuzu-14ab50defb2c6b06aec9a1401154fc57e662fc9d.zip
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.
-rw-r--r--src/common/fs/file.cpp26
-rw-r--r--src/common/fs/file.h11
2 files changed, 34 insertions, 3 deletions
diff --git a/src/common/fs/file.cpp b/src/common/fs/file.cpp
index 022780e4e..274f57659 100644
--- a/src/common/fs/file.cpp
+++ b/src/common/fs/file.cpp
@@ -306,9 +306,9 @@ bool IOFile::Flush() const {
306 errno = 0; 306 errno = 0;
307 307
308#ifdef _WIN32 308#ifdef _WIN32
309 const auto flush_result = std::fflush(file) == 0 && _commit(fileno(file)) == 0; 309 const auto flush_result = std::fflush(file) == 0;
310#else 310#else
311 const auto flush_result = std::fflush(file) == 0 && fsync(fileno(file)) == 0; 311 const auto flush_result = std::fflush(file) == 0;
312#endif 312#endif
313 313
314 if (!flush_result) { 314 if (!flush_result) {
@@ -320,6 +320,28 @@ bool IOFile::Flush() const {
320 return flush_result; 320 return flush_result;
321} 321}
322 322
323bool IOFile::Commit() const {
324 if (!IsOpen()) {
325 return false;
326 }
327
328 errno = 0;
329
330#ifdef _WIN32
331 const auto commit_result = std::fflush(file) == 0 && _commit(fileno(file)) == 0;
332#else
333 const auto commit_result = std::fflush(file) == 0 && fsync(fileno(file)) == 0;
334#endif
335
336 if (!commit_result) {
337 const auto ec = std::error_code{errno, std::generic_category()};
338 LOG_ERROR(Common_Filesystem, "Failed to commit the file at path={}, ec_message={}",
339 PathToUTF8String(file_path), ec.message());
340 }
341
342 return commit_result;
343}
344
323bool IOFile::SetSize(u64 size) const { 345bool IOFile::SetSize(u64 size) const {
324 if (!IsOpen()) { 346 if (!IsOpen()) {
325 return false; 347 return false;
diff --git a/src/common/fs/file.h b/src/common/fs/file.h
index 588fe619d..2c4ab4332 100644
--- a/src/common/fs/file.h
+++ b/src/common/fs/file.h
@@ -396,13 +396,22 @@ public:
396 [[nodiscard]] size_t WriteString(std::span<const char> string) const; 396 [[nodiscard]] size_t WriteString(std::span<const char> string) const;
397 397
398 /** 398 /**
399 * Attempts to flush any unwritten buffered data into the file and flush the file into the disk. 399 * Attempts to flush any unwritten buffered data into the file.
400 * 400 *
401 * @returns True if the flush was successful, false otherwise. 401 * @returns True if the flush was successful, false otherwise.
402 */ 402 */
403 bool Flush() const; 403 bool Flush() const;
404 404
405 /** 405 /**
406 * Attempts to commit the file into the disk.
407 * Note that this is an expensive operation as this forces the operating system to write
408 * the contents of the file associated with the file descriptor into the disk.
409 *
410 * @returns True if the commit was successful, false otherwise.
411 */
412 bool Commit() const;
413
414 /**
406 * Resizes the file to a given size. 415 * Resizes the file to a given size.
407 * If the file is resized to a smaller size, the remainder of the file is discarded. 416 * If the file is resized to a smaller size, the remainder of the file is discarded.
408 * If the file is resized to a larger size, the new area appears as if zero-filled. 417 * If the file is resized to a larger size, the new area appears as if zero-filled.