private static DirectoryScanOptions ReadDirectoryScanOptions(ScanFilterOptions globalExcludes, SetRecord set) { if (set == null) { return(null); } if (!set.TryGetValue("path", out RecordBase pathRecord) || !set.TryGetValue("depth", out RecordBase depthRecord)) { return(null); } if (!(pathRecord is ScalerRecord pathScaler) || !(depthRecord is ScalerRecord depthScaler)) { return(null); } if (!pathScaler.TryReadAs(out string path) || !depthScaler.TryReadAs(out int depth)) { return(null); } if (depth <= 0) { depth = 1; } ScanFilterOptions includeOptions; ScanFilterOptions excludeOptions; if (set.TryGetValue("include", out RecordBase includeRecordBase) && includeRecordBase is SetRecord includeRecord) { includeOptions = ReadFilterOptions(includeRecord); } else { includeOptions = ScanFilterOptions.CreateIncludeDefault(); } if (set.TryGetValue("exclude", out RecordBase excludeRecordBase) && excludeRecordBase is SetRecord excludeRecord) { excludeOptions = ReadFilterOptions(excludeRecord); } else { excludeOptions = ScanFilterOptions.CreateExcludeDefault(); } bool ignoreGlobalExclude = false; if (set.TryGetValue("ignoreGlobalExclude", out RecordBase ignoreGlobalExcludeBase) && ignoreGlobalExcludeBase is ScalerRecord ignoreGlobalExcludeScaler && ignoreGlobalExcludeScaler.TryReadAs <bool>(out bool ignoreGlobalExcludeValue)) { ignoreGlobalExclude = ignoreGlobalExcludeValue; } if (!ignoreGlobalExclude) { excludeOptions.Combine(globalExcludes); } return(new DirectoryScanOptions(path, depth, includeOptions, excludeOptions)); }
public static IConfiguartion Read(SetRecord set) { if (set == null) { throw new ArgumentNullException(nameof(set)); } RecordBase record; SetRecord setIncludeItems = null; SetRecord setGlobalExcludes = null; ListRecord setScanDirectories = null; if (set.TryGetValue("scan", out record)) { setScanDirectories = record as ListRecord; } if (set.TryGetValue("include", out record)) { setIncludeItems = record as SetRecord; } if (set.TryGetValue("exclude", out record)) { setGlobalExcludes = record as SetRecord; } ItemIncludeOptions resultInclude = ReadItemIncludeOptions(setIncludeItems); ScanFilterOptions resultExclude = null; if (setGlobalExcludes == null) { resultExclude = ScanFilterOptions.CreateExcludeDefault(); } else { resultExclude = ReadFilterOptions(setGlobalExcludes); } List <DirectoryScanOptions> resultScanDirectories = new List <DirectoryScanOptions>(); if (setScanDirectories != null) { foreach (RecordBase scanOptions in setScanDirectories) { if (scanOptions is SetRecord setScanOptions) { resultScanDirectories.Add(ReadDirectoryScanOptions(resultExclude, setScanOptions)); } } } return(new Configuration(resultExclude, resultInclude, resultScanDirectories)); }
internal void Combine(ScanFilterOptions option) { if (option == null) { throw new ArgumentNullException(nameof(option)); } (FileFilters as List <Regex>).AddRange(option.FileFilters); (DirectoryFilters as List <Regex>).AddRange(option.DirectoryFilters); (CommonFilters as List <Regex>).AddRange(option.CommonFilters); (PathFilters as List <Regex>).AddRange(option.PathFilters); IsDefault &= option.IsDefault; }
internal Configuration(ScanFilterOptions globalExclude, ItemIncludeOptions includeItems, List <DirectoryScanOptions> scanDirectories) { if (globalExclude == null) { throw new ArgumentNullException(nameof(globalExclude)); } if (includeItems == null) { throw new ArgumentNullException(nameof(includeItems)); } if (scanDirectories == null) { throw new ArgumentNullException(nameof(scanDirectories)); } GlobalExcludes = globalExclude; IncludeItems = includeItems; ScanDirectories = scanDirectories; }
internal DirectoryScanOptions(string directoryPath, int scanDepth, ScanFilterOptions includes, ScanFilterOptions excludes) { if (directoryPath == null) { throw new ArgumentNullException(nameof(directoryPath)); } if (includes == null) { throw new ArgumentNullException(nameof(includes)); } if (excludes == null) { throw new ArgumentNullException(nameof(excludes)); } if (scanDepth < 0) { throw new ArgumentOutOfRangeException(nameof(scanDepth)); } DirectoryPath = directoryPath; ScanDepth = scanDepth; Includes = includes; Excludes = excludes; }
private void EnumerateScanDirectory(DirectoryScanOptions scanOptions, List <DirectoryInfo> directories, List <FileInfo> files, WatchingTree watchingTree) { WatchingTreeItem item = watchingTree.ItemByPath(scanOptions.DirectoryPath, ItemType.Directory); item.Data = new ItemWatchingOptions() { ScanOptions = scanOptions, WatchingStatus = ItemWatchingStatus.ScanNeeded }; if (!Directory.Exists(scanOptions.DirectoryPath)) { return; } Queue <DirectoryInfo> dirque = new Queue <DirectoryInfo>(); DirectoryInfo dir = new DirectoryInfo(scanOptions.DirectoryPath); dirque.Enqueue(dir); dirque.Enqueue(null); int currentDepth = 1; int maxdepth = scanOptions.ScanDepth; DirectoryInfo current; ScanFilterOptions excludeFilters = scanOptions.Excludes; ScanFilterOptions includeFilters = scanOptions.Includes; while (true) { current = dirque.Dequeue(); if (current == null) { if (dirque.Count <= 0) { break; } currentDepth++; if (currentDepth > maxdepth) { foreach (DirectoryInfo directory in dirque) { if (!excludeFilters.IsMatch(directory) && includeFilters.IsMatch(directory)) { directories.Add(directory); } } break; } else { dirque.Enqueue(null); continue; } } IEnumerable <DirectoryInfo> enumeratedDirectories = current.EnumerateDirectories(); if (!includeFilters.IsDefault) { foreach (DirectoryInfo directory in enumeratedDirectories) { if (includeFilters.IsMatch(directory)) { directories.Add(directory); } else if (!excludeFilters.IsMatch(directory)) { dirque.Enqueue(directory); } } } else { foreach (DirectoryInfo directory in enumeratedDirectories) { if (!excludeFilters.IsMatch(directory)) { dirque.Enqueue(directory); } } } IEnumerable <FileInfo> enumeratedFiles = current.EnumerateFiles(); if (!includeFilters.IsDefault) { foreach (FileInfo file in enumeratedFiles) { if (includeFilters.IsMatch(file)) { files.Add(file); } } } else { foreach (FileInfo file in enumeratedFiles) { if (!excludeFilters.IsMatch(file) && includeFilters.IsMatch(file)) { files.Add(file); } } } } }