コード例 #1
0
ファイル: Mirror.cs プロジェクト: NikolayXHD/IndexExercise
        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");
            }
        }
コード例 #2
0
ファイル: Mirror.cs プロジェクト: NikolayXHD/IndexExercise
        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));
        }
コード例 #3
0
ファイル: Mirror.cs プロジェクト: NikolayXHD/IndexExercise
        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));
        }
コード例 #4
0
ファイル: Watcher.cs プロジェクト: NikolayXHD/IndexExercise
        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");
            }
        }
コード例 #5
0
ファイル: Watcher.cs プロジェクト: NikolayXHD/IndexExercise
        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");
            }
        }
コード例 #6
0
ファイル: Watcher.cs プロジェクト: NikolayXHD/IndexExercise
        /// <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));
            }
        }
コード例 #7
0
ファイル: Watcher.cs プロジェクト: NikolayXHD/IndexExercise
        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);
            }
        }
コード例 #8
0
ファイル: Watcher.cs プロジェクト: NikolayXHD/IndexExercise
        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);
        }
コード例 #9
0
ファイル: Watcher.cs プロジェクト: NikolayXHD/IndexExercise
        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);
        }
コード例 #10
0
ファイル: Mirror.cs プロジェクト: NikolayXHD/IndexExercise
        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);
        }
コード例 #11
0
ファイル: Watcher.cs プロジェクト: NikolayXHD/IndexExercise
        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);
                }
            }
        }
コード例 #12
0
ファイル: Mirror.cs プロジェクト: NikolayXHD/IndexExercise
        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);
                }
            }
        }
コード例 #13
0
ファイル: Mirror.cs プロジェクト: NikolayXHD/IndexExercise
 private void suspendRedundantTarget(WatchTarget target)
 {
     _watchTargetStates[target] = WatchTargetState.Suspended;
     _watcher.Unwatch(target);
 }
コード例 #14
0
 public WatcherErrorArgs(WatchTarget target, Exception exception)
 {
     Target    = target;
     Exception = exception;
 }