Bitcoin Core  22.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 
7 #ifndef WIN32
8 #include <cstring>
9 #include <fcntl.h>
10 #include <sys/file.h>
11 #include <sys/utsname.h>
12 #include <unistd.h>
13 #else
14 #ifndef NOMINMAX
15 #define NOMINMAX
16 #endif
17 #include <codecvt>
18 #include <limits>
19 #include <windows.h>
20 #endif
21 
22 #include <cassert>
23 #include <string>
24 
25 namespace fsbridge {
26 
27 FILE *fopen(const fs::path& p, const char *mode)
28 {
29 #ifndef WIN32
30  return ::fopen(p.c_str(), mode);
31 #else
32  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t> utf8_cvt;
33  return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
34 #endif
35 }
36 
37 fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
38 {
39  assert(base.is_absolute());
40  return path.empty() ? base : fs::path(base / path);
41 }
42 
43 #ifndef WIN32
44 
45 static std::string GetErrorReason()
46 {
47  return std::strerror(errno);
48 }
49 
51 {
52  fd = open(file.c_str(), O_RDWR);
53  if (fd == -1) {
55  }
56 }
57 
59 {
60  if (fd != -1) {
61  close(fd);
62  }
63 }
64 
65 static bool IsWSL()
66 {
67  struct utsname uname_data;
68  return uname(&uname_data) == 0 && std::string(uname_data.version).find("Microsoft") != std::string::npos;
69 }
70 
72 {
73  if (fd == -1) {
74  return false;
75  }
76 
77  // Exclusive file locking is broken on WSL using fcntl (issue #18622)
78  // This workaround can be removed once the bug on WSL is fixed
79  static const bool is_wsl = IsWSL();
80  if (is_wsl) {
81  if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
83  return false;
84  }
85  } else {
86  struct flock lock;
87  lock.l_type = F_WRLCK;
88  lock.l_whence = SEEK_SET;
89  lock.l_start = 0;
90  lock.l_len = 0;
91  if (fcntl(fd, F_SETLK, &lock) == -1) {
93  return false;
94  }
95  }
96 
97  return true;
98 }
99 #else
100 
101 static std::string GetErrorReason() {
102  wchar_t* err;
103  FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
104  nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<WCHAR*>(&err), 0, nullptr);
105  std::wstring err_str(err);
106  LocalFree(err);
107  return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
108 }
109 
110 FileLock::FileLock(const fs::path& file)
111 {
112  hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
113  nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
114  if (hFile == INVALID_HANDLE_VALUE) {
116  }
117 }
118 
120 {
121  if (hFile != INVALID_HANDLE_VALUE) {
122  CloseHandle(hFile);
123  }
124 }
125 
126 bool FileLock::TryLock()
127 {
128  if (hFile == INVALID_HANDLE_VALUE) {
129  return false;
130  }
131  _OVERLAPPED overlapped = {0};
132  if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
134  return false;
135  }
136  return true;
137 }
138 #endif
139 
140 std::string get_filesystem_error_message(const fs::filesystem_error& e)
141 {
142 #ifndef WIN32
143  return e.what();
144 #else
145  // Convert from Multi Byte to utf-16
146  std::string mb_string(e.what());
147  int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), nullptr, 0);
148 
149  std::wstring utf16_string(size, L'\0');
150  MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), &*utf16_string.begin(), size);
151  // Convert from utf-16 to utf-8
152  return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
153 #endif
154 }
155 
156 } // fsbridge
assert
assert(!tx.IsCoinBase())
fsbridge::FileLock::fd
int fd
Definition: fs.h:196
fs.h
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:27
fsbridge::get_filesystem_error_message
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:140
fsbridge::GetErrorReason
static std::string GetErrorReason()
Definition: fs.cpp:45
fsbridge::AbsPathJoin
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:37
fsbridge::FileLock::TryLock
bool TryLock()
Definition: fs.cpp:71
fsbridge::IsWSL
static bool IsWSL()
Definition: fs.cpp:65
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
fsbridge::FileLock::~FileLock
~FileLock()
Definition: fs.cpp:58
fsbridge::FileLock::FileLock
FileLock()=delete
fsbridge::FileLock::reason
std::string reason
Definition: fs.h:194
fsbridge
Bridge operations to C stdio.
Definition: fs.cpp:25