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;
|
|
|
|
std::vector< T_WatchedFiles* > watchers;
|
|
|
|
void trigger( );
|
|
|
|
};
|
|
|
|
|
2017-10-04 11:20:27 +02:00
|
|
|
int fd;
|
2017-11-01 12:51:44 +01:00
|
|
|
std::map< std::string , T_File_ > fileWatchers_;
|
|
|
|
std::map< int , std::string > inotify_;
|
|
|
|
std::vector< std::string > missing_;
|
|
|
|
std::vector< T_WatchedFiles* > watched_;
|
|
|
|
|
|
|
|
void watchFile( __rd__ std::string const& path ,
|
|
|
|
__rd__ T_WatchedFiles* const watcher );
|
|
|
|
void unwatchAll( __rd__ 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(
|
|
|
|
__rw__ T_FilesWatcher& watcher ,
|
|
|
|
__rd__ const F_OnFileChanges callback );
|
|
|
|
|
|
|
|
~T_WatchedFiles( );
|
|
|
|
|
|
|
|
void clear( );
|
|
|
|
bool watch( __rd__ std::string const& file );
|
|
|
|
|
|
|
|
private:
|
|
|
|
T_FilesWatcher* watcher;
|
|
|
|
const F_OnFileChanges callback;
|
|
|
|
bool triggered;
|
|
|
|
};
|
2017-10-31 14:21:42 +01:00
|
|
|
using P_WatchedFiles = std::unique_ptr< T_WatchedFiles >;
|