File watcher - Use T_FSPath

This commit is contained in:
Emmanuel BENOîT 2017-12-28 12:42:58 +01:00
parent 1e1c2108ee
commit 915d797a77
2 changed files with 15 additions and 15 deletions

View file

@ -38,7 +38,7 @@ void T_FilesWatcher::check( )
continue; continue;
} }
T_String const* name( inotify_.get( ie.wd ) ); T_FSPath const* name( inotify_.get( ie.wd ) );
if ( !name ) { if ( !name ) {
#ifdef INTRUSIVE_TRACES #ifdef INTRUSIVE_TRACES
printf( " -> no matching file\n" ); fflush( stdout ); printf( " -> no matching file\n" ); fflush( stdout );
@ -46,7 +46,7 @@ void T_FilesWatcher::check( )
continue; continue;
} }
#ifdef INTRUSIVE_TRACES #ifdef INTRUSIVE_TRACES
printf( " -> file '%s'\n" , name->toOSString( ).data( ) ); printf( " -> file '%s'\n" , name->toString( ).toOSString( ).data( ) );
#endif // INTRUSIVE_TRACES #endif // INTRUSIVE_TRACES
auto* entry( fileWatchers_.get( *name ) ); auto* entry( fileWatchers_.get( *name ) );
@ -68,7 +68,7 @@ void T_FilesWatcher::check( )
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 );
#ifdef INTRUSIVE_TRACES #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 #endif // INTRUSIVE_TRACES
auto* const p( fileWatchers_.get( path ) ); auto* const p( fileWatchers_.get( path ) );
#ifdef INTRUSIVE_TRACES #ifdef INTRUSIVE_TRACES
@ -82,7 +82,7 @@ void T_FilesWatcher::check( )
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.toString( ).toOSString( ).data( ) ,
IN_CLOSE_WRITE | IN_DELETE_SELF ) ); IN_CLOSE_WRITE | IN_DELETE_SELF ) );
#ifdef INTRUSIVE_TRACES #ifdef INTRUSIVE_TRACES
printf( " -> inotify add watch %d\n" , nwd ); fflush( stdout ); printf( " -> inotify add watch %d\n" , nwd ); fflush( stdout );
@ -124,11 +124,11 @@ void T_FilesWatcher::T_File_::trigger( )
} }
void T_FilesWatcher::watchFile( void T_FilesWatcher::watchFile(
T_String const& path , T_FSPath const& path ,
T_WatchSet_* const watcher ) T_WatchSet_* const watcher )
{ {
#ifdef INTRUSIVE_TRACES #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 #endif // INTRUSIVE_TRACES
auto* const exFilePos( fileWatchers_.get( path ) ); auto* const exFilePos( fileWatchers_.get( path ) );
if ( exFilePos ) { if ( exFilePos ) {
@ -141,7 +141,8 @@ void T_FilesWatcher::watchFile(
} }
} else { } else {
T_File_ f; 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 ); IN_CLOSE_WRITE | IN_DELETE_SELF );
#ifdef INTRUSIVE_TRACES #ifdef INTRUSIVE_TRACES
printf( " -> inotify wd %d\n" , f.wd ); fflush( stdout ); printf( " -> inotify wd %d\n" , f.wd ); fflush( stdout );
@ -231,12 +232,11 @@ void T_WatchedFiles::clear( )
} }
bool T_WatchedFiles::watch( bool T_WatchedFiles::watch(
T_String const& file ) T_FSPath const& file )
{ {
if ( !watchSet_->watcher ) { if ( !watchSet_->watcher || !file.isValid( ) ) {
return false; return false;
} }
watchSet_->watcher->watchFile( file , watchSet_.get( ) ); watchSet_->watcher->watchFile( file , watchSet_.get( ) );
return true; return true;
} }

View file

@ -48,12 +48,12 @@ struct T_FilesWatcher
}; };
int fd; int fd;
T_KeyValueTable< T_String , T_File_ > fileWatchers_; T_KeyValueTable< T_FSPath , T_File_ > fileWatchers_;
T_KeyValueTable< int , T_String > inotify_; T_KeyValueTable< int , T_FSPath > inotify_;
T_Array< T_String > missing_; T_Array< T_FSPath > missing_;
T_Array< T_WatchSet_* > watched_; T_Array< T_WatchSet_* > watched_;
void watchFile( T_String const& path , void watchFile( T_FSPath const& path ,
T_WatchSet_* const watcher ); T_WatchSet_* const watcher );
void unwatchAll( T_WatchSet_* const watcher ); void unwatchAll( T_WatchSet_* const watcher );
}; };
@ -72,7 +72,7 @@ struct T_WatchedFiles
const F_OnFileChanges callback ); const F_OnFileChanges callback );
void clear( ); void clear( );
bool watch( T_String const& file ); bool watch( T_FSPath const& file );
private: private:
T_FilesWatcher::P_WatchSet_ watchSet_; T_FilesWatcher::P_WatchSet_ watchSet_;