mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-09 10:43:19 -05:00
util: extract {Read,Write}BinaryFile() to its own files
Extract `ReadBinaryFile()` and `WriteBinaryFile()` from `torcontrol.cpp` to its own `readwritefile.{h,cpp}` files, so that it can be reused from other modules.
This commit is contained in:
parent
ad89812c70
commit
4cba2fdafa
4 changed files with 78 additions and 46 deletions
|
@ -242,6 +242,7 @@ BITCOIN_CORE_H = \
|
||||||
util/message.h \
|
util/message.h \
|
||||||
util/moneystr.h \
|
util/moneystr.h \
|
||||||
util/rbf.h \
|
util/rbf.h \
|
||||||
|
util/readwritefile.h \
|
||||||
util/ref.h \
|
util/ref.h \
|
||||||
util/settings.h \
|
util/settings.h \
|
||||||
util/sock.h \
|
util/sock.h \
|
||||||
|
@ -572,6 +573,7 @@ libbitcoin_util_a_SOURCES = \
|
||||||
util/message.cpp \
|
util/message.cpp \
|
||||||
util/moneystr.cpp \
|
util/moneystr.cpp \
|
||||||
util/rbf.cpp \
|
util/rbf.cpp \
|
||||||
|
util/readwritefile.cpp \
|
||||||
util/settings.cpp \
|
util/settings.cpp \
|
||||||
util/threadnames.cpp \
|
util/threadnames.cpp \
|
||||||
util/spanparsing.cpp \
|
util/spanparsing.cpp \
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include <net.h>
|
#include <net.h>
|
||||||
#include <netaddress.h>
|
#include <netaddress.h>
|
||||||
#include <netbase.h>
|
#include <netbase.h>
|
||||||
|
#include <util/readwritefile.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
#include <util/time.h>
|
#include <util/time.h>
|
||||||
|
@ -362,52 +363,6 @@ std::map<std::string,std::string> ParseTorReplyMapping(const std::string &s)
|
||||||
return mapping;
|
return mapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read full contents of a file and return them in a std::string.
|
|
||||||
* Returns a pair <status, string>.
|
|
||||||
* If an error occurred, status will be false, otherwise status will be true and the data will be returned in string.
|
|
||||||
*
|
|
||||||
* @param maxsize Puts a maximum size limit on the file that is read. If the file is larger than this, truncated data
|
|
||||||
* (with len > maxsize) will be returned.
|
|
||||||
*/
|
|
||||||
static std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits<size_t>::max())
|
|
||||||
{
|
|
||||||
FILE *f = fsbridge::fopen(filename, "rb");
|
|
||||||
if (f == nullptr)
|
|
||||||
return std::make_pair(false,"");
|
|
||||||
std::string retval;
|
|
||||||
char buffer[128];
|
|
||||||
size_t n;
|
|
||||||
while ((n=fread(buffer, 1, sizeof(buffer), f)) > 0) {
|
|
||||||
// Check for reading errors so we don't return any data if we couldn't
|
|
||||||
// read the entire file (or up to maxsize)
|
|
||||||
if (ferror(f)) {
|
|
||||||
fclose(f);
|
|
||||||
return std::make_pair(false,"");
|
|
||||||
}
|
|
||||||
retval.append(buffer, buffer+n);
|
|
||||||
if (retval.size() > maxsize)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
fclose(f);
|
|
||||||
return std::make_pair(true,retval);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Write contents of std::string to a file.
|
|
||||||
* @return true on success.
|
|
||||||
*/
|
|
||||||
static bool WriteBinaryFile(const fs::path &filename, const std::string &data)
|
|
||||||
{
|
|
||||||
FILE *f = fsbridge::fopen(filename, "wb");
|
|
||||||
if (f == nullptr)
|
|
||||||
return false;
|
|
||||||
if (fwrite(data.data(), 1, data.size(), f) != data.size()) {
|
|
||||||
fclose(f);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
fclose(f);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/****** Bitcoin specific TorController implementation ********/
|
/****** Bitcoin specific TorController implementation ********/
|
||||||
|
|
||||||
/** Controller that connects to Tor control socket, authenticate, then create
|
/** Controller that connects to Tor control socket, authenticate, then create
|
||||||
|
|
47
src/util/readwritefile.cpp
Normal file
47
src/util/readwritefile.cpp
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
// Copyright (c) 2015-2020 The Bitcoin Core developers
|
||||||
|
// Copyright (c) 2017 The Zcash developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <fs.h>
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits<size_t>::max())
|
||||||
|
{
|
||||||
|
FILE *f = fsbridge::fopen(filename, "rb");
|
||||||
|
if (f == nullptr)
|
||||||
|
return std::make_pair(false,"");
|
||||||
|
std::string retval;
|
||||||
|
char buffer[128];
|
||||||
|
size_t n;
|
||||||
|
while ((n=fread(buffer, 1, sizeof(buffer), f)) > 0) {
|
||||||
|
// Check for reading errors so we don't return any data if we couldn't
|
||||||
|
// read the entire file (or up to maxsize)
|
||||||
|
if (ferror(f)) {
|
||||||
|
fclose(f);
|
||||||
|
return std::make_pair(false,"");
|
||||||
|
}
|
||||||
|
retval.append(buffer, buffer+n);
|
||||||
|
if (retval.size() > maxsize)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
return std::make_pair(true,retval);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WriteBinaryFile(const fs::path &filename, const std::string &data)
|
||||||
|
{
|
||||||
|
FILE *f = fsbridge::fopen(filename, "wb");
|
||||||
|
if (f == nullptr)
|
||||||
|
return false;
|
||||||
|
if (fwrite(data.data(), 1, data.size(), f) != data.size()) {
|
||||||
|
fclose(f);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
return true;
|
||||||
|
}
|
28
src/util/readwritefile.h
Normal file
28
src/util/readwritefile.h
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
// Copyright (c) 2015-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_UTIL_READWRITEFILE_H
|
||||||
|
#define BITCOIN_UTIL_READWRITEFILE_H
|
||||||
|
|
||||||
|
#include <fs.h>
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
/** Read full contents of a file and return them in a std::string.
|
||||||
|
* Returns a pair <status, string>.
|
||||||
|
* If an error occurred, status will be false, otherwise status will be true and the data will be returned in string.
|
||||||
|
*
|
||||||
|
* @param maxsize Puts a maximum size limit on the file that is read. If the file is larger than this, truncated data
|
||||||
|
* (with len > maxsize) will be returned.
|
||||||
|
*/
|
||||||
|
std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits<size_t>::max());
|
||||||
|
|
||||||
|
/** Write contents of std::string to a file.
|
||||||
|
* @return true on success.
|
||||||
|
*/
|
||||||
|
bool WriteBinaryFile(const fs::path &filename, const std::string &data);
|
||||||
|
|
||||||
|
#endif /* BITCOIN_UTIL_READWRITEFILE_H */
|
Loading…
Add table
Reference in a new issue