mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-13 05:12:34 -03:00
369d4c03b7
00e9b97f37
refactor: Move fs.* to util/fs.* (TheCharlatan)106b46d9d2
Add missing fs.h includes (TheCharlatan)b202b3dd63
Add missing cstddef include in assumptions.h (TheCharlatan)18fb36367a
refactor: Extract util/fs_helpers from util/system (Ben Woosley) Pull request description: This pull request is part of the `libbitcoinkernel` project https://github.com/bitcoin/bitcoin/issues/24303 https://github.com/bitcoin/bitcoin/projects/18 and more specifically its "Step 2: Decouple most non-consensus code from libbitcoinkernel". This commit was originally authored by empact and is taken from its parent PR #25152. #### Context There is an ongoing effort to decouple the `ArgsManager` used for command line parsing user-provided arguments from the libbitcoinkernel library (https://github.com/bitcoin/bitcoin/pull/25290, https://github.com/bitcoin/bitcoin/pull/25487, https://github.com/bitcoin/bitcoin/pull/25527, https://github.com/bitcoin/bitcoin/pull/25862, https://github.com/bitcoin/bitcoin/pull/26177, and https://github.com/bitcoin/bitcoin/pull/27125). The `ArgsManager` is defined in `system.h`. A similar pull request extracting functionality from `system.h` has been merged in https://github.com/bitcoin/bitcoin/pull/27238. #### Changes Next to providing better code organization, this PR removes some reliance of the tree of libbitcoinkernel header includes on `system.h` (and thus the `ArgsManager` definition) by moving filesystem related functions out of the `system.*` files. There is already a pair of `fs.h` / `fs.cpp` in the top-level `src/` directory. They were not combined with the files introduced here, to keep the patch cleaner and more importantly because they are often included without the utility functions. The new files are therefore named `fs_helpers` and the existing `fs` files are moved into the util directory. Further commits splitting more functionality out of `system.h` are still in #25152 and will be submitted in separate PRs once this PR has been processed. ACKs for top commit: hebasto: ACK00e9b97f37
Tree-SHA512: 31422f148d14ba3c843b99b1550a6fd77c77f350905ca324f93d4f97b652246bc58fa9696c64d1201979cf88733e40be02d262739bb7d417cf22bf506fdb7666
89 lines
2.8 KiB
C++
89 lines
2.8 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_FLATFILE_H
|
|
#define BITCOIN_FLATFILE_H
|
|
|
|
#include <string>
|
|
|
|
#include <serialize.h>
|
|
#include <util/fs.h>
|
|
|
|
struct FlatFilePos
|
|
{
|
|
int nFile{-1};
|
|
unsigned int nPos{0};
|
|
|
|
SERIALIZE_METHODS(FlatFilePos, obj) { READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED), VARINT(obj.nPos)); }
|
|
|
|
FlatFilePos() {}
|
|
|
|
FlatFilePos(int nFileIn, unsigned int nPosIn) :
|
|
nFile(nFileIn),
|
|
nPos(nPosIn)
|
|
{}
|
|
|
|
friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) {
|
|
return (a.nFile == b.nFile && a.nPos == b.nPos);
|
|
}
|
|
|
|
friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b) {
|
|
return !(a == b);
|
|
}
|
|
|
|
bool IsNull() const { return (nFile == -1); }
|
|
|
|
std::string ToString() const;
|
|
};
|
|
|
|
/**
|
|
* FlatFileSeq represents a sequence of numbered files storing raw data. This class facilitates
|
|
* access to and efficient management of these files.
|
|
*/
|
|
class FlatFileSeq
|
|
{
|
|
private:
|
|
const fs::path m_dir;
|
|
const char* const m_prefix;
|
|
const size_t m_chunk_size;
|
|
|
|
public:
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param dir The base directory that all files live in.
|
|
* @param prefix A short prefix given to all file names.
|
|
* @param chunk_size Disk space is pre-allocated in multiples of this amount.
|
|
*/
|
|
FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size);
|
|
|
|
/** Get the name of the file at the given position. */
|
|
fs::path FileName(const FlatFilePos& pos) const;
|
|
|
|
/** Open a handle to the file at the given position. */
|
|
FILE* Open(const FlatFilePos& pos, bool read_only = false);
|
|
|
|
/**
|
|
* Allocate additional space in a file after the given starting position. The amount allocated
|
|
* will be the minimum multiple of the sequence chunk size greater than add_size.
|
|
*
|
|
* @param[in] pos The starting position that bytes will be allocated after.
|
|
* @param[in] add_size The minimum number of bytes to be allocated.
|
|
* @param[out] out_of_space Whether the allocation failed due to insufficient disk space.
|
|
* @return The number of bytes successfully allocated.
|
|
*/
|
|
size_t Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space);
|
|
|
|
/**
|
|
* Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
|
|
*
|
|
* @param[in] pos The first unwritten position in the file to be flushed.
|
|
* @param[in] finalize True if no more data will be written to this file.
|
|
* @return true on success, false on failure.
|
|
*/
|
|
bool Flush(const FlatFilePos& pos, bool finalize = false);
|
|
};
|
|
|
|
#endif // BITCOIN_FLATFILE_H
|