13 FILE *
fopen(
const fs::path& p,
const char *mode)
18 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t> utf8_cvt;
19 return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
25 static std::string GetErrorReason() {
26 return std::strerror(errno);
31 fd = open(file.string().c_str(), O_RDWR);
50 lock.l_type = F_WRLCK;
51 lock.l_whence = SEEK_SET;
54 if (fcntl(
fd, F_SETLK, &lock) == -1) {
62 static std::string GetErrorReason() {
64 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
65 nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<WCHAR*>(&err), 0,
nullptr);
66 std::wstring err_str(err);
68 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
73 hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
74 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
75 if (hFile == INVALID_HANDLE_VALUE) {
82 if (hFile != INVALID_HANDLE_VALUE) {
89 if (hFile == INVALID_HANDLE_VALUE) {
92 _OVERLAPPED overlapped = {0};
93 if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
107 std::string mb_string(e.what());
108 int size = MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(),
nullptr, 0);
110 std::wstring utf16_string(size, L
'\0');
111 MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), &*utf16_string.begin(), size);
113 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>().to_bytes(utf16_string);
122 static std::string openmodeToStr(std::ios_base::openmode mode)
124 switch (mode & ~std::ios_base::ate) {
125 case std::ios_base::out:
126 case std::ios_base::out | std::ios_base::trunc:
128 case std::ios_base::out | std::ios_base::app:
129 case std::ios_base::app:
131 case std::ios_base::in:
133 case std::ios_base::in | std::ios_base::out:
135 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc:
137 case std::ios_base::in | std::ios_base::out | std::ios_base::app:
138 case std::ios_base::in | std::ios_base::app:
140 case std::ios_base::out | std::ios_base::binary:
141 case std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
143 case std::ios_base::out | std::ios_base::app | std::ios_base::binary:
144 case std::ios_base::app | std::ios_base::binary:
146 case std::ios_base::in | std::ios_base::binary:
148 case std::ios_base::in | std::ios_base::out | std::ios_base::binary:
150 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
152 case std::ios_base::in | std::ios_base::out | std::ios_base::app | std::ios_base::binary:
153 case std::ios_base::in | std::ios_base::app | std::ios_base::binary:
156 return std::string();
160 void ifstream::open(
const fs::path& p, std::ios_base::openmode mode)
164 if (m_file ==
nullptr) {
167 m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
169 if (mode & std::ios_base::ate) {
174 void ifstream::close()
176 if (m_file !=
nullptr) {
183 void ofstream::open(
const fs::path& p, std::ios_base::openmode mode)
187 if (m_file ==
nullptr) {
190 m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
192 if (mode & std::ios_base::ate) {
197 void ofstream::close()
199 if (m_file !=
nullptr) {
207 static_assert(
sizeof(*fs::path().BOOST_FILESYSTEM_C_STR) ==
sizeof(
wchar_t),
208 "Warning: This build is using boost::filesystem ofstream and ifstream " 209 "implementations which will fail to open paths containing multibyte " 210 "characters. You should delete this static_assert to ignore this warning, " 211 "or switch to a different C++ standard library like the Microsoft C++ " 212 "Standard Library (where boost uses non-standard extensions to construct " 213 "stream objects with wide filenames), or the GNU libstdc++ library (where " 214 "a more complicated workaround has been implemented above).");
216 #endif // __GLIBCXX__ FILE * fopen(const fs::path &p, const char *mode)
Filesystem operations and types.
std::string get_filesystem_error_message(const fs::filesystem_error &e)