Bitcoin Core  26.1.0
P2P Digital Currency
fs.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2022 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 <util/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 
64 {
65  if (fd == -1) {
66  return false;
67  }
68 
69  struct flock lock;
70  lock.l_type = F_WRLCK;
71  lock.l_whence = SEEK_SET;
72  lock.l_start = 0;
73  lock.l_len = 0;
74  if (fcntl(fd, F_SETLK, &lock) == -1) {
76  return false;
77  }
78 
79  return true;
80 }
81 #else
82 
83 static std::string GetErrorReason() {
84  return Win32ErrorString(GetLastError());
85 }
86 
87 FileLock::FileLock(const fs::path& file)
88 {
89  hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
90  nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
91  if (hFile == INVALID_HANDLE_VALUE) {
93  }
94 }
95 
97 {
98  if (hFile != INVALID_HANDLE_VALUE) {
99  CloseHandle(hFile);
100  }
101 }
102 
103 bool FileLock::TryLock()
104 {
105  if (hFile == INVALID_HANDLE_VALUE) {
106  return false;
107  }
108  _OVERLAPPED overlapped = {};
109  if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
111  return false;
112  }
113  return true;
114 }
115 #endif
116 
117 std::string get_filesystem_error_message(const fs::filesystem_error& e)
118 {
119 #ifndef WIN32
120  return e.what();
121 #else
122  // Convert from Multi Byte to utf-16
123  std::string mb_string(e.what());
124  int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), nullptr, 0);
125 
126  std::wstring utf16_string(size, L'\0');
127  MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), &*utf16_string.begin(), size);
128  // Convert from utf-16 to utf-8
129  return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
130 #endif
131 }
132 
133 } // fsbridge
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:35
assert(!tx.IsCoinBase())
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
std::string reason
Definition: fs.h:233
Bridge operations to C stdio.
Definition: fs.cpp:23
std::string SysErrorString(int err)
Return system error string from errno value.
Definition: syserror.cpp:21
static std::string GetErrorReason()
Definition: fs.cpp:43
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:117
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
bool TryLock()
Definition: fs.cpp:63