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

View file

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