2018-04-08 12:18:16 +00:00
|
|
|
#ifndef LLARP_FS_HPP
|
|
|
|
#define LLARP_FS_HPP
|
2019-07-29 21:40:58 +00:00
|
|
|
|
2018-08-26 12:51:22 +00:00
|
|
|
#include <functional>
|
2018-09-20 19:24:13 +00:00
|
|
|
|
2018-06-14 07:48:42 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
2018-06-14 19:22:26 +00:00
|
|
|
#define PATH_SEP "\\"
|
2018-06-14 07:48:42 +00:00
|
|
|
#else
|
2018-06-14 19:22:26 +00:00
|
|
|
#define PATH_SEP "/"
|
2018-06-14 07:48:42 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-20 02:14:21 +00:00
|
|
|
#ifdef USE_GHC_FILESYSTEM
|
|
|
|
#include <ghc/filesystem.hpp>
|
|
|
|
namespace fs = ghc::filesystem;
|
|
|
|
#else
|
2020-05-01 19:51:15 +00:00
|
|
|
#include <filesystem>
|
|
|
|
namespace fs = std::filesystem;
|
2020-05-20 02:14:21 +00:00
|
|
|
#endif
|
2019-06-24 15:51:58 +00:00
|
|
|
|
2019-04-19 18:24:33 +00:00
|
|
|
#ifndef _MSC_VER
|
2018-09-17 12:02:09 +00:00
|
|
|
#include <dirent.h>
|
2019-04-19 18:24:33 +00:00
|
|
|
#endif
|
2018-06-14 19:28:27 +00:00
|
|
|
|
2020-05-01 19:51:15 +00:00
|
|
|
#include <optional>
|
2019-06-24 16:26:15 +00:00
|
|
|
|
2018-08-26 12:51:22 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
2019-06-24 15:51:58 +00:00
|
|
|
using error_code_t = std::error_code;
|
|
|
|
|
|
|
|
/// Ensure that a file exists and has correct permissions
|
|
|
|
/// return any error code or success
|
|
|
|
error_code_t
|
|
|
|
EnsurePrivateFile(fs::path pathname);
|
|
|
|
|
|
|
|
/// open a stream to a file and ensure it exists before open
|
|
|
|
/// sets any permissions on creation
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename T>
|
2020-05-01 19:51:15 +00:00
|
|
|
std::optional<T>
|
2019-06-24 16:26:15 +00:00
|
|
|
OpenFileStream(fs::path pathname, std::ios::openmode mode)
|
2019-06-24 15:51:58 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (EnsurePrivateFile(pathname))
|
2019-06-24 17:36:25 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
std::string f = pathname.string();
|
|
|
|
return T{pathname, mode};
|
2019-06-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using PathVisitor = std::function<bool(const fs::path&)>;
|
|
|
|
using PathIter = std::function<void(const fs::path&, PathVisitor)>;
|
2018-09-17 12:02:09 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
static PathIter IterDir = [](const fs::path& path, PathVisitor visit) {
|
2019-04-19 18:24:33 +00:00
|
|
|
#ifdef _MSC_VER
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& p : fs::directory_iterator(path))
|
2019-04-19 18:24:33 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!visit(p.path()))
|
2019-04-19 18:24:33 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2020-04-07 18:38:56 +00:00
|
|
|
DIR* d = opendir(path.string().c_str());
|
|
|
|
if (d == nullptr)
|
2018-09-14 15:22:44 +00:00
|
|
|
return;
|
2020-04-07 18:38:56 +00:00
|
|
|
struct dirent* ent = nullptr;
|
2018-09-14 15:22:44 +00:00
|
|
|
do
|
2018-08-26 12:51:22 +00:00
|
|
|
{
|
2018-09-14 15:22:44 +00:00
|
|
|
ent = readdir(d);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!ent)
|
2018-09-14 15:22:44 +00:00
|
|
|
break;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ent->d_name[0] == '.')
|
2018-09-14 17:46:02 +00:00
|
|
|
continue;
|
2018-09-14 15:22:44 +00:00
|
|
|
fs::path p = path / fs::path(ent->d_name);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!visit(p))
|
2018-09-14 15:22:44 +00:00
|
|
|
break;
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (ent);
|
2018-09-14 15:22:44 +00:00
|
|
|
closedir(d);
|
2019-04-19 18:24:33 +00:00
|
|
|
#endif
|
2018-08-26 12:51:22 +00:00
|
|
|
};
|
|
|
|
} // namespace util
|
|
|
|
} // namespace llarp
|
2018-07-27 03:41:55 +00:00
|
|
|
#endif // end LLARP_FS_HPP
|