/ src / flatfile.cpp
flatfile.cpp
  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  
 28  fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const
 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) const
 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          LogError("Unable to open file %s", fs::PathToString(path));
 45          return nullptr;
 46      }
 47      if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) {
 48          LogError("Unable to seek to position %u of %s", pos.nPos, fs::PathToString(path));
 49          if (fclose(file) != 0) {
 50              LogError("Unable to close file %s", fs::PathToString(path));
 51          }
 52          return nullptr;
 53      }
 54      return file;
 55  }
 56  
 57  size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const
 58  {
 59      out_of_space = false;
 60  
 61      unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size;
 62      unsigned int n_new_chunks = (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size;
 63      if (n_new_chunks > n_old_chunks) {
 64          size_t old_size = pos.nPos;
 65          size_t new_size = n_new_chunks * m_chunk_size;
 66          size_t inc_size = new_size - old_size;
 67  
 68          if (CheckDiskSpace(m_dir, inc_size)) {
 69              FILE *file = Open(pos);
 70              if (file) {
 71                  LogDebug(BCLog::VALIDATION, "Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
 72                  AllocateFileRange(file, pos.nPos, inc_size);
 73                  if (fclose(file) != 0) {
 74                      LogError("Cannot close file %s%05u.dat after extending it with %u bytes", m_prefix, pos.nFile, new_size);
 75                      return 0;
 76                  }
 77                  return inc_size;
 78              }
 79          } else {
 80              out_of_space = true;
 81          }
 82      }
 83      return 0;
 84  }
 85  
 86  bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize) const
 87  {
 88      FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
 89      if (!file) {
 90          LogError("%s: failed to open file %d\n", __func__, pos.nFile);
 91          return false;
 92      }
 93      if (finalize && !TruncateFile(file, pos.nPos)) {
 94          LogError("%s: failed to truncate file %d\n", __func__, pos.nFile);
 95          if (fclose(file) != 0) {
 96              LogError("Failed to close file %d", pos.nFile);
 97          }
 98          return false;
 99      }
100      if (!FileCommit(file)) {
101          LogError("%s: failed to commit file %d\n", __func__, pos.nFile);
102          if (fclose(file) != 0) {
103              LogError("Failed to close file %d", pos.nFile);
104          }
105          return false;
106      }
107      DirectoryCommit(m_dir);
108  
109      if (fclose(file) != 0) {
110          LogError("Failed to close file %d after flush", pos.nFile);
111          return false;
112      }
113      return true;
114  }