Bitcoin Core 22.99.0
P2P Digital Currency
fs.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-2020 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <fs.h>
6
7#ifndef WIN32
8#include <cstring>
9#include <fcntl.h>
10#include <string>
11#include <sys/file.h>
12#include <sys/utsname.h>
13#include <unistd.h>
14#else
15#ifndef NOMINMAX
16#define NOMINMAX
17#endif
18#include <codecvt>
19#include <limits>
20#include <windows.h>
21#endif
22
23namespace fsbridge {
24
25FILE *fopen(const fs::path& p, const char *mode)
26{
27#ifndef WIN32
28 return ::fopen(p.c_str(), mode);
29#else
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());
32#endif
33}
34
35fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
36{
37 assert(base.is_absolute());
38 return fs::absolute(path, base);
39}
40
41#ifndef WIN32
42
43static std::string GetErrorReason()
44{
45 return std::strerror(errno);
46}
47
49{
50 fd = open(file.c_str(), O_RDWR);
51 if (fd == -1) {
53 }
54}
55
57{
58 if (fd != -1) {
59 close(fd);
60 }
61}
62
63static bool IsWSL()
64{
65 struct utsname uname_data;
66 return uname(&uname_data) == 0 && std::string(uname_data.version).find("Microsoft") != std::string::npos;
67}
68
70{
71 if (fd == -1) {
72 return false;
73 }
74
75 // Exclusive file locking is broken on WSL using fcntl (issue #18622)
76 // This workaround can be removed once the bug on WSL is fixed
77 static const bool is_wsl = IsWSL();
78 if (is_wsl) {
79 if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
81 return false;
82 }
83 } else {
84 struct flock lock;
85 lock.l_type = F_WRLCK;
86 lock.l_whence = SEEK_SET;
87 lock.l_start = 0;
88 lock.l_len = 0;
89 if (fcntl(fd, F_SETLK, &lock) == -1) {
91 return false;
92 }
93 }
94
95 return true;
96}
97#else
98
99static std::string GetErrorReason() {
100 wchar_t* err;
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);
104 LocalFree(err);
105 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
106}
107
108FileLock::FileLock(const fs::path& file)
109{
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) {
114 }
115}
116
118{
119 if (hFile != INVALID_HANDLE_VALUE) {
120 CloseHandle(hFile);
121 }
122}
123
125{
126 if (hFile == INVALID_HANDLE_VALUE) {
127 return false;
128 }
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)) {
132 return false;
133 }
134 return true;
135}
136#endif
137
138std::string get_filesystem_error_message(const fs::filesystem_error& e)
139{
140#ifndef WIN32
141 return e.what();
142#else
143 // Convert from Multi Byte to utf-16
144 std::string mb_string(e.what());
145 int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), nullptr, 0);
146
147 std::wstring utf16_string(size, L'\0');
148 MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), &*utf16_string.begin(), size);
149 // Convert from utf-16 to utf-8
150 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
151#endif
152}
153
154#ifdef WIN32
155#ifdef __GLIBCXX__
156
157// reference: https://github.com/gcc-mirror/gcc/blob/gcc-7_3_0-release/libstdc%2B%2B-v3/include/std/fstream#L270
158#if defined(__GNUC__) && !defined(__clang__)
159#pragma GCC diagnostic push
160#pragma GCC diagnostic ignored "-Wswitch"
161#endif
162static std::string openmodeToStr(std::ios_base::openmode mode)
163{
164 switch (mode & ~std::ios_base::ate) {
165 case std::ios_base::out:
166 case std::ios_base::out | std::ios_base::trunc:
167 return "w";
168 case std::ios_base::out | std::ios_base::app:
169 case std::ios_base::app:
170 return "a";
171 case std::ios_base::in:
172 return "r";
173 case std::ios_base::in | std::ios_base::out:
174 return "r+";
175 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc:
176 return "w+";
177 case std::ios_base::in | std::ios_base::out | std::ios_base::app:
178 case std::ios_base::in | std::ios_base::app:
179 return "a+";
180 case std::ios_base::out | std::ios_base::binary:
181 case std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
182 return "wb";
183 case std::ios_base::out | std::ios_base::app | std::ios_base::binary:
184 case std::ios_base::app | std::ios_base::binary:
185 return "ab";
186 case std::ios_base::in | std::ios_base::binary:
187 return "rb";
188 case std::ios_base::in | std::ios_base::out | std::ios_base::binary:
189 return "r+b";
190 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
191 return "w+b";
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:
194 return "a+b";
195 default:
196 return std::string();
197 }
198}
199#if defined(__GNUC__) && !defined(__clang__)
200#pragma GCC diagnostic pop
201#endif
202
203void ifstream::open(const fs::path& p, std::ios_base::openmode mode)
204{
205 close();
206 mode |= std::ios_base::in;
207 m_file = fsbridge::fopen(p, openmodeToStr(mode).c_str());
208 if (m_file == nullptr) {
209 return;
210 }
211 m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
212 rdbuf(&m_filebuf);
213 if (mode & std::ios_base::ate) {
214 seekg(0, std::ios_base::end);
215 }
216}
217
218void ifstream::close()
219{
220 if (m_file != nullptr) {
221 m_filebuf.close();
222 fclose(m_file);
223 }
224 m_file = nullptr;
225}
226
227void ofstream::open(const fs::path& p, std::ios_base::openmode mode)
228{
229 close();
230 mode |= std::ios_base::out;
231 m_file = fsbridge::fopen(p, openmodeToStr(mode).c_str());
232 if (m_file == nullptr) {
233 return;
234 }
235 m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
236 rdbuf(&m_filebuf);
237 if (mode & std::ios_base::ate) {
238 seekp(0, std::ios_base::end);
239 }
240}
241
242void ofstream::close()
243{
244 if (m_file != nullptr) {
245 m_filebuf.close();
246 fclose(m_file);
247 }
248 m_file = nullptr;
249}
250#else // __GLIBCXX__
251
252#if BOOST_VERSION >= 107700
253static_assert(sizeof(*BOOST_FILESYSTEM_C_STR(boost::filesystem::path())) == sizeof(wchar_t),
254#else
255static_assert(sizeof(*boost::filesystem::path().BOOST_FILESYSTEM_C_STR) == sizeof(wchar_t),
256#endif // BOOST_VERSION >= 107700
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).");
264
265#endif // __GLIBCXX__
266#endif // WIN32
267
268} // fsbridge
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:34
std::string reason
Definition: fs.h:170
bool TryLock()
Definition: fs.cpp:69
Bridge operations to C stdio.
Definition: fs.cpp:23
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:138
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:35
static std::string GetErrorReason()
Definition: fs.cpp:43
static bool IsWSL()
Definition: fs.cpp:63
assert(!tx.IsCoinBase())