Skip to content

Commit fad3a97

Browse files
author
MarcoFalke
committed
Return LockResult::ErrorWrite in LockDirectory
This allows the caller to remove a call to DirIsWritable(), which did a similar check. Users should not notice any different behavior.
1 parent fa0afe7 commit fad3a97

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/init.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,10 +1028,9 @@ static bool LockDataDirectory(bool probeOnly)
10281028
{
10291029
// Make sure only a single Bitcoin process is using the data directory.
10301030
const fs::path& datadir = gArgs.GetDataDirNet();
1031-
if (!DirIsWritable(datadir)) {
1032-
return InitError(strprintf(_("Cannot write to data directory '%s'; check permissions."), fs::PathToString(datadir)));
1033-
}
10341031
switch (util::LockDirectory(datadir, ".lock", probeOnly)) {
1032+
case util::LockResult::ErrorWrite:
1033+
return InitError(strprintf(_("Cannot write to data directory '%s'; check permissions."), fs::PathToString(datadir)));
10351034
case util::LockResult::ErrorLock:
10361035
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running."), fs::PathToString(datadir), PACKAGE_NAME));
10371036
case util::LockResult::Success: return true;

src/test/util_tests.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,7 @@ static constexpr char UnlockCommand = 'U';
11121112
static constexpr char ExitCommand = 'X';
11131113
enum : char {
11141114
ResSuccess = 2, // Start with 2 to avoid accidental collision with common values 0 and 1
1115+
ResErrorWrite,
11151116
ResErrorLock,
11161117
ResUnlockSuccess,
11171118
};
@@ -1127,6 +1128,7 @@ enum : char {
11271128
ch = [&] {
11281129
switch (util::LockDirectory(dirname, lockname)) {
11291130
case util::LockResult::Success: return ResSuccess;
1131+
case util::LockResult::ErrorWrite: return ResErrorWrite;
11301132
case util::LockResult::ErrorLock: return ResErrorLock;
11311133
} // no default case, so the compiler can warn about missing cases
11321134
assert(false);
@@ -1171,9 +1173,15 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
11711173
TestOtherProcess(dirname, lockname, fd[0]);
11721174
}
11731175
BOOST_CHECK_EQUAL(close(fd[0]), 0); // Parent: close child end
1176+
1177+
char ch;
1178+
// Lock on non-existent directory should fail
1179+
BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
1180+
BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
1181+
BOOST_CHECK_EQUAL(ch, ResErrorWrite);
11741182
#endif
11751183
// Lock on non-existent directory should fail
1176-
BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname), util::LockResult::ErrorLock);
1184+
BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname), util::LockResult::ErrorWrite);
11771185

11781186
fs::create_directories(dirname);
11791187

@@ -1193,7 +1201,6 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
11931201
BOOST_CHECK_EQUAL(threadresult, util::LockResult::Success);
11941202
#ifndef WIN32
11951203
// Try to acquire lock in child process while we're holding it, this should fail.
1196-
char ch;
11971204
BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
11981205
BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
11991206
BOOST_CHECK_EQUAL(ch, ResErrorLock);

src/util/fs_helpers.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ LockResult LockDirectory(const fs::path& directory, const fs::path& lockfile_nam
6565
}
6666

6767
// Create empty lock file if it doesn't exist.
68-
FILE* file = fsbridge::fopen(pathLockFile, "a");
69-
if (file) fclose(file);
68+
if (auto created{fsbridge::fopen(pathLockFile, "a")}) {
69+
std::fclose(created);
70+
} else {
71+
return LockResult::ErrorWrite;
72+
}
7073
auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
7174
if (!lock->TryLock()) {
7275
error("Error while attempting to lock directory %s: %s", fs::PathToString(directory), lock->GetReason());

src/util/fs_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length);
3838
namespace util {
3939
enum class LockResult {
4040
Success,
41+
ErrorWrite,
4142
ErrorLock,
4243
};
4344
[[nodiscard]] LockResult LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only = false);

0 commit comments

Comments
 (0)