private void inspectWatchTarget(WatchTarget target, WatchTargetState storedState) { var actualState = getActualTargetState(target); if (actualState == storedState) { return; } switch (actualState) { case WatchTargetState.Active: activateTarget(target); break; case WatchTargetState.Suspended: suspendRedundantTarget(target); break; case WatchTargetState.Failed: suspendFailedTarget(target); break; default: throw new NotSupportedException($"{nameof(WatchTargetState)} {actualState} is not supported"); } }
private void suspendFailedTarget(WatchTarget target) { _watchTargetStates[target] = WatchTargetState.Failed; _watcher.Unwatch(target); var watchedDirectory = _watcher.GetWatcherDirectory(target); // We don't know what happens to the watched directory while the watcher is broken. // To reflect this we assume the watched directory is deleted. _changeQueue.TryEnqueue(new Change(EntryType.Directory, WatcherChangeTypes.Deleted, watchedDirectory)); }
private void activateTarget(WatchTarget target) { _watchTargetStates[target] = WatchTargetState.Active; _watcher.Watch(target); string directory = _watcher.GetWatcherDirectory(target); // after _watcher.Watch to make sure the files or directories // created while executing scanDirectory are detected by Watcher entryFound(new Find(EntryType.Directory, directory)); }
private FileSystemWatcher createWatcher(WatchTarget target) { switch (target.Type) { case EntryType.File: return(createFileWatcher(target)); case EntryType.Directory: return(createDirectoryWatcher(target)); default: throw new NotSupportedException($"Watching {target.Type} is not supported"); } }
public string GetWatcherDirectory(WatchTarget target) { switch (target.Type) { case EntryType.File: return(Path.GetDirectoryName(target.Path)); case EntryType.Directory: return(target.Path); default: throw new NotSupportedException($"Watching {target.Type} is not supported"); } }
/// <summary> /// Returns false if <see cref="Change.Path"/> and / or <see cref="Change.OldPath"/> /// properties have incorrect values. /// /// The performance of this check is same as <see cref="Directory.Exists"/> /// </summary> public bool IsPathCorrect(WatchTarget target) { lock (_sync) { if (!_watchers.TryGetValue(target, out var watcher)) { return(true); } string expectedPath = _initialWatchPaths[watcher]; string actualPath = WatcherPathInspector.GetActualPath(watcher); return(PathString.Comparer.Equals(expectedPath, actualPath)); } }
public void Unwatch(WatchTarget target) { lock (_sync) { if (!_watchers.TryGetValue(target, out var watcher)) { return; } watcher.EnableRaisingEvents = false; watcher.Dispose(); _watchers.Remove(target); _initialWatchPaths.Remove(watcher); } }
private FileSystemWatcher createDirectoryWatcher(WatchTarget target) { var watcher = new FileSystemWatcher { Path = GetWatcherDirectory(target), Filter = null, IncludeSubdirectories = true }; watcher.Created += directoryChildCreated; watcher.Deleted += directoryChildDeleted; watcher.Changed += directoryChildChanged; watcher.Renamed += directoryChildRenamed; watcher.Error += directoryWatcherError; return(watcher); }
private FileSystemWatcher createFileWatcher(WatchTarget target) { var watcher = new FileSystemWatcher { Path = GetWatcherDirectory(target), Filter = Path.GetFileName(target.Path), IncludeSubdirectories = false }; watcher.NotifyFilter &= ~NotifyFilters.DirectoryName; watcher.Created += fileCreated; watcher.Deleted += fileDeleted; watcher.Changed += fileChanged; watcher.Renamed += fileRenamed; watcher.Error += fileWatcherError; return(watcher); }
private WatchTargetState getActualTargetState(WatchTarget target) { if (existsActiveWatchedParent(target.Path)) { return(WatchTargetState.Suspended); } if (!_watcher.IsPathCorrect(target)) { return(WatchTargetState.Failed); } string directory = _watcher.GetWatcherDirectory(target); if (!Directory.Exists(directory)) { return(WatchTargetState.Failed); } return(WatchTargetState.Active); }
public void Watch(WatchTarget target) { lock (_sync) { if (_watchers.ContainsKey(target)) { return; } var watcher = createWatcher(target); watcher.EnableRaisingEvents = _enabled; _watchers.Add(target, watcher); if (_enabled) { saveInitialWatchLocation(watcher); } } }
public void Watch(WatchTarget target) { lock (_syncWatcher) { _watchTargetStates[target] = WatchTargetState.Suspended; var watchedLocation = _watchedLocations.GetEntry(target.Path); if (watchedLocation == null) { _watchedLocations.Add( EntryType.Directory, target.Path, new HashSet <WatchTarget> { target }); } else { watchedLocation.Data.Add(target); } } }
private void suspendRedundantTarget(WatchTarget target) { _watchTargetStates[target] = WatchTargetState.Suspended; _watcher.Unwatch(target); }
public WatcherErrorArgs(WatchTarget target, Exception exception) { Target = target; Exception = exception; }