Bitcoin ABC  0.22.12
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/system.h>
10 
11 #include <stdexcept>
12 
13 FlatFileSeq::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 
20 std::string FlatFilePos::ToString() const {
21  return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
22 }
23 
24 fs::path FlatFileSeq::FileName(const FlatFilePos &pos) const {
25  return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile);
26 }
27 
28 FILE *FlatFileSeq::Open(const FlatFilePos &pos, bool read_only) {
29  if (pos.IsNull()) {
30  return nullptr;
31  }
32  fs::path path = FileName(pos);
33  fs::create_directories(path.parent_path());
34  FILE *file = fsbridge::fopen(path, read_only ? "rb" : "rb+");
35  if (!file && !read_only) {
36  file = fsbridge::fopen(path, "wb+");
37  }
38  if (!file) {
39  LogPrintf("Unable to open file %s\n", path.string());
40  return nullptr;
41  }
42  if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) {
43  LogPrintf("Unable to seek to position %u of %s\n", pos.nPos,
44  path.string());
45  fclose(file);
46  return nullptr;
47  }
48  return file;
49 }
50 
51 size_t FlatFileSeq::Allocate(const FlatFilePos &pos, size_t add_size,
52  bool &out_of_space) {
53  out_of_space = false;
54 
55  unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size;
56  unsigned int n_new_chunks =
57  (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size;
58  if (n_new_chunks > n_old_chunks) {
59  size_t old_size = pos.nPos;
60  size_t new_size = n_new_chunks * m_chunk_size;
61  size_t inc_size = new_size - old_size;
62 
63  if (CheckDiskSpace(m_dir, inc_size)) {
64  FILE *file = Open(pos);
65  if (file) {
66  LogPrintf("Pre-allocating up to position 0x%x in %s%05u.dat\n",
67  new_size, m_prefix, pos.nFile);
68  AllocateFileRange(file, pos.nPos, inc_size);
69  fclose(file);
70  return inc_size;
71  }
72  } else {
73  out_of_space = true;
74  }
75  }
76  return 0;
77 }
78 
79 bool FlatFileSeq::Flush(const FlatFilePos &pos, bool finalize) {
80  // Avoid fseek to nPos
81  FILE *file = Open(FlatFilePos(pos.nFile, 0));
82  if (!file) {
83  return error("%s: failed to open file %d", __func__, pos.nFile);
84  }
85  if (finalize && !TruncateFile(file, pos.nPos)) {
86  fclose(file);
87  return error("%s: failed to truncate file %d", __func__, pos.nFile);
88  }
89  if (!FileCommit(file)) {
90  fclose(file);
91  return error("%s: failed to commit file %d", __func__, pos.nFile);
92  }
93 
94  fclose(file);
95  return true;
96 }
bool FileCommit(FILE *file)
Definition: system.cpp:1120
const fs::path m_dir
Definition: flatfile.h:54
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:22
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
bool IsNull() const
Definition: flatfile.h:43
const char * prefix
Definition: rest.cpp:754
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:174
int nFile
Definition: flatfile.h:15
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
Definition: system.cpp:141
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:79
FILE * Open(const FlatFilePos &pos, bool read_only=false)
Open a handle to the file at the given position.
Definition: flatfile.cpp:28
const char *const m_prefix
Definition: flatfile.h:55
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: system.cpp:1195
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:51
std::string ToString() const
Definition: flatfile.cpp:20
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
Definition: flatfile.cpp:24
bool TruncateFile(FILE *file, unsigned int length)
Definition: system.cpp:1156
const size_t m_chunk_size
Definition: flatfile.h:56
FlatFileSeq(fs::path dir, const char *prefix, size_t chunk_size)
Constructor.
Definition: flatfile.cpp:13
unsigned int nPos
Definition: flatfile.h:16
bool error(const char *fmt, const Args &... args)
Definition: system.h:47