Skip to content

Commit 2ddce35

Browse files
committed
Merge #13862: utils: drop boost::interprocess::file_lock
1661a47 add unicode compatible file_lock for Windows (Chun Kuan Lee) Pull request description: boost::interprocess::file_lock cannot open the files that contain characters which cannot be parsed by the user's code page on Windows. This PR is seperated from #13426 for easier review. Tree-SHA512: e240479cda65958bf6e1319840b83928b2b50da81d99f4f002fb3b62621370bcd4bcfacd2b8c0678c443a650d6ba53d9d12618b591e5bfd67ac14388a18fd822
2 parents 13887f4 + 1661a47 commit 2ddce35

File tree

5 files changed

+114
-16
lines changed

5 files changed

+114
-16
lines changed

src/fs.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#include <fs.h>
22

3+
#ifndef WIN32
4+
#include <fcntl.h>
5+
#else
6+
#include <codecvt>
7+
#include <windows.h>
8+
#endif
9+
310
namespace fsbridge {
411

512
FILE *fopen(const fs::path& p, const char *mode)
@@ -12,4 +19,82 @@ FILE *freopen(const fs::path& p, const char *mode, FILE *stream)
1219
return ::freopen(p.string().c_str(), mode, stream);
1320
}
1421

22+
#ifndef WIN32
23+
24+
static std::string GetErrorReason() {
25+
return std::strerror(errno);
26+
}
27+
28+
FileLock::FileLock(const fs::path& file)
29+
{
30+
fd = open(file.string().c_str(), O_RDWR);
31+
if (fd == -1) {
32+
reason = GetErrorReason();
33+
}
34+
}
35+
36+
FileLock::~FileLock()
37+
{
38+
if (fd != -1) {
39+
close(fd);
40+
}
41+
}
42+
43+
bool FileLock::TryLock()
44+
{
45+
if (fd == -1) {
46+
return false;
47+
}
48+
struct flock lock;
49+
lock.l_type = F_WRLCK;
50+
lock.l_whence = SEEK_SET;
51+
lock.l_start = 0;
52+
lock.l_len = 0;
53+
if (fcntl(fd, F_SETLK, &lock) == -1) {
54+
reason = GetErrorReason();
55+
return false;
56+
}
57+
return true;
58+
}
59+
#else
60+
61+
static std::string GetErrorReason() {
62+
wchar_t* err;
63+
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
64+
nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<WCHAR*>(&err), 0, nullptr);
65+
std::wstring err_str(err);
66+
LocalFree(err);
67+
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
68+
}
69+
70+
FileLock::FileLock(const fs::path& file)
71+
{
72+
hFile = CreateFileW(file.wstring().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
73+
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
74+
if (hFile == INVALID_HANDLE_VALUE) {
75+
reason = GetErrorReason();
76+
}
77+
}
78+
79+
FileLock::~FileLock()
80+
{
81+
if (hFile != INVALID_HANDLE_VALUE) {
82+
CloseHandle(hFile);
83+
}
84+
}
85+
86+
bool FileLock::TryLock()
87+
{
88+
if (hFile == INVALID_HANDLE_VALUE) {
89+
return false;
90+
}
91+
_OVERLAPPED overlapped = {0};
92+
if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, 0, 0, &overlapped)) {
93+
reason = GetErrorReason();
94+
return false;
95+
}
96+
return true;
97+
}
98+
#endif
99+
15100
} // fsbridge

src/fs.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ namespace fs = boost::filesystem;
1919
namespace fsbridge {
2020
FILE *fopen(const fs::path& p, const char *mode);
2121
FILE *freopen(const fs::path& p, const char *mode, FILE *stream);
22+
23+
class FileLock
24+
{
25+
public:
26+
FileLock() = delete;
27+
FileLock(const FileLock&) = delete;
28+
FileLock(FileLock&&) = delete;
29+
explicit FileLock(const fs::path& file);
30+
~FileLock();
31+
bool TryLock();
32+
std::string GetReason() { return reason; }
33+
34+
private:
35+
std::string reason;
36+
#ifndef WIN32
37+
int fd = -1;
38+
#else
39+
void* hFile = (void*)-1; // INVALID_HANDLE_VALUE
40+
#endif
41+
};
2242
};
2343

2444
#endif // BITCOIN_FS_H

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@
5151

5252
#ifndef WIN32
5353
#include <signal.h>
54+
#include <sys/stat.h>
5455
#endif
5556

5657
#include <boost/algorithm/string/classification.hpp>
5758
#include <boost/algorithm/string/replace.hpp>
5859
#include <boost/algorithm/string/split.hpp>
5960
#include <boost/bind.hpp>
60-
#include <boost/interprocess/sync/file_lock.hpp>
6161
#include <boost/thread.hpp>
6262
#include <openssl/crypto.h>
6363

src/util.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
#include <malloc.h>
7272
#endif
7373

74-
#include <boost/interprocess/sync/file_lock.hpp>
7574
#include <boost/thread.hpp>
7675
#include <openssl/crypto.h>
7776
#include <openssl/rand.h>
@@ -139,7 +138,7 @@ instance_of_cinit;
139138
* cleans them up and thus automatically unlocks them, or ReleaseDirectoryLocks
140139
* is called.
141140
*/
142-
static std::map<std::string, std::unique_ptr<boost::interprocess::file_lock>> dir_locks;
141+
static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks;
143142
/** Mutex to protect dir_locks. */
144143
static std::mutex cs_dir_locks;
145144

@@ -156,18 +155,13 @@ bool LockDirectory(const fs::path& directory, const std::string lockfile_name, b
156155
// Create empty lock file if it doesn't exist.
157156
FILE* file = fsbridge::fopen(pathLockFile, "a");
158157
if (file) fclose(file);
159-
160-
try {
161-
auto lock = MakeUnique<boost::interprocess::file_lock>(pathLockFile.string().c_str());
162-
if (!lock->try_lock()) {
163-
return false;
164-
}
165-
if (!probe_only) {
166-
// Lock successful and we're not just probing, put it into the map
167-
dir_locks.emplace(pathLockFile.string(), std::move(lock));
168-
}
169-
} catch (const boost::interprocess::interprocess_exception& e) {
170-
return error("Error while attempting to lock directory %s: %s", directory.string(), e.what());
158+
auto lock = MakeUnique<fsbridge::FileLock>(pathLockFile);
159+
if (!lock->TryLock()) {
160+
return error("Error while attempting to lock directory %s: %s", directory.string(), lock->GetReason());
161+
}
162+
if (!probe_only) {
163+
// Lock successful and we're not just probing, put it into the map
164+
dir_locks.emplace(pathLockFile.string(), std::move(lock));
171165
}
172166
return true;
173167
}

test/lint/lint-includes.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ EXPECTED_BOOST_INCLUDES=(
5757
boost/filesystem.hpp
5858
boost/filesystem/detail/utf8_codecvt_facet.hpp
5959
boost/filesystem/fstream.hpp
60-
boost/interprocess/sync/file_lock.hpp
6160
boost/multi_index/hashed_index.hpp
6261
boost/multi_index/ordered_index.hpp
6362
boost/multi_index/sequenced_index.hpp

0 commit comments

Comments
 (0)