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_helpers.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <bitcoin-build-config.h> // IWYU pragma: keep
7
8
#include <util/fs_helpers.h>
9
10
#include <sync.h>
11
#include <util/byte_units.h> // IWYU pragma: keep
12
#include <util/fs.h>
13
#include <util/log.h>
14
#include <util/syserror.h>
15
16
#include <cerrno>
17
#include <fstream>
18
#include <map>
19
#include <memory>
20
#include <optional>
21
#include <string>
22
#include <system_error>
23
#include <utility>
24
25
#ifndef WIN32
26
#include <fcntl.h>
27
#include <sys/resource.h>
28
#include <sys/types.h>
29
#include <unistd.h>
30
#else
31
#include <io.h>
32
#include <shlobj.h>
33
#endif // WIN32
34
35
#ifdef __APPLE__
36
#include <sys/mount.h>
37
#include <sys/param.h>
38
#endif
39
40
/** Mutex to protect dir_locks. */
41
static GlobalMutex cs_dir_locks;
42
/** A map that contains all the currently held directory locks. After
43
 * successful locking, these will be held here until the global destructor
44
 * cleans them up and thus automatically unlocks them, or ReleaseDirectoryLocks
45
 * is called.
46
 */
47
static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks GUARDED_BY(cs_dir_locks);
48
namespace util {
49
LockResult LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only)
50
4.47k
{
51
4.47k
    LOCK(cs_dir_locks);
52
4.47k
    fs::path pathLockFile = directory / lockfile_name;
53
54
    // If a lock for this directory already exists in the map, don't try to re-lock it
55
4.47k
    if (dir_locks.contains(fs::PathToString(pathLockFile))) {
56
2
        return LockResult::Success;
57
2
    }
58
59
    // Create empty lock file if it doesn't exist.
60
4.47k
    if (auto created{fsbridge::fopen(pathLockFile, "a")}) {
61
4.46k
        std::fclose(created);
62
4.46k
    } else {
63
2
        return LockResult::ErrorWrite;
64
2
    }
65
4.46k
    auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
66
4.46k
    if (!lock->TryLock()) {
67
4
        LogError("Error while attempting to lock directory %s: %s\n", fs::PathToString(directory), lock->GetReason());
68
4
        return LockResult::ErrorLock;
69
4
    }
70
4.46k
    if (!probe_only) {
71
        // Lock successful and we're not just probing, put it into the map
72
2.23k
        dir_locks.emplace(fs::PathToString(pathLockFile), std::move(lock));
73
2.23k
    }
74
4.46k
    return LockResult::Success;
75
4.46k
}
76
} // namespace util
77
void UnlockDirectory(const fs::path& directory, const fs::path& lockfile_name)
78
0
{
79
0
    LOCK(cs_dir_locks);
80
0
    dir_locks.erase(fs::PathToString(directory / lockfile_name));
81
0
}
82
83
void ReleaseDirectoryLocks()
84
3
{
85
3
    LOCK(cs_dir_locks);
86
3
    dir_locks.clear();
87
3
}
88
89
bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes)
90
10.0k
{
91
10.0k
    constexpr uint64_t min_disk_space{50_MiB};
92
93
10.0k
    uint64_t free_bytes_available = fs::space(dir).available;
94
10.0k
    return free_bytes_available >= min_disk_space + additional_bytes;
95
10.0k
}
96
97
std::streampos GetFileSize(const char* path, std::streamsize max)
98
0
{
99
0
    std::ifstream file{path, std::ios::binary};
100
0
    file.ignore(max);
101
0
    return file.gcount();
102
0
}
103
104
bool FileCommit(FILE* file)
105
9.24k
{
106
9.24k
    if (fflush(file) != 0) { // harmless if redundantly called
107
0
        LogError("fflush failed: %s", SysErrorString(errno));
108
0
        return false;
109
0
    }
110
#ifdef WIN32
111
    HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
112
    if (FlushFileBuffers(hFile) == 0) {
113
        LogError("FlushFileBuffers failed: %s", Win32ErrorString(GetLastError()));
114
        return false;
115
    }
116
#elif defined(__APPLE__) && defined(F_FULLFSYNC)
117
    if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) { // Manpage says "value other than -1" is returned on success
118
        LogError("fcntl F_FULLFSYNC failed: %s", SysErrorString(errno));
119
        return false;
120
    }
