11 #include <sys/utsname.h> 22 FILE *
fopen(
const fs::path &p,
const char *mode) {
26 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t> utf8_cvt;
27 return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
34 return std::strerror(errno);
38 fd = open(file.string().c_str(), O_RDWR);
51 struct utsname uname_data;
52 return uname(&uname_data) == 0 &&
53 std::string(uname_data.version).find(
"Microsoft") !=
64 static const bool is_wsl =
IsWSL();
66 if (flock(
fd, LOCK_EX | LOCK_NB) == -1) {
72 lock.l_type = F_WRLCK;
73 lock.l_whence = SEEK_SET;
76 if (fcntl(
fd, F_SETLK, &lock) == -1) {
88 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
89 FORMAT_MESSAGE_IGNORE_INSERTS,
90 nullptr, GetLastError(),
91 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
92 reinterpret_cast<WCHAR *>(&err), 0,
nullptr);
93 std::wstring err_str(err);
95 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(
100 hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE,
101 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
102 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
103 if (hFile == INVALID_HANDLE_VALUE) {
109 if (hFile != INVALID_HANDLE_VALUE) {
115 if (hFile == INVALID_HANDLE_VALUE) {
118 _OVERLAPPED overlapped = {0};
119 if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
120 0, std::numeric_limits<DWORD>::max(),
121 std::numeric_limits<DWORD>::max(), &overlapped)) {
134 std::string mb_string(e.what());
135 int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(),
136 mb_string.size(),
nullptr, 0);
138 std::wstring utf16_string(size, L
'\0');
139 MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(),
140 &*utf16_string.begin(), size);
142 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>()
143 .to_bytes(utf16_string);
153 static std::string openmodeToStr(std::ios_base::openmode mode) {
154 switch (mode & ~std::ios_base::ate) {
155 case std::ios_base::out:
156 case std::ios_base::out | std::ios_base::trunc:
158 case std::ios_base::out | std::ios_base::app:
159 case std::ios_base::app:
161 case std::ios_base::in:
163 case std::ios_base::in | std::ios_base::out:
165 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc:
167 case std::ios_base::in | std::ios_base::out | std::ios_base::app:
168 case std::ios_base::in | std::ios_base::app:
170 case std::ios_base::out | std::ios_base::binary:
171 case std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
173 case std::ios_base::out | std::ios_base::app | std::ios_base::binary:
174 case std::ios_base::app | std::ios_base::binary:
176 case std::ios_base::in | std::ios_base::binary:
178 case std::ios_base::in | std::ios_base::out | std::ios_base::binary:
180 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc |
181 std::ios_base::binary:
183 case std::ios_base::in | std::ios_base::out | std::ios_base::app |
184 std::ios_base::binary:
185 case std::ios_base::in | std::ios_base::app | std::ios_base::binary:
188 return std::string();
192 void ifstream::open(
const fs::path &p, std::ios_base::openmode mode) {
194 mode |= std::ios_base::in;
196 if (m_file ==
nullptr) {
199 m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
201 if (mode & std::ios_base::ate) {
202 seekg(0, std::ios_base::end);
206 void ifstream::close() {
207 if (m_file !=
nullptr) {
214 void ofstream::open(
const fs::path &p, std::ios_base::openmode mode) {
216 mode |= std::ios_base::out;
218 if (m_file ==
nullptr) {
221 m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
223 if (mode & std::ios_base::ate) {
224 seekp(0, std::ios_base::end);
228 void ofstream::close() {
229 if (m_file !=
nullptr) {
238 sizeof(*fs::path().BOOST_FILESYSTEM_C_STR) ==
sizeof(
wchar_t),
239 "Warning: This build is using boost::filesystem ofstream and ifstream " 240 "implementations which will fail to open paths containing multibyte " 241 "characters. You should delete this static_assert to ignore this warning, " 242 "or switch to a different C++ standard library like the Microsoft C++ " 243 "Standard Library (where boost uses non-standard extensions to construct " 244 "stream objects with wide filenames), or the GNU libstdc++ library (where " 245 "a more complicated workaround has been implemented above).");
247 #endif // __GLIBCXX__ FILE * fopen(const fs::path &p, const char *mode)
Filesystem operations and types.
static std::string GetErrorReason()
std::string get_filesystem_error_message(const fs::filesystem_error &e)