• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-database / 45741729766

26 Nov 2026 02:20AM UTC coverage: 92.73% (-0.02%) from 92.751%
45741729766

Pull #1109

github

web-flow
Merge 4d3dfc66e into 89c48bacf
Pull Request #1109: Flush snapshot head files to disk before promotion.

27 of 32 new or added lines in 2 files covered. (84.38%)

12066 of 13012 relevant lines covered (92.73%)

103525.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.25
/src/file/utilities.cpp
1
/**
2
 * Copyright (c) 2011-2026 libbitcoin developers
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/database/file/utilities.hpp>
20

21
#if defined(HAVE_WINDOWS)
22
    #include <io.h>
23
    #include <share.h>
24
#else
25
    #include <unistd.h>
26
#endif
27
#include <fcntl.h>
28
#include <sys/stat.h>
29
#include <sys/types.h>
30
#include <filesystem>
31
#include <ios>
32
#include <bitcoin/database/define.hpp>
33

34
#if defined(HAVE_WINDOWS) || defined(HAVE_LINUX)
35
    #define ADVISED_ONLY(name) name
36
#else
37
    #define ADVISED_ONLY(name)
38
#endif
39

40
#if defined(POSIX_FADV_DONTNEED)
41
    #define DONTNEED_ONLY(name) name
42
#else
43
    #define DONTNEED_ONLY(name)
44
#endif
45

46
namespace libbitcoin {
47
namespace database {
48
namespace file {
49

50
using namespace system;
51
using namespace std::filesystem;
52
using errorno_t = system::error::errorno_t;
53

54
// Many return values are redundant with return codes.
55
// There is no reason to obtain the return value of these non-discardables.
56
BC_PUSH_WARNING(DISCARDING_NON_DISCARDABLE)
57
BC_PUSH_WARNING(NO_IGNORE_RETURN_VALUE)
58
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
59

60
// File and directory manipulation.
61
// ----------------------------------------------------------------------------
62

63
inline path trim(const path& value) NOEXCEPT
10 ✔
64
{
65
    static const string_list trims{ "/", "\\", "\x20" };
14 ✔
66

67
    // Trailing slash may cause successful create_directories returning false.
68
    return to_path(trim_right_copy(from_path(value), trims));
10 ✔
69
}
2 ✔
70

71
// ec not used: it is set (not found) when false, with no way to differentiate
72
// from false with failure code (and never a code on success).
73
bool is_directory(const path& directory) NOEXCEPT
194 ✔
74
{
75
    code ec{ errorno_t::no_error };
194 ✔
76
    return is_directory(system::extended_path(directory), ec);
194 ✔
77
}
78

79
bool clear_directory(const path& directory) NOEXCEPT
18 ✔
80
{
81
    return !clear_directory_ex(directory);
18 ✔
82
}
83

84
code clear_directory_ex(const path& directory) NOEXCEPT
748 ✔
85
{
86
    code ec{ errorno_t::no_error };
748 ✔
87
    const auto path = system::extended_path(directory);
748 ✔
88
    remove_all(path, ec);
748 ✔
89
    if (ec) return ec;
748 ✔
90
    create_directories(path, ec);
748 ✔
91
    return ec;
748 ✔
92
}
748 ✔
93

94
bool create_directory(const path& directory) NOEXCEPT
10 ✔
95
{
96
    return !create_directory_ex(directory);
10 ✔
97
}
98

99
code create_directory_ex(const path& directory) NOEXCEPT
10 ✔
100
{
101
    code ec{ errorno_t::no_error };
10 ✔
102
    const auto path = system::extended_path(trim(directory));
10 ✔
103
    const auto created = create_directories(path, ec);
10 ✔
104
    if (ec || created) return ec;
10 ✔
105
    return errorno_t::is_a_directory;
×
106
}
10 ✔
107

108
// ec not used: it is set (not found) when false, with no way to differentiate
109
// from false with failure code (and never a code on success).
110
bool is_file(const path& filename) NOEXCEPT
29 ✔
111
{
112
    code ec{ errorno_t::no_error };
29 ✔
113
    return is_regular_file(filename, ec);
29 ✔
114
}
115

116
bool create_file(const path& filename) NOEXCEPT
3 ✔
117
{
118
    return !create_file_ex(filename);
3 ✔
119
}
120

121
// std::filesystem does not provide file creation.
122
code create_file_ex(const path& filename) NOEXCEPT
8,998 ✔
123
{
124
    try
125
    {
126
        // Throws.
127
        ofstream file(filename);
8,998 ✔
128

129
        // Allow throw.
130
        file.exceptions(std::ifstream::failbit);
8,998 ✔
131

132
        // noexcept.
133
        if (!file.good())
8,998 ✔
134
            return errorno_t::not_a_stream;
×
135

136
        // Sets failbit (but not noexcept).
137
        file.close();
8,998 ✔
138

139
        // noexcept.
140
        return file.good() ? errorno_t::no_error : errorno_t::not_a_stream;
8,998 ✔
141
    }
8,998 ✔
142
    catch (const std::ios_base::failure& e)
×
143
    {
144
        // Prefer throw, since we get a platform code.
145
        return e.code();
×
146
    }
×
147
}
148

149
bool create_file(const path& to, const uint8_t* data, size_t size) NOEXCEPT
5 ✔
150
{
151
    return !create_file_ex(to, data, size);
5 ✔
152
}
153

154
// std::filesystem does not provide file creation.
155
code create_file_ex(const path& to, const uint8_t* data, size_t size) NOEXCEPT
653 ✔
156
{
157
    // Binary mode on Windows ensures that \n not replaced with \r\n.
158
    try
159
    {
160
        // Throws.
161
        ofstream file(to, std::ios_base::binary);
653 ✔
162

163
        // Allow throw.
164
        file.exceptions(std::ifstream::failbit);
653 ✔
165

166
        // noexcept.
167
        if (!file.good())
653 ✔
168
            return errorno_t::not_a_stream;
×
169

170
        // May throw.
171
        file.write(pointer_cast<const char>(data), size);
653 ✔
172

173
        // noexcept.
174
        if (!file.good())
653 ✔
175
            return errorno_t::not_a_stream;
×
176

177
        // Sets failbit (but not noexcept).
178
        file.close();
653 ✔
179

180
        // noexcept.
181
        return file.good() ? errorno_t::no_error : errorno_t::stream_timeout;
653 ✔
182
    }
653 ✔
183
    catch (const std::ios_base::failure& e)
×
184
    {
185
        // Prefer throw, since we get a platform code.
186
        return e.code();
×
187
    }
×
188
}
189

190
// directory|file
191
bool remove(const path& name) NOEXCEPT
24 ✔
192
{
193
    return !remove_ex(name);
24 ✔
194
}
195

196
// directory|file
197
// Error code not set if file did not exist.
198
code remove_ex(const path& name) NOEXCEPT
38 ✔
199
{
200
    code ec{ errorno_t::no_error };
38 ✔
201
    remove(system::extended_path(name), ec);
38 ✔
202
    return ec;
38 ✔
203
}
204

205
// directory|file
206
bool rename(const path& from, const path& to) NOEXCEPT
13 ✔
207
{
208
    return !rename_ex(from, to);
13 ✔
209
}
210

211
// directory|file
212
code rename_ex(const path& from, const path& to) NOEXCEPT
32 ✔
213
{
214
    code ec{ errorno_t::no_error };
32 ✔
215
    rename(system::extended_path(from), system::extended_path(to), ec);
32 ✔
216
    return ec;
32 ✔
217
}
218

219
// file
220
bool copy(const path& from, const path& to) NOEXCEPT
4 ✔
221
{
222
    return !copy_ex(from, to);
4 ✔
223
}
224

225
// file
226
code copy_ex(const path& from, const path& to) NOEXCEPT
4 ✔
227
{
228
    code ec{ errorno_t::no_error };
4 ✔
229
    copy_file(system::extended_path(from), system::extended_path(to), ec);
4 ✔
230
    return ec;
4 ✔
231
}
232

233
// directory
234
bool copy_directory(const path& from, const path& to) NOEXCEPT
6 ✔
235
{
236
    return !copy_directory_ex(from, to);
6 ✔
237
}
238

239
// directory
240
code copy_directory_ex(const path& from, const path& to) NOEXCEPT
15 ✔
241
{
242
    if (file::is_directory(to))
15 ✔
243
        return errorno_t::is_a_directory;
2 ✔
244

245
    if (!file::is_directory(from))
13 ✔
246
        return errorno_t::not_a_directory;
1 ✔
247

248
    code ec{ errorno_t::no_error };
12 ✔
249
    copy(system::extended_path(from), system::extended_path(to), ec);
12 ✔
250
    return ec;
12 ✔
251
}
252

253
// directory
254
bool flush_directory(const path& directory) NOEXCEPT
3 ✔
255
{
256
    return !flush_directory_ex(directory);
3 ✔
257
}
258

259
// directory
260
code flush_directory_ex(const path& directory) NOEXCEPT
19 ✔
261
{
262
    code ec{ errorno_t::no_error };
19 ✔
263
    directory_iterator it{ system::extended_path(directory), ec };
19 ✔
264

265
    for (const directory_iterator end{}; !ec && (it != end);
628 ✔
266
        it.increment(ec))
609 ✔
267
    {
268
        if (!it->is_regular_file(ec) || ec)
609 ✔
NEW
269
            continue;
×
270

271
        int descriptor{};
609 ✔
272
        if ((ec = open_ex(descriptor, it->path())))
609 ✔
NEW
273
            return ec;
×
274

275
        if ((ec = flush_ex(descriptor)))
609 ✔
276
        {
NEW
277
            /* bool */ close(descriptor);