121
#elif HAVE_FDATASYNC
122
9.24k
    if (fdatasync(fileno(file)) != 0 && errno != EINVAL) { // Ignore EINVAL for filesystems that don't support sync
123
0
        LogError("fdatasync failed: %s", SysErrorString(errno));
124
0
        return false;
125
0
    }
126
#else
127
    if (fsync(fileno(file)) != 0 && errno != EINVAL) {
128
        LogError("fsync failed: %s", SysErrorString(errno));
129
        return false;
130
    }
131
#endif
132
9.24k
    return true;
133
9.24k
}
134
135
void DirectoryCommit(const fs::path& dirname)
136
6.74k
{
137
6.74k
#ifndef WIN32
138
6.74k
    FILE* file = fsbridge::fopen(dirname, "r");
139
6.74k
    if (file) {
140
6.74k
        fsync(fileno(file));
141
6.74k
        fclose(file);
142
6.74k
    }
143
6.74k
#endif
144
6.74k
}
145
146
bool TruncateFile(FILE* file, unsigned int length)
147
64
{
148
#if defined(WIN32)
149
    return _chsize(_fileno(file), length) == 0;
150
#else
151
64
    return ftruncate(fileno(file), length) == 0;
152
64
#endif
153
64
}
154
155
/**
156
 * this function tries to raise the file descriptor limit to the requested number.
157
 * It returns the actual file descriptor limit (which may be more or less than nMinFD)
158
 */
159
int RaiseFileDescriptorLimit(int nMinFD)
160
1.79k
{
161
#if defined(WIN32)
162
    return 2048;
163
#else
164
1.79k
    struct rlimit limitFD;
165
1.79k
    if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
166
1.79k
        if (limitFD.rlim_cur < (rlim_t)nMinFD) {
167
0
            limitFD.rlim_cur = nMinFD;
168
0
            if (limitFD.rlim_cur > limitFD.rlim_max)
169
0
                limitFD.rlim_cur = limitFD.rlim_max;
170
0
            setrlimit(RLIMIT_NOFILE, &limitFD);
171
0
            getrlimit(RLIMIT_NOFILE, &limitFD);
172
0
        }
173
1.79k
        return limitFD.rlim_cur;
174
1.79k
    }
175
0
    return nMinFD; // getrlimit failed, assume it's fine
176
1.79k
#endif
177
1.79k
}
178
179
/**
180
 * this function tries to make a particular range of a file allocated (corresponding to disk space)
181
 * it is advisory, and the range specified in the arguments will never contain live data
182
 */
183
void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length)
184
950
{
185
#if defined(WIN32)
186
    // Windows-specific version
187
    HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
188
    LARGE_INTEGER nFileSize;
189
    int64_t nEndPos = (int64_t)offset + length;
190
    nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
191
    nFileSize.u.HighPart = nEndPos >> 32;
192
    SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
193
    SetEndOfFile(hFile);
194
#elif defined(__APPLE__)
195
    // OSX specific version
196
    // NOTE: Contrary to other OS versions, the OSX version assumes that
197
    // NOTE: offset is the size of the file.
198
    fstore_t fst;
199
    fst.fst_flags = F_ALLOCATECONTIG;
200
    fst.fst_posmode = F_PEOFPOSMODE;
201
    fst.fst_offset = 0;
202
    fst.fst_length = length; // mac os fst_length takes the # of free bytes to allocate, not desired file size
203
    fst.fst_bytesalloc = 0;
204
    if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
205
        fst.fst_flags = F_ALLOCATEALL;
206
        fcntl(fileno(file), F_PREALLOCATE, &fst);
207
    }
208
    ftruncate(fileno(file), static_cast<off_t>(offset) + length);
209
#else
210
950
#if defined(HAVE_POSIX_FALLOCATE)
211
    // Version using posix_fallocate
212
950
    off_t nEndPos = (off_t)offset + length;
213
950
    if (0 == posix_fallocate(fileno(file), 0, nEndPos)) return;
