summaryrefslogtreecommitdiff
path: root/src/common/fs
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/fs')
-rw-r--r--src/common/fs/file.cpp29
-rw-r--r--src/common/fs/file.h11
2 files changed, 37 insertions, 3 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 {
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;
@@ -347,6 +369,9 @@ u64 IOFile::GetSize() const {
347 return 0; 369 return 0;
348 } 370 }
349 371
372 // Flush any unwritten buffered data into the file prior to retrieving the file size.
373 std::fflush(file);
374
350 std::error_code ec; 375 std::error_code ec;
351 376
352 const auto file_size = fs::file_size(file_path, ec); 377 const auto file_size = fs::file_size(file_path, ec);
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.