From 915d797a770c8471170dfd0fcff393e798da2deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Thu, 28 Dec 2017 12:42:58 +0100 Subject: [PATCH] File watcher - Use T_FSPath --- c-filewatcher.cc | 20 ++++++++++---------- c-filewatcher.hh | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/c-filewatcher.cc b/c-filewatcher.cc index e28b5e6..f6ae18d 100644 --- a/c-filewatcher.cc +++ b/c-filewatcher.cc @@ -38,7 +38,7 @@ void T_FilesWatcher::check( ) continue; } - T_String const* name( inotify_.get( ie.wd ) ); + T_FSPath const* name( inotify_.get( ie.wd ) ); if ( !name ) { #ifdef INTRUSIVE_TRACES printf( " -> no matching file\n" ); fflush( stdout ); @@ -46,7 +46,7 @@ void T_FilesWatcher::check( ) continue; } #ifdef INTRUSIVE_TRACES - printf( " -> file '%s'\n" , name->toOSString( ).data( ) ); + printf( " -> file '%s'\n" , name->toString( ).toOSString( ).data( ) ); #endif // INTRUSIVE_TRACES auto* entry( fileWatchers_.get( *name ) ); @@ -68,7 +68,7 @@ void T_FilesWatcher::check( ) for ( auto it = missing_.begin( ) ; it < missing_.end( ) ; ++it ) { auto const& path( *it ); #ifdef INTRUSIVE_TRACES - printf( "Checking missing file '%s'\n" , path.toOSString( ).data( ) ); fflush( stdout ); + printf( "Checking missing file '%s'\n" , path.toString( ).toOSString( ).data( ) ); fflush( stdout ); #endif // INTRUSIVE_TRACES auto* const p( fileWatchers_.get( path ) ); #ifdef INTRUSIVE_TRACES @@ -82,7 +82,7 @@ void T_FilesWatcher::check( ) auto& we( *p ); const auto nwd( inotify_add_watch( fd , - (char*) path.toOSString( ).data( ) , + (char*) path.toString( ).toOSString( ).data( ) , IN_CLOSE_WRITE | IN_DELETE_SELF ) ); #ifdef INTRUSIVE_TRACES printf( " -> inotify add watch %d\n" , nwd ); fflush( stdout ); @@ -124,11 +124,11 @@ void T_FilesWatcher::T_File_::trigger( ) } void T_FilesWatcher::watchFile( - T_String const& path , + T_FSPath const& path , T_WatchSet_* const watcher ) { #ifdef INTRUSIVE_TRACES - printf( "creating watch on '%s' for watcher %p\n" , path.toOSString( ).data( ) , watcher ); fflush( stdout ); + printf( "creating watch on '%s' for watcher %p\n" , path.toString( ).toOSString( ).data( ) , watcher ); fflush( stdout ); #endif // INTRUSIVE_TRACES auto* const exFilePos( fileWatchers_.get( path ) ); if ( exFilePos ) { @@ -141,7 +141,8 @@ void T_FilesWatcher::watchFile( } } else { T_File_ f; - f.wd = inotify_add_watch( fd , (char*) path.toOSString( ).data( ) , + const auto fstr{ path.toString( ).toOSString( ) }; + f.wd = inotify_add_watch( fd , (char*) fstr.data( ) , IN_CLOSE_WRITE | IN_DELETE_SELF ); #ifdef INTRUSIVE_TRACES printf( " -> inotify wd %d\n" , f.wd ); fflush( stdout ); @@ -231,12 +232,11 @@ void T_WatchedFiles::clear( ) } bool T_WatchedFiles::watch( - T_String const& file ) + T_FSPath const& file ) { - if ( !watchSet_->watcher ) { + if ( !watchSet_->watcher || !file.isValid( ) ) { return false; } watchSet_->watcher->watchFile( file , watchSet_.get( ) ); return true; } - diff --git a/c-filewatcher.hh b/c-filewatcher.hh index b337f2e..bd22ede 100644 --- a/c-filewatcher.hh +++ b/c-filewatcher.hh @@ -48,12 +48,12 @@ struct T_FilesWatcher }; int fd; - T_KeyValueTable< T_String , T_File_ > fileWatchers_; - T_KeyValueTable< int , T_String > inotify_; - T_Array< T_String > missing_; + T_KeyValueTable< T_FSPath , T_File_ > fileWatchers_; + T_KeyValueTable< int , T_FSPath > inotify_; + T_Array< T_FSPath > missing_; T_Array< T_WatchSet_* > watched_; - void watchFile( T_String const& path , + void watchFile( T_FSPath const& path , T_WatchSet_* const watcher ); void unwatchAll( T_WatchSet_* const watcher ); }; @@ -72,7 +72,7 @@ struct T_WatchedFiles const F_OnFileChanges callback ); void clear( ); - bool watch( T_String const& file ); + bool watch( T_FSPath const& file ); private: T_FilesWatcher::P_WatchSet_ watchSet_;