remove some string conversions entirely

This commit is contained in:
Rick V 2020-05-26 22:42:01 -05:00
parent 5529371637
commit 1340cd0dce
6 changed files with 19 additions and 20 deletions

View File

@ -186,7 +186,7 @@ namespace llarp
bool bool
KeyManager::backupKeyFilesByMoving() const KeyManager::backupKeyFilesByMoving() const
{ {
std::vector<std::string> files = {m_rcPath.string(), m_idKeyPath.string(), m_encKeyPath.string(), m_transportKeyPath.string()}; std::vector<fs::path> files = {m_rcPath, m_idKeyPath, m_encKeyPath, m_transportKeyPath};
for (auto& filepath : files) for (auto& filepath : files)
{ {
@ -216,7 +216,7 @@ namespace llarp
LogInfo("Generating new key", filepath); LogInfo("Generating new key", filepath);
keygen(key); keygen(key);
if (!key.SaveToFile(filepath.string().c_str())) if (!key.SaveToFile(filepath))
{ {
LogError("Failed to save new key"); LogError("Failed to save new key");
return false; return false;
@ -224,7 +224,7 @@ namespace llarp
} }
LogDebug("Loading key from file ", filepath); LogDebug("Loading key from file ", filepath);
return key.LoadFromFile(filepath.string().c_str()); return key.LoadFromFile(filepath);
} }
bool bool

View File

@ -27,9 +27,9 @@ namespace llarp
} }
bool bool
SecretKey::LoadFromFile(const char* fname) SecretKey::LoadFromFile(const fs::path& fname)
{ {
std::ifstream f(fname, std::ios::in | std::ios::binary); std::ifstream f(fname.string(), std::ios::in | std::ios::binary);
if (!f.is_open()) if (!f.is_open())
{ {
return false; return false;
@ -89,7 +89,7 @@ namespace llarp
} }
bool bool
SecretKey::SaveToFile(const char* fname) const SecretKey::SaveToFile(const fs::path& fname) const
{ {
std::array<byte_t, 128> tmp; std::array<byte_t, 128> tmp;
llarp_buffer_t buf(tmp); llarp_buffer_t buf(tmp);
@ -97,8 +97,7 @@ namespace llarp
{ {
return false; return false;
} }
const fs::path fpath = std::string(fname); auto optional_f = llarp::util::OpenFileStream<std::ofstream>(fname, std::ios::binary);
auto optional_f = llarp::util::OpenFileStream<std::ofstream>(fpath, std::ios::binary);
if (!optional_f) if (!optional_f)
return false; return false;
auto& f = *optional_f; auto& f = *optional_f;
@ -109,10 +108,9 @@ namespace llarp
} }
bool bool
IdentitySecret::LoadFromFile(const char* fname) IdentitySecret::LoadFromFile(const fs::path& fname)
{ {
const fs::path fpath = std::string(fname); auto optional = util::OpenFileStream<std::ifstream>(fname, std::ios::binary | std::ios::in);
auto optional = util::OpenFileStream<std::ifstream>(fpath, std::ios::binary | std::ios::in);
if (!optional) if (!optional)
return false; return false;
auto& f = *optional; auto& f = *optional;

View File

@ -5,6 +5,7 @@
#include <router_id.hpp> #include <router_id.hpp>
#include <util/aligned.hpp> #include <util/aligned.hpp>
#include <util/types.hpp> #include <util/types.hpp>
#include <util/fs.hpp>
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
@ -122,10 +123,10 @@ namespace llarp
toPrivate(PrivateKey& key) const; toPrivate(PrivateKey& key) const;
bool bool
LoadFromFile(const char* fname); LoadFromFile(const fs::path& fname);
bool bool
SaveToFile(const char* fname) const; SaveToFile(const fs::path& fname) const;
}; };
inline std::ostream& inline std::ostream&
@ -204,7 +205,7 @@ namespace llarp
/// load service node seed from file /// load service node seed from file
bool bool
LoadFromFile(const char* fname); LoadFromFile(const fs::path& fname);
}; };
inline std::ostream& inline std::ostream&

View File

@ -301,12 +301,12 @@ llarp_nodedb::loadfile(const fs::path& fpath)
llarp::RouterContact rc; llarp::RouterContact rc;
if (!rc.Read(fpath)) if (!rc.Read(fpath))
{ {
llarp::LogError("failed to read file ", fpath.string()); llarp::LogError("failed to read file ", fpath);
return false; return false;
} }
if (!rc.Verify(llarp::time_now_ms())) if (!rc.Verify(llarp::time_now_ms()))
{ {
llarp::LogError(fpath.string(), " contains invalid RC"); llarp::LogError(fpath, " contains invalid RC");
return false; return false;
} }
{ {

View File

@ -473,7 +473,7 @@ namespace llarp
} }
if (isListFile) if (isListFile)
{ {
if (not BDecodeReadFile(router.string().c_str(), b_list)) if (not BDecodeReadFile(router, b_list))
{ {
throw std::runtime_error(stringify("failed to read bootstrap list file '", router, "'")); throw std::runtime_error(stringify("failed to read bootstrap list file '", router, "'"));
} }
@ -481,7 +481,7 @@ namespace llarp
else else
{ {
RouterContact rc; RouterContact rc;
if (not rc.Read(router.string().c_str())) if (not rc.Read(router))
{ {
throw std::runtime_error( throw std::runtime_error(
stringify("failed to decode bootstrap RC, file='", router, "' rc=", rc)); stringify("failed to decode bootstrap RC, file='", router, "' rc=", rc));

View File

@ -296,12 +296,12 @@ namespace llarp
/// read entire file and decode its contents into t /// read entire file and decode its contents into t
template <typename T> template <typename T>
bool bool
BDecodeReadFile(const char* fpath, T& t) BDecodeReadFile(const fs::path& fpath, T& t)
{ {
std::vector<byte_t> ptr; std::vector<byte_t> ptr;
{ {
std::ifstream f; std::ifstream f;
f.open(fpath); f.open(fpath.string());
if (!f.is_open()) if (!f.is_open())
{ {
return false; return false;