Add fs::file::set_delete

This commit is contained in:
Nekotekina 2019-04-07 23:55:38 +03:00
parent a54d9c527f
commit 0736fc8b28
3 changed files with 16 additions and 6 deletions

View File

@ -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<std::logic_error>("fs::dir is null");

View File

@ -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

View File

@ -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);