forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validation: Extract basic block file logic into FlatFileSeq class.
- Loading branch information
Showing
7 changed files
with
107 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 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.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"); | ||
} | ||
} | ||
|
||
fs::path FlatFileSeq::FileName(const CDiskBlockPos& pos) const | ||
{ | ||
return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile); | ||
} | ||
|
||
FILE* FlatFileSeq::Open(const CDiskBlockPos& pos, bool fReadOnly) | ||
{ | ||
if (pos.IsNull()) | ||
return nullptr; | ||
fs::path path = FileName(pos); | ||
fs::create_directories(path.parent_path()); | ||
FILE* file = fsbridge::fopen(path, fReadOnly ? "rb": "rb+"); | ||
if (!file && !fReadOnly) | ||
file = fsbridge::fopen(path, "wb+"); | ||
if (!file) { | ||
LogPrintf("Unable to open file %s\n", path.string()); | ||
return nullptr; | ||
} | ||
if (pos.nPos) { | ||
if (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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 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. | ||
|
||
#ifndef BITCOIN_FLATFILE_H | ||
#define BITCOIN_FLATFILE_H | ||
|
||
#include "chain.h" | ||
#include "fs.h" | ||
|
||
/** | ||
* 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 CDiskBlockPos& pos) const; | ||
|
||
/** Open a handle to the file at the given position. */ | ||
FILE* Open(const CDiskBlockPos& pos, bool fReadOnly = false); | ||
}; | ||
|
||
#endif // BITCOIN_FLATFILE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters