12#include <sys/utsname.h>
30 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t> utf8_cvt;
31 return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
37 assert(base.is_absolute());
38 return fs::absolute(path, base);
45 return std::strerror(errno);
50 fd = open(file.c_str(), O_RDWR);
65 struct utsname uname_data;
66 return uname(&uname_data) == 0 && std::string(uname_data.version).find(
"Microsoft") != std::string::npos;
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 | FORMAT_MESSAGE_IGNORE_INSERTS,
102 nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<WCHAR*
>(&err), 0,
nullptr);
103 std::wstring err_str(err);
105 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
110 hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
111 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
112 if (hFile == INVALID_HANDLE_VALUE) {
119 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, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
144 std::string mb_string(e.what());
145 int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(),
nullptr, 0);
147 std::wstring utf16_string(size, L
'\0');
148 MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), &*utf16_string.begin(), size);
150 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>().to_bytes(utf16_string);
158#if defined(__GNUC__) && !defined(__clang__)
159#pragma GCC diagnostic push
160#pragma GCC diagnostic ignored "-Wswitch"
162static std::string openmodeToStr(std::ios_base::openmode mode)
164 switch (mode & ~std::ios_base::ate) {
165 case std::ios_base::out:
166 case std::ios_base::out | std::ios_base::trunc:
168 case std::ios_base::out | std::ios_base::app:
169 case std::ios_base::app:
171 case std::ios_base::in:
173 case std::ios_base::in | std::ios_base::out:
175 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc:
177 case std::ios_base::in | std::ios_base::out | std::ios_base::app:
178 case std::ios_base::in | std::ios_base::app:
180 case std::ios_base::out | std::ios_base::binary:
181 case std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
183 case std::ios_base::out | std::ios_base::app | std::ios_base::binary:
184 case std::ios_base::app | std::ios_base::binary:
186 case std::ios_base::in | std::ios_base::binary:
188 case std::ios_base::in | std::ios_base::out | std::ios_base::binary:
190 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
192 case std::ios_base::in | std::ios_base::out | std::ios_base::app | std::ios_base::binary:
193 case std::ios_base::in | std::ios_base::app | std::ios_base::binary:
196 return std::string();
199#if defined(__GNUC__) && !defined(__clang__)
200#pragma GCC diagnostic pop
203void ifstream::open(
const fs::path& p, std::ios_base::openmode mode)
206 mode |= std::ios_base::in;
208 if (m_file ==
nullptr) {
211 m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
213 if (mode & std::ios_base::ate) {
214 seekg(0, std::ios_base::end);
218void ifstream::close()
220 if (m_file !=
nullptr) {
227void ofstream::open(
const fs::path& p, std::ios_base::openmode mode)
230 mode |= std::ios_base::out;
232 if (m_file ==
nullptr) {
235 m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
237 if (mode & std::ios_base::ate) {
238 seekp(0, std::ios_base::end);
242void ofstream::close()
244 if (m_file !=
nullptr) {
252#if BOOST_VERSION >= 107700
253static_assert(
sizeof(*BOOST_FILESYSTEM_C_STR(boost::filesystem::path())) ==
sizeof(
wchar_t),
255static_assert(
sizeof(*boost::filesystem::path().BOOST_FILESYSTEM_C_STR) ==
sizeof(
wchar_t),
257 "Warning: This build is using boost::filesystem ofstream and ifstream "
258 "implementations which will fail to open paths containing multibyte "
259 "characters. You should delete this static_assert to ignore this warning, "
260 "or switch to a different C++ standard library like the Microsoft C++ "
261 "Standard Library (where boost uses non-standard extensions to construct "
262 "stream objects with wide filenames), or the GNU libstdc++ library (where "
263 "a more complicated workaround has been implemented above).");
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Bridge operations to C stdio.
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()