90 lines
2.8 KiB
C++
90 lines
2.8 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/System/Types.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace Void::FileSystem
|
|
{
|
|
|
|
enum class DirectoryEventKind : std::uint8_t {
|
|
Created,
|
|
Modified,
|
|
Deleted,
|
|
Renamed,
|
|
AttributeChanged,
|
|
RescanRequired,
|
|
};
|
|
|
|
struct DirectoryEvent {
|
|
DirectoryEventKind kind{DirectoryEventKind::Modified};
|
|
std::filesystem::path path{};
|
|
std::filesystem::path old_path{};
|
|
bool is_directory{false};
|
|
std::string detail{};
|
|
};
|
|
|
|
/// Predicate queried for every discovered/modified path. Returning true skips
|
|
/// watching (for directories) and suppresses event delivery for that path.
|
|
using IgnorePredicate =
|
|
std::function<bool(const std::filesystem::path& path, bool is_directory)>;
|
|
|
|
struct DirectoryWatcherOptions {
|
|
IgnorePredicate ignore{};
|
|
std::size_t max_watches{65536};
|
|
std::size_t max_queued_events{4096};
|
|
std::chrono::milliseconds rename_pair_window{std::chrono::milliseconds{50}};
|
|
};
|
|
|
|
/// Kernel-event (Linux inotify) recursive directory watcher with auto-watch
|
|
/// attachment on new subdirectories, rename pairing, ignore filtering, and
|
|
/// explicit RescanRequired signaling on queue overflow or watch exhaustion.
|
|
class DirectoryWatcher final
|
|
{
|
|
public:
|
|
~DirectoryWatcher() noexcept;
|
|
|
|
DirectoryWatcher(const DirectoryWatcher&) = delete;
|
|
DirectoryWatcher& operator=(const DirectoryWatcher&) = delete;
|
|
DirectoryWatcher(DirectoryWatcher&&) noexcept;
|
|
DirectoryWatcher& operator=(DirectoryWatcher&&) noexcept;
|
|
|
|
[[nodiscard]] static Void::Result::Result<DirectoryWatcher,
|
|
Void::Result::Error>
|
|
create(const std::filesystem::path& root,
|
|
DirectoryWatcherOptions options = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<std::vector<DirectoryEvent>,
|
|
Void::Result::Error>
|
|
poll(std::chrono::milliseconds timeout = std::chrono::milliseconds{0});
|
|
|
|
/// Re-walk `root()` and rebuild all inotify watches from scratch.
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error> rescan();
|
|
|
|
/// Wake any thread blocked inside poll().
|
|
void wake() noexcept;
|
|
|
|
/// Enqueue a RescanRequired event and wake waiters (deterministic test hook).
|
|
void simulate_overflow_for_test();
|
|
|
|
[[nodiscard]] const std::filesystem::path& root() const noexcept;
|
|
[[nodiscard]] std::size_t watch_count() const noexcept;
|
|
[[nodiscard]] int native_handle() const noexcept;
|
|
|
|
private:
|
|
struct Impl;
|
|
std::unique_ptr<Impl> impl_{};
|
|
|
|
explicit DirectoryWatcher(std::unique_ptr<Impl> impl) noexcept;
|
|
};
|
|
|
|
} // namespace Void::FileSystem
|