torcontrol: Use fs::path instead of std::string for private key path

This commit is contained in:
Wladimir J. van der Laan 2017-03-02 07:25:32 +01:00
parent 2a5f574762
commit 75594bd7f2

View File

@ -314,9 +314,9 @@ static std::map<std::string,std::string> ParseTorReplyMapping(const std::string
* @param maxsize Puts a maximum size limit on the file that is read. If the file is larger than this, truncated data * @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. * (with len > maxsize) will be returned.
*/ */
static std::pair<bool,std::string> ReadBinaryFile(const std::string &filename, size_t maxsize=std::numeric_limits<size_t>::max()) static std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits<size_t>::max())
{ {
FILE *f = fopen(filename.c_str(), "rb"); FILE *f = fsbridge::fopen(filename, "rb");
if (f == NULL) if (f == NULL)
return std::make_pair(false,""); return std::make_pair(false,"");
std::string retval; std::string retval;
@ -334,9 +334,9 @@ static std::pair<bool,std::string> ReadBinaryFile(const std::string &filename, s
/** Write contents of std::string to a file. /** Write contents of std::string to a file.
* @return true on success. * @return true on success.
*/ */
static bool WriteBinaryFile(const std::string &filename, const std::string &data) static bool WriteBinaryFile(const fs::path &filename, const std::string &data)
{ {
FILE *f = fopen(filename.c_str(), "wb"); FILE *f = fsbridge::fopen(filename, "wb");
if (f == NULL) if (f == NULL)
return false; return false;
if (fwrite(data.data(), 1, data.size(), f) != data.size()) { if (fwrite(data.data(), 1, data.size(), f) != data.size()) {
@ -359,7 +359,7 @@ public:
~TorController(); ~TorController();
/** Get name fo file to store private key in */ /** Get name fo file to store private key in */
std::string GetPrivateKeyFile(); fs::path GetPrivateKeyFile();
/** Reconnect, after getting disconnected */ /** Reconnect, after getting disconnected */
void Reconnect(); void Reconnect();
@ -411,7 +411,7 @@ TorController::TorController(struct event_base* _base, const std::string& _targe
// Read service private key if cached // Read service private key if cached
std::pair<bool,std::string> pkf = ReadBinaryFile(GetPrivateKeyFile()); std::pair<bool,std::string> pkf = ReadBinaryFile(GetPrivateKeyFile());
if (pkf.first) { if (pkf.first) {
LogPrint(BCLog::TOR, "tor: Reading cached private key from %s\n", GetPrivateKeyFile()); LogPrint(BCLog::TOR, "tor: Reading cached private key from %s\n", GetPrivateKeyFile().string());
private_key = pkf.second; private_key = pkf.second;
} }
} }
@ -442,9 +442,9 @@ void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlRe
service = LookupNumeric(std::string(service_id+".onion").c_str(), GetListenPort()); service = LookupNumeric(std::string(service_id+".onion").c_str(), GetListenPort());
LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString()); LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString());
if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) { if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) {
LogPrint(BCLog::TOR, "tor: Cached service private key to %s\n", GetPrivateKeyFile()); LogPrint(BCLog::TOR, "tor: Cached service private key to %s\n", GetPrivateKeyFile().string());
} else { } else {
LogPrintf("tor: Error writing service private key to %s\n", GetPrivateKeyFile()); LogPrintf("tor: Error writing service private key to %s\n", GetPrivateKeyFile().string());
} }
AddLocal(service, LOCAL_MANUAL); AddLocal(service, LOCAL_MANUAL);
// ... onion requested - keep connection open // ... onion requested - keep connection open
@ -651,9 +651,9 @@ void TorController::Reconnect()
} }
} }
std::string TorController::GetPrivateKeyFile() fs::path TorController::GetPrivateKeyFile()
{ {
return (GetDataDir() / "onion_private_key").string(); return GetDataDir() / "onion_private_key";
} }
void TorController::reconnect_cb(evutil_socket_t fd, short what, void *arg) void TorController::reconnect_cb(evutil_socket_t fd, short what, void *arg)