finish up issue 17

pull/686/head
Jeff Becker 5 years ago
parent aad71c2022
commit 38d5a4855a
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -141,12 +141,14 @@ llarp_ensure_config(const char *fname, const char *basedir, bool overwrite,
}
// write fname ini
std::ofstream f(fname, std::ios::out | std::ios::binary);
if(!f.is_open())
auto optional_f =
llarp::util::OpenFileStream< std::ofstream >(fname, std::ios::binary);
if(!optional_f || !optional_f.value().is_open())
{
llarp::LogError("failed to open ", fname, " for writing");
return false;
}
auto &f = optional_f.value();
llarp_generic_ensure_config(f, basepath);
if(asRouter)
{
@ -294,9 +296,12 @@ llarp_ensure_client_config(std::ofstream &f, std::string basepath)
// write snapp-example.ini
const std::string snappExample_fpath = basepath + "snapp-example.ini";
{
std::ofstream example_f(snappExample_fpath,
std::ios::binary | std::ios::out);
if(f.is_open())
auto f = llarp::util::OpenFileStream< std::ofstream >(snappExample_fpath,
std::ios::binary);
if(!f)
return false;
auto &example_f = f.value();
if(example_f.is_open())
{
// pick ip
// don't revert me

@ -3,6 +3,8 @@
#include <util/buffer.hpp>
#include <fstream>
#include <util/fs.hpp>
#include <iterator>
namespace llarp
@ -23,8 +25,18 @@ namespace llarp
bool
SecretKey::LoadFromFile(const char* fname)
{
std::ifstream f(fname, std::ios::in | std::ios::binary);
const fs::path fpath = std::string(fname);
std::error_code ec;
if(!fs::exists(fpath, ec))
{
return false;
}
auto optional_f = util::OpenFileStream< std::ifstream >(
fpath, std::ios::in | std::ios::binary);
if(!optional_f)
return false;
auto& f = optional_f.value();
if(!f.is_open())
{
return false;
@ -59,9 +71,12 @@ namespace llarp
{
return false;
}
std::ofstream f;
f.open(fname, std::ios::binary);
const fs::path fpath = std::string(fname);
auto optional_f = llarp::util::OpenFileStream< std::ofstream >(
fpath, std::ios::binary | std::ios::out);
if(!optional_f)
return false;
auto& f = optional_f.value();
if(!f.is_open())
return false;
f.write((char*)buf.base, buf.cur - buf.base);
@ -71,9 +86,12 @@ namespace llarp
bool
IdentitySecret::LoadFromFile(const char* fname)
{
std::ifstream f(fname, std::ios::binary | std::ios::in);
if(!f.is_open())
const fs::path fpath = std::string(fname);
auto optional = util::OpenFileStream< std::ifstream >(
fpath, std::ios::binary | std::ios::in);
if(!optional)
return false;
auto& f = optional.value();
f.seekg(0, std::ios::end);
const size_t sz = f.tellg();
f.seekg(0, std::ios::beg);

@ -13,22 +13,25 @@ namespace llarp
{
auto str = pathname.string();
error_code_t ec;
if(fs::exists(pathname, ec)) // file exists
if(fs::exists(pathname, ec)) // file exists
{
fs::permissions(pathname, ~fs::perms::group_all | ~fs::perms::others_all | fs::perms::owner_read | fs::perms::owner_write, ec);
fs::permissions(pathname,
~fs::perms::group_all | ~fs::perms::others_all
| fs::perms::owner_read | fs::perms::owner_write,
ec);
}
else if(!ec) // file is not there
else if(!ec) // file is not there
{
int fd = ::open(str.c_str(), O_WRONLY | O_CREAT, 0600);
int e = errno;
int e = errno;
if(fd != -1)
{
::close(fd);
}
ec = std::error_code(e, std::generic_category());
ec = std::error_code(e, std::generic_category());
errno = 0;
}
return ec;
}
}
}
} // namespace util
} // namespace llarp

@ -11,16 +11,16 @@
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#ifndef _MSC_VER
#include <dirent.h>
#endif
#include <absl/types/optional.h>
namespace llarp
{
namespace util
{
using error_code_t = std::error_code;
/// Ensure that a file exists and has correct permissions
@ -30,16 +30,16 @@ namespace llarp
/// open a stream to a file and ensure it exists before open
/// sets any permissions on creation
template<typename T>
T OpenFileStream(fs::path pathname)
template < typename T >
absl::optional< T >
OpenFileStream(fs::path pathname, std::ios::openmode mode)
{
T stream;
if(EnsurePrivateFile(pathname))
{
std::string f = pathname.string();
stream.open(f);
return T{pathname, mode};
}
return stream;
return {};
}
using PathVisitor = std::function< bool(const fs::path &) >;

Loading…
Cancel
Save