filesystem: integrate inotify for file changes

This commit is contained in:
Alibek Omarov 2022-09-10 22:55:07 +03:00
parent 7ed0499aa9
commit 234c843f60
4 changed files with 134 additions and 0 deletions

View File

@ -29,6 +29,9 @@ GNU General Public License for more details.
#endif
#include <stdio.h>
#include <stdarg.h>
#if XASH_LINUX
#include <sys/inotify.h>
#endif
#include "port.h"
#include "const.h"
#include "crtlib.h"

View File

@ -120,6 +120,9 @@ typedef struct fs_globals_t
int numgames;
} fs_globals_t;
typedef void (*fs_event_callback_t)( const char *path );
typedef struct fs_api_t
{
qboolean (*InitStdio)( qboolean caseinsensitive, const char *rootdir, const char *basedir, const char *gamedir, const char *rodir );
@ -174,6 +177,10 @@ typedef struct fs_api_t
qboolean (*Delete)( const char *path );
qboolean (*SysFileExists)( const char *path, qboolean casesensitive );
const char *(*GetDiskPath)( const char *name, qboolean gamedironly );
// file watcher
void (*WatchFrame)( void ); // engine will read all events and call appropriate callbacks
qboolean (*AddWatch)( const char *path, fs_event_callback_t callback );
} fs_api_t;
typedef struct fs_interface_t

View File

@ -189,6 +189,13 @@ void FS_SearchWAD( stringlist_t *list, wfile_t *wad, const char *pattern );
byte *FS_LoadWADFile( const char *path, fs_offset_t *sizeptr, qboolean gamedironly );
qboolean FS_AddWad_Fullpath( const char *wadfile, qboolean *already_loaded, int flags );
//
// watch.c
//
qboolean FS_WatchInitialize( void );
int FS_AddWatch( const char *path, fs_event_callback_t callback );
void FS_WatchFrame( void );
//
// zip.c
//

117
filesystem/watch.c Normal file
View File

@ -0,0 +1,117 @@
#if 0
#include "build.h"
#if XASH_LINUX
#include <sys/inotify.h>
#include <errno.h>
#include <string.h>
#endif
#include "filesystem_internal.h"
#include "common/com_strings.h"
#define MAX_FS_WATCHES 256
struct
{
#if XASH_LINUX
int fd;
int count;
struct
{
fs_event_callback_t callback;
int fd;
} watch[MAX_FS_WATCHES];
#endif // XASH_LINUX
} fsnotify;
#if XASH_LINUX
static qboolean FS_InotifyInit( void )
{
int fd;
if(( fd = inotify_init1( IN_NONBLOCK )) < 0 )
{
Con_Printf( S_ERROR "inotify_init1 failed: %s", strerror( errno ));
return false;
}
fsnotify.fd = fd;
return true;
}
static qboolean FS_InotifyWasInit( void )
{
return fsnotify.fd >= 0;
}
#endif
/*
===============
FS_AddWatch
Adds on-disk path to filesystem watcher list
Every file modification will call back
===============
*/
int FS_AddWatch( const char *path, fs_event_callback_t callback )
{
#if XASH_LINUX
int fd;
const uint mask = IN_CREATE | IN_DELETE | IN_MODIFY;
if( !FS_InotifyWasInit() && !FS_InotifyInit())
return false;
if(( fd = inotify_add_watch( fsnotify.fd, path, mask )) < 0 )
{
Con_Printf( S_ERROR "inotify_add_watch failed: %s", strerror( errno ));
return false;
}
fsnotify.watch[fsnotify.count].fd = fd;
fsnotify.watch[fsnotify.count].callback = callback;
return true;
#else
return false;
#endif
}
/*
===============
FS_WatchFrame
Polls any changes and runs call backs
===============
*/
void FS_WatchFrame( void )
{
#if XASH_LINUX
int i;
for( i = 0; i < fsnotify.count; i++ )
{
struct inotify_event events;
}
#endif
}
/*
===============
FS_WatchInitialize
initializes filesystem watcher subsystem
===============
*/
qboolean FS_WatchInitialize( void )
{
#if XASH_LINUX
fsnotify.fd = -1; // only call inotify init when requested
fsnotify.count = 0;
return true;
#else
return false;
#endif
}
#endif // 0