2019-01-10 19:41:51 +00:00
|
|
|
#include <util/fs.hpp>
|
2019-07-16 23:37:06 +00:00
|
|
|
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-07-16 23:37:06 +00:00
|
|
|
|
2019-06-24 15:51:58 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2019-07-16 23:37:06 +00:00
|
|
|
#include <sys/types.h>
|
2019-06-24 15:51:58 +00:00
|
|
|
#include <system_error>
|
|
|
|
|
2019-07-16 23:44:36 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#else
|
2019-07-16 23:13:11 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2019-07-01 09:33:03 +00:00
|
|
|
namespace cpp17
|
|
|
|
{
|
|
|
|
namespace filesystem
|
|
|
|
{
|
|
|
|
#ifdef LOKINET_USE_CPPBACKPORT
|
2020-04-07 18:38:56 +00:00
|
|
|
const fs::perms active_bits(
|
|
|
|
fs::perms::all | fs::perms::set_uid | fs::perms::set_gid | fs::perms::sticky_bit);
|
2019-07-01 09:33:03 +00:00
|
|
|
inline mode_t
|
|
|
|
mode_cast(fs::perms prms)
|
|
|
|
{
|
|
|
|
return prms & active_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
permissions(const fs::path& p, fs::perms prms, std::error_code& ec)
|
|
|
|
{
|
|
|
|
std::error_code local_ec;
|
|
|
|
|
|
|
|
// OS X <10.10, iOS <8.0 and some other platforms don't support
|
|
|
|
// fchmodat(). Solaris (SunPro and gcc) only support fchmodat() on
|
|
|
|
// Solaris 11 and higher, and a runtime check is too much trouble. Linux
|
|
|
|
// does not support permissions on symbolic links and has no plans to
|
|
|
|
// support them in the future. The chmod() code is thus more practical,
|
|
|
|
// rather than always hitting ENOTSUP when sending in
|
|
|
|
// AT_SYMLINK_NO_FOLLOW.
|
|
|
|
// - See the 3rd paragraph of
|
|
|
|
// "Symbolic link ownership, permissions, and timestamps" at:
|
|
|
|
// "http://man7.org/linux/man-pages/man7/symlink.7.html"
|
|
|
|
// - See the fchmodat() Linux man page:
|
|
|
|
// "http://man7.org/linux/man-pages/man2/fchmodat.2.html"
|
2020-04-07 18:38:56 +00:00
|
|
|
#if defined(AT_FDCWD) && defined(AT_SYMLINK_NOFOLLOW) \
|
|
|
|
&& !(defined(__SUNPRO_CC) || defined(__sun) || defined(sun)) \
|
|
|
|
&& !(defined(linux) || defined(__linux) || defined(__linux__)) \
|
|
|
|
&& !(defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101000) \
|
|
|
|
&& !(defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 80000) \
|
2019-07-01 09:33:03 +00:00
|
|
|
&& !(defined(__QNX__) && (_NTO_VERSION <= 700))
|
2020-04-07 18:38:56 +00:00
|
|
|
if (::fchmodat(AT_FDCWD, p.c_str(), mode_cast(prms), 0))
|
2019-07-01 09:33:03 +00:00
|
|
|
#else // fallback if fchmodat() not supported
|
2020-04-07 18:38:56 +00:00
|
|
|
if (::chmod(p.c_str(), mode_cast(prms)))
|
2019-07-01 09:33:03 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
const int err = errno;
|
|
|
|
ec.assign(err, std::generic_category());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // namespace filesystem
|
|
|
|
} // namespace cpp17
|
|
|
|
|
2019-06-24 15:51:58 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
2019-06-24 17:36:25 +00:00
|
|
|
static std::error_code
|
|
|
|
errno_error()
|
|
|
|
{
|
|
|
|
int e = errno;
|
|
|
|
errno = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
return std::make_error_code(static_cast<std::errc>(e));
|
2019-06-24 17:36:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 15:51:58 +00:00
|
|
|
error_code_t
|
|
|
|
EnsurePrivateFile(fs::path pathname)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
errno = 0;
|
2019-06-24 17:36:25 +00:00
|
|
|
error_code_t ec = errno_error();
|
2020-04-07 18:38:56 +00:00
|
|
|
const auto str = pathname.string();
|
|
|
|
if (fs::exists(pathname, ec)) // file exists
|
2019-06-24 15:51:58 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto st = fs::status(pathname);
|
2019-06-24 19:34:30 +00:00
|
|
|
auto perms = st.permissions();
|
2020-04-07 18:38:56 +00:00
|
|
|
if ((perms & fs::perms::others_exec) != fs::perms::none)
|
2019-07-01 09:33:03 +00:00
|
|
|
perms = perms ^ fs::perms::others_exec;
|
2020-04-07 18:38:56 +00:00
|
|
|
if ((perms & fs::perms::others_write) != fs::perms::none)
|
2019-07-01 09:33:03 +00:00
|
|
|
perms = perms ^ fs::perms::others_write;
|
2020-04-07 18:38:56 +00:00
|
|
|
if ((perms & fs::perms::others_write) != fs::perms::none)
|
2019-07-01 09:33:03 +00:00
|
|
|
perms = perms ^ fs::perms::others_write;
|
2020-04-07 18:38:56 +00:00
|
|
|
if ((perms & fs::perms::group_read) != fs::perms::none)
|
2019-07-01 09:33:03 +00:00
|
|
|
perms = perms ^ fs::perms::group_read;
|
2020-04-07 18:38:56 +00:00
|
|
|
if ((perms & fs::perms::others_read) != fs::perms::none)
|
2019-07-01 09:33:03 +00:00
|
|
|
perms = perms ^ fs::perms::others_read;
|
2020-04-07 18:38:56 +00:00
|
|
|
if ((perms & fs::perms::owner_exec) != fs::perms::none)
|
2019-07-01 09:33:03 +00:00
|
|
|
perms = perms ^ fs::perms::owner_exec;
|
|
|
|
|
2019-06-24 19:34:30 +00:00
|
|
|
fs::permissions(pathname, perms, ec);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ec)
|
2019-06-24 19:34:30 +00:00
|
|
|
llarp::LogError("failed to set permissions on ", pathname);
|
2019-06-24 15:51:58 +00:00
|
|
|
}
|
2019-07-13 17:51:16 +00:00
|
|
|
else // file is not there
|
2019-06-24 15:51:58 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
errno = 0;
|
2019-06-24 17:36:25 +00:00
|
|
|
int fd = ::open(str.c_str(), O_RDWR | O_CREAT, 0600);
|
2020-04-07 18:38:56 +00:00
|
|
|
ec = errno_error();
|
|
|
|
if (fd != -1)
|
2019-06-24 15:51:58 +00:00
|
|
|
{
|
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
}
|
2019-07-13 17:51:16 +00:00
|
|
|
|
2019-07-18 09:42:30 +00:00
|
|
|
#ifndef WIN32
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ec)
|
2019-06-24 17:36:25 +00:00
|
|
|
llarp::LogError("failed to ensure ", str, ", ", ec.message());
|
2019-06-24 15:51:58 +00:00
|
|
|
return ec;
|
2019-07-18 09:42:30 +00:00
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
2019-06-24 15:51:58 +00:00
|
|
|
}
|
2019-06-24 16:26:15 +00:00
|
|
|
} // namespace util
|
2019-07-01 09:33:03 +00:00
|
|
|
} // namespace llarp
|