reset errno and make unit tests pass

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

@ -25,18 +25,7 @@ namespace llarp
bool
SecretKey::LoadFromFile(const char* fname)
{
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();
std::ifstream f(fname, std::ios::in | std::ios::binary);
if(!f.is_open())
{
return false;
@ -72,26 +61,23 @@ namespace llarp
return false;
}
const fs::path fpath = std::string(fname);
auto optional_f = llarp::util::OpenFileStream< std::ofstream >(
fpath, std::ios::binary | std::ios::out);
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.cur - buf.base);
return true;
return f.good();
}
bool
IdentitySecret::LoadFromFile(const char* fname)
{
const fs::path fpath = std::string(fname);
auto optional = util::OpenFileStream< std::ifstream >(
fpath, std::ios::binary | std::ios::in);
if(!optional)
std::ifstream f(fname, std::ios::binary | std::ios::in);
if(!f.is_open())
return false;
auto& f = optional.value();
f.seekg(0, std::ios::end);
const size_t sz = f.tellg();
f.seekg(0, std::ios::beg);

@ -3,16 +3,25 @@
#include <sys/stat.h>
#include <unistd.h>
#include <system_error>
#include <util/logger.hpp>
namespace llarp
{
namespace util
{
static std::error_code
errno_error()
{
int e = errno;
errno = 0;
return std::make_error_code(static_cast< std::errc >(e));
}
error_code_t
EnsurePrivateFile(fs::path pathname)
{
auto str = pathname.string();
error_code_t ec;
const auto str = pathname.string();
error_code_t ec = errno_error();
if(fs::exists(pathname, ec)) // file exists
{
fs::permissions(pathname,
@ -22,15 +31,16 @@ namespace llarp
}
else if(!ec) // file is not there
{
int fd = ::open(str.c_str(), O_WRONLY | O_CREAT, 0600);
int e = errno;
errno = 0;
int fd = ::open(str.c_str(), O_RDWR | O_CREAT, 0600);
ec = errno_error();
if(fd != -1)
{
::close(fd);
}
ec = std::error_code(e, std::generic_category());
errno = 0;
}
if(ec)
llarp::LogError("failed to ensure ", str, ", ", ec.message());
return ec;
}
} // namespace util

@ -35,11 +35,10 @@ namespace llarp
OpenFileStream(fs::path pathname, std::ios::openmode mode)
{
if(EnsurePrivateFile(pathname))
{
std::string f = pathname.string();
return T{pathname, mode};
}
return {};
return {};
std::string f = pathname.string();
return T{pathname, mode};
}
using PathVisitor = std::function< bool(const fs::path &) >;

Loading…
Cancel
Save