continue using llarp::openfilestream

pull/664/head
Jeff Becker 5 years ago
parent bf3212820b
commit ba6f9ed2d7
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -152,9 +152,12 @@ llarp_nodedb::Insert(const llarp::RouterContact &rc)
buf.sz = buf.cur - buf.base;
auto filepath = getRCFilePath(rc.pubkey);
llarp::LogDebug("saving RC.pubkey ", filepath);
std::ofstream ofs(
auto optional_ofs = llarp::util::OpenFileStream< std::ofstream >(
filepath,
std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
if(!optional_ofs)
return false;
auto &ofs = optional_ofs.value();
ofs.write((char *)buf.base, buf.sz);
ofs.close();
if(!ofs)
@ -191,6 +194,38 @@ llarp_nodedb::Load(const fs::path &path)
return loaded;
}
void
llarp_nodedb::SaveAll()
{
std::array< byte_t, MAX_RC_SIZE > tmp;
llarp::util::Lock lock(&access);
for(const auto &item : entries)
{
llarp_buffer_t buf(tmp);
if(!item.second.rc.BEncode(&buf))
continue;
buf.sz = buf.cur - buf.base;
const auto filepath = getRCFilePath(item.second.rc.pubkey);
auto optional_ofs = llarp::util::OpenFileStream< std::ofstream >(
filepath,
std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
if(!optional_ofs)
continue;
auto &ofs = optional_ofs.value();
ofs.write((char *)buf.base, buf.sz);
ofs.flush();
ofs.close();
}
}
void
llarp_nodedb::AsyncFlushToDisk()
{
disk->addJob(std::bind(&llarp_nodedb::SaveAll, this));
}
ssize_t
llarp_nodedb::loadSubdir(const fs::path &dir)
{

@ -145,6 +145,12 @@ struct llarp_nodedb
static bool
ensure_dir(const char *dir);
void
SaveAll() LOCKS_EXCLUDED(access);
void
AsyncFlushToDisk()
};
/// struct for async rc verification

@ -1,6 +1,7 @@
#include <profiling.hpp>
#include <fstream>
#include <util/fs.hpp>
namespace llarp
{
@ -219,9 +220,13 @@ namespace llarp
auto res = BEncodeNoLock(&buf);
if(res)
{
buf.sz = buf.cur - buf.base;
std::ofstream f;
f.open(fname);
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;
auto& f = optional_f.value();
if(f.is_open())
{
f.write((char*)buf.base, buf.sz);

@ -11,6 +11,7 @@
#include <util/time.hpp>
#include <fstream>
#include <util/fs.hpp>
namespace llarp
{
@ -333,10 +334,14 @@ namespace llarp
llarp_buffer_t buf(tmp);
if(!BEncode(&buf))
return false;
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
std::ofstream f; // why was this in its own scope?
f.open(fname, std::ios::binary);
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
const fs::path fpath = std::string(fname); /* */
auto optional_f =
llarp::util::OpenFileStream< std::ofstream >(fpath, std::ios::binary);
if(!optional_f)
return false;
auto &f = optional_f.value();
if(!f.is_open())
return false;
f.write((char *)buf.base, buf.sz);

@ -99,8 +99,11 @@ namespace llarp
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
// write
std::ofstream f;
f.open(fname, std::ios::binary);
auto optional_f =
util::OpenFileStream< std::ofstream >(fname, std::ios::binary);
if(!optional_f)
return false;
auto& f = optional_f.value();
if(!f.is_open())
return false;
f.write((char*)buf.cur, buf.sz);

@ -16,8 +16,8 @@ struct llarp_threadpool
const pid_t callingPID;
llarp_threadpool(int workers, const char *name)
: impl(std::make_unique< llarp::thread::ThreadPool >(workers,
workers * 128))
: impl(
std::make_unique< llarp::thread::ThreadPool >(workers, workers * 128))
, jobs(nullptr)
, callingPID(0)
{

Loading…
Cancel
Save