twn_filewatch.c: file and directory change api, initial support for texture reload

This commit is contained in:
veclavtalica
2025-01-29 07:21:09 +03:00
parent 630c6fb5d4
commit 4b2a22bf3c
7 changed files with 246 additions and 29 deletions

View File

@ -37,7 +37,7 @@ typedef struct xWatcher_reference {
void *additional_data;
} xWatcher_reference;
struct file {
typedef struct xWatcher_file {
// just the file name alone
char *name;
// used for adding (additional) context in the handler (if needed)
@ -50,11 +50,11 @@ struct file {
const char *path,
int context,
void *additional_data);
} file;
} xWatcher_file;
struct directory {
typedef struct xWatcher_directory {
// list of files
struct file *files;
struct xWatcher_file *files;
char *path;
// used for adding (additional) context in the handler (if needed)
@ -78,10 +78,10 @@ struct directory {
#else
#error "Unsupported"
#endif
} directory;
} xWatcher_directory;
typedef struct x_watcher {
struct directory *directories;
struct xWatcher_directory *directories;
pthread_t thread;
int thread_id;
bool alive;
@ -118,7 +118,7 @@ typedef struct x_watcher {
x_watcher *watcher = (x_watcher*) argument;
char buffer[BUF_LEN];
ssize_t lenght;
struct directory *directories = watcher->directories;
struct xWatcher_directory *directories = watcher->directories;
while(watcher->alive) {
// poll for events
@ -150,7 +150,7 @@ typedef struct x_watcher {
&buffer[i];
// find directory for which this even matches via the descriptor
struct directory *directory = NULL;
struct xWatcher_directory *directory = NULL;
for(size_t j = 0; j < arr_count(directories); j++) {
if(directories[j].inotify_watch_fd == event->wd) {
directory = &directories[j];
@ -163,7 +163,7 @@ typedef struct x_watcher {
}
// find matching file (if any)
struct file *file = NULL;
struct xWatcher_file *file = NULL;
for(size_t j = 0; j < arr_count(directory->files); j++) {
if(strcmp(directory->files[j].name, event->name) == 0) {
file = &directory->files[j];
@ -225,9 +225,9 @@ typedef struct x_watcher {
// cleanup time
for(size_t i = 0; i < arr_count(watcher->directories); i++) {
struct directory *directory = &watcher->directories[i];
struct xWatcher_directory *directory = &watcher->directories[i];
for(size_t j = 0; j < arr_count(directory->files); j++) {
struct file *file = &directory->files[j];
struct xWatcher_file *file = &directory->files[j];
free(file->name);
}
arr_free(directory->files);
@ -247,7 +247,7 @@ typedef struct x_watcher {
static inline void *__internal_xWatcherProcess(void *argument) {
x_watcher *watcher = (x_watcher*) argument;
struct directory *directories = watcher->directories;
struct xWatcher_directory *directories = watcher->directories;
// create an event list so we can still make use of the Windows API
HANDLE events[arr_count(directories)];
@ -281,7 +281,7 @@ typedef struct x_watcher {
}
// shorhand for convenience
struct directory *dir = &directories[object_index];
struct xWatcher_directory *dir = &directories[object_index];
// retrieve event data
DWORD bytes_transferred;
@ -326,7 +326,7 @@ typedef struct x_watcher {
}
// find matching file (if any)
struct file *file = NULL;
struct xWatcher_file *file = NULL;
for(size_t j = 0; j < arr_count(dir->files); j++) {
if(strcmp(dir->files[j].name, name_char) == 0) {
file = &dir->files[j];
@ -423,9 +423,9 @@ typedef struct x_watcher {
}
// cleanup time
for(size_t i = 0; i < arr_count(watcher->directories); i++) {
struct directory *directory = &watcher->directories[i];
struct xWatcher_directory *directory = &watcher->directories[i];
for(size_t j = 0; j < arr_count(directory->files); j++) {
struct file *file = &directory->files[j];
struct xWatcher_file *file = &directory->files[j];
free(file->name);
}
arr_free(directory->files);
@ -487,7 +487,7 @@ static inline bool xWatcher_appendFile(
filename = path;
}
struct directory *dir = NULL;
struct xWatcher_directory *dir = NULL;
// check against the database of (pre-existing) directories
for(size_t i = 0; i < arr_count(watcher->directories); i++) {
@ -499,7 +499,7 @@ static inline bool xWatcher_appendFile(
// directory exists, check if an callback has been already added
if(dir == NULL) {
struct directory new_dir;
struct xWatcher_directory new_dir;
new_dir.callback_func = NULL; // DO NOT add callbacks if it's a file
new_dir.context = 0; // context should be invalid as well
@ -524,7 +524,7 @@ static inline bool xWatcher_appendFile(
}
// search for the file
struct file *file = NULL;
struct xWatcher_file *file = NULL;
for(size_t i = 0; i < arr_count(dir->files); i++) {
if(strcmp(dir->files[i].name, filename) == 0) {
file = &dir->files[i];
@ -535,7 +535,7 @@ static inline bool xWatcher_appendFile(
return false; // file already exists, that's an ERROR
}
struct file new_file;
struct xWatcher_file new_file;
// avoid an invalid free because this shares the memory space
// of the full path string
new_file.name = strdup(filename);
@ -667,7 +667,7 @@ static inline bool xWatcher_appendDir(
if(path[strlen(path)-1] == DIRBRK)
path[strlen(path)-1] = '\0';
struct directory *dir = NULL;
struct xWatcher_directory *dir = NULL;
// check against the database of (pre-existing) directories
for(size_t i=0; i < arr_count(watcher->directories); i++) {
@ -689,7 +689,7 @@ static inline bool xWatcher_appendDir(
dir->additional_data = reference->additional_data;
} else {
// keep an eye for this one as it's on the stack
struct directory dir;
struct xWatcher_directory dir;
dir.path = path;
dir.callback_func = reference->callback_func;