12 #include <sys/utsname.h>
23 FILE *
fopen(
const fs::path& p,
const char *mode)
28 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t> utf8_cvt;
29 return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
36 static std::string GetErrorReason() {
37 return std::strerror(errno);
42 fd = open(file.string().c_str(), O_RDWR);
57 struct utsname uname_data;
58 return uname(&uname_data) == 0 && std::string(uname_data.version).find(
"Microsoft") != std::string::npos;
69 static const bool is_wsl = IsWSL();
71 if (flock(
fd, LOCK_EX | LOCK_NB) == -1) {
77 lock.l_type = F_WRLCK;
78 lock.l_whence = SEEK_SET;
81 if (fcntl(
fd, F_SETLK, &lock) == -1) {
91 static std::string GetErrorReason() {
93 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
94 nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<WCHAR*
>(&err), 0,
nullptr);
95 std::wstring err_str(err);
97 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
102 hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
103 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
104 if (hFile == INVALID_HANDLE_VALUE) {
105 reason = GetErrorReason();
111 if (hFile != INVALID_HANDLE_VALUE) {
118 if (hFile == INVALID_HANDLE_VALUE) {
121 _OVERLAPPED overlapped = {0};
122 if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
123 reason = GetErrorReason();
136 std::string mb_string(e.what());
137 int size = MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(),
nullptr, 0);
139 std::wstring utf16_string(size,
L'\0');
140 MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), &*utf16_string.begin(), size);
142 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>().to_bytes(utf16_string);
151 static std::string openmodeToStr(std::ios_base::openmode mode)
153 switch (mode & ~std::ios_base::ate) {
154 case std::ios_base::out:
155 case std::ios_base::out | std::ios_base::trunc:
157 case std::ios_base::out | std::ios_base::app:
158 case std::ios_base::app:
160 case std::ios_base::in:
162 case std::ios_base::in | std::ios_base::out:
164 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc:
166 case std::ios_base::in | std::ios_base::out | std::ios_base::app:
167 case std::ios_base::in | std::ios_base::app:
169 case std::ios_base::out | std::ios_base::binary:
170 case std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
172 case std::ios_base::out | std::ios_base::app | std::ios_base::binary:
173 case std::ios_base::app | std::ios_base::binary:
175 case std::ios_base::in | std::ios_base::binary:
177 case std::ios_base::in | std::ios_base::out | std::ios_base::binary:
179 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
181 case std::ios_base::in | std::ios_base::out | std::ios_base::app | std::ios_base::binary:
182 case std::ios_base::in | std::ios_base::app | std::ios_base::binary:
185 return std::string();
189 void ifstream::open(
const fs::path& p, std::ios_base::openmode mode)
193 if (m_file ==
nullptr) {
196 m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
198 if (mode & std::ios_base::ate) {
199 seekg(0, std::ios_base::end);
203 void ifstream::close()
205 if (m_file !=
nullptr) {
212 void ofstream::open(
const fs::path& p, std::ios_base::openmode mode)
216 if (m_file ==
nullptr) {
219 m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
221 if (mode & std::ios_base::ate) {
222 seekp(0, std::ios_base::end);
226 void ofstream::close()
228 if (m_file !=
nullptr) {
236 static_assert(
sizeof(*fs::path().BOOST_FILESYSTEM_C_STR) ==
sizeof(
wchar_t),
237 "Warning: This build is using boost::filesystem ofstream and ifstream "
238 "implementations which will fail to open paths containing multibyte "
239 "characters. You should delete this static_assert to ignore this warning, "
240 "or switch to a different C++ standard library like the Microsoft C++ "
241 "Standard Library (where boost uses non-standard extensions to construct "
242 "stream objects with wide filenames), or the GNU libstdc++ library (where "
243 "a more complicated workaround has been implemented above).");
#define L(x0, x1, x2, x3, x4, x5, x6, x7)
Filesystem operations and types.
FILE * fopen(const fs::path &p, const char *mode)
std::string get_filesystem_error_message(const fs::filesystem_error &e)