Branch data Line data Source code
# 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 : 305730 : {
# 29 : 305730 : #ifndef WIN32
# 30 : 305730 : 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 : 305730 : }
# 36 : :
# 37 : : fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
# 38 : 14673 : {
# 39 : 14673 : assert(base.is_absolute());
# 40 [ + + ]: 14673 : return path.empty() ? base : fs::path(base / path);
# 41 : 14673 : }
# 42 : :
# 43 : : #ifndef WIN32
# 44 : :
# 45 : : static std::string GetErrorReason()
# 46 : 10 : {
# 47 : 10 : return std::strerror(errno);
# 48 : 10 : }
# 49 : :
# 50 : : FileLock::FileLock(const fs::path& file)
# 51 : 2190 : {
# 52 : 2190 : fd = open(file.c_str(), O_RDWR);
# 53 [ + + ]: 2190 : if (fd == -1) {
# 54 : 1 : reason = GetErrorReason();
# 55 : 1 : }
# 56 : 2190 : }
# 57 : :
# 58 : : FileLock::~FileLock()
# 59 : 1395 : {
# 60 [ + + ]: 1395 : if (fd != -1) {
# 61 : 1394 : close(fd);
# 62 : 1394 : }
# 63 : 1395 : }
# 64 : :
# 65 : : static bool IsWSL()
# 66 : 815 : {
# 67 : 815 : struct utsname uname_data;
# 68 [ + - ][ - + ]: 815 : return uname(&uname_data) == 0 && std::string(uname_data.version).find("Microsoft") != std::string::npos;
# 69 : 815 : }
# 70 : :
# 71 : : bool FileLock::TryLock()
# 72 : 2190 : {
# 73 [ + + ]: 2190 : if (fd == -1) {
# 74 : 1 : return false;
# 75 : 1 : }
# 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 : 2189 : static const bool is_wsl = IsWSL();
# 80 [ - + ]: 2189 : if (is_wsl) {
# 81 [ # # ]: 0 : if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
# 82 : 0 : reason = GetErrorReason();
# 83 : 0 : return false;
# 84 : 0 : }
# 85 : 2189 : } else {
# 86 : 2189 : struct flock lock;
# 87 : 2189 : lock.l_type = F_WRLCK;
# 88 : 2189 : lock.l_whence = SEEK_SET;
# 89 : 2189 : lock.l_start = 0;
# 90 : 2189 : lock.l_len = 0;
# 91 [ + + ]: 2189 : if (fcntl(fd, F_SETLK, &lock) == -1) {
# 92 : 9 : reason = GetErrorReason();
# 93 : 9 : return false;
# 94 : 9 : }
# 95 : 2189 : }
# 96 : :
# 97 : 2180 : return true;
# 98 : 2189 : }
# 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) {
# 115 : : reason = GetErrorReason();
# 116 : : }
# 117 : : }
# 118 : :
# 119 : : FileLock::~FileLock()
# 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)) {
# 133 : : reason = GetErrorReason();
# 134 : : return false;
# 135 : : }
# 136 : : return true;
# 137 : : }
# 138 : : #endif
# 139 : :
# 140 : : std::string get_filesystem_error_message(const fs::filesystem_error& e)
# 141 : 0 : {
# 142 : 0 : #ifndef WIN32
# 143 : 0 : 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 : 0 : }
# 155 : :
# 156 : : } // fsbridge
|