×
NEW
278
            return ec;
×
279
        }
280

281
        if ((ec = close_ex(descriptor)))
609 ✔
NEW
282
            return ec;
×
283
    }
19 ✔
284

285
    return ec;
19 ✔
286
}
19 ✔
287

288
// directory
289
bool discharge_directory(const path& to) NOEXCEPT
×
290
{
291
    return !discharge_directory_ex(to);
×
292
}
293

294
// directory
295
// Remove archival clone from page cache. Synchronize before advising.
296
code discharge_directory_ex(const path& DONTNEED_ONLY(to)) NOEXCEPT
9 ✔
297
{
298
    code ec{ errorno_t::no_error };
9 ✔
299

300
#if defined(POSIX_FADV_DONTNEED)
301
    directory_iterator it{ system::extended_path(to), ec };
9 ✔
302

303
    for (const directory_iterator end{}; !ec && (it != end);
351 ✔
304
        it.increment(ec))
342 ✔
305
    {
306
        if (!it->is_regular_file(ec) || ec)
342 ✔
307
            continue;
×
308

309
        system::error::clear_errno();
342 ✔
310
        const auto descriptor = ::open(it->path().c_str(), O_RDONLY);
342 ✔
311
        if (descriptor == -1)
342 ✔
312
            return system::error::get_errno();
×
313

314
        if (::fsync(descriptor) == -1)
342 ✔
315
        {
316
            /* bool */ close(descriptor);
×
317
            return system::error::get_errno();
×
318
        }
319

320
        // posix_fadvise returns the error value (does not set errno).
321
        const auto result = ::posix_fadvise(descriptor, 0, 0,
342 ✔
322
            POSIX_FADV_DONTNEED);
323
        if (!is_zero(result))
342 ✔
324
        {
325
            /* bool */ close(descriptor);
×
326
            errno = result;
×
327
            return system::error::get_errno();
×
328
        }
329

330
        if (!close(descriptor))
342 ✔
331
            return system::error::get_errno();
×
332
    }
9 ✔
333
#endif // POSIX_FADV_DONTNEED
334

335
    return ec;
9 ✔
336
}
9 ✔
337

