Bitcoin ABC  0.29.9
P2P Digital Currency
fs.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 The Bitcoin Core developers
2 // Copyright (c) 2019 The Bitcoin developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <util/fs.h>
7 
8 #include <util/syserror.h>
9 
10 #ifndef WIN32
11 #include <cstring>
12 #include <fcntl.h>
13 #include <sys/file.h>
14 #include <sys/utsname.h>
15 #include <unistd.h>
16 #else
17 #ifndef NOMINMAX
18 #define NOMINMAX
19 #endif
20 #include <codecvt>
21 #include <windows.h>
22 #endif
23 
24 #include <cassert>
25 #include <limits>
26 #include <string>
27 
28 namespace fsbridge {
29 
30 FILE *fopen(const fs::path &p, const char *mode) {
31 #ifndef WIN32
32  return ::fopen(p.c_str(), mode);
33 #else
34  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf8_cvt;
35  return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
36 #endif
37 }
38 
39 fs::path AbsPathJoin(const fs::path &base, const fs::path &path) {
40  assert(base.is_absolute());
41  return path.empty() ? base : fs::path(base / path);
42 }
43 
44 #ifndef WIN32
45 
46 static std::string GetErrorReason() {
47  return SysErrorString(errno);
48 }
49 
51  fd = open(file.c_str(), O_RDWR);
52  if (fd == -1) {
54  }
55 }
56 
58  if (fd != -1) {
59  close(fd);
60  }
61 }
62 
63 static bool IsWSL() {
64  struct utsname uname_data;
65  return uname(&uname_data) == 0 &&
66  std::string(uname_data.version).find("Microsoft") !=
67  std::string::npos;
68 }
69 
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 
99 static std::string GetErrorReason() {
100  wchar_t *err;
101  FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
102  FORMAT_MESSAGE_IGNORE_INSERTS,
103  nullptr, GetLastError(),
104  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
105  reinterpret_cast<WCHAR *>(&err), 0, nullptr);
106  std::wstring err_str(err);
107  LocalFree(err);
108  return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(
109  err_str);
110 }
111 
112 FileLock::FileLock(const fs::path &file) {
113  hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE,
114  FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
115  nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
116  if (hFile == INVALID_HANDLE_VALUE) {
118  }
119 }
120 
122  if (hFile != INVALID_HANDLE_VALUE) {
123  CloseHandle(hFile);
124  }
125 }
126 
127 bool FileLock::TryLock() {
128  if (hFile == INVALID_HANDLE_VALUE) {
129  return false;
130  }
131  _OVERLAPPED overlapped = {0};
132  if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
133  0, std::numeric_limits<DWORD>::max(),
134  std::numeric_limits<DWORD>::max(), &overlapped)) {
136  return false;
137  }
138  return true;
139 }
140 #endif
141 
142 std::string get_filesystem_error_message(const fs::filesystem_error &e) {
143 #ifndef WIN32
144  return e.what();
145 #else
146  // Convert from Multi Byte to utf-16
147  std::string mb_string(e.what());
148  int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(),
149  mb_string.size(), nullptr, 0);
150 
151  std::wstring utf16_string(size, L'\0');
152  MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(),
153  &*utf16_string.begin(), size);
154  // Convert from utf-16 to utf-8
155  return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>()
156  .to_bytes(utf16_string);
157 #endif
158 }
159 
161 #ifndef WIN32
162  return fs::temp_directory_path();
163 #else
164  wchar_t dirPathWchars[MAX_PATH];
165  GetTempPathW(MAX_PATH, dirPathWchars);
166  std::wstring dirPathWstring(dirPathWchars);
167  return fs::PathFromString(
168  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(
169  dirPathWstring));
170 #endif
171 }
172 
173 } // namespace fsbridge
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
std::string reason
Definition: fs.h:225
bool TryLock()
Definition: fs.cpp:70
#define MAX_PATH
Definition: compat.h:70
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:165
Bridge operations to C stdio.
Definition: fs.cpp:28
fs::path GetTempDirectoryPath()
Definition: fs.cpp:160
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:142
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:39
static std::string GetErrorReason()
Definition: fs.cpp:46
static bool IsWSL()
Definition: fs.cpp:63
std::string SysErrorString(int err)
Return system error string from errno value.
Definition: syserror.cpp:14
assert(!tx.IsCoinBase())