profiles.dat is always relative so if you spawn lokinet in / then it will error about permissions. (#1449)

* use absolute path for profiles.dat.

* remove old bencoding bits to use const fs::path instead of const char *
pull/1451/head
Jeff 4 years ago committed by GitHub
parent 30d7662ef2
commit 34c7f0da0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -239,7 +239,7 @@ namespace llarp
}
bool
Profiling::Save(const char* fname)
Profiling::Save(const fs::path fpath)
{
std::shared_lock lock{m_ProfilesMutex};
size_t sz = (m_Profiles.size() * (RouterProfile::MaxSize + 32 + 8)) + 8;
@ -250,7 +250,6 @@ namespace llarp
if (res)
{
buf.sz = buf.cur - buf.base;
const fs::path fpath = std::string(fname);
auto optional_f = util::OpenFileStream<std::ofstream>(fpath, std::ios::binary);
if (!optional_f)
return false;
@ -302,11 +301,17 @@ namespace llarp
}
bool
Profiling::Load(const char* fname)
Profiling::BDecode(llarp_buffer_t* buf)
{
return bencode_decode_dict(*this, buf);
}
bool
Profiling::Load(const fs::path fname)
{
util::Lock lock(m_ProfilesMutex);
m_Profiles.clear();
if (!BDecodeReadFromFile(fname, *this))
if (!BDecodeReadFile(fname, *this))
{
llarp::LogWarn("failed to load router profiles from ", fname);
return false;

@ -90,15 +90,18 @@ namespace llarp
bool
BEncode(llarp_buffer_t* buf) const EXCLUDES(m_ProfilesMutex);
bool
BDecode(llarp_buffer_t* buf);
bool
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf) NO_THREAD_SAFETY_ANALYSIS;
// disabled because we do load -> bencode::BDecodeReadFromFile -> DecodeKey
bool
Load(const char* fname) EXCLUDES(m_ProfilesMutex);
Load(const fs::path fname) EXCLUDES(m_ProfilesMutex);
bool
Save(const char* fname) EXCLUDES(m_ProfilesMutex);
Save(const fs::path fname) EXCLUDES(m_ProfilesMutex);
bool
ShouldSave(llarp_time_t now) const;

@ -612,11 +612,22 @@ namespace llarp
_linkManager.AddLink(std::move(server), true);
}
// profiling
_profilesFile = conf.router.m_dataDir / "profiles.dat";
// Network config
if (conf.network.m_enableProfiling.value_or(false))
{
LogInfo("router profiling enabled; loading from ", routerProfilesFile);
routerProfiling().Load(routerProfilesFile.c_str());
LogInfo("router profiling enabled");
if (not fs::exists(_profilesFile))
{
LogInfo("no profiles file at ", _profilesFile, " skipping");
}
else
{
LogInfo("loading router profiles from ", _profilesFile);
routerProfiling().Load(_profilesFile);
}
}
else
{
@ -816,7 +827,7 @@ namespace llarp
// save profiles
if (routerProfiling().ShouldSave(now))
{
QueueDiskIO([&]() { routerProfiling().Save(routerProfilesFile.c_str()); });
QueueDiskIO([&]() { routerProfiling().Save(_profilesFile); });
}
// save nodedb
if (nodedb()->ShouldSaveToDisk(now))
@ -964,8 +975,6 @@ namespace llarp
if (_running || _stopping)
return false;
routerProfiling().Load(routerProfilesFile.c_str());
// set public signing key
_rc.pubkey = seckey_topublic(identity());
// set router version if service node

@ -273,10 +273,8 @@ namespace llarp
std::shared_ptr<rpc::LokidRpcClient> m_lokidRpcClient;
lokimq::address lokidRPCAddr;
Profiling _routerProfiling;
std::string routerProfilesFile = "profiles.dat";
fs::path _profilesFile;
OutboundMessageHandler _outboundMessageHandler;
OutboundSessionMaker _outboundSessionMaker;
LinkManager _linkManager;

@ -296,7 +296,7 @@ namespace llarp
/// read entire file and decode its contents into t
template <typename T>
bool
BDecodeReadFile(const fs::path& fpath, T& t)
BDecodeReadFile(const fs::path fpath, T& t)
{
std::vector<byte_t> ptr;
{
@ -316,39 +316,10 @@ namespace llarp
return t.BDecode(&buf);
}
/// read entire file and decode its contents into t
template <typename T>
bool
BDecodeReadFromFile(const char* fpath, T& t)
{
std::vector<byte_t> ptr;
{
std::ifstream f;
f.open(fpath);
if (!f.is_open())
{
return false;
}
f.seekg(0, std::ios::end);
const std::streampos sz = f.tellg();
f.seekg(0, std::ios::beg);
ptr.resize(sz);
f.read((char*)ptr.data(), sz);
}
llarp_buffer_t buf(ptr);
auto result = bencode_decode_dict(t, &buf);
if (!result)
{
LogError("BDecodeReadFromFile() failed for file ", fpath, " contents:");
DumpBuffer(buf);
}
return result;
}
/// bencode and write to file
template <typename T, size_t bufsz>
bool
BEncodeWriteFile(const char* fpath, const T& t)
BEncodeWriteFile(const fs::path fpath, const T& t)
{
std::array<byte_t, bufsz> tmp;
llarp_buffer_t buf(tmp);
@ -356,8 +327,7 @@ namespace llarp
return false;
buf.sz = buf.cur - buf.base;
{
const fs::path path = std::string(fpath);
auto f = llarp::util::OpenFileStream<std::ofstream>(path, std::ios::binary);
auto f = llarp::util::OpenFileStream<std::ofstream>(fpath, std::ios::binary);
if (not f or not f->is_open())
return false;
f->write((char*)buf.base, buf.sz);

Loading…
Cancel
Save