2017-10-04 11:20:27 +02:00
|
|
|
#include "externals.hh"
|
2017-11-23 23:05:14 +01:00
|
|
|
#include "c-filewatcher.hh"
|
2017-10-04 11:20:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*= T_FilesWatcher ===========================================================*/
|
|
|
|
|
|
|
|
T_FilesWatcher::T_FilesWatcher( )
|
|
|
|
: fd( inotify_init1( O_NONBLOCK ) )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
T_FilesWatcher::~T_FilesWatcher( )
|
|
|
|
{
|
|
|
|
if ( fd ) {
|
|
|
|
close( fd );
|
|
|
|
}
|
2017-11-05 09:55:37 +01:00
|
|
|
for ( T_WatchSet_* wf : watched_ ) {
|
|
|
|
wf->watcher = nullptr;
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_FilesWatcher::check( )
|
|
|
|
{
|
2017-11-01 12:51:44 +01:00
|
|
|
// Reset triggered status
|
2017-11-05 09:55:37 +01:00
|
|
|
for ( T_WatchSet_* wf : watched_ ) {
|
2017-11-01 12:51:44 +01:00
|
|
|
wf->triggered = false;
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 12:51:44 +01:00
|
|
|
// Handle events from inotify
|
2017-10-04 11:20:27 +02:00
|
|
|
inotify_event ie;
|
|
|
|
while ( read( fd , &ie , sizeof( ie ) ) == sizeof( ie ) ) {
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( "inotify wd %d / evt %x\n" , ie.wd , ie.mask ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-10-04 11:20:27 +02:00
|
|
|
if ( ( ie.mask & ( IN_CLOSE_WRITE | IN_DELETE_SELF ) ) == 0 ) {
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> skipping this event\n" );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-10-04 11:20:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-04 09:17:31 +01:00
|
|
|
T_String const* name( inotify_.get( ie.wd ) );
|
|
|
|
if ( !name ) {
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> no matching file\n" ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-01 12:51:44 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
2017-11-04 09:17:31 +01:00
|
|
|
printf( " -> file '%s'\n" , name->toOSString( ).data( ) );
|
2017-11-05 09:55:37 +01:00
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-01 12:51:44 +01:00
|
|
|
|
2017-11-04 09:17:31 +01:00
|
|
|
auto* entry( fileWatchers_.get( *name ) );
|
|
|
|
assert( entry );
|
|
|
|
entry->trigger( );
|
2017-11-01 12:51:44 +01:00
|
|
|
|
|
|
|
if ( ( ie.mask & IN_DELETE_SELF ) != 0 ) {
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> deleted, added as missing\n" ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-04 09:17:31 +01:00
|
|
|
inotify_rm_watch( fd , entry->wd );
|
|
|
|
missing_.add( *name );
|
|
|
|
inotify_.remove( entry->wd );
|
|
|
|
entry->wd = -2;
|
2017-11-01 12:51:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle missing/deleted files
|
|
|
|
for ( auto it = missing_.begin( ) ; it < missing_.end( ) ; ++it ) {
|
|
|
|
auto const& path( *it );
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( "Checking missing file '%s'\n" , path.toOSString( ).data( ) ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-04 09:17:31 +01:00
|
|
|
auto* const p( fileWatchers_.get( path ) );
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> found? %c\n" , p ? 'Y' : 'N' );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-04 09:17:31 +01:00
|
|
|
assert( p );
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> wd = %d\n" , p->wd );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-04 09:17:31 +01:00
|
|
|
assert( p->wd < 0 );
|
|
|
|
auto& we( *p );
|
2017-11-01 12:51:44 +01:00
|
|
|
|
2017-11-04 09:17:31 +01:00
|
|
|
const auto nwd( inotify_add_watch( fd ,
|
|
|
|
(char*) path.toOSString( ).data( ) ,
|
|
|
|
IN_CLOSE_WRITE | IN_DELETE_SELF ) );
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> inotify add watch %d\n" , nwd ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-01 12:51:44 +01:00
|
|
|
if ( nwd < 0 ) {
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> still missing\n" ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-01 12:51:44 +01:00
|
|
|
we.wd = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger if this was really a missing file
|
|
|
|
if ( we.wd == -1 ) {
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> triggering\n" ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-01 12:51:44 +01:00
|
|
|
we.trigger( );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update cache
|
|
|
|
we.wd = nwd;
|
2017-11-04 09:17:31 +01:00
|
|
|
inotify_.add( nwd , path );
|
|
|
|
missing_.remove( it.pos( ) );
|
|
|
|
it --;
|
2017-11-01 12:51:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void T_FilesWatcher::T_File_::trigger( )
|
|
|
|
{
|
2017-11-05 09:55:37 +01:00
|
|
|
for ( T_WatchSet_* const wf : watchers ) {
|
2017-11-01 12:51:44 +01:00
|
|
|
if ( !wf->triggered ) {
|
|
|
|
wf->triggered = true;
|
|
|
|
wf->callback( );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_FilesWatcher::watchFile(
|
2017-11-04 09:17:31 +01:00
|
|
|
T_String const& path ,
|
2017-11-05 09:55:37 +01:00
|
|
|
T_WatchSet_* const watcher )
|
2017-11-01 12:51:44 +01:00
|
|
|
{
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( "creating watch on '%s' for watcher %p\n" , path.toOSString( ).data( ) , watcher ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-04 09:17:31 +01:00
|
|
|
auto* const exFilePos( fileWatchers_.get( path ) );
|
|
|
|
if ( exFilePos ) {
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> existing\n" ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-04 09:17:31 +01:00
|
|
|
auto& wl( exFilePos->watchers );
|
|
|
|
if ( !wl.contains( watcher ) ) {
|
|
|
|
wl.add( watcher );
|
2017-11-01 12:51:44 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
T_File_ f;
|
2017-11-04 09:17:31 +01:00
|
|
|
f.wd = inotify_add_watch( fd , (char*) path.toOSString( ).data( ) ,
|
2017-11-01 12:51:44 +01:00
|
|
|
IN_CLOSE_WRITE | IN_DELETE_SELF );
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> inotify wd %d\n" , f.wd ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-01 12:51:44 +01:00
|
|
|
if ( f.wd == -1 ) {
|
2017-11-04 09:17:31 +01:00
|
|
|
missing_.add( path );
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> missing\n" ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-01 12:51:44 +01:00
|
|
|
} else {
|
2017-11-04 09:17:31 +01:00
|
|
|
inotify_.add( f.wd , path );
|
2017-11-05 09:55:37 +01:00
|
|
|
#ifdef INTRUSIVE_TRACES
|
|
|
|
printf( " -> found\n" ); fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES
|
2017-11-01 12:51:44 +01:00
|
|
|
}
|
2017-11-04 09:17:31 +01:00
|
|
|
f.watchers.add( watcher );
|
|
|
|
fileWatchers_.add( path , std::move( f ) );
|
2017-11-01 12:51:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_FilesWatcher::unwatchAll(
|
2017-11-05 09:55:37 +01:00
|
|
|
T_WatchSet_* const watcher )
|
2017-11-01 12:51:44 +01:00
|
|
|
{
|
2017-11-04 09:17:31 +01:00
|
|
|
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 ++;
|
2017-11-01 12:51:44 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-04 09:17:31 +01:00
|
|
|
|
|
|
|
wl.remove( pos );
|
2017-11-01 12:51:44 +01:00
|
|
|
if ( wl.empty( ) ) {
|
2017-11-04 09:17:31 +01:00
|
|
|
const auto wd( fw.wd );
|
2017-11-01 12:51:44 +01:00
|
|
|
if ( wd < 0 ) {
|
2017-11-04 09:17:31 +01:00
|
|
|
auto const pos( missing_.indexOf( path ) );
|
|
|
|
assert( pos != -1 );
|
|
|
|
missing_.remove( pos );
|
2017-11-01 12:51:44 +01:00
|
|
|
} else {
|
|
|
|
inotify_rm_watch( fd , wd );
|
2017-11-04 09:17:31 +01:00
|
|
|
inotify_.remove( wd );
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
2017-11-04 09:17:31 +01:00
|
|
|
fileWatchers_.remove( path );
|
|
|
|
} else {
|
|
|
|
i ++;
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 09:55:37 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-11-01 12:51:44 +01:00
|
|
|
|
2017-11-05 09:55:37 +01:00
|
|
|
T_FilesWatcher::T_WatchSet_::T_WatchSet_( T_FilesWatcher* watcher ,
|
|
|
|
F_OnFileChanges callback ) noexcept
|
|
|
|
: watcher( watcher ) , callback( std::move( callback ) ) ,
|
|
|
|
triggered( false )
|
2017-10-04 11:20:27 +02:00
|
|
|
{
|
2017-11-05 09:55:37 +01:00
|
|
|
watcher->watched_.add( this );
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 09:55:37 +01:00
|
|
|
T_FilesWatcher::T_WatchSet_::~T_WatchSet_( )
|
2017-10-04 11:20:27 +02:00
|
|
|
{
|
|
|
|
if ( watcher ) {
|
2017-11-01 12:51:44 +01:00
|
|
|
watcher->unwatchAll( this );
|
2017-11-04 09:17:31 +01:00
|
|
|
watcher->watched_.remove( watcher->watched_.indexOf( this ) );
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 09:55:37 +01:00
|
|
|
/*= T_WatchedFiles ===========================================================*/
|
|
|
|
|
|
|
|
T_WatchedFiles::T_WatchedFiles( T_WatchedFiles&& other ) noexcept
|
|
|
|
: watchSet_( std::move( other.watchSet_ ) )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
T_WatchedFiles::T_WatchedFiles(
|
|
|
|
T_FilesWatcher& watcher ,
|
|
|
|
F_OnFileChanges callback )
|
|
|
|
: watchSet_( NewOwned< T_FilesWatcher::T_WatchSet_ >( &watcher , std::move( callback ) ) )
|
|
|
|
{ }
|
|
|
|
|
2017-10-04 11:20:27 +02:00
|
|
|
void T_WatchedFiles::clear( )
|
|
|
|
{
|
2017-11-05 09:55:37 +01:00
|
|
|
if ( watchSet_->watcher ) {
|
|
|
|
watchSet_->watcher->unwatchAll( watchSet_.get( ) );
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool T_WatchedFiles::watch(
|
2017-11-04 09:17:31 +01:00
|
|
|
T_String const& file )
|
2017-10-04 11:20:27 +02:00
|
|
|
{
|
2017-11-05 09:55:37 +01:00
|
|
|
if ( !watchSet_->watcher ) {
|
2017-11-01 12:51:44 +01:00
|
|
|
return false;
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
2017-11-05 09:55:37 +01:00
|
|
|
watchSet_->watcher->watchFile( file , watchSet_.get( ) );
|
2017-11-01 12:51:44 +01:00
|
|
|
return true;
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
|