Bitcoin Core  27.99.0
P2P Digital Currency
flatfile.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 <stdexcept>
7 
8 #include <flatfile.h>
9 #include <logging.h>
10 #include <tinyformat.h>
11 #include <util/fs_helpers.h>
12 
13 FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
14  m_dir(std::move(dir)),
15  m_prefix(prefix),
16  m_chunk_size(chunk_size)
17 {
18  if (chunk_size == 0) {
19  throw std::invalid_argument("chunk_size must be positive");
20  }
21 }
22 
23 std::string FlatFilePos::ToString() const
24 {
25  return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
26 }
27 
29 {
30  return m_dir / fs::u8path(strprintf("%s%05u.dat", m_prefix, pos.nFile));
31 }
32 
33 FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only)
34 {
35  if (pos.IsNull()) {
36  return nullptr;
37  }
38  fs::path path = FileName(pos);
39  fs::create_directories(path.parent_path());
40  FILE* file = fsbridge::fopen(path, read_only ? "rb": "rb+");
41  if (!file && !read_only)
42  file = fsbridge::fopen(path, "wb+");
43  if (!file) {
44  LogPrintf("Unable to open file %s\n", fs::PathToString(path));
45  return nullptr;
46  }
47  if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) {
48  LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, fs::PathToString(path));
49  fclose(file);
50  return nullptr;
51  }
52  return file;
53 }
54 
55 size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space)
56 {
57  out_of_space = false;
58 
59  unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size;
60  unsigned int n_new_chunks = (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size;
61  if (n_new_chunks > n_old_chunks) {
62  size_t old_size = pos.nPos;
63  size_t new_size = n_new_chunks * m_chunk_size;
64  size_t inc_size = new_size - old_size;
65 
66  if (CheckDiskSpace(m_dir, inc_size)) {
67  FILE *file = Open(pos);
68  if (file) {
69  LogPrint(BCLog::VALIDATION, "Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
70  AllocateFileRange(file, pos.nPos, inc_size);
71  fclose(file);
72  return inc_size;
73  }
74  } else {
75  out_of_space = true;
76  }
77  }
78  return 0;
79 }
80 
81 bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize)
82 {
83  FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
84  if (!file) {
85  LogError("%s: failed to open file %d\n", __func__, pos.nFile);
86  return false;
87  }
88  if (finalize && !TruncateFile(file, pos.nPos)) {
89  fclose(file);
90  LogError("%s: failed to truncate file %d\n", __func__, pos.nFile);
91  return false;
92  }
93  if (!FileCommit(file)) {
94  fclose(file);
95  LogError("%s: failed to commit file %d\n", __func__, pos.nFile);
96  return false;
97  }
99 
100  fclose(file);
101  return true;
102 }
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
Definition: flatfile.cpp:28
const fs::path m_dir
Definition: flatfile.h:48
const size_t m_chunk_size
Definition: flatfile.h:50
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:55
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:81
const char *const m_prefix
Definition: flatfile.h:49
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:33
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
void DirectoryCommit(const fs::path &dirname)
Sync directory contents.
Definition: fs_helpers.cpp:140
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:188
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
Definition: fs_helpers.cpp:94
bool TruncateFile(FILE *file, unsigned int length)
Definition: fs_helpers.cpp:151
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: fs_helpers.cpp:109
#define LogPrint(category,...)
Definition: logging.h:263
#define LogError(...)
Definition: logging.h:241
#define LogPrintf(...)
Definition: logging.h:244
@ VALIDATION
Definition: logging.h:62
static path u8path(const std::string &utf8_str)
Definition: fs.h:75
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:190
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:26
const char * prefix
Definition: rest.cpp:1007
int nFile
Definition: flatfile.h:16
std::string ToString() const
Definition: flatfile.cpp:23
unsigned int nPos
Definition: flatfile.h:17
bool IsNull() const
Definition: flatfile.h:36
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162