Bitcoin Core  24.99.0
P2P Digital Currency
fs.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2021 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 #include <util/syserror.h>
7 
8 #ifndef WIN32
9 #include <cstring>
10 #include <fcntl.h>
11 #include <sys/file.h>
12 #include <sys/utsname.h>
13 #include <unistd.h>
14 #else
15 #include <codecvt>
16 #include <limits>
17 #include <windows.h>
18 #endif
19 
20 #include <cassert>
21 #include <string>
22 
23 namespace fsbridge {
24 
25 FILE *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 
35 fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
36 {
37  assert(base.is_absolute());
38  return path.empty() ? base : fs::path(base / path);
39 }
40 
41 #ifndef WIN32
42 
43 static std::string GetErrorReason()
44 {
45  return SysErrorString(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 
63 static 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 
99 static 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 
108 FileLock::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 
124 bool FileLock::TryLock()
125 {
126  if (hFile == INVALID_HANDLE_VALUE) {
127  return false;
128  }
129  _OVERLAPPED overlapped = {};
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 
138 std::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 } // fsbridge
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
std::string reason
Definition: fs.h:233
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
std::string SysErrorString(int err)
Return system error string from errno value.
Definition: syserror.cpp:15
assert(!tx.IsCoinBase())