add threaded filewatcher

This commit is contained in:
2025-03-27 01:24:57 +02:00
parent 6d0cbbb011
commit 2015894068
20 changed files with 600 additions and 95 deletions

View File

@@ -0,0 +1,77 @@
#ifndef HI_FILEWATCHER_H_
#define HI_FILEWATCHER_H_
#include "types.h"
/**
* File event watching related functionality and types
*/
typedef enum {
HI_FILE_NONE = 0,
HI_FILE_ACCESS = 1 << 0,
HI_FILE_MODIFY = 1 << 1,
HI_FILE_CREATE = 1 << 2,
HI_FILE_DELETE = 1 << 3,
HI_FILE_ALL =
(HI_FILE_ACCESS | HI_FILE_MODIFY | HI_FILE_CREATE | HI_FILE_DELETE)
} HiFileEventType;
typedef struct {
const char *watcher; // Path given to the corresponding wd. Not owning.
const char *file; // Owning, should be freed by the event handler by calling
// `hi_file_event_destroy`)
HiFileEventType type;
} hiFileEvent;
struct hiFileWatcherContext;
/**
* FileEvents is a thread safe list of events.
*
* It is implemented as a stack, so the events will be popped
* in reverse chronological order.
* */
struct hiFileEvents;
/**
* Create watcher and necessary data to run it.
*
* Will start the watcher thread immediately.
*/
struct hiFileWatcherContext *hi_file_watcher_create();
/**
* Destroy a previously created file watcher context.
*
* Will join the thread and destroy event stack.
*/
void hi_file_watcher_destroy(struct hiFileWatcherContext *context);
/**
* Add a file to the watch list of a file watcher.
*
* @param context Previously created watcher context
* @param pathname Absolute path to the file or directory
* @param flags The events that will be watched for
*/
HiloadResult hi_file_watcher_add(struct hiFileWatcherContext *context,
const char *pathname, u32 flags);
/**
* Can be used to poke file watcher thread to make sure it empties
* events before doing something, e.g. for shutting it down.
*/
void hi_file_watcher_notify(struct hiFileWatcherContext *context);
/**
* Frees held memory but doesn't zero fields
*/
void hi_file_event_free(hiFileEvent *event);
/**
* Pop an event from event stack. Call `hi_file_event_destroy` when done with
* it.
*/
hiFileEvent hi_file_event_pop(struct hiFileWatcherContext *ctx);
/**
* Free and zero held memory. Call after pop.
*/
void hi_file_event_destroy(hiFileEvent *event);
#endif // HI_FILEWATCHER_H_