lokinet/llarp/util/fs.hpp

87 lines
1.8 KiB
C++
Raw Normal View History

#pragma once
2019-07-29 21:40:58 +00:00
2018-08-26 12:51:22 +00:00
#include <functional>
#include <set>
2018-06-14 07:48:42 +00:00
#ifdef USE_GHC_FILESYSTEM
#include <ghc/filesystem.hpp>
namespace fs = ghc::filesystem;
#else
#include <filesystem>
namespace fs
{
using namespace std::filesystem;
using ifstream = std::ifstream;
using ofstream = std::ofstream;
using fstream = std::fstream;
} // namespace fs
#endif
2019-06-24 15:51:58 +00:00
#ifndef _MSC_VER
2018-09-17 12:02:09 +00:00
#include <dirent.h>
#endif
2018-06-14 19:28:27 +00:00
#include <optional>
2019-06-24 16:26:15 +00:00
2018-08-26 12:51:22 +00:00
namespace llarp
{
namespace util
{
struct FileHash
{
size_t
operator()(const fs::path& f) const
{
std::hash<std::string> h;
return h(f.string());
}
};
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
template <typename T>
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
{
if (EnsurePrivateFile(pathname))
2019-06-24 17:36:25 +00:00
return {};
return std::make_optional<T>(pathname, mode);
2019-06-24 15:51:58 +00:00
}
template <typename PathVisitor>
static void
IterDir(const fs::path& path, PathVisitor visit)
{
DIR* d = opendir(path.string().c_str());
if (d == nullptr)
2018-09-14 15:22:44 +00:00
return;
struct dirent* ent = nullptr;
std::set<fs::path> entries;
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);
if (not ent)
2018-09-14 15:22:44 +00:00
break;
if (ent->d_name[0] == '.')
2018-09-14 17:46:02 +00:00
continue;
2021-10-06 18:37:09 +00:00
entries.emplace(path / fs::path{ent->d_name});
} while (ent);
2018-09-14 15:22:44 +00:00
closedir(d);
for (const auto& p : entries)
{
if (not visit(p))
return;
}
2018-08-26 12:51:22 +00:00
};
} // namespace util
} // namespace llarp