|
|
@ -3,34 +3,60 @@
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <system_error>
|
|
|
|
#include <system_error>
|
|
|
|
|
|
|
|
#include <util/logger.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
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
|
|
|
|
error_code_t
|
|
|
|
EnsurePrivateFile(fs::path pathname)
|
|
|
|
EnsurePrivateFile(fs::path pathname)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto str = pathname.string();
|
|
|
|
const auto str = pathname.string();
|
|
|
|
error_code_t ec;
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
error_code_t ec = errno_error();
|
|
|
|
if(fs::exists(pathname, ec)) // file exists
|
|
|
|
if(fs::exists(pathname, ec)) // file exists
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fs::permissions(pathname,
|
|
|
|
auto st = fs::status(pathname, ec);
|
|
|
|
~fs::perms::group_all | ~fs::perms::others_all
|
|
|
|
if(ec)
|
|
|
|
| fs::perms::owner_read | fs::perms::owner_write,
|
|
|
|
return ec;
|
|
|
|
ec);
|
|
|
|
auto perms = st.permissions();
|
|
|
|
|
|
|
|
if((perms & fs::perms::others_exec) != fs::perms::none)
|
|
|
|
|
|
|
|
perms ^= fs::perms::others_exec;
|
|
|
|
|
|
|
|
if((perms & fs::perms::others_write) != fs::perms::none)
|
|
|
|
|
|
|
|
perms ^= fs::perms::others_write;
|
|
|
|
|
|
|
|
if((perms & fs::perms::others_write) != fs::perms::none)
|
|
|
|
|
|
|
|
perms ^= fs::perms::others_write;
|
|
|
|
|
|
|
|
if((perms & fs::perms::group_read) != fs::perms::none)
|
|
|
|
|
|
|
|
perms ^= fs::perms::group_read;
|
|
|
|
|
|
|
|
if((perms & fs::perms::others_read) != fs::perms::none)
|
|
|
|
|
|
|
|
perms ^= fs::perms::others_read;
|
|
|
|
|
|
|
|
if((perms & fs::perms::owner_exec) != fs::perms::none)
|
|
|
|
|
|
|
|
perms ^= fs::perms::owner_exec;
|
|
|
|
|
|
|
|
fs::permissions(pathname, perms, ec);
|
|
|
|
|
|
|
|
if(ec)
|
|
|
|
|
|
|
|
llarp::LogError("failed to set permissions on ", pathname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
errno = 0;
|
|
|
|
int e = errno;
|
|
|
|
int fd = ::open(str.c_str(), O_RDWR | O_CREAT, 0600);
|
|
|
|
|
|
|
|
ec = errno_error();
|
|
|
|
if(fd != -1)
|
|
|
|
if(fd != -1)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
::close(fd);
|
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ec = std::error_code(e, std::generic_category());
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ec)
|
|
|
|
|
|
|
|
llarp::LogError("failed to ensure ", str, ", ", ec.message());
|
|
|
|
return ec;
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace util
|
|
|
|
} // namespace util
|
|
|
|