Line data Source code
1 : // Copyright (c) 2017-2020 The Bitcoin Core developers
2 : // Copyright (c) 2020-2021 The PIVX Core developers
3 : // Distributed under the MIT/X11 software license, see the accompanying
4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 :
6 : #include "fs.h"
7 :
8 : #ifndef WIN32
9 : #include <fcntl.h>
10 : #include <string>
11 : #include <sys/file.h>
12 : #include <sys/utsname.h>
13 : #else
14 : #ifndef NOMINMAX
15 : #define NOMINMAX
16 : #endif
17 : #include <codecvt>
18 : #include <windows.h>
19 : #endif
20 :
21 : namespace fsbridge {
22 :
23 382513 : FILE *fopen(const fs::path& p, const char *mode)
24 : {
25 : #ifndef WIN32
26 382513 : return ::fopen(p.string().c_str(), mode);
27 : #else
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());
30 : #endif
31 : }
32 :
33 :
34 : #ifndef WIN32
35 :
36 6 : static std::string GetErrorReason() {
37 6 : return std::strerror(errno);
38 : }
39 :
40 1141 : FileLock::FileLock(const fs::path& file)
41 : {
42 1141 : fd = open(file.string().c_str(), O_RDWR);
43 1141 : if (fd == -1) {
44 1 : reason = GetErrorReason();
45 : }
46 1141 : }
47 :
48 1141 : FileLock::~FileLock()
49 : {
50 1141 : if (fd != -1) {
51 1140 : close(fd);
52 : }
53 1141 : }
54 :
55 382 : static bool IsWSL()
56 : {
57 382 : struct utsname uname_data;
58 764 : return uname(&uname_data) == 0 && std::string(uname_data.version).find("Microsoft") != std::string::npos;
59 : }
60 :
61 1141 : bool FileLock::TryLock()
62 : {
63 1141 : if (fd == -1) {
64 : return false;
65 : }
66 :
67 : // Exclusive file locking is broken on WSL using fcntl (issue #18622)
68 : // This workaround can be removed once the bug on WSL is fixed
69 1140 : static const bool is_wsl = IsWSL();
70 1140 : if (is_wsl) {
71 0 : if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
72 0 : reason = GetErrorReason();
73 0 : return false;
74 : }
75 : } else {
76 1140 : struct flock lock;
77 1140 : lock.l_type = F_WRLCK;
78 1140 : lock.l_whence = SEEK_SET;
79 1140 : lock.l_start = 0;
80 1140 : lock.l_len = 0;
81 1140 : if (fcntl(fd, F_SETLK, &lock) == -1) {
82 5 : reason = GetErrorReason();
83 5 : return false;
84 : }
85 : }
86 :
87 : return true;
88 : }
89 : #else
90 :
91 : static std::string GetErrorReason() {
92 : wchar_t* err;
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);
96 : LocalFree(err);
97 : return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
98 : }
99 :
100 : FileLock::FileLock(const fs::path& file)
101 : {
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();
106 : }
107 : }
108 :
109 : FileLock::~FileLock()
110 : {
111 : if (hFile != INVALID_HANDLE_VALUE) {
112 : CloseHandle(hFile);
113 : }
114 : }
115 :
116 : bool FileLock::TryLock()
117 : {
118 : if (hFile == INVALID_HANDLE_VALUE) {
119 : return false;
120 : }
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();
124 : return false;
125 : }
126 : return true;
127 : }
128 : #endif
129 :
130 0 : std::string get_filesystem_error_message(const fs::filesystem_error& e)
131 : {
132 : #ifndef WIN32
133 0 : return e.what();
134 : #else
135 : // Convert from Multi Byte to utf-16
136 : std::string mb_string(e.what());
137 : int size = MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), nullptr, 0);
138 :
139 : std::wstring utf16_string(size, L'\0');
140 : MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), &*utf16_string.begin(), size);
141 : // Convert from utf-16 to utf-8
142 : return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
143 : #endif
144 : }
145 :
146 : #ifdef WIN32
147 : #ifdef __GLIBCXX__
148 :
149 : // reference: https://github.com/gcc-mirror/gcc/blob/gcc-7_3_0-release/libstdc%2B%2B-v3/include/std/fstream#L270
150 :
151 : static std::string openmodeToStr(std::ios_base::openmode mode)
152 : {
153 : switch (mode & ~std::ios_base::ate) {
154 : case std::ios_base::out:
155 : case std::ios_base::out | std::ios_base::trunc:
156 : return "w";
157 : case std::ios_base::out | std::ios_base::app:
158 : case std::ios_base::app:
159 : return "a";
160 : case std::ios_base::in:
161 : return "r";
162 : case std::ios_base::in | std::ios_base::out:
163 : return "r+";
164 : case std::ios_base::in | std::ios_base::out | std::ios_base::trunc:
165 : return "w+";
166 : case std::ios_base::in | std::ios_base::out | std::ios_base::app:
167 : case std::ios_base::in | std::ios_base::app:
168 : return "a+";
169 : case std::ios_base::out | std::ios_base::binary:
170 : case std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
171 : return "wb";
172 : case std::ios_base::out | std::ios_base::app | std::ios_base::binary:
173 : case std::ios_base::app | std::ios_base::binary:
174 : return "ab";
175 : case std::ios_base::in | std::ios_base::binary:
176 : return "rb";
177 : case std::ios_base::in | std::ios_base::out | std::ios_base::binary:
178 : return "r+b";
179 : case std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
180 : return "w+b";
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:
183 : return "a+b";
184 : default:
185 : return std::string();
186 : }
187 : }
188 :
189 : void ifstream::open(const fs::path& p, std::ios_base::openmode mode)
190 : {
191 : close();
192 : m_file = fsbridge::fopen(p, openmodeToStr(mode).c_str());
193 : if (m_file == nullptr) {
194 : return;
195 : }
196 : m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
197 : rdbuf(&m_filebuf);
198 : if (mode & std::ios_base::ate) {
199 : seekg(0, std::ios_base::end);
200 : }
201 : }
202 :
203 : void ifstream::close()
204 : {
205 : if (m_file != nullptr) {
206 : m_filebuf.close();
207 : fclose(m_file);
208 : }
209 : m_file = nullptr;
210 : }
211 :
212 : void ofstream::open(const fs::path& p, std::ios_base::openmode mode)
213 : {
214 : close();
215 : m_file = fsbridge::fopen(p, openmodeToStr(mode).c_str());
216 : if (m_file == nullptr) {
217 : return;
218 : }
219 : m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
220 : rdbuf(&m_filebuf);
221 : if (mode & std::ios_base::ate) {
222 : seekp(0, std::ios_base::end);
223 : }
224 : }
225 :
226 : void ofstream::close()
227 : {
228 : if (m_file != nullptr) {
229 : m_filebuf.close();
230 : fclose(m_file);
231 : }
232 : m_file = nullptr;
233 : }
234 : #else // __GLIBCXX__
235 :
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).");
244 :
245 : #endif // __GLIBCXX__
246 : #endif // WIN32
247 :
248 : } // fsbridge
|