mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
5669afb80e
fs: drop old WSL1 hack. (sinetek) Pull request description: Following discussion, the WSL1 patch will be removed, as WSL1 is no longer being developed by Microsoft. Instead, please upgrade to a mainstream WSL2 version. More information can be found on [the official website](https://docs.microsoft.com/en-us/windows/wsl/). ACKs for top commit: 1440000bytes: ACK5669afb80e
fanquake: ACK5669afb80e
- seems ok as-is. Tree-SHA512: 256c13985f6dd3453caf39c7ef1c951dbdfa8457a18cd05e4624db36d8ed8a4f809bb78a7b3c82c72997e9ed3823d5566a5c2d0812d2501aba2e54bc5e6eec79
138 lines
3.3 KiB
C++
138 lines
3.3 KiB
C++
// Copyright (c) 2017-2022 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <fs.h>
|
|
#include <util/syserror.h>
|
|
|
|
#ifndef WIN32
|
|
#include <cstring>
|
|
#include <fcntl.h>
|
|
#include <sys/file.h>
|
|
#include <sys/utsname.h>
|
|
#include <unistd.h>
|
|
#else
|
|
#include <codecvt>
|
|
#include <limits>
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
#include <cassert>
|
|
#include <string>
|
|
|
|
namespace fsbridge {
|
|
|
|
FILE *fopen(const fs::path& p, const char *mode)
|
|
{
|
|
#ifndef WIN32
|
|
return ::fopen(p.c_str(), mode);
|
|
#else
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t> utf8_cvt;
|
|
return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
|
|
#endif
|
|
}
|
|
|
|
fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
|
|
{
|
|
assert(base.is_absolute());
|
|
return path.empty() ? base : fs::path(base / path);
|
|
}
|
|
|
|
#ifndef WIN32
|
|
|
|
static std::string GetErrorReason()
|
|
{
|
|
return SysErrorString(errno);
|
|
}
|
|
|
|
FileLock::FileLock(const fs::path& file)
|
|
{
|
|
fd = open(file.c_str(), O_RDWR);
|
|
if (fd == -1) {
|
|
reason = GetErrorReason();
|
|
}
|
|
}
|
|
|
|
FileLock::~FileLock()
|
|
{
|
|
if (fd != -1) {
|
|
close(fd);
|
|
}
|
|
}
|
|
|
|
bool FileLock::TryLock()
|
|
{
|
|
if (fd == -1) {
|
|
return false;
|
|
}
|
|
|
|
struct flock lock;
|
|
lock.l_type = F_WRLCK;
|
|
lock.l_whence = SEEK_SET;
|
|
lock.l_start = 0;
|
|
lock.l_len = 0;
|
|
if (fcntl(fd, F_SETLK, &lock) == -1) {
|
|
reason = GetErrorReason();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
#else
|
|
|
|
static std::string GetErrorReason() {
|
|
wchar_t* err;
|
|
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<WCHAR*>(&err), 0, nullptr);
|
|
std::wstring err_str(err);
|
|
LocalFree(err);
|
|
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
|
|
}
|
|
|
|
FileLock::FileLock(const fs::path& file)
|
|
{
|
|
hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
if (hFile == INVALID_HANDLE_VALUE) {
|
|
reason = GetErrorReason();
|
|
}
|
|
}
|
|
|
|
FileLock::~FileLock()
|
|
{
|
|
if (hFile != INVALID_HANDLE_VALUE) {
|
|
CloseHandle(hFile);
|
|
}
|
|
}
|
|
|
|
bool FileLock::TryLock()
|
|
{
|
|
if (hFile == INVALID_HANDLE_VALUE) {
|
|
return false;
|
|
}
|
|
_OVERLAPPED overlapped = {};
|
|
if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
|
|
reason = GetErrorReason();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
std::string get_filesystem_error_message(const fs::filesystem_error& e)
|
|
{
|
|
#ifndef WIN32
|
|
return e.what();
|
|
#else
|
|
// Convert from Multi Byte to utf-16
|
|
std::string mb_string(e.what());
|
|
int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), nullptr, 0);
|
|
|
|
std::wstring utf16_string(size, L'\0');
|
|
MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), &*utf16_string.begin(), size);
|
|
// Convert from utf-16 to utf-8
|
|
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
|
|
#endif
|
|
}
|
|
|
|
} // fsbridge
|