BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
fs.cpp
Go to the documentation of this file.
1 #include <fs.h>
2 
3 #ifndef WIN32
4 #include <fcntl.h>
5 #else
6 #define NOMINMAX
7 #include <codecvt>
8 #include <windows.h>
9 #endif
10 
11 namespace fsbridge {
12 
13 FILE *fopen(const fs::path& p, const char *mode)
14 {
15 #ifndef WIN32
16  return ::fopen(p.string().c_str(), mode);
17 #else
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());
20 #endif
21 }
22 
23 #ifndef WIN32
24 
25 static std::string GetErrorReason() {
26  return std::strerror(errno);
27 }
28 
29 FileLock::FileLock(const fs::path& file)
30 {
31  fd = open(file.string().c_str(), O_RDWR);
32  if (fd == -1) {
33  reason = GetErrorReason();
34  }
35 }
36 
38 {
39  if (fd != -1) {
40  close(fd);
41  }
42 }
43 
45 {
46  if (fd == -1) {
47  return false;
48  }
49  struct flock lock;
50  lock.l_type = F_WRLCK;
51  lock.l_whence = SEEK_SET;
52  lock.l_start = 0;
53  lock.l_len = 0;
54  if (fcntl(fd, F_SETLK, &lock) == -1) {
55  reason = GetErrorReason();
56  return false;
57  }
58  return true;
59 }
60 #else
61 
62 static std::string GetErrorReason() {
63  wchar_t* err;
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);
67  LocalFree(err);
68  return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
69 }
70 
71 FileLock::FileLock(const fs::path& file)
72 {
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) {
76  reason = GetErrorReason();
77  }
78 }
79 
81 {
82  if (hFile != INVALID_HANDLE_VALUE) {
83  CloseHandle(hFile);
84  }
85 }
86 
87 bool FileLock::TryLock()
88 {
89  if (hFile == INVALID_HANDLE_VALUE) {
90  return false;
91  }
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)) {
94  reason = GetErrorReason();
95  return false;
96  }
97  return true;
98 }
99 #endif
100 
101 std::string get_filesystem_error_message(const fs::filesystem_error& e)
102 {
103 #ifndef WIN32
104  return e.what();
105 #else
106  // Convert from Multi Byte to utf-16
107  std::string mb_string(e.what());
108  int size = MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), nullptr, 0);
109 
110  std::wstring utf16_string(size, L'\0');
111  MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), &*utf16_string.begin(), size);
112  // Convert from utf-16 to utf-8
113  return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
114 #endif
115 }
116 
117 #ifdef WIN32
118 #ifdef __GLIBCXX__
119 
120 // reference: https://github.com/gcc-mirror/gcc/blob/gcc-7_3_0-release/libstdc%2B%2B-v3/include/std/fstream#L270
121 
122 static std::string openmodeToStr(std::ios_base::openmode mode)
123 {
124  switch (mode & ~std::ios_base::ate) {
125  case std::ios_base::out:
126  case std::ios_base::out | std::ios_base::trunc:
127  return "w";
128  case std::ios_base::out | std::ios_base::app:
129  case std::ios_base::app:
130  return "a";
131  case std::ios_base::in:
132  return "r";
133  case std::ios_base::in | std::ios_base::out:
134  return "r+";
135  case std::ios_base::in | std::ios_base::out | std::ios_base::trunc:
136  return "w+";
137  case std::ios_base::in | std::ios_base::out | std::ios_base::app:
138  case std::ios_base::in | std::ios_base::app:
139  return "a+";
140  case std::ios_base::out | std::ios_base::binary:
141  case std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
142  return "wb";
143  case std::ios_base::out | std::ios_base::app | std::ios_base::binary:
144  case std::ios_base::app | std::ios_base::binary:
145  return "ab";
146  case std::ios_base::in | std::ios_base::binary:
147  return "rb";
148  case std::ios_base::in | std::ios_base::out | std::ios_base::binary:
149  return "r+b";
150  case std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
151  return "w+b";
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:
154  return "a+b";
155  default:
156  return std::string();
157  }
158 }
159 
160 void ifstream::open(const fs::path& p, std::ios_base::openmode mode)
161 {
162  close();
163  m_file = fsbridge::fopen(p, openmodeToStr(mode).c_str());
164  if (m_file == nullptr) {
165  return;
166  }
167  m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
168  rdbuf(&m_filebuf);
169  if (mode & std::ios_base::ate) {
170  seekg(0, std::ios_base::end);
171  }
172 }
173 
174 void ifstream::close()
175 {
176  if (m_file != nullptr) {
177  m_filebuf.close();
178  fclose(m_file);
179  }
180  m_file = nullptr;
181 }
182 
183 void ofstream::open(const fs::path& p, std::ios_base::openmode mode)
184 {
185  close();
186  m_file = fsbridge::fopen(p, openmodeToStr(mode).c_str());
187  if (m_file == nullptr) {
188  return;
189  }
190  m_filebuf = __gnu_cxx::stdio_filebuf<char>(m_file, mode);
191  rdbuf(&m_filebuf);
192  if (mode & std::ios_base::ate) {
193  seekp(0, std::ios_base::end);
194  }
195 }
196 
197 void ofstream::close()
198 {
199  if (m_file != nullptr) {
200  m_filebuf.close();
201  fclose(m_file);
202  }
203  m_file = nullptr;
204 }
205 #else // __GLIBCXX__
206 
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).");
215 
216 #endif // __GLIBCXX__
217 #endif // WIN32
218 
219 } // fsbridge
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:13
std::string reason
Definition: fs.h:36
Filesystem operations and types.
Definition: fs.cpp:11
auto end
Definition: rpcwallet.cpp:1068
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:101
bool TryLock()
Definition: fs.cpp:44