AuroraRuntime/Include/Aurora/IO/FS/Watcher.hpp

125 lines
3.4 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Watcher.hpp
Date: 2022-4-1
Author: Reece
***/
#pragma once
namespace Aurora::IO::FS
{
struct UserWatchData
{
virtual ~UserWatchData() {};
};
struct WatchedFile
{
/**
* @brief A user/caller provided private context
*/
AuSPtr<UserWatchData> userData;
/**
* @brief A path of a file or directory to watch
*/
AuString path;
};
AUE_DEFINE(EWatchEvent,
(
// The file as defined in the WatchedFile was modified
eSelfModify,
// The file as defined in the WatchedFile was removed
// NOTE: you will not recieve updates if a new file is made by an old watch target
eSelfDelete,
// A file in a watched directory was modified
eFileModify,
// A file in a watched directory was deleted
eFileDelete,
// A file in a watched directory was created
eFileCreate
));
struct WatchRequest
{
/**
* @brief A path of a file or directory to watch
*/
WatchedFile watch;
/**
* @brief You must provide an array of relevant file operations you wish to subscribe to.
* Additional events may be provided, if available.
*/
AuList<EWatchEvent> events;
};
struct WatchEvent
{
/**
* @brief The type of the event
*/
EWatchEvent event;
/**
* @brief The initial file watch that created this event
*/
WatchedFile watch;
/**
* @brief The Aurora path of the resource that triggered this event
*/
AuString file;
};
struct IWatcher
{
/**
* @brief
* @param file A file or a directory to watch for changes
* @return
* @warning Other Windows libraries,including libfswatch and libuv, will lock the parent directory.
* This is considered an issue by many on the NT platform. This is not a platform limitation,
* and in fact, can be worked around by using a lower level lock instead of some high level
* shell abstraction. This allows all platforms to share the same lock-free semantics.
* *Nobody* will be locked out of any file or directory for any reason.
*
*/
virtual bool AddWatch(const WatchRequest &file) = 0;
/**
* @brief Removes a fs watch
* @param path An Aurora path as defined in the README
* @return
*/
virtual bool RemoveByName(const AuString &path) = 0;
/**
* @brief Removes a fs watch
* @param file The private context object provided to IWatcher::AddWatch via WatchedFile (in WatchRequest)
* @return
*/
virtual bool RemoveByPrivateContext(const AuSPtr<UserWatchData> &file) = 0;
/**
* @brief Returns an IO resources that immediately signals upon a watch update notification
* @return
*/
virtual AuSPtr<Loop::ILoopSource> AsLoopSource() = 0;
/**
* @brief Non-blocking poll for updates
* @return
*/
virtual AuList<WatchEvent> QueryUpdates() = 0;
};
AUKN_SHARED_API(NewWatcher, IWatcher);
}