Fixed bug in filewatcher
This commit is contained in:
parent
0ed49a30b0
commit
cd55994f91
2 changed files with 94 additions and 69 deletions
133
filewatcher.cc
133
filewatcher.cc
|
@ -9,59 +9,55 @@ T_FilesWatcher::T_FilesWatcher( )
|
||||||
: fd( inotify_init1( O_NONBLOCK ) )
|
: 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( )
|
T_FilesWatcher::~T_FilesWatcher( )
|
||||||
{
|
{
|
||||||
if ( fd ) {
|
if ( fd ) {
|
||||||
close( fd );
|
close( fd );
|
||||||
}
|
}
|
||||||
for ( T_WatchedFiles* wf : watched_ ) {
|
for ( T_WatchSet_* wf : watched_ ) {
|
||||||
if ( wf ) {
|
wf->watcher = nullptr;
|
||||||
wf->watcher = nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void T_FilesWatcher::check( )
|
void T_FilesWatcher::check( )
|
||||||
{
|
{
|
||||||
// Reset triggered status
|
// Reset triggered status
|
||||||
for ( T_WatchedFiles* wf : watched_ ) {
|
for ( T_WatchSet_* wf : watched_ ) {
|
||||||
wf->triggered = false;
|
wf->triggered = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle events from inotify
|
// Handle events from inotify
|
||||||
inotify_event ie;
|
inotify_event ie;
|
||||||
while ( read( fd , &ie , sizeof( ie ) ) == sizeof( ie ) ) {
|
while ( read( fd , &ie , sizeof( ie ) ) == sizeof( ie ) ) {
|
||||||
// printf( "inotify wd %d / evt %x\n" , ie.wd , ie.mask ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( "inotify wd %d / evt %x\n" , ie.wd , ie.mask ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
if ( ( ie.mask & ( IN_CLOSE_WRITE | IN_DELETE_SELF ) ) == 0 ) {
|
if ( ( ie.mask & ( IN_CLOSE_WRITE | IN_DELETE_SELF ) ) == 0 ) {
|
||||||
// printf( " -> skipping this event\n" );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> skipping this event\n" );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
T_String const* name( inotify_.get( ie.wd ) );
|
T_String const* name( inotify_.get( ie.wd ) );
|
||||||
if ( !name ) {
|
if ( !name ) {
|
||||||
// printf( " -> no matching file\n" ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> no matching file\n" ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
#ifdef INTRUSIVE_TRACES
|
||||||
printf( " -> file '%s'\n" , name->toOSString( ).data( ) );
|
printf( " -> file '%s'\n" , name->toOSString( ).data( ) );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
|
|
||||||
auto* entry( fileWatchers_.get( *name ) );
|
auto* entry( fileWatchers_.get( *name ) );
|
||||||
assert( entry );
|
assert( entry );
|
||||||
entry->trigger( );
|
entry->trigger( );
|
||||||
|
|
||||||
if ( ( ie.mask & IN_DELETE_SELF ) != 0 ) {
|
if ( ( ie.mask & IN_DELETE_SELF ) != 0 ) {
|
||||||
//printf( " -> deleted, added as missing\n" ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> deleted, added as missing\n" ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
inotify_rm_watch( fd , entry->wd );
|
inotify_rm_watch( fd , entry->wd );
|
||||||
missing_.add( *name );
|
missing_.add( *name );
|
||||||
inotify_.remove( entry->wd );
|
inotify_.remove( entry->wd );
|
||||||
|
@ -72,27 +68,39 @@ void T_FilesWatcher::check( )
|
||||||
// Handle missing/deleted files
|
// Handle missing/deleted files
|
||||||
for ( auto it = missing_.begin( ) ; it < missing_.end( ) ; ++it ) {
|
for ( auto it = missing_.begin( ) ; it < missing_.end( ) ; ++it ) {
|
||||||
auto const& path( *it );
|
auto const& path( *it );
|
||||||
//printf( "Checking missing file '%s'\n" , path.c_str( ) ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( "Checking missing file '%s'\n" , path.toOSString( ).data( ) ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
auto* const p( fileWatchers_.get( path ) );
|
auto* const p( fileWatchers_.get( path ) );
|
||||||
//printf( " -> found? %c\n" , p != fileWatchers_.end( ) ? 'Y' : 'N' );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> found? %c\n" , p ? 'Y' : 'N' );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
assert( p );
|
assert( p );
|
||||||
//printf( " -> wd = %d\n" , p->second.wd );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> wd = %d\n" , p->wd );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
assert( p->wd < 0 );
|
assert( p->wd < 0 );
|
||||||
auto& we( *p );
|
auto& we( *p );
|
||||||
|
|
||||||
const auto nwd( inotify_add_watch( fd ,
|
const auto nwd( inotify_add_watch( fd ,
|
||||||
(char*) path.toOSString( ).data( ) ,
|
(char*) path.toOSString( ).data( ) ,
|
||||||
IN_CLOSE_WRITE | IN_DELETE_SELF ) );
|
IN_CLOSE_WRITE | IN_DELETE_SELF ) );
|
||||||
//printf( " -> inotify add watch %d\n" , nwd ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> inotify add watch %d\n" , nwd ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
if ( nwd < 0 ) {
|
if ( nwd < 0 ) {
|
||||||
//printf( " -> still missing\n" ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> still missing\n" ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
we.wd = -1;
|
we.wd = -1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trigger if this was really a missing file
|
// Trigger if this was really a missing file
|
||||||
if ( we.wd == -1 ) {
|
if ( we.wd == -1 ) {
|
||||||
//printf( " -> triggering\n" ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> triggering\n" ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
we.trigger( );
|
we.trigger( );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +116,7 @@ void T_FilesWatcher::check( )
|
||||||
|
|
||||||
void T_FilesWatcher::T_File_::trigger( )
|
void T_FilesWatcher::T_File_::trigger( )
|
||||||
{
|
{
|
||||||
for ( T_WatchedFiles* const wf : watchers ) {
|
for ( T_WatchSet_* const wf : watchers ) {
|
||||||
if ( !wf->triggered ) {
|
if ( !wf->triggered ) {
|
||||||
wf->triggered = true;
|
wf->triggered = true;
|
||||||
wf->callback( );
|
wf->callback( );
|
||||||
|
@ -118,12 +126,16 @@ void T_FilesWatcher::T_File_::trigger( )
|
||||||
|
|
||||||
void T_FilesWatcher::watchFile(
|
void T_FilesWatcher::watchFile(
|
||||||
T_String const& path ,
|
T_String const& path ,
|
||||||
T_WatchedFiles* const watcher )
|
T_WatchSet_* const watcher )
|
||||||
{
|
{
|
||||||
//printf( "creating watch on '%s' for watcher %p\n" , path.c_str( ) , watcher ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( "creating watch on '%s' for watcher %p\n" , path.toOSString( ).data( ) , watcher ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
auto* const exFilePos( fileWatchers_.get( path ) );
|
auto* const exFilePos( fileWatchers_.get( path ) );
|
||||||
if ( exFilePos ) {
|
if ( exFilePos ) {
|
||||||
//printf( " -> existing\n" ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> existing\n" ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
auto& wl( exFilePos->watchers );
|
auto& wl( exFilePos->watchers );
|
||||||
if ( !wl.contains( watcher ) ) {
|
if ( !wl.contains( watcher ) ) {
|
||||||
wl.add( watcher );
|
wl.add( watcher );
|
||||||
|
@ -132,13 +144,19 @@ void T_FilesWatcher::watchFile(
|
||||||
T_File_ f;
|
T_File_ f;
|
||||||
f.wd = inotify_add_watch( fd , (char*) path.toOSString( ).data( ) ,
|
f.wd = inotify_add_watch( fd , (char*) path.toOSString( ).data( ) ,
|
||||||
IN_CLOSE_WRITE | IN_DELETE_SELF );
|
IN_CLOSE_WRITE | IN_DELETE_SELF );
|
||||||
//printf( " -> inotify wd %d\n" , f.wd ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> inotify wd %d\n" , f.wd ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
if ( f.wd == -1 ) {
|
if ( f.wd == -1 ) {
|
||||||
missing_.add( path );
|
missing_.add( path );
|
||||||
//printf( " -> missing\n" ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> missing\n" ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
} else {
|
} else {
|
||||||
inotify_.add( f.wd , path );
|
inotify_.add( f.wd , path );
|
||||||
//printf( " -> found\n" ); fflush( stdout );
|
#ifdef INTRUSIVE_TRACES
|
||||||
|
printf( " -> found\n" ); fflush( stdout );
|
||||||
|
#endif // INTRUSIVE_TRACES
|
||||||
}
|
}
|
||||||
f.watchers.add( watcher );
|
f.watchers.add( watcher );
|
||||||
fileWatchers_.add( path , std::move( f ) );
|
fileWatchers_.add( path , std::move( f ) );
|
||||||
|
@ -146,7 +164,7 @@ void T_FilesWatcher::watchFile(
|
||||||
}
|
}
|
||||||
|
|
||||||
void T_FilesWatcher::unwatchAll(
|
void T_FilesWatcher::unwatchAll(
|
||||||
T_WatchedFiles* const watcher )
|
T_WatchSet_* const watcher )
|
||||||
{
|
{
|
||||||
for ( auto i = 0u ; i < fileWatchers_.size( ) ; ) {
|
for ( auto i = 0u ; i < fileWatchers_.size( ) ; ) {
|
||||||
auto const& path( fileWatchers_.keys( )[ i ] );
|
auto const& path( fileWatchers_.keys( )[ i ] );
|
||||||
|
@ -176,28 +194,17 @@ void T_FilesWatcher::unwatchAll(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
/*= T_WatchedFiles ===========================================================*/
|
T_FilesWatcher::T_WatchSet_::T_WatchSet_( T_FilesWatcher* watcher ,
|
||||||
|
F_OnFileChanges callback ) noexcept
|
||||||
T_WatchedFiles::T_WatchedFiles( T_WatchedFiles&& other ) noexcept
|
: watcher( watcher ) , callback( std::move( callback ) ) ,
|
||||||
: watcher( other.watcher ) , callback( other.callback ) ,
|
triggered( false )
|
||||||
triggered( other.triggered )
|
|
||||||
{
|
{
|
||||||
if ( watcher ) {
|
watcher->watched_.add( this );
|
||||||
other.watcher = nullptr;
|
|
||||||
*( find( watcher->watched_ , &other ) ) = this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
T_WatchedFiles::T_WatchedFiles(
|
T_FilesWatcher::T_WatchSet_::~T_WatchSet_( )
|
||||||
T_FilesWatcher& watcher ,
|
|
||||||
const F_OnFileChanges callback )
|
|
||||||
: watcher( &watcher ) , callback( callback ) , triggered( false )
|
|
||||||
{
|
|
||||||
watcher.watched_.add( this );
|
|
||||||
}
|
|
||||||
|
|
||||||
T_WatchedFiles::~T_WatchedFiles( )
|
|
||||||
{
|
{
|
||||||
if ( watcher ) {
|
if ( watcher ) {
|
||||||
watcher->unwatchAll( this );
|
watcher->unwatchAll( this );
|
||||||
|
@ -205,20 +212,32 @@ T_WatchedFiles::~T_WatchedFiles( )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*= 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 ) ) )
|
||||||
|
{ }
|
||||||
|
|
||||||
void T_WatchedFiles::clear( )
|
void T_WatchedFiles::clear( )
|
||||||
{
|
{
|
||||||
if ( watcher ) {
|
if ( watchSet_->watcher ) {
|
||||||
watcher->unwatchAll( this );
|
watchSet_->watcher->unwatchAll( watchSet_.get( ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool T_WatchedFiles::watch(
|
bool T_WatchedFiles::watch(
|
||||||
T_String const& file )
|
T_String const& file )
|
||||||
{
|
{
|
||||||
if ( !watcher ) {
|
if ( !watchSet_->watcher ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
watcher->watchFile( file , this );
|
watchSet_->watcher->watchFile( file , watchSet_.get( ) );
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,17 +22,28 @@ struct T_FilesWatcher
|
||||||
friend struct T_WatchedFiles;
|
friend struct T_WatchedFiles;
|
||||||
|
|
||||||
T_FilesWatcher( T_FilesWatcher const& ) = delete;
|
T_FilesWatcher( T_FilesWatcher const& ) = delete;
|
||||||
|
T_FilesWatcher( T_FilesWatcher&& ) = delete;
|
||||||
|
|
||||||
T_FilesWatcher( );
|
T_FilesWatcher( );
|
||||||
T_FilesWatcher( T_FilesWatcher&& ) noexcept;
|
|
||||||
~T_FilesWatcher( );
|
~T_FilesWatcher( );
|
||||||
|
|
||||||
void check( );
|
void check( );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct T_WatchSet_ {
|
||||||
|
T_FilesWatcher* watcher;
|
||||||
|
F_OnFileChanges callback;
|
||||||
|
bool triggered;
|
||||||
|
|
||||||
|
T_WatchSet_( T_FilesWatcher* watcher ,
|
||||||
|
F_OnFileChanges callback ) noexcept;
|
||||||
|
~T_WatchSet_( );
|
||||||
|
};
|
||||||
|
using P_WatchSet_ = T_OwnPtr< T_WatchSet_ >;
|
||||||
|
|
||||||
struct T_File_ {
|
struct T_File_ {
|
||||||
int wd;
|
int wd;
|
||||||
T_Array< T_WatchedFiles* > watchers;
|
T_Array< T_WatchSet_* > watchers;
|
||||||
void trigger( );
|
void trigger( );
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -40,11 +51,11 @@ struct T_FilesWatcher
|
||||||
T_KeyValueTable< T_String , T_File_ > fileWatchers_;
|
T_KeyValueTable< T_String , T_File_ > fileWatchers_;
|
||||||
T_KeyValueTable< int , T_String > inotify_;
|
T_KeyValueTable< int , T_String > inotify_;
|
||||||
T_Array< T_String > missing_;
|
T_Array< T_String > missing_;
|
||||||
T_Array< T_WatchedFiles* > watched_;
|
T_Array< T_WatchSet_* > watched_;
|
||||||
|
|
||||||
void watchFile( T_String const& path ,
|
void watchFile( T_String const& path ,
|
||||||
T_WatchedFiles* const watcher );
|
T_WatchSet_* const watcher );
|
||||||
void unwatchAll( T_WatchedFiles* const watcher );
|
void unwatchAll( T_WatchSet_* const watcher );
|
||||||
};
|
};
|
||||||
|
|
||||||
/*----------------------------------------------------------------------------*/
|
/*----------------------------------------------------------------------------*/
|
||||||
|
@ -57,18 +68,13 @@ struct T_WatchedFiles
|
||||||
T_WatchedFiles( T_WatchedFiles const& ) = delete;
|
T_WatchedFiles( T_WatchedFiles const& ) = delete;
|
||||||
|
|
||||||
T_WatchedFiles( T_WatchedFiles&& ) noexcept;
|
T_WatchedFiles( T_WatchedFiles&& ) noexcept;
|
||||||
T_WatchedFiles(
|
T_WatchedFiles( T_FilesWatcher& watcher ,
|
||||||
T_FilesWatcher& watcher ,
|
|
||||||
const F_OnFileChanges callback );
|
const F_OnFileChanges callback );
|
||||||
|
|
||||||
~T_WatchedFiles( );
|
|
||||||
|
|
||||||
void clear( );
|
void clear( );
|
||||||
bool watch( T_String const& file );
|
bool watch( T_String const& file );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T_FilesWatcher* watcher;
|
T_FilesWatcher::P_WatchSet_ watchSet_;
|
||||||
const F_OnFileChanges callback;
|
|
||||||
bool triggered;
|
|
||||||
};
|
};
|
||||||
using P_WatchedFiles = T_OwnPtr< T_WatchedFiles >;
|
using P_WatchedFiles = T_OwnPtr< T_WatchedFiles >;
|
||||||
|
|
Loading…
Reference in a new issue