Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/util/fs.cpp
Line
Count
Source
1
// Copyright (c) 2017-present 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 <util/fs.h>
6
7
#include <util/check.h>
8
#include <util/syserror.h>
9
10
#include <cerrno>
11
#include <string>
12
13
#ifndef WIN32
14
#include <fcntl.h>
15
#include <unistd.h>
16
#else
17
#include <limits>
18
#include <windows.h>
19
#endif
20
21
namespace fsbridge {
22
23
FILE *fopen(const fs::path& p, const char *mode)
24
439k
{
25
439k
#ifndef WIN32
26
439k
    return ::fopen(p.c_str(), mode);
27
#else
28
    return ::fopen(p.utf8string().c_str(), mode);
29
#endif
30
439k
}
31
32
fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
33
17.3k
{
34
17.3k
    assert(base.is_absolute());
35
17.3k
    return path.empty() ? base : fs::path(base / path);
36
17.3k
}
37
38
#ifndef WIN32
39
40
static std::string GetErrorReason()
41
4
{
42
4
    return SysErrorString(errno);
43
4
}
44
45
FileLock::FileLock(const fs::path& file)
46
4.46k
{
47
4.46k
    fd = open(file.c_str(), O_RDWR);
48
4.46k
    if (fd == -1) {
49
0
        reason = GetErrorReason();
50
0
    }
51
4.46k
}
52
53
FileLock::~FileLock()
54
2.23k
{
55
2.23k
    if (fd != -1) {
56
2.23k
        close(fd);
57
2.23k
    }
58
2.23k
}
59
60
bool FileLock::TryLock()
61
4.46k
{
62
4.46k
    if (fd == -1) {
63
0
        return false;
64
0
    }
65
66
4.46k
    struct flock lock;
67
4.46k
    lock.l_type = F_WRLCK;
68
4.46k
    lock.l_whence = SEEK_SET;
69
4.46k
    lock.l_start = 0;
70
4.46k
    lock.l_len = 0;
71
4.46k
    if (fcntl(fd, F_SETLK, &lock) == -1) {
72
4
        reason = GetErrorReason();
73
4
        return false;
74
4
    }
75
76
4.46k
    return true;
77
4.46k
}
78
#else
79
80
static std::string GetErrorReason() {
81
    return Win32ErrorString(GetLastError());
82
}
83
84
FileLock::FileLock(const fs::path& file)
85
{
86
    hFile = CreateFileW(file.wstring().c_str(),  GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
87
        nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
88
    if (hFile == INVALID_HANDLE_VALUE) {
89
        reason = GetErrorReason();
90
    }
91
}
92
93
FileLock::~FileLock()
94
{
95
    if (hFile != INVALID_HANDLE_VALUE) {
96
        CloseHandle(hFile);
97
    }
98
}
99
100
bool FileLock::TryLock()
101
{
102
    if (hFile == INVALID_HANDLE_VALUE) {
103
        return false;
104
    }
105
    _OVERLAPPED overlapped = {};
106
    if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
107
        reason = GetErrorReason();
108
        return false;
109
    }
110
    return true;
111
}
112
#endif
113
114
} // namespace fsbridge