llarp/util/file.hpp

Namespaces

Name
llarp
[crypto.hpp]
llarp::util

Classes

Name
struct llarp::util::FileHash

Source code

#pragma once
#include "fs.hpp"

#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <dirent.h>

namespace llarp::util
{
  std::string
  slurp_file(const fs::path& filename);

  size_t
  slurp_file(const fs::path& filename, char* buffer, size_t buffer_size);

  template <
      typename Char,
      std::enable_if_t<sizeof(Char) == 1 and not std::is_same_v<Char, char>, int> = 1>
  inline size_t
  slurp_file(const fs::path& filename, Char* buffer, size_t buffer_size)
  {
    return slurp_file(filename, reinterpret_cast<char*>(buffer), buffer_size);
  }

  void
  dump_file(const fs::path& filename, std::string_view contents);

  template <typename Char, std::enable_if_t<sizeof(Char) == 1, int> = 0>
  inline void
  dump_file(const fs::path& filename, const Char* buffer, size_t buffer_size)
  {
    return dump_file(
        filename, std::string_view{reinterpret_cast<const char*>(buffer), buffer_size});
  }

  std::string
  filepath(const fs::path& f);

  struct FileHash
  {
    size_t
    operator()(const fs::path& f) const
    {
      std::hash<std::string> h;
      return h(filepath(f));
    }
  };

  using error_code_t = std::error_code;

  error_code_t
  EnsurePrivateFile(const fs::path& pathname);

  template <typename T>
  std::optional<T>
  OpenFileStream(const fs::path& pathname, std::ios::openmode mode)
  {
    if (EnsurePrivateFile(pathname))
      return {};
    return std::make_optional<T>(pathname, mode);
  }

  template <typename PathVisitor>
  static void
  IterDir(const fs::path& _path, PathVisitor visit)
  {
    auto fpath = filepath(_path);
    DIR* d = opendir(fpath.c_str());
    if (d == nullptr)
      return;
    struct dirent* ent = nullptr;
    std::set<fs::path> entries;
    fs::path path{_path};
    do
    {
      ent = readdir(d);
      if (not ent)
        break;
      if (ent->d_name[0] == '.')
        continue;
      entries.emplace(path / fs::path{ent->d_name});
    } while (ent);
    closedir(d);

    for (const auto& p : entries)
    {
      if (not visit(p))
        return;
    }
  }

}  // namespace llarp::util

Updated on 2026-01-10 at 22:49:45 +0000