Skip to content

Commit 4cba2fd

Browse files
committed
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.
1 parent ad89812 commit 4cba2fd

File tree

4 files changed

+78
-46
lines changed

4 files changed

+78
-46
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ BITCOIN_CORE_H = \
242242
util/message.h \
243243
util/moneystr.h \
244244
util/rbf.h \
245+
util/readwritefile.h \
245246
util/ref.h \
246247
util/settings.h \
247248
util/sock.h \
@@ -572,6 +573,7 @@ libbitcoin_util_a_SOURCES = \
572573
util/message.cpp \
573574
util/moneystr.cpp \
574575
util/rbf.cpp \
576+
util/readwritefile.cpp \
575577
util/settings.cpp \
576578
util/threadnames.cpp \
577579
util/spanparsing.cpp \

src/torcontrol.cpp

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <net.h>
1313
#include <netaddress.h>
1414
#include <netbase.h>
15+
#include <util/readwritefile.h>
1516
#include <util/strencodings.h>
1617
#include <util/system.h>
1718
#include <util/time.h>
@@ -362,52 +363,6 @@ std::map<std::string,std::string> ParseTorReplyMapping(const std::string &s)
362363
return mapping;
363364
}
364365

365-
/** Read full contents of a file and return them in a std::string.
366-
* Returns a pair <status, string>.
367-
* If an error occurred, status will be false, otherwise status will be true and the data will be returned in string.
368-
*
369-
* @param maxsize Puts a maximum size limit on the file that is read. If the file is larger than this, truncated data
370-
* (with len > maxsize) will be returned.
371-
*/
372-
static std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits<size_t>::max())
373-
{
374-
FILE *f = fsbridge::fopen(filename, "rb");
375-
if (f == nullptr)
376-
return std::make_pair(false,"");
377-
std::string retval;
378-
char buffer[128];
379-
size_t n;
380-
while ((n=fread(buffer, 1, sizeof(buffer), f)) > 0) {
381-
// Check for reading errors so we don't return any data if we couldn't
382-
// read the entire file (or up to maxsize)
383-
if (ferror(f)) {
384-
fclose(f);
385-
return std::make_pair(false,"");
386-
}
387-
retval.append(buffer, buffer+n);
388-
if (retval.size() > maxsize)
389-
break;
390-
}
391-
fclose(f);
392-
return std::make_pair(true,retval);
393-
}
394-
395-
/** Write contents of std::string to a file.
396-
* @return true on success.
397-
*/
398-
static bool WriteBinaryFile(const fs::path &filename, const std::string &data)
399-
{
400-
FILE *f = fsbridge::fopen(filename, "wb");
401-
if (f == nullptr)
402-
return false;
403-
if (fwrite(data.data(), 1, data.size(), f) != data.size()) {
404-
fclose(f);
405-
return false;
406-
}
407-
fclose(f);
408-
return true;
409-
}
410-
411366
/****** Bitcoin specific TorController implementation ********/
412367

413368
/** Controller that connects to Tor control socket, authenticate, then create

src/util/readwritefile.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2015-2020 The Bitcoin Core developers
2+
// Copyright (c) 2017 The Zcash developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#include <fs.h>
7+
8+
#include <limits>
9+
#include <stdio.h>
10+
#include <string>
11+
#include <utility>
12+
13+
std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits<size_t>::max())
14+
{
15+
FILE *f = fsbridge::fopen(filename, "rb");
16+
if (f == nullptr)
17+
return std::make_pair(false,"");
18+
std::string retval;
19+
char buffer[128];
20+
size_t n;
21+
while ((n=fread(buffer, 1, sizeof(buffer), f)) > 0) {
22+
// Check for reading errors so we don't return any data if we couldn't
23+
// read the entire file (or up to maxsize)
24+
if (ferror(f)) {
25+
fclose(f);
26+
return std::make_pair(false,"");
27+
}
28+
retval.append(buffer, buffer+n);
29+
if (retval.size() > maxsize)
30+
break;
31+
}
32+
fclose(f);
33+
return std::make_pair(true,retval);
34+
}
35+
36+
bool WriteBinaryFile(const fs::path &filename, const std::string &data)
37+
{
38+
FILE *f = fsbridge::fopen(filename, "wb");
39+
if (f == nullptr)
40+
return false;
41+
if (fwrite(data.data(), 1, data.size(), f) != data.size()) {
42+
fclose(f);
43+
return false;
44+
}
45+
fclose(f);
46+
return true;
47+
}

src/util/readwritefile.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2015-2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_UTIL_READWRITEFILE_H
6+
#define BITCOIN_UTIL_READWRITEFILE_H
7+
8+
#include <fs.h>
9+
10+
#include <limits>
11+
#include <string>
12+
#include <utility>
13+
14+
/** Read full contents of a file and return them in a std::string.
15+
* Returns a pair <status, string>.
16+
* If an error occurred, status will be false, otherwise status will be true and the data will be returned in string.
17+
*
18+
* @param maxsize Puts a maximum size limit on the file that is read. If the file is larger than this, truncated data
19+
* (with len > maxsize) will be returned.
20+
*/
21+
std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits<size_t>::max());
22+
23+
/** Write contents of std::string to a file.
24+
* @return true on success.
25+
*/
26+
bool WriteBinaryFile(const fs::path &filename, const std::string &data);
27+
28+
#endif /* BITCOIN_UTIL_READWRITEFILE_H */

0 commit comments

Comments
 (0)