diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/fs/file.cpp | 29 | ||||
| -rw-r--r-- | src/common/fs/file.h | 11 | ||||
| -rw-r--r-- | src/common/logging/backend.cpp | 19 |
3 files changed, 48 insertions, 11 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 | ||
| 323 | bool 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 | |||
| 323 | bool IOFile::SetSize(u64 size) const { | 345 | bool 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. |
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index b6fa4affb..61dddab3f 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp | |||
| @@ -171,19 +171,22 @@ FileBackend::FileBackend(const std::filesystem::path& filename) { | |||
| 171 | FileBackend::~FileBackend() = default; | 171 | FileBackend::~FileBackend() = default; |
| 172 | 172 | ||
| 173 | void FileBackend::Write(const Entry& entry) { | 173 | void FileBackend::Write(const Entry& entry) { |
| 174 | if (!file->IsOpen()) { | ||
| 175 | return; | ||
| 176 | } | ||
| 177 | |||
| 174 | using namespace Common::Literals; | 178 | using namespace Common::Literals; |
| 175 | // prevent logs from going over the maximum size (in case its spamming and the user doesn't | 179 | // Prevent logs from exceeding a set maximum size in the event that log entries are spammed. |
| 176 | // know) | ||
| 177 | constexpr std::size_t MAX_BYTES_WRITTEN = 100_MiB; | 180 | constexpr std::size_t MAX_BYTES_WRITTEN = 100_MiB; |
| 178 | constexpr std::size_t MAX_BYTES_WRITTEN_EXTENDED = 1_GiB; | 181 | constexpr std::size_t MAX_BYTES_WRITTEN_EXTENDED = 1_GiB; |
| 179 | 182 | ||
| 180 | if (!file->IsOpen()) { | 183 | const bool write_limit_exceeded = |
| 181 | return; | 184 | bytes_written > MAX_BYTES_WRITTEN_EXTENDED || |
| 182 | } | 185 | (bytes_written > MAX_BYTES_WRITTEN && !Settings::values.extended_logging); |
| 183 | 186 | ||
| 184 | if (Settings::values.extended_logging && bytes_written > MAX_BYTES_WRITTEN_EXTENDED) { | 187 | // Close the file after the write limit is exceeded. |
| 185 | return; | 188 | if (write_limit_exceeded) { |
| 186 | } else if (!Settings::values.extended_logging && bytes_written > MAX_BYTES_WRITTEN) { | 189 | file->Close(); |
| 187 | return; | 190 | return; |
| 188 | } | 191 | } |
| 189 | 192 | ||