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) {
101 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
102 FORMAT_MESSAGE_IGNORE_INSERTS,
103 nullptr, GetLastError(),
104 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
105 reinterpret_cast<WCHAR *
>(&err), 0,
nullptr);
106 std::wstring err_str(err);
108 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(
113 hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE,
114 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
115 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
116 if (hFile == INVALID_HANDLE_VALUE) {
122 if (hFile != INVALID_HANDLE_VALUE) {
128 if (hFile == INVALID_HANDLE_VALUE) {
131 _OVERLAPPED overlapped = {0};
132 if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
133 0, std::numeric_limits<DWORD>::max(),
134 std::numeric_limits<DWORD>::max(), &overlapped)) {
147 std::string mb_string(e.what());
148 int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(),
149 mb_string.size(),
nullptr, 0);
151 std::wstring utf16_string(size, L
'\0');
152 MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(),
153 &*utf16_string.begin(), size);
155 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>()
156 .to_bytes(utf16_string);
162 return fs::temp_directory_path();
165 GetTempPathW(
MAX_PATH, dirPathWchars);
166 std::wstring dirPathWstring(dirPathWchars);
168 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.