2019-01-06 20:43:38 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
2019-01-06 20:06:31 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include <flatfile.h>
|
2019-01-06 20:14:22 +01:00
|
|
|
#include <logging.h>
|
2019-01-06 20:06:31 +01:00
|
|
|
#include <tinyformat.h>
|
2019-01-06 20:27:31 +01:00
|
|
|
#include <util/system.h>
|
2019-01-06 20:06:31 +01:00
|
|
|
|
|
|
|
FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
|
|
|
|
m_dir(std::move(dir)),
|
|
|
|
m_prefix(prefix),
|
|
|
|
m_chunk_size(chunk_size)
|
|
|
|
{
|
|
|
|
if (chunk_size == 0) {
|
|
|
|
throw std::invalid_argument("chunk_size must be positive");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:46:30 +01:00
|
|
|
std::string FlatFilePos::ToString() const
|
2019-01-06 20:43:38 +01:00
|
|
|
{
|
2019-01-06 20:46:30 +01:00
|
|
|
return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
|
2019-01-06 20:43:38 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 20:46:30 +01:00
|
|
|
fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const
|
2019-01-06 20:06:31 +01:00
|
|
|
{
|
|
|
|
return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile);
|
|
|
|
}
|
2019-01-06 20:14:22 +01:00
|
|
|
|
2019-01-24 20:20:57 +01:00
|
|
|
FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only)
|
2019-01-06 20:14:22 +01:00
|
|
|
{
|
2019-01-24 20:20:57 +01:00
|
|
|
if (pos.IsNull()) {
|
2019-01-06 20:14:22 +01:00
|
|
|
return nullptr;
|
2019-01-24 20:20:57 +01:00
|
|
|
}
|
2019-01-06 20:14:22 +01:00
|
|
|
fs::path path = FileName(pos);
|
|
|
|
fs::create_directories(path.parent_path());
|
2019-01-24 20:20:57 +01:00
|
|
|
FILE* file = fsbridge::fopen(path, read_only ? "rb": "rb+");
|
|
|
|
if (!file && !read_only)
|
2019-01-06 20:14:22 +01:00
|
|
|
file = fsbridge::fopen(path, "wb+");
|
|
|
|
if (!file) {
|
|
|
|
LogPrintf("Unable to open file %s\n", path.string());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-01-24 20:20:57 +01:00
|
|
|
if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) {
|
|
|
|
LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
|
|
|
|
fclose(file);
|
|
|
|
return nullptr;
|
2019-01-06 20:14:22 +01:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
2019-01-06 20:27:31 +01:00
|
|
|
|
2019-01-06 20:46:30 +01:00
|
|
|
size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space)
|
2019-01-06 20:27:31 +01:00
|
|
|
{
|
|
|
|
out_of_space = false;
|
|
|
|
|
|
|
|
unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size;
|
|
|
|
unsigned int n_new_chunks = (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size;
|
|
|
|
if (n_new_chunks > n_old_chunks) {
|
|
|
|
size_t old_size = pos.nPos;
|
|
|
|
size_t new_size = n_new_chunks * m_chunk_size;
|
|
|
|
size_t inc_size = new_size - old_size;
|
|
|
|
|
|
|
|
if (CheckDiskSpace(m_dir, inc_size)) {
|
|
|
|
FILE *file = Open(pos);
|
|
|
|
if (file) {
|
|
|
|
LogPrintf("Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
|
|
|
|
AllocateFileRange(file, pos.nPos, inc_size);
|
|
|
|
fclose(file);
|
|
|
|
return inc_size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out_of_space = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-06 19:14:35 +01:00
|
|
|
|
2019-01-06 20:46:30 +01:00
|
|
|
bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize)
|
2019-01-06 19:14:35 +01:00
|
|
|
{
|
|
|
|
FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
|
|
|
|
if (!file) {
|
|
|
|
return error("%s: failed to open file %d", __func__, pos.nFile);
|
|
|
|
}
|
|
|
|
if (finalize && !TruncateFile(file, pos.nPos)) {
|
|
|
|
fclose(file);
|
|
|
|
return error("%s: failed to truncate file %d", __func__, pos.nFile);
|
|
|
|
}
|
|
|
|
if (!FileCommit(file)) {
|
|
|
|
fclose(file);
|
|
|
|
return error("%s: failed to commit file %d", __func__, pos.nFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
return true;
|
|
|
|
}
|