Skip to content

Commit

Permalink
Merge bitcoin#13862: drop boost::interprocess::file_lock
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed May 20, 2021
1 parent 4476a7e commit fc96dd7
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 16 deletions.
85 changes: 85 additions & 0 deletions src/fs.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include <fs.h>

#ifndef WIN32
#include <fcntl.h>
#else
#include <codecvt>
#include <windows.h>
#endif

namespace fsbridge {

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

#ifndef WIN32

static std::string GetErrorReason() {
return std::strerror(errno);
}

FileLock::FileLock(const fs::path& file)
{
fd = open(file.string().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 = {0};
if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, 0, 0, &overlapped)) {
reason = GetErrorReason();
return false;
}
return true;
}
#endif

} // fsbridge
20 changes: 20 additions & 0 deletions src/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ namespace fs = boost::filesystem;
namespace fsbridge {
FILE *fopen(const fs::path& p, const char *mode);
FILE *freopen(const fs::path& p, const char *mode, FILE *stream);

class FileLock
{
public:
FileLock() = delete;
FileLock(const FileLock&) = delete;
FileLock(FileLock&&) = delete;
explicit FileLock(const fs::path& file);
~FileLock();
bool TryLock();
std::string GetReason() { return reason; }

private:
std::string reason;
#ifndef WIN32
int fd = -1;
#else
void* hFile = (void*)-1; // INVALID_HANDLE_VALUE
#endif
};
};

#endif // BITCOIN_FS_H
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@

#ifndef WIN32
#include <signal.h>
#include <sys/stat.h>
#endif

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/bind.hpp>
#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/thread.hpp>
#include <openssl/crypto.h>

Expand Down
22 changes: 8 additions & 14 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
#include <malloc.h>
#endif

#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/program_options/detail/config_file.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/thread.hpp>
Expand Down Expand Up @@ -159,7 +158,7 @@ instance_of_cinit;
* cleans them up and thus automatically unlocks them, or ReleaseDirectoryLocks
* is called.
*/
static std::map<std::string, std::unique_ptr<boost::interprocess::file_lock>> dir_locks;
static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks;
/** Mutex to protect dir_locks. */
static std::mutex cs_dir_locks;

Expand All @@ -176,18 +175,13 @@ bool LockDirectory(const fs::path& directory, const std::string lockfile_name, b
// Create empty lock file if it doesn't exist.
FILE* file = fsbridge::fopen(pathLockFile, "a");
if (file) fclose(file);

try {
auto lock = MakeUnique<boost::interprocess::file_lock>(pathLockFile.string().c_str());
if (!lock->try_lock()) {
return false;
}
if (!probe_only) {
// Lock successful and we're not just probing, put it into the map
dir_locks.emplace(pathLockFile.string(), std::move(lock));
}
} catch (const boost::interprocess::interprocess_exception& e) {
return error("Error while attempting to lock directory %s: %s", directory.string(), e.what());
auto lock = MakeUnique<fsbridge::FileLock>(pathLockFile);
if (!lock->TryLock()) {
return error("Error while attempting to lock directory %s: %s", directory.string(), lock->GetReason());
}
if (!probe_only) {
// Lock successful and we're not just probing, put it into the map
dir_locks.emplace(pathLockFile.string(), std::move(lock));
}
return true;
}
Expand Down
1 change: 0 additions & 1 deletion test/lint/lint-includes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ EXPECTED_BOOST_INCLUDES=(
boost/filesystem/detail/utf8_codecvt_facet.hpp
boost/filesystem/fstream.hpp
boost/function.hpp
boost/interprocess/sync/file_lock.hpp
boost/lexical_cast.hpp
boost/lockfree/queue.hpp
boost/multi_index/hashed_index.hpp
Expand Down

0 comments on commit fc96dd7

Please sign in to comment.