fs: atomic truncation

This commit is contained in:
Nekotekina 2017-09-04 01:55:03 +03:00
parent 2e510e5ca0
commit b415b0e737
2 changed files with 7 additions and 18 deletions

View File

@ -682,11 +682,10 @@ bool fs::truncate_file(const std::string& path, u64 length)
return false;
}
LARGE_INTEGER distance;
distance.QuadPart = length;
FILE_END_OF_FILE_INFO _eof;
_eof.EndOfFile.QuadPart = length;
// Seek and truncate
if (!SetFilePointerEx(handle, distance, NULL, FILE_BEGIN) || !SetEndOfFile(handle))
if (!SetFileInformationByHandle(handle, FileEndOfFileInfo, &_eof, sizeof(_eof)))
{
g_tls_error = to_error(GetLastError());
CloseHandle(handle);
@ -846,23 +845,15 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
bool trunc(u64 length) override
{
LARGE_INTEGER old, pos;
FILE_END_OF_FILE_INFO _eof;
_eof.EndOfFile.QuadPart = length;
pos.QuadPart = 0;
if (!SetFilePointerEx(m_handle, pos, &old, FILE_CURRENT)) // get old position
if (!SetFileInformationByHandle(m_handle, FileEndOfFileInfo, &_eof, sizeof(_eof)))
{
g_tls_error = to_error(GetLastError());
return false;
}
pos.QuadPart = length;
if (!SetFilePointerEx(m_handle, pos, NULL, FILE_BEGIN)) // set new position
{
g_tls_error = to_error(GetLastError());
return false;
}
verify("file::trunc" HERE), SetEndOfFile(m_handle), SetFilePointerEx(m_handle, old, NULL, FILE_BEGIN);
return true;
}

View File

@ -348,12 +348,10 @@ logs::file_writer::~file_writer()
#ifdef _WIN32
UnmapViewOfFile(m_fptr);
CloseHandle(m_fmap);
m_file.seek(m_size);
SetEndOfFile(m_file.get_handle());
#else
::munmap(m_fptr, s_log_size);
m_file.trunc(m_size);
#endif
m_file.trunc(m_size);
}
void logs::file_writer::log(logs::level sev, const char* text, std::size_t size)