12 #include <sys/utsname.h>
32 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t> utf8_cvt;
33 return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
38 assert(base.is_absolute());
39 return path.empty() ? base :
fs::path(base / path);
45 return std::strerror(errno);
49 fd = open(file.c_str(), O_RDWR);
62 struct utsname uname_data;
63 return uname(&uname_data) == 0 &&
64 std::string(uname_data.version).find(
"Microsoft") !=
75 static const bool is_wsl =
IsWSL();
77 if (flock(
fd, LOCK_EX | LOCK_NB) == -1) {
83 lock.l_type = F_WRLCK;
84 lock.l_whence = SEEK_SET;
87 if (fcntl(
fd, F_SETLK, &lock) == -1) {
99 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
100 FORMAT_MESSAGE_IGNORE_INSERTS,
101 nullptr, GetLastError(),
102 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
103 reinterpret_cast<WCHAR *
>(&err), 0,
nullptr);
104 std::wstring err_str(err);
106 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(
111 hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE,
112 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
113 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
114 if (hFile == INVALID_HANDLE_VALUE) {
120 if (hFile != INVALID_HANDLE_VALUE) {
126 if (hFile == INVALID_HANDLE_VALUE) {
129 _OVERLAPPED overlapped = {0};
130 if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
131 0, std::numeric_limits<DWORD>::max(),
132 std::numeric_limits<DWORD>::max(), &overlapped)) {
145 std::string mb_string(e.what());
146 int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(),
147 mb_string.size(),
nullptr, 0);
149 std::wstring utf16_string(size, L
'\0');
150 MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(),
151 &*utf16_string.begin(), size);
153 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>()
154 .to_bytes(utf16_string);
160 return fs::temp_directory_path();
163 GetTempPathW(
MAX_PATH, dirPathWchars);
164 std::wstring dirPathWstring(dirPathWchars);
166 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()