From 0736fc8b28ec7f4113392025b8b771353ccfb688 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sun, 7 Apr 2019 23:55:38 +0300 Subject: [PATCH] Add fs::file::set_delete --- Utilities/File.cpp | 9 +++++++++ Utilities/File.h | 5 +++++ Utilities/Log.cpp | 8 ++------ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index 2ed5ac4500..52e43da2c2 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -1229,6 +1229,15 @@ fs::native_handle fs::file::get_handle() const #endif } +#ifdef _WIN32 +bool fs::file::set_delete(bool autodelete) const +{ + FILE_DISPOSITION_INFO disp; + disp.DeleteFileW = autodelete; + return SetFileInformationByHandle(get_handle(), FileDispositionInfo, &disp, sizeof(disp)) != 0; +} +#endif + void fs::dir::xnull() const { fmt::throw_exception("fs::dir is null"); diff --git a/Utilities/File.h b/Utilities/File.h index ef4db36ff6..641fc954ee 100644 --- a/Utilities/File.h +++ b/Utilities/File.h @@ -361,6 +361,11 @@ namespace fs // Get native handle if available native_handle get_handle() const; + +#ifdef _WIN32 + // Windows-specific function + bool set_delete(bool autodelete = true) const; +#endif }; class dir final diff --git a/Utilities/Log.cpp b/Utilities/Log.cpp index 47383ef567..77237e45a1 100644 --- a/Utilities/Log.cpp +++ b/Utilities/Log.cpp @@ -387,9 +387,7 @@ logs::file_writer::file_writer(const std::string& name) #ifdef _WIN32 // Autodelete compressed log file - FILE_DISPOSITION_INFO disp; - disp.DeleteFileW = TRUE; - SetFileInformationByHandle(m_fout2.get_handle(), FileDispositionInfo, &disp, sizeof(disp)); + m_fout2.set_delete(); #endif } catch (const std::exception& e) @@ -469,9 +467,7 @@ logs::file_writer::~file_writer() #ifdef _WIN32 // Cancel compressed log file autodeletion - FILE_DISPOSITION_INFO disp; - disp.DeleteFileW = FALSE; - SetFileInformationByHandle(m_fout2.get_handle(), FileDispositionInfo, &disp, sizeof(disp)); + m_fout2.set_delete(false); UnmapViewOfFile(m_fptr); CloseHandle(m_fmap);