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 = {0};
123 if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
124 0, std::numeric_limits<DWORD>::max(),
125 std::numeric_limits<DWORD>::max(), &overlapped)) {
138 std::string mb_string(e.what());
139 int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(),
140 mb_string.size(),
nullptr, 0);
142 std::wstring utf16_string(size, L
'\0');
143 MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(),
144 &*utf16_string.begin(), size);
146 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>()
147 .to_bytes(utf16_string);
153 return fs::temp_directory_path();
156 GetTempPathW(
MAX_PATH, dirPathWchars);
157 std::wstring dirPathWstring(dirPathWchars);
159 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)
std::string get_filesystem_error_message(const fs::filesystem_error &e)
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.