Add sync to file_helper (#2343)

This commit is contained in:
espkk
2023-01-15 15:33:40 +02:00
committed by GitHub
parent 5a63426d1c
commit f29f369a12
4 changed files with 27 additions and 2 deletions

View File

@@ -23,9 +23,10 @@
#ifdef _WIN32
# include <io.h> // _get_osfhandle and _isatty support
# include <process.h> // _get_pid support
# include <io.h> // for _get_osfhandle, _isatty, _fileno
# include <process.h> // for _get_pid
# include <spdlog/details/windows_include.h>
# include <fileapi.h> // for FlushFileBuffers
# ifdef __MINGW32__
# include <share.h>
@@ -601,6 +602,17 @@ std::string SPDLOG_INLINE getenv(const char *field)
#endif
}
// Do fsync by FILE descriptor
// Return true on success
SPDLOG_INLINE bool fsync(FILE *fd)
{
#ifdef _WIN32
return FlushFileBuffers(reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fd)))) != 0;
#else
return ::fsync(fileno(fd)) == 0;
#endif
}
} // namespace os
} // namespace details
} // namespace spdlog