demotool/filewatcher.cc

224 lines
5.2 KiB
C++

#include "externals.hh"
#include "filewatcher.hh"
#include "utilities.hh"
/*= T_FilesWatcher ===========================================================*/
T_FilesWatcher::T_FilesWatcher( )
: fd( inotify_init1( O_NONBLOCK ) )
{ }
T_FilesWatcher::T_FilesWatcher( T_FilesWatcher&& other ) noexcept
: fd( 0 ) , watched_( std::move( other.watched_ ) )
{
std::swap( fd , other.fd );
other.watched_.clear( );
for ( T_WatchedFiles* wf : watched_ ) {
if ( wf ) {
wf->watcher = this;
}
}
}
T_FilesWatcher::~T_FilesWatcher( )
{
if ( fd ) {
close( fd );
}
for ( T_WatchedFiles* wf : watched_ ) {
if ( wf ) {
wf->watcher = nullptr;
}
}
}
void T_FilesWatcher::check( )
{
// Reset triggered status
for ( T_WatchedFiles* wf : watched_ ) {
wf->triggered = false;
}
// Handle events from inotify
inotify_event ie;
while ( read( fd , &ie , sizeof( ie ) ) == sizeof( ie ) ) {
// printf( "inotify wd %d / evt %x\n" , ie.wd , ie.mask ); fflush( stdout );
if ( ( ie.mask & ( IN_CLOSE_WRITE | IN_DELETE_SELF ) ) == 0 ) {
// printf( " -> skipping this event\n" );
continue;
}
T_String const* name( inotify_.get( ie.wd ) );
if ( !name ) {
// printf( " -> no matching file\n" ); fflush( stdout );
continue;
}
printf( " -> file '%s'\n" , name->toOSString( ).data( ) );
auto* entry( fileWatchers_.get( *name ) );
assert( entry );
entry->trigger( );
if ( ( ie.mask & IN_DELETE_SELF ) != 0 ) {
//printf( " -> deleted, added as missing\n" ); fflush( stdout );
inotify_rm_watch( fd , entry->wd );
missing_.add( *name );
inotify_.remove( entry->wd );
entry->wd = -2;
}
}
// Handle missing/deleted files
for ( auto it = missing_.begin( ) ; it < missing_.end( ) ; ++it ) {
auto const& path( *it );
//printf( "Checking missing file '%s'\n" , path.c_str( ) ); fflush( stdout );
auto* const p( fileWatchers_.get( path ) );
//printf( " -> found? %c\n" , p != fileWatchers_.end( ) ? 'Y' : 'N' );
assert( p );
//printf( " -> wd = %d\n" , p->second.wd );
assert( p->wd < 0 );
auto& we( *p );
const auto nwd( inotify_add_watch( fd ,
(char*) path.toOSString( ).data( ) ,
IN_CLOSE_WRITE | IN_DELETE_SELF ) );
//printf( " -> inotify add watch %d\n" , nwd ); fflush( stdout );
if ( nwd < 0 ) {
//printf( " -> still missing\n" ); fflush( stdout );
we.wd = -1;
continue;
}
// Trigger if this was really a missing file
if ( we.wd == -1 ) {
//printf( " -> triggering\n" ); fflush( stdout );
we.trigger( );
}
// Update cache
we.wd = nwd;
inotify_.add( nwd , path );
missing_.remove( it.pos( ) );
it --;
}
}
/*----------------------------------------------------------------------------*/
void T_FilesWatcher::T_File_::trigger( )
{
for ( T_WatchedFiles* const wf : watchers ) {
if ( !wf->triggered ) {
wf->triggered = true;
wf->callback( );
}
}
}
void T_FilesWatcher::watchFile(
T_String const& path ,
T_WatchedFiles* const watcher )
{
//printf( "creating watch on '%s' for watcher %p\n" , path.c_str( ) , watcher ); fflush( stdout );
auto* const exFilePos( fileWatchers_.get( path ) );
if ( exFilePos ) {
//printf( " -> existing\n" ); fflush( stdout );
auto& wl( exFilePos->watchers );
if ( !wl.contains( watcher ) ) {
wl.add( watcher );
}
} else {
T_File_ f;
f.wd = inotify_add_watch( fd , (char*) path.toOSString( ).data( ) ,
IN_CLOSE_WRITE | IN_DELETE_SELF );
//printf( " -> inotify wd %d\n" , f.wd ); fflush( stdout );
if ( f.wd == -1 ) {
missing_.add( path );
//printf( " -> missing\n" ); fflush( stdout );
} else {
inotify_.add( f.wd , path );
//printf( " -> found\n" ); fflush( stdout );
}
f.watchers.add( watcher );
fileWatchers_.add( path , std::move( f ) );
}
}
void T_FilesWatcher::unwatchAll(
T_WatchedFiles* const watcher )
{
for ( auto i = 0u ; i < fileWatchers_.size( ) ; ) {
auto const& path( fileWatchers_.keys( )[ i ] );
auto& fw( *fileWatchers_.get( path ) );
auto& wl( fw.watchers );
const auto pos( wl.indexOf( watcher ) );
if ( pos == -1 ) {
i ++;
continue;
}
wl.remove( pos );
if ( wl.empty( ) ) {
const auto wd( fw.wd );
if ( wd < 0 ) {
auto const pos( missing_.indexOf( path ) );
assert( pos != -1 );
missing_.remove( pos );
} else {
inotify_rm_watch( fd , wd );
inotify_.remove( wd );
}
fileWatchers_.remove( path );
} else {
i ++;
}
}
}
/*= T_WatchedFiles ===========================================================*/
T_WatchedFiles::T_WatchedFiles( T_WatchedFiles&& other ) noexcept
: watcher( other.watcher ) , callback( other.callback ) ,
triggered( other.triggered )
{
if ( watcher ) {
other.watcher = nullptr;
*( find( watcher->watched_ , &other ) ) = this;
}
}
T_WatchedFiles::T_WatchedFiles(
T_FilesWatcher& watcher ,
const F_OnFileChanges callback )
: watcher( &watcher ) , callback( callback ) , triggered( false )
{
watcher.watched_.add( this );
}
T_WatchedFiles::~T_WatchedFiles( )
{
if ( watcher ) {
watcher->unwatchAll( this );
watcher->watched_.remove( watcher->watched_.indexOf( this ) );
}
}
void T_WatchedFiles::clear( )
{
if ( watcher ) {
watcher->unwatchAll( this );
}
}
bool T_WatchedFiles::watch(
T_String const& file )
{
if ( !watcher ) {
return false;
}
watcher->watchFile( file , this );
return true;
}