Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2021 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/system.h> 12 : 13 3836020 : FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) : 14 1918010 : m_dir(std::move(dir)), 15 1918010 : m_prefix(prefix), 16 1918010 : m_chunk_size(chunk_size) 17 1918010 : { 18 1918010 : if (chunk_size == 0) { 19 0 : throw std::invalid_argument("chunk_size must be positive"); 20 : } 21 3836020 : } 22 : 23 137 : std::string FlatFilePos::ToString() const 24 : { 25 137 : return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos); 26 : } 27 : 28 1591381 : fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const 29 : { 30 1591381 : return m_dir / fs::u8path(strprintf("%s%05u.dat", m_prefix, pos.nFile)); 31 0 : } 32 : 33 1591394 : FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only) 34 : { 35 1591394 : if (pos.IsNull()) { 36 135 : return nullptr; 37 : } 38 1591259 : fs::path path = FileName(pos); 39 1591259 : fs::create_directories(path.parent_path()); 40 1591238 : FILE* file = fsbridge::fopen(path, read_only ? "rb": "rb+"); 41 1591228 : if (!file && !read_only) 42 2374 : file = fsbridge::fopen(path, "wb+"); 43 1591228 : if (!file) { 44 20 : LogPrintf("Unable to open file %s\n", fs::PathToString(path)); 45 20 : return nullptr; 46 : } 47 1591208 : if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) { 48 0 : LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, fs::PathToString(path)); 49 0 : fclose(file); 50 0 : return nullptr; 51 : } 52 1591219 : return file; 53 1591416 : } 54 : 55 631804 : size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) 56 : { 57 631804 : out_of_space = false; 58 : 59 631804 : unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size; 60 631804 : unsigned int n_new_chunks = (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size; 61 631804 : if (n_new_chunks > n_old_chunks) { 62 7314 : size_t old_size = pos.nPos; 63 7314 : size_t new_size = n_new_chunks * m_chunk_size; 64 7314 : size_t inc_size = new_size - old_size; 65 : 66 7314 : if (CheckDiskSpace(m_dir, inc_size)) { 67 7314 : FILE *file = Open(pos); 68 7314 : if (file) { 69 7314 : LogPrint(BCLog::VALIDATION, "Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile); 70 7314 : AllocateFileRange(file, pos.nPos, inc_size); 71 7314 : fclose(file); 72 7314 : return inc_size; 73 : } 74 0 : } else { 75 0 : out_of_space = true; 76 : } 77 0 : } 78 624490 : return 0; 79 631804 : } 80 : 81 15098 : bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize) 82 : { 83 15098 : FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos 84 15098 : if (!file) { 85 0 : return error("%s: failed to open file %d", __func__, pos.nFile); 86 : } 87 15098 : if (finalize && !TruncateFile(file, pos.nPos)) { 88 0 : fclose(file); 89 0 : return error("%s: failed to truncate file %d", __func__, pos.nFile); 90 : } 91 15098 : if (!FileCommit(file)) { 92 0 : fclose(file); 93 0 : return error("%s: failed to commit file %d", __func__, pos.nFile); 94 : } 95 15098 : DirectoryCommit(m_dir); 96 : 97 15098 : fclose(file); 98 15098 : return true; 99 15098 : }