214
0
#endif
215
    // Fallback version
216
    // TODO: just write one byte per block
217
0
    static const char buf[65536] = {};
218
0
    if (fseek(file, offset, SEEK_SET)) {
219
0
        return;
220
0
    }
221
0
    while (length > 0) {
222
0
        unsigned int now = 65536;
223
0
        if (length < now)
224
0
            now = length;
225
0
        fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
226
0
        length -= now;
227
0
    }
228
0
#endif
229
0
}
230
231
#ifdef WIN32
232
fs::path GetSpecialFolderPath(int nFolder, bool fCreate)
233
{
234
    WCHAR pszPath[MAX_PATH] = L"";
235
236
    if (SHGetSpecialFolderPathW(nullptr, pszPath, nFolder, fCreate)) {
237
        return fs::path(pszPath);
238
    }
239
240
    LogError("SHGetSpecialFolderPathW() failed, could not obtain requested path.");
241
    return fs::path("");
242
}
243
#endif
244
245
bool RenameOver(fs::path src, fs::path dest)
246
4.85k
{
247
4.85k
    std::error_code error;
248
4.85k
    fs::rename(src, dest, error);
249
4.85k
    return !error;
250
4.85k
}
251
252
/**
253
 * Ignores exceptions thrown by create_directories if the requested directory exists.
254
 * Specifically handles case where path p exists, but it wasn't possible for the user to
255
 * write to the parent directory.
256
 */
257
bool TryCreateDirectories(const fs::path& p)
258
4.16k
{
259
4.16k
    try {
260
4.16k
        return fs::create_directories(p);
261
4.16k
    } catch (const fs::filesystem_error&) {
262
2
        if (!fs::exists(p) || !fs::is_directory(p))
263
2
            throw;
264
2
    }
265
266
    // create_directories didn't create the directory, it had to have existed already
267
0
    return false;
268
4.16k
}
269
270
std::string PermsToSymbolicString(fs::perms p)
271
1.09k
{
272
1.09k
    std::string perm_str(9, '-');
273
274
9.81k
    auto set_perm = [&](size_t pos, fs::perms required_perm, char letter) {
275
9.81k
        if ((p & required_perm) != fs::perms::none) {
276
2.18k
            perm_str[pos] = letter;
277
2.18k
        }
278
9.81k
    };
279
280
1.09k
    set_perm(0, fs::perms::owner_read,   'r');
281
1.09k
    set_perm(1, fs::perms::owner_write,  'w');
282
1.09k
    set_perm(2, fs::perms::owner_exec,   'x');
283
1.09k
    set_perm(3, fs::perms::group_read,   'r');
284
1.09k
    set_perm(4, fs::perms::group_write,  'w');
285
1.09k
    set_perm(5, fs::perms::group_exec,   'x');
286
1.09k
    set_perm(6, fs::perms::others_read,  'r');
287
1.09k
    set_perm(7, fs::perms::others_write, 'w');
288
1.09k
    set_perm(8, fs::perms::others_exec,  'x');
289
290
1.09k
    return perm_str;
291
1.09k
}
292
293
std::optional<fs::perms> InterpretPermString(const std::string& s)
294
3
{
295
3
    if (s == "owner") {
296
1
        return fs::perms::owner_read | fs::perms::owner_write;
297
2
    } else if (s == "group") {
298
1
        return fs::perms::owner_read | fs::perms::owner_write |
299
1
               fs::perms::group_read;
300
1
    } else if (s == "all") {
301
1
        return fs::perms::owner_read | fs::perms::owner_write |
302
1
               fs::perms::group_read |
303
1
               fs::perms::others_read;
304
1
    } else {
305
0
        return std::nullopt;
306
0
    }
307
3
}
308
309
#ifdef __APPLE__
310
FSType GetFilesystemType(const fs::path& path)
311
{
312
    if (struct statfs fs_info; statfs(path.c_str(), &fs_info)) {
313
        return FSType::ERROR;
314
    } else if (std::string_view{fs_info.f_fstypename} == "exfat") {
315
        return FSType::EXFAT;
316
    }
317
    return FSType::OTHER;
318
}
319
#endif