Skip to content

Commit

Permalink
torcontrol: Use fs::path instead of std::string for private key path
Browse files Browse the repository at this point in the history
Cherry-picked from: 75594bd
  • Loading branch information
laanwj authored and patricklodder committed Mar 22, 2024
1 parent b5538a5 commit c7de0da
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/torcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
* (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)
return std::make_pair(false,"");
std::string retval;
Expand All @@ -334,9 +334,9 @@ static std::pair<bool,std::string> ReadBinaryFile(const std::string &filename, s
/** Write contents of std::string to a file.
* @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)
return false;
if (fwrite(data.data(), 1, data.size(), f) != data.size()) {
Expand All @@ -358,8 +358,8 @@ class TorController
TorController(struct event_base* base, const std::string& target);
~TorController();

/** Get name for file to store private key in */
std::string GetPrivateKeyFile();
/** Get name fo file to store private key in */
fs::path GetPrivateKeyFile();

/** Reconnect, after getting disconnected */
void Reconnect();
Expand Down Expand Up @@ -412,7 +412,7 @@ TorController::TorController(struct event_base* _base, const std::string& _targe
// Read service private key if cached
std::pair<bool,std::string> pkf = ReadBinaryFile(GetPrivateKeyFile());
if (pkf.first) {
LogPrint("tor", "tor: Reading cached private key from %s\n", GetPrivateKeyFile());
LogPrint("tor", "tor: Reading cached private key from %s\n", GetPrivateKeyFile().string());
private_key = pkf.second;
}
}
Expand Down Expand Up @@ -443,9 +443,9 @@ void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlRe
service = LookupNumeric(std::string(service_id+".onion"), GetListenPort());
LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString());
if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) {
LogPrint("tor", "tor: Cached service private key to %s\n", GetPrivateKeyFile());
LogPrint("tor", "tor: Cached service private key to %s\n", GetPrivateKeyFile().string());
} 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);
// ... onion requested - keep connection open
Expand Down Expand Up @@ -665,9 +665,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)
Expand Down Expand Up @@ -720,4 +720,3 @@ void StopTorControl()
base = 0;
}
}

0 comments on commit c7de0da

Please sign in to comment.