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
|
|
|
|
2018-03-30 21:21:14 +02:00
|
|
|
//#define INTRUSIVE_TRACES
|
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 ) ) {
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( "inotify wd %d / evt %x\n" , ie.wd , ie.mask ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#endif // INTRUSIVE_TRACES }}}
|
2017-10-04 11:20:27 +02:00
|
|
|
if ( ( ie.mask & ( IN_CLOSE_WRITE | IN_DELETE_SELF ) ) == 0 ) {
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> skipping this event\n" );
|
2018-03-30 19:16:04 +02:00
|
|
|
#endif // INTRUSIVE_TRACES }}}
|
2017-10-04 11:20:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-30 19:16:04 +02:00
|
|
|
T_FSPath const* namePtr( inotify_.get( ie.wd ) );
|
|
|
|
if ( !namePtr ) {
|
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> no matching file\n" ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#endif // INTRUSIVE_TRACES }}}
|
2017-11-01 12:51:44 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-30 19:16:04 +02:00
|
|
|
const auto name( *namePtr );
|
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
|
|
|
printf( " -> file '%s'\n" , name.toString( ).toOSString( ).data( ) );
|
|
|
|
#endif // INTRUSIVE_TRACES }}}
|
2017-11-01 12:51:44 +01:00
|
|
|
|
2018-03-30 19:16:04 +02:00
|
|
|
auto* const entry( fileWatchers_.get( name ) );
|
2017-11-04 09:17:31 +01:00
|
|
|
assert( entry );
|
2018-03-30 19:16:04 +02:00
|
|
|
const auto wd{ entry->wd };
|
2017-11-04 09:17:31 +01:00
|
|
|
entry->trigger( );
|
2017-11-01 12:51:44 +01:00
|
|
|
|
|
|
|
if ( ( ie.mask & IN_DELETE_SELF ) != 0 ) {
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
|
|
|
printf( " -> %s deleted, added as missing\n" ,
|
|
|
|
name.toString( ).toOSString( ).data( ) );
|
|
|
|
fflush( stdout );
|
|
|
|
#endif // INTRUSIVE_TRACES }}}
|
|
|
|
inotify_rm_watch( fd , wd );
|
|
|
|
missing_.add( name );
|
|
|
|
inotify_.remove( wd );
|
|
|
|
// We need to fetch the correct entry back, as the callback
|
|
|
|
// might have changed the underlying data.
|
|
|
|
auto* const nEntry{ fileWatchers_.get( name ) };
|
|
|
|
assert( nEntry );
|
|
|
|
nEntry->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 );
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-12-28 12:42:58 +01:00
|
|
|
printf( "Checking missing file '%s'\n" , path.toString( ).toOSString( ).data( ) ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#endif // INTRUSIVE_TRACES }}}
|
2017-11-04 09:17:31 +01:00
|
|
|
auto* const p( fileWatchers_.get( path ) );
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> found? %c\n" , p ? 'Y' : 'N' );
|
2018-03-30 19:16:04 +02:00
|
|
|
#endif // INTRUSIVE_TRACES }}}
|
2017-11-04 09:17:31 +01:00
|
|
|
assert( p );
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> wd = %d\n" , p->wd );
|
2018-03-30 19:16:04 +02:00
|
|
|
#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 ,
|
2017-12-28 12:42:58 +01:00
|
|
|
(char*) path.toString( ).toOSString( ).data( ) ,
|
2017-11-04 09:17:31 +01:00
|
|
|
IN_CLOSE_WRITE | IN_DELETE_SELF ) );
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> inotify add watch %d\n" , nwd ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#endif // INTRUSIVE_TRACES }}}
|
2017-11-01 12:51:44 +01:00
|
|
|
if ( nwd < 0 ) {
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> still missing\n" ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#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 ) {
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> triggering\n" ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#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-12-28 12:42:58 +01:00
|
|
|
T_FSPath const& path ,
|
2017-11-05 09:55:37 +01:00
|
|
|
T_WatchSet_* const watcher )
|
2017-11-01 12:51:44 +01:00
|
|
|
{
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-12-28 12:42:58 +01:00
|
|
|
printf( "creating watch on '%s' for watcher %p\n" , path.toString( ).toOSString( ).data( ) , watcher ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#endif // INTRUSIVE_TRACES }}}
|
2017-11-04 09:17:31 +01:00
|
|
|
auto* const exFilePos( fileWatchers_.get( path ) );
|
|
|
|
if ( exFilePos ) {
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> existing\n" ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#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-12-28 12:42:58 +01:00
|
|
|
const auto fstr{ path.toString( ).toOSString( ) };
|
|
|
|
f.wd = inotify_add_watch( fd , (char*) fstr.data( ) ,
|
2017-11-01 12:51:44 +01:00
|
|
|
IN_CLOSE_WRITE | IN_DELETE_SELF );
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> inotify wd %d\n" , f.wd ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#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 );
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> missing\n" ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#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 );
|
2018-03-30 19:16:04 +02:00
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
2017-11-05 09:55:37 +01:00
|
|
|
printf( " -> found\n" ); fflush( stdout );
|
2018-03-30 19:16:04 +02:00
|
|
|
#endif // INTRUSIVE_TRACES }}}
|
|
|
|
const int32_t idx{ missing_.indexOf( path ) };
|
|
|
|
if ( idx >= 0 ) {
|
|
|
|
missing_.removeSwap( idx );
|
|
|
|
#ifdef INTRUSIVE_TRACES // {{{
|
|
|
|
printf( " -> removing from missing files (index %d)\n" , idx ); 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-12-28 12:42:58 +01:00
|
|
|
T_FSPath const& file )
|
2017-10-04 11:20:27 +02:00
|
|
|
{
|
2017-12-28 12:42:58 +01:00
|
|
|
if ( !watchSet_->watcher || !file.isValid( ) ) {
|
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
|
|
|
}
|