Coverage Report

Created: 2025-06-10 13:21

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