338
// File sizing.
339
// ----------------------------------------------------------------------------
340

341
bool size(size_t& out, const path& filename) NOEXCEPT
8 ✔
342
{
343
    return !size_ex(out, filename);
8 ✔
344
}
345

346
code size_ex(size_t& out, const path& filename) NOEXCEPT
8 ✔
347
{
348
    code ec{ errorno_t::no_error };
8 ✔
349
    const auto size = file_size(system::extended_path(filename), ec);
8 ✔
350
    if (ec)
8 ✔
351
        return ec;
1 ✔
352

353
    if (is_limited<size_t>(size))
7 ✔
354
        return errorno_t::value_too_large;
×
355

356
    out = possible_narrow_cast<size_t>(size);
7 ✔
357
    return ec;
7 ✔
358
}
359

360
bool space(uint64_t& out, const path& filename) NOEXCEPT
2,401 ✔
361
{
362
    return !space_ex(out, filename);
2,401 ✔
363
}
364

365
code space_ex(uint64_t& out, const path& filename) NOEXCEPT
2,401 ✔
366
{
367
    code ec{ errorno_t::no_error };
2,401 ✔
368
    const auto space_ = space( system::extended_path(filename), ec);
2,401 ✔
369
    if (ec)
2,401 ✔
370
        return ec;
×
371

372
    out = space_.available;
2,401 ✔
373
    return ec;
2,401 ✔
374
}
375

