14#include <sys/utsname.h>
34 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t> utf8_cvt;
35 return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
40 assert(base.is_absolute());
41 return path.empty() ? base :
fs::path(base / path);
51 fd = open(file.c_str(), O_RDWR);
64 struct utsname uname_data;
65 return uname(&uname_data) == 0 &&
66 std::string(uname_data.version).find(
"Microsoft") !=
77 static const bool is_wsl =
IsWSL();
79 if (flock(
fd, LOCK_EX | LOCK_NB) == -1) {
85 lock.l_type = F_WRLCK;
86 lock.l_whence = SEEK_SET;
89 if (fcntl(
fd, F_SETLK, &lock) == -1) {
100 return Win32ErrorString(GetLastError());
104 hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE,
105 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
106 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
107 if (hFile == INVALID_HANDLE_VALUE) {
113 if (hFile != INVALID_HANDLE_VALUE) {
119 if (hFile == INVALID_HANDLE_VALUE) {
122 _OVERLAPPED overlapped = {};
123 if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
124 0, std::numeric_limits<DWORD>::max(),
125 std::numeric_limits<DWORD>::max(), &overlapped)) {
135 return fs::temp_directory_path();
138 GetTempPathW(
MAX_PATH, dirPathWchars);
139 std::wstring dirPathWstring(dirPathWchars);
141 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static path PathFromString(const std::string &string)
Convert byte string to path object.
Bridge operations to C stdio.
fs::path GetTempDirectoryPath()
FILE * fopen(const fs::path &p, const char *mode)
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
static std::string GetErrorReason()
std::string SysErrorString(int err)
Return system error string from errno value.