2020-12-31 18:50:11 +01:00
|
|
|
// Copyright (c) 2017-2020 The Bitcoin Core developers
|
2020-01-16 21:57:24 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <fs.h>
|
2017-04-06 20:19:21 +02:00
|
|
|
|
2018-07-25 11:33:22 +02:00
|
|
|
#ifndef WIN32
|
2020-06-17 10:30:19 +02:00
|
|
|
#include <cstring>
|
2018-07-25 11:33:22 +02:00
|
|
|
#include <fcntl.h>
|
2020-05-28 17:25:45 +02:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/utsname.h>
|
2020-06-17 10:30:19 +02:00
|
|
|
#include <unistd.h>
|
2018-07-25 11:33:22 +02:00
|
|
|
#else
|
2019-04-11 15:31:34 +02:00
|
|
|
#ifndef NOMINMAX
|
2018-10-20 03:59:28 +02:00
|
|
|
#define NOMINMAX
|
2019-04-11 15:31:34 +02:00
|
|
|
#endif
|
2018-07-25 11:33:22 +02:00
|
|
|
#include <codecvt>
|
Merge bitcoin/bitcoin#23335: refactor: include a missing <limits> header in fs.cpp
077a875d94b51e3c87381133657be98989c8643e refactor: include a missing <limits> header in fs.cpp (Joan Karadimov)
Pull request description:
I get this compilation error on versions `0.21.1` and `22.0`:
```
fs.cpp: In member function 'bool fsbridge::FileLock::TryLock()':
fs.cpp:123:89: error: 'numeric_limits' is not a member of 'std'
123 | if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
| ^~~~~~~~~~~~~~
fs.cpp:123:109: error: expected primary-expression before '>' token
123 | if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
| ^
fs.cpp:123:112: error: '::max' has not been declared; did you mean 'std::max'?
123 | if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
| ^~~
| std::max
In file included from C:/dev/msys64/mingw64/include/c++/11.2.0/bits/char_traits.h:39,
from C:/dev/msys64/mingw64/include/c++/11.2.0/string:40,
from ./fs.h:9,
from fs.cpp:5:
C:/dev/msys64/mingw64/include/c++/11.2.0/bits/stl_algobase.h:300:5: note: 'std::max' declared here
300 | max(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
fs.cpp:123:124: error: 'numeric_limits' is not a member of 'std'
123 | if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
| ^~~~~~~~~~~~~~
fs.cpp:123:144: error: expected primary-expression before '>' token
123 | if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
| ^
fs.cpp:123:147: error: '::max' has not been declared; did you mean 'std::max'?
123 | if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
| ^~~
| std::max
In file included from C:/dev/msys64/mingw64/include/c++/11.2.0/bits/char_traits.h:39,
from C:/dev/msys64/mingw64/include/c++/11.2.0/string:40,
from ./fs.h:9,
from fs.cpp:5:
C:/dev/msys64/mingw64/include/c++/11.2.0/bits/stl_algobase.h:300:5: note: 'std::max' declared here
300 | max(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
```
It appears that `std::numeric_limits<T>::max` is used without the `limits` header being included. Probably on other STL implementations it's included transitively, but not in the one in MinGW. Including it fixes the compilation problem.
Environment:
OS: Windows 10
Compiler: gcc 11.2.0
Qt: 5.15.2 (included in msys2)
Using the latest mingw w64 shipped with msys2.
ACKs for top commit:
fanquake:
ACK 077a875d94b51e3c87381133657be98989c8643e - Thanks.
hebasto:
ACK 077a875d94b51e3c87381133657be98989c8643e
Tree-SHA512: 2289cb72fa3a28470f4250833be66079482d1392189b1e4679330dad109a8ae67b1d6d51cb635dbc1947c00c6c4cfbc4d7c9ae02269b932cfa1475583adaf73d
2021-10-22 12:28:03 +02:00
|
|
|
#include <limits>
|
2018-07-25 11:33:22 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2021-12-24 09:19:02 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
|
2017-04-06 20:19:21 +02:00
|
|
|
namespace fsbridge {
|
|
|
|
|
|
|
|
FILE *fopen(const fs::path& p, const char *mode)
|
|
|
|
{
|
2018-07-26 15:39:13 +02:00
|
|
|
#ifndef WIN32
|
2024-08-06 19:39:26 +02:00
|
|
|
return ::fopen(p.c_str(), mode);
|
2018-07-26 15:39:13 +02:00
|
|
|
#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
|
2017-04-06 20:19:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 18:47:57 +01:00
|
|
|
fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
|
|
|
|
{
|
|
|
|
assert(base.is_absolute());
|
2024-08-06 19:40:56 +02:00
|
|
|
return path.empty() ? base : fs::path(base / path);
|
2021-01-21 18:47:57 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 11:33:22 +02:00
|
|
|
#ifndef WIN32
|
|
|
|
|
2020-06-17 10:30:19 +02:00
|
|
|
static std::string GetErrorReason()
|
|
|
|
{
|
2018-07-25 11:33:22 +02:00
|
|
|
return std::strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLock::FileLock(const fs::path& file)
|
|
|
|
{
|
2024-08-06 19:39:26 +02:00
|
|
|
fd = open(file.c_str(), O_RDWR);
|
2018-07-25 11:33:22 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
reason = GetErrorReason();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLock::~FileLock()
|
|
|
|
{
|
|
|
|
if (fd != -1) {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileLock::TryLock()
|
|
|
|
{
|
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-28 17:25:45 +02:00
|
|
|
|
2022-08-26 17:45:54 +02:00
|
|
|
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;
|
2018-07-25 11:33:22 +02:00
|
|
|
}
|
2020-05-28 17:25:45 +02:00
|
|
|
|
2018-07-25 11:33:22 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-09-15 16:29:22 +02:00
|
|
|
_OVERLAPPED overlapped = {};
|
2018-10-20 03:59:28 +02:00
|
|
|
if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
|
2018-07-25 11:33:22 +02:00
|
|
|
reason = GetErrorReason();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-09-10 20:08:56 +02:00
|
|
|
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());
|
2019-10-28 13:41:45 +01:00
|
|
|
int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), nullptr, 0);
|
2018-09-10 20:08:56 +02:00
|
|
|
|
|
|
|
std::wstring utf16_string(size, L'\0');
|
2019-10-28 13:41:45 +01:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), &*utf16_string.begin(), size);
|
2018-09-10 20:08:56 +02:00
|
|
|
// Convert from utf-16 to utf-8
|
|
|
|
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-06 20:19:21 +02:00
|
|
|
} // fsbridge
|