diff options
| author | Li Jin <dragon-fly@qq.com> | 2022-11-15 17:23:46 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2022-11-15 17:52:09 +0800 |
| commit | 94f8330613877b3582d32bd11abd83a97b4399ad (patch) | |
| tree | 5359de314be1ebde17f8d1e48632a97d18f9e50f /src/3rdParty/efsw/FileWatcherGeneric.cpp | |
| parent | 60f8f00a022ac08701792b2897b72d8c99b50f52 (diff) | |
| download | yuescript-94f8330613877b3582d32bd11abd83a97b4399ad.tar.gz yuescript-94f8330613877b3582d32bd11abd83a97b4399ad.tar.bz2 yuescript-94f8330613877b3582d32bd11abd83a97b4399ad.zip | |
adding -w option to Yuescript tool.
Diffstat (limited to '')
| -rwxr-xr-x | src/3rdParty/efsw/FileWatcherGeneric.cpp | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/3rdParty/efsw/FileWatcherGeneric.cpp b/src/3rdParty/efsw/FileWatcherGeneric.cpp new file mode 100755 index 0000000..074cff1 --- /dev/null +++ b/src/3rdParty/efsw/FileWatcherGeneric.cpp | |||
| @@ -0,0 +1,156 @@ | |||
| 1 | #include <efsw/FileSystem.hpp> | ||
| 2 | #include <efsw/FileWatcherGeneric.hpp> | ||
| 3 | #include <efsw/Lock.hpp> | ||
| 4 | #include <efsw/System.hpp> | ||
| 5 | |||
| 6 | namespace efsw { | ||
| 7 | |||
| 8 | FileWatcherGeneric::FileWatcherGeneric( FileWatcher* parent ) : | ||
| 9 | FileWatcherImpl( parent ), mThread( NULL ), mLastWatchID( 0 ) { | ||
| 10 | mInitOK = true; | ||
| 11 | mIsGeneric = true; | ||
| 12 | } | ||
| 13 | |||
| 14 | FileWatcherGeneric::~FileWatcherGeneric() { | ||
| 15 | mInitOK = false; | ||
| 16 | |||
| 17 | efSAFE_DELETE( mThread ); | ||
| 18 | |||
| 19 | /// Delete the watches | ||
| 20 | WatchList::iterator it = mWatches.begin(); | ||
| 21 | |||
| 22 | for ( ; it != mWatches.end(); ++it ) { | ||
| 23 | efSAFE_DELETE( ( *it ) ); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | WatchID FileWatcherGeneric::addWatch( const std::string& directory, FileWatchListener* watcher, | ||
| 28 | bool recursive ) { | ||
| 29 | std::string dir( directory ); | ||
| 30 | |||
| 31 | FileSystem::dirAddSlashAtEnd( dir ); | ||
| 32 | |||
| 33 | FileInfo fi( dir ); | ||
| 34 | |||
| 35 | if ( !fi.isDirectory() ) { | ||
| 36 | return Errors::Log::createLastError( Errors::FileNotFound, dir ); | ||
| 37 | } else if ( !fi.isReadable() ) { | ||
| 38 | return Errors::Log::createLastError( Errors::FileNotReadable, dir ); | ||
| 39 | } else if ( pathInWatches( dir ) ) { | ||
| 40 | return Errors::Log::createLastError( Errors::FileRepeated, dir ); | ||
| 41 | } | ||
| 42 | |||
| 43 | std::string curPath; | ||
| 44 | std::string link( FileSystem::getLinkRealPath( dir, curPath ) ); | ||
| 45 | |||
| 46 | if ( "" != link ) { | ||
| 47 | if ( pathInWatches( link ) ) { | ||
| 48 | return Errors::Log::createLastError( Errors::FileRepeated, dir ); | ||
| 49 | } else if ( !linkAllowed( curPath, link ) ) { | ||
| 50 | return Errors::Log::createLastError( Errors::FileOutOfScope, dir ); | ||
| 51 | } else { | ||
| 52 | dir = link; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | mLastWatchID++; | ||
| 57 | |||
| 58 | WatcherGeneric* pWatch = new WatcherGeneric( mLastWatchID, dir, watcher, this, recursive ); | ||
| 59 | |||
| 60 | Lock lock( mWatchesLock ); | ||
| 61 | mWatches.push_back( pWatch ); | ||
| 62 | |||
| 63 | return pWatch->ID; | ||
| 64 | } | ||
| 65 | |||
| 66 | void FileWatcherGeneric::removeWatch( const std::string& directory ) { | ||
| 67 | WatchList::iterator it = mWatches.begin(); | ||
| 68 | |||
| 69 | for ( ; it != mWatches.end(); ++it ) { | ||
| 70 | if ( ( *it )->Directory == directory ) { | ||
| 71 | WatcherGeneric* watch = ( *it ); | ||
| 72 | |||
| 73 | Lock lock( mWatchesLock ); | ||
| 74 | |||
| 75 | mWatches.erase( it ); | ||
| 76 | |||
| 77 | efSAFE_DELETE( watch ); | ||
| 78 | |||
| 79 | return; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | void FileWatcherGeneric::removeWatch( WatchID watchid ) { | ||
| 85 | WatchList::iterator it = mWatches.begin(); | ||
| 86 | |||
| 87 | for ( ; it != mWatches.end(); ++it ) { | ||
| 88 | if ( ( *it )->ID == watchid ) { | ||
| 89 | WatcherGeneric* watch = ( *it ); | ||
| 90 | |||
| 91 | Lock lock( mWatchesLock ); | ||
| 92 | |||
| 93 | mWatches.erase( it ); | ||
| 94 | |||
| 95 | efSAFE_DELETE( watch ); | ||
| 96 | |||
| 97 | return; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | void FileWatcherGeneric::watch() { | ||
| 103 | if ( NULL == mThread ) { | ||
| 104 | mThread = new Thread( &FileWatcherGeneric::run, this ); | ||
| 105 | mThread->launch(); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | void FileWatcherGeneric::run() { | ||
| 110 | do { | ||
| 111 | { | ||
| 112 | Lock lock( mWatchesLock ); | ||
| 113 | |||
| 114 | WatchList::iterator it = mWatches.begin(); | ||
| 115 | |||
| 116 | for ( ; it != mWatches.end(); ++it ) { | ||
| 117 | ( *it )->watch(); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | if ( mInitOK ) | ||
| 122 | System::sleep( 1000 ); | ||
| 123 | } while ( mInitOK ); | ||
| 124 | } | ||
| 125 | |||
| 126 | void FileWatcherGeneric::handleAction( Watcher*, const std::string&, unsigned long, std::string ) { | ||
| 127 | /// Not used | ||
| 128 | } | ||
| 129 | |||
| 130 | std::list<std::string> FileWatcherGeneric::directories() { | ||
| 131 | std::list<std::string> dirs; | ||
| 132 | |||
| 133 | Lock lock( mWatchesLock ); | ||
| 134 | |||
| 135 | WatchList::iterator it = mWatches.begin(); | ||
| 136 | |||
| 137 | for ( ; it != mWatches.end(); ++it ) { | ||
| 138 | dirs.push_back( ( *it )->Directory ); | ||
| 139 | } | ||
| 140 | |||
| 141 | return dirs; | ||
| 142 | } | ||
| 143 | |||
| 144 | bool FileWatcherGeneric::pathInWatches( const std::string& path ) { | ||
| 145 | WatchList::iterator it = mWatches.begin(); | ||
| 146 | |||
| 147 | for ( ; it != mWatches.end(); ++it ) { | ||
| 148 | if ( ( *it )->Directory == path || ( *it )->pathInWatches( path ) ) { | ||
| 149 | return true; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | return false; | ||
| 154 | } | ||
| 155 | |||
| 156 | } // namespace efsw | ||
