2017-10-04 11:20:27 +02:00
|
|
|
#pragma once
|
|
|
|
#ifndef REAL_BUILD
|
|
|
|
# include "externals.hh"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*= T_FilesWatcher / T_WatchedFiles ==========================================*/
|
|
|
|
|
2017-11-01 12:51:44 +01:00
|
|
|
/* So we need to be able to watch for files being created, updated and deleted.
|
|
|
|
* Watching for updates & deletions is pretty easy. For files being created,
|
|
|
|
* one has to watch the parent directory. In addition, the case where a file
|
|
|
|
* is being updated by rotation (move old, write new, rm old) needs to be
|
|
|
|
* handled correctly as it is what vim does.
|
|
|
|
*/
|
|
|
|
|
2017-10-04 11:20:27 +02:00
|
|
|
struct T_FilesWatcher;
|
|
|
|
struct T_WatchedFiles;
|
|
|
|
using F_OnFileChanges = std::function< void( void ) >;
|
|
|
|
|
|
|
|
struct T_FilesWatcher
|
|
|
|
{
|
|
|
|
friend struct T_WatchedFiles;
|
|
|
|
|
|
|
|
T_FilesWatcher( T_FilesWatcher const& ) = delete;
|
|
|
|
|
|
|
|
T_FilesWatcher( );
|
|
|
|
T_FilesWatcher( T_FilesWatcher&& ) noexcept;
|
|
|
|
~T_FilesWatcher( );
|
|
|
|
|
|
|
|
void check( );
|
|
|
|
|
|
|
|
private:
|
2017-11-01 12:51:44 +01:00
|
|
|
struct T_File_ {
|
|
|
|
int wd;
|
2017-11-04 09:17:31 +01:00
|
|
|
T_Array< T_WatchedFiles* > watchers;
|
2017-11-01 12:51:44 +01:00
|
|
|
void trigger( );
|
|
|
|
};
|
|
|
|
|
2017-10-04 11:20:27 +02:00
|
|
|
int fd;
|
2017-11-04 09:17:31 +01:00
|
|
|
T_KeyValueTable< T_String , T_File_ > fileWatchers_;
|
|
|
|
T_KeyValueTable< int , T_String > inotify_;
|
|
|
|
T_Array< T_String > missing_;
|
|
|
|
T_Array< T_WatchedFiles* > watched_;
|
|
|
|
|
|
|
|
void watchFile( T_String const& path ,
|
|
|
|
T_WatchedFiles* const watcher );
|
|
|
|
void unwatchAll( T_WatchedFiles* const watcher );
|
2017-10-04 11:20:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
struct T_WatchedFiles
|
|
|
|
{
|
|
|
|
friend struct T_FilesWatcher;
|
|
|
|
|
|
|
|
T_WatchedFiles( ) = delete;
|
|
|
|
T_WatchedFiles( T_WatchedFiles const& ) = delete;
|
|
|
|
|
|
|
|
T_WatchedFiles( T_WatchedFiles&& ) noexcept;
|
|
|
|
T_WatchedFiles(
|
2017-11-04 09:17:31 +01:00
|
|
|
T_FilesWatcher& watcher ,
|
|
|
|
const F_OnFileChanges callback );
|
2017-10-04 11:20:27 +02:00
|
|
|
|
|
|
|
~T_WatchedFiles( );
|
|
|
|
|
|
|
|
void clear( );
|
2017-11-04 09:17:31 +01:00
|
|
|
bool watch( T_String const& file );
|
2017-10-04 11:20:27 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
T_FilesWatcher* watcher;
|
|
|
|
const F_OnFileChanges callback;
|
|
|
|
bool triggered;
|
|
|
|
};
|
2017-11-04 09:17:31 +01:00
|
|
|
using P_WatchedFiles = T_OwnPtr< T_WatchedFiles >;
|