1
0
Fork 0
forked from suyu/suyu

Merge pull request #6562 from Morph1984/flush-behavior

common: fs: More misc. changes
This commit is contained in:
bunnei 2021-07-07 00:40:31 -07:00 committed by GitHub
commit 2eb018c80f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 12 deletions

View file

@ -306,9 +306,9 @@ bool IOFile::Flush() const {
errno = 0; errno = 0;
#ifdef _WIN32 #ifdef _WIN32
const auto flush_result = std::fflush(file) == 0 && _commit(fileno(file)) == 0; const auto flush_result = std::fflush(file) == 0;
#else #else
const auto flush_result = std::fflush(file) == 0 && fsync(fileno(file)) == 0; const auto flush_result = std::fflush(file) == 0;
#endif #endif
if (!flush_result) { if (!flush_result) {
@ -320,6 +320,28 @@ bool IOFile::Flush() const {
return flush_result; 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 { bool IOFile::SetSize(u64 size) const {
if (!IsOpen()) { if (!IsOpen()) {
return false; return false;
@ -347,6 +369,9 @@ u64 IOFile::GetSize() const {
return 0; return 0;
} }
// Flush any unwritten buffered data into the file prior to retrieving the file size.
std::fflush(file);
std::error_code ec; std::error_code ec;
const auto file_size = fs::file_size(file_path, ec); const auto file_size = fs::file_size(file_path, ec);

View file

@ -396,12 +396,21 @@ public:
[[nodiscard]] size_t WriteString(std::span<const char> string) const; [[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. * @returns True if the flush was successful, false otherwise.
*/ */
bool Flush() const; 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. * Resizes the file to a given size.
* If the file is resized to a smaller size, the remainder of the file is discarded. * If the file is resized to a smaller size, the remainder of the file is discarded.

View file

@ -171,19 +171,22 @@ FileBackend::FileBackend(const std::filesystem::path& filename) {
FileBackend::~FileBackend() = default; FileBackend::~FileBackend() = default;
void FileBackend::Write(const Entry& entry) { void FileBackend::Write(const Entry& entry) {
using namespace Common::Literals;
// prevent logs from going over the maximum size (in case its spamming and the user doesn't
// know)
constexpr std::size_t MAX_BYTES_WRITTEN = 100_MiB;
constexpr std::size_t MAX_BYTES_WRITTEN_EXTENDED = 1_GiB;
if (!file->IsOpen()) { if (!file->IsOpen()) {
return; return;
} }
if (Settings::values.extended_logging && bytes_written > MAX_BYTES_WRITTEN_EXTENDED) { using namespace Common::Literals;
return; // Prevent logs from exceeding a set maximum size in the event that log entries are spammed.
} else if (!Settings::values.extended_logging && bytes_written > MAX_BYTES_WRITTEN) { constexpr std::size_t MAX_BYTES_WRITTEN = 100_MiB;
constexpr std::size_t MAX_BYTES_WRITTEN_EXTENDED = 1_GiB;
const bool write_limit_exceeded =
bytes_written > MAX_BYTES_WRITTEN_EXTENDED ||
(bytes_written > MAX_BYTES_WRITTEN && !Settings::values.extended_logging);
// Close the file after the write limit is exceeded.
if (write_limit_exceeded) {
file->Close();
return; return;
} }