mirror of
				https://github.com/pocoproject/poco.git
				synced 2025-10-27 19:10:20 +01:00 
			
		
		
		
	Optional force scan for DirectoryWatcher #853
This commit is contained in:
		| @@ -63,6 +63,8 @@ class Foundation_API DirectoryWatcher: protected Runnable | |||||||
| 	/// Therefore, the interval in which scans are done can be specified in | 	/// Therefore, the interval in which scans are done can be specified in | ||||||
| 	/// the constructor. Note that periodic scanning will also be done on FreeBSD | 	/// the constructor. Note that periodic scanning will also be done on FreeBSD | ||||||
| 	/// and Darwin if events for changes to files (DW_ITEM_MODIFIED) are enabled. | 	/// and Darwin if events for changes to files (DW_ITEM_MODIFIED) are enabled. | ||||||
|  | 	/// To avoid problems (e.g. with network shares notifications), scanning | ||||||
|  | 	/// can be forced as the only mechanism, regardless of platform default. | ||||||
| 	/// | 	/// | ||||||
| 	/// DW_ITEM_MOVED_FROM and DW_ITEM_MOVED_TO events will only be reported | 	/// DW_ITEM_MOVED_FROM and DW_ITEM_MOVED_TO events will only be reported | ||||||
| 	/// on Linux. On other platforms, a file rename or move operation | 	/// on Linux. On other platforms, a file rename or move operation | ||||||
| @@ -134,21 +136,29 @@ public: | |||||||
| 	BasicEvent<const Exception> scanError; | 	BasicEvent<const Exception> scanError; | ||||||
| 		/// Fired when an error occurs while scanning for changes. | 		/// Fired when an error occurs while scanning for changes. | ||||||
| 	 | 	 | ||||||
| 	DirectoryWatcher(const std::string& path, int eventMask = DW_FILTER_ENABLE_ALL, int scanInterval = DW_DEFAULT_SCAN_INTERVAL); | 	DirectoryWatcher(const std::string& path, | ||||||
|  | 		int eventMask = DW_FILTER_ENABLE_ALL, | ||||||
|  | 		int scanInterval = DW_DEFAULT_SCAN_INTERVAL, | ||||||
|  | 		bool forceScan = false); | ||||||
| 		/// Creates a DirectoryWatcher for the directory given in path. | 		/// Creates a DirectoryWatcher for the directory given in path. | ||||||
| 		/// To enable only specific events, an eventMask can be specified by | 		/// To enable only specific events, an eventMask can be specified by | ||||||
| 		/// OR-ing the desired event IDs (e.g., DW_ITEM_ADDED | DW_ITEM_MODIFIED). | 		/// OR-ing the desired event IDs (e.g., DW_ITEM_ADDED | DW_ITEM_MODIFIED). | ||||||
| 		/// On platforms where no native filesystem notifications are available, | 		/// On platforms where no native filesystem notifications are available, | ||||||
| 		/// scanInterval specifies the interval in seconds between scans | 		/// scanInterval specifies the interval in seconds between scans | ||||||
| 		/// of the directory. | 		/// of the directory. Native notification mechanism can also be disabled | ||||||
|  | 		/// (i.e. replaced with scanning) by setting forceScan to true. | ||||||
| 		 | 		 | ||||||
| 	DirectoryWatcher(const File& directory, int eventMask = DW_FILTER_ENABLE_ALL, int scanInterval = DW_DEFAULT_SCAN_INTERVAL); | 	DirectoryWatcher(const File& directory, | ||||||
|  | 		int eventMask = DW_FILTER_ENABLE_ALL, | ||||||
|  | 		int scanInterval = DW_DEFAULT_SCAN_INTERVAL, | ||||||
|  | 		bool forceScan = false); | ||||||
| 		/// Creates a DirectoryWatcher for the specified directory | 		/// Creates a DirectoryWatcher for the specified directory | ||||||
| 		/// To enable only specific events, an eventMask can be specified by | 		/// To enable only specific events, an eventMask can be specified by | ||||||
| 		/// OR-ing the desired event IDs (e.g., DW_ITEM_ADDED | DW_ITEM_MODIFIED). | 		/// OR-ing the desired event IDs (e.g., DW_ITEM_ADDED | DW_ITEM_MODIFIED). | ||||||
| 		/// On platforms where no native filesystem notifications are available, | 		/// On platforms where no native filesystem notifications are available, | ||||||
| 		/// scanInterval specifies the interval in seconds between scans | 		/// scanInterval specifies the interval in seconds between scans | ||||||
| 		/// of the directory. | 		/// of the directory. Native notification mechanism can also be disabled | ||||||
|  | 		/// (i.e. replaced with scanning) by setting forceScan to true. | ||||||
|  |  | ||||||
| 	~DirectoryWatcher(); | 	~DirectoryWatcher(); | ||||||
| 		/// Destroys the DirectoryWatcher. | 		/// Destroys the DirectoryWatcher. | ||||||
| @@ -191,6 +201,7 @@ private: | |||||||
| 	int                       _eventMask; | 	int                       _eventMask; | ||||||
| 	AtomicCounter             _eventsSuspended; | 	AtomicCounter             _eventsSuspended; | ||||||
| 	int                       _scanInterval; | 	int                       _scanInterval; | ||||||
|  | 	bool                      _forceScan; | ||||||
| 	DirectoryWatcherStrategy* _pStrategy; | 	DirectoryWatcherStrategy* _pStrategy; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -468,7 +468,7 @@ private: | |||||||
| }; | }; | ||||||
|  |  | ||||||
|  |  | ||||||
| #else | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| class PollingDirectoryWatcherStrategy: public DirectoryWatcherStrategy | class PollingDirectoryWatcherStrategy: public DirectoryWatcherStrategy | ||||||
| @@ -518,22 +518,24 @@ private: | |||||||
| }; | }; | ||||||
|  |  | ||||||
|  |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
|  | DirectoryWatcher::DirectoryWatcher(const std::string& path, int eventMask, int scanInterval, | ||||||
| DirectoryWatcher::DirectoryWatcher(const std::string& path, int eventMask, int scanInterval): | 	bool forceScan) : | ||||||
| 	_directory(path), | 	_directory(path), | ||||||
| 	_eventMask(eventMask), | 	_eventMask(eventMask), | ||||||
| 	_scanInterval(scanInterval) | 	_scanInterval(scanInterval), | ||||||
|  | 	_forceScan(forceScan) | ||||||
| { | { | ||||||
| 	init(); | 	init(); | ||||||
| } | } | ||||||
|  |  | ||||||
| 	 | 	 | ||||||
| DirectoryWatcher::DirectoryWatcher(const Poco::File& directory, int eventMask, int scanInterval): | DirectoryWatcher::DirectoryWatcher(const Poco::File& directory, int eventMask, int scanInterval, | ||||||
|  | 	bool forceScan) : | ||||||
| 	_directory(directory), | 	_directory(directory), | ||||||
| 	_eventMask(eventMask), | 	_eventMask(eventMask), | ||||||
| 	_scanInterval(scanInterval) | 	_scanInterval(scanInterval), | ||||||
|  | 	_forceScan(forceScan) | ||||||
| { | { | ||||||
| 	init(); | 	init(); | ||||||
| } | } | ||||||
| @@ -575,6 +577,8 @@ void DirectoryWatcher::init() | |||||||
| 	if (!_directory.isDirectory()) | 	if (!_directory.isDirectory()) | ||||||
| 		throw Poco::InvalidArgumentException("not a directory", _directory.path()); | 		throw Poco::InvalidArgumentException("not a directory", _directory.path()); | ||||||
|  |  | ||||||
|  | 	if (!_forceScan) | ||||||
|  | 	{ | ||||||
| #if POCO_OS == POCO_OS_WINDOWS_NT | #if POCO_OS == POCO_OS_WINDOWS_NT | ||||||
| 		_pStrategy = new WindowsDirectoryWatcherStrategy(*this); | 		_pStrategy = new WindowsDirectoryWatcherStrategy(*this); | ||||||
| #elif POCO_OS == POCO_OS_LINUX | #elif POCO_OS == POCO_OS_LINUX | ||||||
| @@ -584,6 +588,12 @@ void DirectoryWatcher::init() | |||||||
| #else | #else | ||||||
| 		_pStrategy = new PollingDirectoryWatcherStrategy(*this); | 		_pStrategy = new PollingDirectoryWatcherStrategy(*this); | ||||||
| #endif | #endif | ||||||
|  | 	} | ||||||
|  | 	else | ||||||
|  | 	{ | ||||||
|  | 		_pStrategy = new PollingDirectoryWatcherStrategy(*this); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	_thread.start(*this); | 	_thread.start(*this); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 unknown
					unknown