Bitcoin ABC 0.32.11
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
28namespace fsbridge {
29
30FILE *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
39fs::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
46static 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
63static 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
99static std::string GetErrorReason() {
100 return Win32ErrorString(GetLastError());
101}
102
103FileLock::FileLock(const fs::path &file) {
104 hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE,
105 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
106 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
107 if (hFile == INVALID_HANDLE_VALUE) {
109 }
110}
111
113 if (hFile != INVALID_HANDLE_VALUE) {
114 CloseHandle(hFile);
115 }
116}
117
118bool FileLock::TryLock() {
119 if (hFile == INVALID_HANDLE_VALUE) {
120 return false;
121 }
122 _OVERLAPPED overlapped = {0};
123 if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
124 0, std::numeric_limits<DWORD>::max(),
125 std::numeric_limits<DWORD>::max(), &overlapped)) {
127 return false;
128 }
129 return true;
130}
131#endif
132
133std::string get_filesystem_error_message(const fs::filesystem_error &e) {
134#ifndef WIN32
135 return e.what();
136#else
137 // Convert from Multi Byte to utf-16
138 std::string mb_string(e.what());
139 int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(),
140 mb_string.size(), nullptr, 0);
141
142 std::wstring utf16_string(size, L'\0');
143 MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(),
144 &*utf16_string.begin(), size);
145 // Convert from utf-16 to utf-8
146 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>()
147 .to_bytes(utf16_string);
148#endif
149}
150
152#ifndef WIN32
153 return fs::temp_directory_path();
154#else
155 wchar_t dirPathWchars[MAX_PATH];
156 GetTempPathW(MAX_PATH, dirPathWchars);
157 std::wstring dirPathWstring(dirPathWchars);
158 return fs::PathFromString(
159 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(
160 dirPathWstring));
161#endif
162}
163
164} // 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:231
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:170
Bridge operations to C stdio.
Definition: fs.cpp:28
fs::path GetTempDirectoryPath()
Definition: fs.cpp:151
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:133
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:20
assert(!tx.IsCoinBase())