lokinet/llarp/util/fs.cpp

37 lines
906 B
C++
Raw Normal View History

#include <util/fs.hpp>
2019-06-24 15:51:58 +00:00
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <system_error>
namespace llarp
{
namespace util
{
error_code_t
EnsurePrivateFile(fs::path pathname)
{
auto str = pathname.string();
error_code_t ec;
2019-06-24 16:26:15 +00:00
if(fs::exists(pathname, ec)) // file exists
2019-06-24 15:51:58 +00:00
{
2019-06-24 16:26:15 +00:00
fs::permissions(pathname,
~fs::perms::group_all | ~fs::perms::others_all
| fs::perms::owner_read | fs::perms::owner_write,
ec);
2019-06-24 15:51:58 +00:00
}
2019-06-24 16:26:15 +00:00
else if(!ec) // file is not there
2019-06-24 15:51:58 +00:00
{
int fd = ::open(str.c_str(), O_WRONLY | O_CREAT, 0600);
2019-06-24 16:26:15 +00:00
int e = errno;
2019-06-24 15:51:58 +00:00
if(fd != -1)
{
::close(fd);
}
2019-06-24 16:26:15 +00:00
ec = std::error_code(e, std::generic_category());
2019-06-24 15:51:58 +00:00
errno = 0;
}
return ec;
}
2019-06-24 16:26:15 +00:00
} // namespace util
} // namespace llarp