376
// File descriptor functions required for memory mapping.
377
// ----------------------------------------------------------------------------
378

379
int open(const path& filename, bool,
13,888 ✔
380
    advice ADVISED_ONLY(access)) NOEXCEPT
381
{
382
    const auto path = system::extended_path(filename);
13,888 ✔
383
    int file_descriptor{};
384

385
#if defined(HAVE_WINDOWS)
386
    // Advice is elective (unhinted is cache manager default) and configured
387
    // from read pattern (see database::advice).
388
    // Order follows advice enumeration.
389
    static constexpr std::array<int, 4> hints
390
    {
391
        0, _O_RANDOM, _O_SEQUENTIAL, _O_RANDOM
392
    };
393

394
    // _wsopen_s and wstring do not throw (but are unannotated).
395
    // sets file_descriptor = -1 and errno on error.
396
    const auto hint = hints.at(to_value(access));
397
    ::_wsopen_s(&file_descriptor, path.c_str(),
398
        O_RDWR | _O_BINARY | hint, _SH_DENYWR, _S_IREAD | _S_IWRITE);
399
#elif defined(HAVE_LINUX)
400
    // open sets errno on failure.
401
    file_descriptor = ::open(path.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
13,888 ✔
402

403
    // Advice is elective (normal is kernel default) and configured from read
404
    // pattern (see database::advice); random is structural.
405
    if ((file_descriptor != -1) && (access != advice::normal))
13,888 ✔
406
    {
407
        // Order follows the advice enumeration.
408
        static constexpr std::array<int, 4> advices
409
        {
410
            POSIX_FADV_NORMAL, POSIX_FADV_RANDOM, POSIX_FADV_SEQUENTIAL,
411
            POSIX_FADV_RANDOM
412
        };
413

414
        // posix_fadvise returns error on failure.
415
        const auto result = ::posix_fadvise(file_descriptor, 0, 0,
13,258 ✔
416
            advices.at(to_value(access)));
13,258 ✔
417
        if (!is_zero(result))
13,258 ✔
418
        {
419
            close(file_descriptor);
×
420
            file_descriptor = -1;
421
            errno = result;
×
422
        }
423
    }
424

425
    if (file_descriptor != -1)
13,888 ✔
426
    {
427
        // _SH_DENYWR equivalent.
428
        const struct flock lock{ F_WRLCK, SEEK_SET, 0, 0 };
13,879 ✔
429
        if (::fcntl(file_descriptor, F_SETLK, &lock) == -1)
13,879 ✔
430
        {
431
            const auto last = errno;
×
432
            close(file_descriptor);
×
433
            file_descriptor = -1;
434
            errno = last;
×
435
        }
436
    }
437
#else
438
    // open sets errno on failure.
439
    file_descriptor = ::open(path.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
440
#endif // HAVE_WINDOWS
441

442
    return file_descriptor;
13,888 ✔
443
}
13,888 ✔
444

445
code open_ex(int& file_descriptor, const path& filename, bool random,
13,875 ✔
446
    advice access) NOEXCEPT
447
{
448
    system::error::clear_errno();
13,875 ✔
449
    file_descriptor = open(filename, random, access);
13,875 ✔
450
    return system::error::get_errno();
13,875 ✔
451
}
452

453
bool close(int file_descriptor) NOEXCEPT
14,221 ✔
454
{
455
    // close and _close() set errno on failure.
456
#if defined(HAVE_WINDOWS)
457
    return is_zero(::_close(file_descriptor));
458
#else
459
    return is_zero(::close(file_descriptor));
14,221 ✔
460
#endif
461
}
462

463
code close_ex(int file_descriptor) NOEXCEPT
13,867 ✔
464
{
465
    system::error::clear_errno();
13,867 ✔
466
    close(file_descriptor);
13,867 ✔
467
    return system::error::get_errno();
13,867 ✔
468
}
469

470
bool flush(int file_descriptor) NOEXCEPT
613 ✔
471
{
472
    if (file_descriptor == -1)
613 ✔
473
    {
474
        system::error::set_errno(errorno_t::invalid_argument);
2 ✔
475
        return false;
2 ✔
476
    }
477

478
    // _commit, fcntl and fsync set errno on failure.
479
#if defined(HAVE_WINDOWS)
480
    return is_zero(::_commit(file_descriptor));
481
#elif defined(F_FULLFSYNC)
482
    // fsync does not flush drive cache on macOS.
483
    return ::fcntl(file_descriptor, F_FULLFSYNC, 0) != -1;
484
#else
485
    return is_zero(::fsync(file_descriptor));
611 ✔
486
#endif
487
}
488

489
code flush_ex(int file_descriptor) NOEXCEPT
611 ✔
490
{
491
    system::error::clear_errno();
611 ✔
492
    flush(file_descriptor);
611 ✔
493
    return system::error::get_errno();
611 ✔
494
}
495

496
bool size(size_t& out, int file_descriptor) NOEXCEPT
8,634 ✔
497
{
498
    if (file_descriptor == -1)
8,634 ✔
499
    {
500
        system::error::set_errno(errorno_t::invalid_argument);
2 ✔
501
        return false;
2 ✔
502
    }
503

504
    // _fstat64, _fstat32 and fstat set errno on failure.
505
    // Required because off_t is defined as long, which is 32|64 bits in msvc
506
    // and 64 bits in linux/osx, and stat contains off_t.
507
#if defined(HAVE_WINDOWS)
508
#if defined(HAVE_X64)
509
    struct _stat64 sbuf{};
510
    if (_fstat64(file_descriptor, &sbuf) == -1)
511
        return false;
512
#else
513
    struct _stat32 sbuf{};
514
    if (_fstat32(file_descriptor, &sbuf) == -1)
515
        return false;
516
#endif
517
#else
518
    // Limited to 32 bit files on 32 bit systems.
519
    struct stat sbuf{};
8,632 ✔
520
    if (fstat(file_descriptor, &sbuf) == -1)
8,632 ✔
521
        return false;
522
#endif
523
    if (is_limited<size_t>(sbuf.st_size))
8,632 ✔
524
    {
525
        system::error::set_errno(errorno_t::value_too_large);
×
526
        return false;
×
527
    }
528

529
    out = possible_narrow_sign_cast<size_t>(sbuf.st_size);
8,632 ✔
530
    return true;
8,632 ✔
531
}
532

533
code size_ex(size_t& out, int file_descriptor) NOEXCEPT
8,627 ✔
534
{
535
    system::error::clear_errno();
8,627 ✔
536
    size(out, file_descriptor);
8,627 ✔
537
    return system::error::get_errno();
8,627 ✔
538
}
539

540
BC_POP_WARNING()
541
BC_POP_WARNING()
542
BC_POP_WARNING()
543

544
} // namespace file
545
} // namespace database
546
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc