Bitcoin ABC 0.31.8
P2P Digital Currency
flatfile.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2019 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 <flatfile.h>
7#include <logging.h>
8#include <tinyformat.h>
9#include <util/fs_helpers.h>
10
11#include <stdexcept>
12
13FlatFileSeq::FlatFileSeq(fs::path dir, const char *prefix, size_t chunk_size)
14 : m_dir(std::move(dir)), m_prefix(prefix), m_chunk_size(chunk_size) {
15 if (chunk_size == 0) {
16 throw std::invalid_argument("chunk_size must be positive");
17 }
18}
19
20std::string FlatFilePos::ToString() const {
21 return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
22}
23
25 return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile);
26}
27
28FILE *FlatFileSeq::Open(const FlatFilePos &pos, bool read_only) {
29 if (pos.IsNull()) {
30 return nullptr;
31 }
32 fs::path path = FileName(pos);
33 if (!read_only) {
34 fs::create_directories(path.parent_path());
35 }
36 FILE *file = fsbridge::fopen(path, read_only ? "rb" : "rb+");
37 if (!file && !read_only) {
38 file = fsbridge::fopen(path, "wb+");
39 }
40 if (!file) {
41 LogPrintf("Unable to open file %s\n", fs::PathToString(path));
42 return nullptr;
43 }
44 if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) {
45 LogPrintf("Unable to seek to position %u of %s\n", pos.nPos,
46 fs::PathToString(path));
47 fclose(file);
48 return nullptr;
49 }
50 return file;
51}
52
53size_t FlatFileSeq::Allocate(const FlatFilePos &pos, size_t add_size,
54 bool &out_of_space) {
55 out_of_space = false;
56
57 unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size;
58 unsigned int n_new_chunks =
59 (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size;
60 if (n_new_chunks > n_old_chunks) {
61 size_t old_size = pos.nPos;
62 size_t new_size = n_new_chunks * m_chunk_size;
63 size_t inc_size = new_size - old_size;
64
65 if (CheckDiskSpace(m_dir, inc_size)) {
66 FILE *file = Open(pos);
67 if (file) {
69 "Pre-allocating up to position 0x%x in %s%05u.dat\n",
70 new_size, m_prefix, pos.nFile);
71 AllocateFileRange(file, pos.nPos, inc_size);
72 fclose(file);
73 return inc_size;
74 }
75 } else {
76 out_of_space = true;
77 }
78 }
79 return 0;
80}
81
82bool FlatFileSeq::Flush(const FlatFilePos &pos, bool finalize) {
83 // Avoid fseek to nPos
84 FILE *file = Open(FlatFilePos(pos.nFile, 0));
85 if (!file) {
86 return error("%s: failed to open file %d", __func__, pos.nFile);
87 }
88 if (finalize && !TruncateFile(file, pos.nPos)) {
89 fclose(file);
90 return error("%s: failed to truncate file %d", __func__, pos.nFile);
91 }
92 if (!FileCommit(file)) {
93 fclose(file);
94 return error("%s: failed to commit file %d", __func__, pos.nFile);
95 }
97
98 fclose(file);
99 return true;
100}
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
Definition: flatfile.cpp:24
const fs::path m_dir
Definition: flatfile.h:51
const size_t m_chunk_size
Definition: flatfile.h:53
size_t Allocate(const FlatFilePos &pos, size_t add_size, bool &out_of_space)
Allocate additional space in a file after the given starting position.
Definition: flatfile.cpp:53
bool Flush(const FlatFilePos &pos, bool finalize=false)
Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
Definition: flatfile.cpp:82
const char *const m_prefix
Definition: flatfile.h:52
FlatFileSeq(fs::path dir, const char *prefix, size_t chunk_size)
Constructor.
Definition: flatfile.cpp:13
FILE * Open(const FlatFilePos &pos, bool read_only=false)
Open a handle to the file at the given position.
Definition: flatfile.cpp:28
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
void DirectoryCommit(const fs::path &dirname)
Sync directory contents.
Definition: fs_helpers.cpp:159
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
This function tries to make a particular range of a file allocated (corresponding to disk space) it i...
Definition: fs_helpers.cpp:208
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
Definition: fs_helpers.cpp:111
bool TruncateFile(FILE *file, unsigned int length)
Definition: fs_helpers.cpp:169
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: fs_helpers.cpp:125
bool error(const char *fmt, const Args &...args)
Definition: logging.h:302
#define LogPrint(category,...)
Definition: logging.h:291
#define LogPrintf(...)
Definition: logging.h:270
@ VALIDATION
Definition: logging.h:64
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
Definition: fs.h:179
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:142
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
const char * prefix
Definition: rest.cpp:816
int nFile
Definition: flatfile.h:15
std::string ToString() const
Definition: flatfile.cpp:20
unsigned int nPos
Definition: flatfile.h:16
bool IsNull() const
Definition: flatfile.h:40
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202