mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 03:52:49 +01:00
2bacbcf1fd
ef712298c3f8bc2afdad783f05080443b72b3f77 util: Check for file being NULL in DirectoryCommit (Luke Dashjr) 457490403853321d308c6ca6aaa90d6f8f29b4cf Fix possible data race when committing block files (Evan Klitzke) 220bb16cbee5b91d0bc0fcc6c71560d631295fa5 util: Introduce DirectoryCommit commit function to sync a directory (Evan Klitzke) ce5cbaea63ad4ea78e533bdb14f47f414061ae7f util.h: Document FileCommit function (Evan Klitzke) 844d650eea3bd809884cc5dd996a388bdc58314e util: Prefer Mac-specific F_FULLSYNC over fdatasync in FileCommit (Evan Klitzke) f6cec0bcaf560fa310853ad3fe17022602b63d5f util: Refactor FileCommit from an #if sequence nested in #else, to a sequence of #elif (Evan Klitzke) Pull request description: Reviving #12696 ACKs for top commit: laanwj: Code review ACK ef712298c3f8bc2afdad783f05080443b72b3f77 Tree-SHA512: 07d650990ef4c18d645dee3f9a199a940683ad17557d79d93979a76c4e710d8d70e6eae01d1a5991494a24a7654eb7db868be0c34a31e70b2509945d95bc9cce
100 lines
3.0 KiB
C++
100 lines
3.0 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
|
// 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>
|
|
#include <logging.h>
|
|
#include <tinyformat.h>
|
|
#include <util/system.h>
|
|
|
|
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");
|
|
}
|
|
}
|
|
|
|
std::string FlatFilePos::ToString() const
|
|
{
|
|
return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
|
|
}
|
|
|
|
fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const
|
|
{
|
|
return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile);
|
|
}
|
|
|
|
FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only)
|
|
{
|
|
if (pos.IsNull()) {
|
|
return nullptr;
|
|
}
|
|
fs::path path = FileName(pos);
|
|
fs::create_directories(path.parent_path());
|
|
FILE* file = fsbridge::fopen(path, read_only ? "rb": "rb+");
|
|
if (!file && !read_only)
|
|
file = fsbridge::fopen(path, "wb+");
|
|
if (!file) {
|
|
LogPrintf("Unable to open file %s\n", path.string());
|
|
return nullptr;
|
|
}
|
|
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;
|
|
}
|
|
return file;
|
|
}
|
|
|
|
size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space)
|
|
{
|
|
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;
|
|
}
|
|
|
|
bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize)
|
|
{
|
|
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);
|
|
}
|
|
DirectoryCommit(m_dir);
|
|
|
|
fclose(file);
|
|
return true;
|
|
}
|