protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { var search = SearchPattern.Parse(ref path, ref searchPattern); return(zip.Cast <ZipEntry>().Map(x => { if (searchTarget == SearchTarget.File && !x.IsFile) { return null; } if (searchTarget == SearchTarget.Directory && !x.IsDirectory) { return null; } UPath child = UPath.DirectorySeparator + x.Name; if (!child.FullName.StartsWith(path.FullName)) { return null; } if (searchOption == SearchOption.TopDirectoryOnly) { child = child.RemovePrefix(path).GetFirstPart(); } if (!search.Match(child)) { return null; } return child; }).Filter(x => x != null).Distinct()); }
public void TestExpectedExceptions() { var path = new UPath("/yoyo"); Assert.Throws <ArgumentNullException>(() => { var nullPath = new UPath(); string searchPattern = "valid"; SearchPattern.Parse(ref nullPath, ref searchPattern); }); Assert.Throws <ArgumentNullException>(() => { string searchPattern = null; SearchPattern.Parse(ref path, ref searchPattern); }); Assert.Throws <ArgumentException>(() => { string searchPattern = "/notvalid"; SearchPattern.Parse(ref path, ref searchPattern); }); { var searchPattern = "*"; var search = SearchPattern.Parse(ref path, ref searchPattern); Assert.Throws <ArgumentNullException>(() => search.Match(null)); } }
public IEnumerable <UPath> EnumeratePaths(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { var search = SearchPattern.Parse(ref path, ref searchPattern); var hashset = new HashSet <UPath>(); // ReSharper disable once LoopCanBePartlyConvertedToQuery foreach (var entry in this.archive.Entries) { var p = new UPath('/' + entry.FullName); if (searchTarget == SearchTarget.Both || searchTarget == SearchTarget.File) { if (p.IsInDirectory(path, searchOption == SearchOption.AllDirectories) && search.Match(p)) { hashset.Add(p); } } if (searchTarget != SearchTarget.Both && searchTarget != SearchTarget.Directory) { continue; } p = p.GetDirectory(); if (p.IsInDirectory(path, searchOption == SearchOption.AllDirectories) && search.Match(p)) { hashset.Add(p); } } return(hashset); }
// ---------------------------------------------- // Search API // ---------------------------------------------- /// <inheritdoc /> protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { var search = SearchPattern.Parse(ref path, ref searchPattern); IEnumerable <string> results; switch (searchTarget) { case SearchTarget.File: results = Directory.EnumerateFiles(ConvertPathToInternal(path), searchPattern, searchOption); break; case SearchTarget.Directory: results = Directory.EnumerateDirectories(ConvertPathToInternal(path), searchPattern, searchOption); break; case SearchTarget.Both: results = Directory.EnumerateFileSystemEntries(ConvertPathToInternal(path), searchPattern, searchOption); break; default: yield break; } foreach (var subPath in results) { // Windows will truncate the search pattern's extension to three characters if the filesystem // has 8.3 paths enabled. This means searching for *.docx will list *.doc as well which is // not what we want. Check against the search pattern again to filter out those false results. if (search.Match(Path.GetFileName(subPath))) { yield return(ConvertPathFromInternal(subPath)); } } }
protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { var pattern = SearchPattern.Parse(ref path, ref searchPattern); var paths = Adapter.ListPaths(this.Connection, path, searchOption); // Remember directories do not exist in this file system // I can't work out how to easily filter directory components within SQLite's query language, so // we'll just have to do it here. HashSet <UPath> foundDirectories = new HashSet <UPath>(); var searchDepth = path.FullName.Count(c => c == UPath.DirectorySeparator); foreach (var foundPath in paths) { if (searchTarget != SearchTarget.File) { var parent = foundPath.GetDirectory(); // have we seen this directory before? if (!foundDirectories.Contains(parent)) { // if not yield it and all it's parent directories var fragments = parent.Split(); var directory = path; for (int i = searchDepth; i <= fragments.Count; i++) { // construct a fragment directory (or if at end, it's the whole directory) directory = i == searchDepth ? directory : directory / fragments[i - 1]; bool added = foundDirectories.Add(directory); if (added) { if (pattern.Match(directory)) { yield return(directory); } } } } } if (searchTarget != SearchTarget.Directory) { if (pattern.Match(foundPath)) { yield return(foundPath); } } } }
public void TestMatch(string path, string searchPattern, string pathToSearch, bool match = true) { var pathInfo = new UPath(path); var pathInfoCopy = pathInfo; var search = SearchPattern.Parse(ref pathInfoCopy, ref searchPattern); { var pathInfoCopy2 = pathInfoCopy; var searchPattern2 = searchPattern; SearchPattern.Normalize(ref pathInfoCopy2, ref searchPattern2); Assert.Equal(pathInfoCopy, pathInfoCopy2); Assert.Equal(searchPattern, searchPattern2); } var pathToSearchInfo = new UPath(pathToSearch); Assert.Equal(match, search.Match(pathToSearchInfo)); }
// ---------------------------------------------- // Search API // ---------------------------------------------- /// <inheritdoc /> protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { SearchPattern.Parse(ref path, ref searchPattern); var entries = new SortedSet <UPath>(); var fileSystems = new List <IFileSystem>(); if (Fallback != null) { fileSystems.Add(Fallback); } // Query all filesystems just once fileSystems.AddRange(_fileSystems); for (var i = fileSystems.Count - 1; i >= 0; i--) { var fileSystem = fileSystems[i]; if (!fileSystem.DirectoryExists(path)) { continue; } foreach (var item in fileSystem.EnumeratePaths(path, searchPattern, searchOption, searchTarget)) { if (entries.Contains(item)) { continue; } entries.Add(item); } } // Return entries foreach (var entry in entries) { yield return(entry); } }
/// <inheritdoc /> protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { // Use the search pattern to normalize the path/search pattern var search = SearchPattern.Parse(ref path, ref searchPattern); // Query all mounts just once List <KeyValuePair <UPath, IFileSystem> > mounts; lock (_mounts) { mounts = _mounts.ToList(); } // Internal method used to retrieve the list of search locations List <SearchLocation> GetSearchLocations(UPath basePath) { var locations = new List <SearchLocation>(); var matchedMount = false; foreach (var kvp in mounts) { // Check if path partially matches a mount name var remainingPath = GetRemaining(basePath, kvp.Key); if (!remainingPath.IsNull && remainingPath != UPath.Root) { locations.Add(new SearchLocation(this, basePath, remainingPath)); continue; } if (!matchedMount) { // Check if path fully matches a mount name remainingPath = GetRemaining(kvp.Key, basePath); if (!remainingPath.IsNull) { matchedMount = true; // don't check other mounts, we don't want to merge them together if (kvp.Value.DirectoryExists(remainingPath)) { locations.Add(new SearchLocation(kvp.Value, kvp.Key, remainingPath)); } } } } if (!matchedMount && NextFileSystem != null && NextFileSystem.DirectoryExists(basePath)) { locations.Add(new SearchLocation(NextFileSystem, null, basePath)); } return(locations); } var directoryToVisit = new List <UPath>(); directoryToVisit.Add(path); var entries = new SortedSet <UPath>(UPath.DefaultComparerIgnoreCase); var sortedDirectories = new SortedSet <UPath>(UPath.DefaultComparerIgnoreCase); var first = true; while (directoryToVisit.Count > 0) { var pathToVisit = directoryToVisit[0]; directoryToVisit.RemoveAt(0); var dirIndex = 0; entries.Clear(); sortedDirectories.Clear(); var locations = GetSearchLocations(pathToVisit); // Only need to search within one filesystem, no need to sort or do other work if (locations.Count == 1 && locations[0].FileSystem != this && (!first || searchOption == SearchOption.AllDirectories)) { var last = locations[0]; foreach (var item in last.FileSystem.EnumeratePaths(last.Path, searchPattern, searchOption, searchTarget)) { yield return(CombinePrefix(last.Prefix, item)); } } else { for (var i = locations.Count - 1; i >= 0; i--) { var location = locations[i]; var fileSystem = location.FileSystem; var searchPath = location.Path; if (fileSystem == this) { // List a single part of a mount name, queue it to be visited if needed var mountPart = new UPath(searchPath.GetFirstDirectory(out _)).ToRelative(); var mountPath = location.Prefix / mountPart; var isMatching = search.Match(mountPath); if (isMatching && searchTarget != SearchTarget.File) { entries.Add(mountPath); } if (searchOption == SearchOption.AllDirectories) { sortedDirectories.Add(mountPath); } } else { // List files in the mounted filesystems, merged and sorted into one list foreach (var item in fileSystem.EnumeratePaths(searchPath, "*", SearchOption.TopDirectoryOnly, SearchTarget.Both)) { var publicName = CombinePrefix(location.Prefix, item); if (entries.Contains(publicName)) { continue; } var isFile = fileSystem.FileExists(item); var isDirectory = fileSystem.DirectoryExists(item); var isMatching = search.Match(publicName); if (isMatching && ((isFile && searchTarget != SearchTarget.Directory) || (isDirectory && searchTarget != SearchTarget.File))) { entries.Add(publicName); } if (searchOption == SearchOption.AllDirectories && isDirectory) { sortedDirectories.Add(publicName); } } } } } if (first) { if (locations.Count == 0 && path != UPath.Root) { throw NewDirectoryNotFoundException(path); } first = false; } // Enqueue directories and respect order foreach (var nextDir in sortedDirectories) { directoryToVisit.Insert(dirIndex++, nextDir); } // Return entries foreach (var entry in entries) { yield return(entry); } } }
/// <inheritdoc /> protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { // Use the search pattern to normalize the path/search pattern var search = SearchPattern.Parse(ref path, ref searchPattern); var originalSrcPath = path; // Internal method used to retrieve the list of root directories SortedSet <UPath> GetRootDirectories() { var directories = new SortedSet <UPath>(UPath.DefaultComparerIgnoreCase); lock (_mounts) { foreach (var mountName in _mounts.Keys) { directories.Add(mountName); } } if (NextFileSystem != null) { foreach (var dir in NextFileSystem.EnumeratePaths(path, "*", SearchOption.TopDirectoryOnly, SearchTarget.Directory)) { if (!directories.Contains(dir)) { directories.Add(dir); } } } return(directories); } IEnumerable <UPath> EnumeratePathFromFileSystem(UPath subPath, bool failOnInvalidPath) { var fs = TryGetMountOrNext(ref subPath, out var mountPath); if (fs == null) { if (failOnInvalidPath) { throw NewDirectoryNotFoundException(originalSrcPath); } yield break; } if (fs != NextFileSystem) { // In the case of a mount, we need to return the full path Debug.Assert(!mountPath.IsNull); foreach (var entry in fs.EnumeratePaths(subPath, searchPattern, searchOption, searchTarget)) { yield return(mountPath / entry.ToRelative()); } } else { foreach (var entry in fs.EnumeratePaths(subPath, searchPattern, searchOption, searchTarget)) { yield return(entry); } } } // Special case for the root as we have to return the list of mount directories // and merge them with the underlying FileSystem if (path == UPath.Root) { var entries = new SortedSet <UPath>(UPath.DefaultComparerIgnoreCase); // Return the list of dircetories var directories = GetRootDirectories(); // Process the files first if (NextFileSystem != null && (searchTarget == SearchTarget.File || searchTarget == SearchTarget.Both)) { foreach (var file in NextFileSystem.EnumeratePaths(path, searchPattern, SearchOption.TopDirectoryOnly, SearchTarget.File)) { entries.Add(file); } } if (searchTarget != SearchTarget.File) { foreach (var dir in directories) { if (search.Match(dir)) { entries.Add(dir); } } } // Return all entries sorted foreach (var entry in entries) { yield return(entry); } if (searchOption == SearchOption.AllDirectories) { foreach (var dir in directories) { foreach (var entry in EnumeratePathFromFileSystem(dir, false)) { yield return(entry); } } } } else { foreach (var entry in EnumeratePathFromFileSystem(path, true)) { yield return(entry); } } }
// ---------------------------------------------- // Search API // ---------------------------------------------- /// <inheritdoc /> protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { var search = SearchPattern.Parse(ref path, ref searchPattern); var directoryToVisit = new List <UPath>(); directoryToVisit.Add(path); var entries = new SortedSet <UPath>(UPath.DefaultComparerIgnoreCase); var sortedDirectories = new SortedSet <UPath>(UPath.DefaultComparerIgnoreCase); var fileSystems = new List <IFileSystem>(); if (NextFileSystem != null) { fileSystems.Add(NextFileSystem); } // Query all filesystems just once lock (_fileSystems) { fileSystems.AddRange(_fileSystems); } while (directoryToVisit.Count > 0) { var pathToVisit = directoryToVisit[0]; directoryToVisit.RemoveAt(0); int dirIndex = 0; entries.Clear(); sortedDirectories.Clear(); for (var i = fileSystems.Count - 1; i >= 0; i--) { var fileSystem = fileSystems[i]; if (fileSystem.DirectoryExists(pathToVisit)) { foreach (var item in fileSystem.EnumeratePaths(pathToVisit, "*", SearchOption.TopDirectoryOnly, SearchTarget.Both)) { if (!entries.Contains(item)) { var isFile = fileSystem.FileExists(item); var isDirectory = fileSystem.DirectoryExists(item); var isMatching = search.Match(item); if (isMatching && ((isFile && searchTarget != SearchTarget.Directory) || (isDirectory && searchTarget != SearchTarget.File))) { entries.Add(item); } if (searchOption == SearchOption.AllDirectories && isDirectory) { sortedDirectories.Add(item); } } } } } // Enqueue directories and respect order foreach (var nextDir in sortedDirectories) { directoryToVisit.Insert(dirIndex++, nextDir); } // Return entries foreach (var entry in entries) { yield return(entry); } } }
// ---------------------------------------------- // Search API // ---------------------------------------------- /// <inheritdoc /> protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { // Special case for Windows as we need to provide list for: // - the root folder / (which should just return the /drive folder) // - the drive folders /drive/c, drive/e...etc. var search = SearchPattern.Parse(ref path, ref searchPattern); if (IsOnWindows) { if (IsWithinSpecialDirectory(path)) { if (!SpecialDirectoryExists(path)) { throw NewDirectoryNotFoundException(path); } var searchForDirectory = searchTarget == SearchTarget.Both || searchTarget == SearchTarget.Directory; // Only sub folder "/drive/" on root folder / if (path == UPath.Root) { if (searchForDirectory) { yield return(PathDrivePrefixOnWindows); if (searchOption == SearchOption.AllDirectories) { foreach (var subPath in EnumeratePathsImpl(PathDrivePrefixOnWindows, searchPattern, searchOption, searchTarget)) { yield return(subPath); } } } yield break; } // When listing for /drive, return the list of drives available if (path == PathDrivePrefixOnWindows) { var pathDrives = new List <UPath>(); foreach (var drive in DriveInfo.GetDrives()) { if (drive.Name.Length < 2 || drive.Name[1] != ':') { continue; } var pathDrive = PathDrivePrefixOnWindows / char.ToLowerInvariant(drive.Name[0]).ToString(); if (search.Match(pathDrive)) { pathDrives.Add(pathDrive); if (searchForDirectory) { yield return(pathDrive); } } } if (searchOption == SearchOption.AllDirectories) { foreach (var pathDrive in pathDrives) { foreach (var subPath in EnumeratePathsImpl(pathDrive, searchPattern, searchOption, searchTarget)) { yield return(subPath); } } } yield break; } } } IEnumerable <string> results; switch (searchTarget) { case SearchTarget.File: results = Directory.EnumerateFiles(ConvertPathToInternal(path), searchPattern, searchOption); break; case SearchTarget.Directory: results = Directory.EnumerateDirectories(ConvertPathToInternal(path), searchPattern, searchOption); break; case SearchTarget.Both: results = Directory.EnumerateFileSystemEntries(ConvertPathToInternal(path), searchPattern, searchOption); break; default: yield break; } foreach (var subPath in results) { // Windows will truncate the search pattern's extension to three characters if the filesystem // has 8.3 paths enabled. This means searching for *.docx will list *.doc as well which is // not what we want. Check against the search pattern again to filter out those false results. if (!IsOnWindows || search.Match(Path.GetFileName(subPath))) { yield return(ConvertPathFromInternal(subPath)); } } }
// ---------------------------------------------- // Search API // ---------------------------------------------- /// <inheritdoc /> protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { // Special case for Windows as we need to provide list for: // - the root folder / (which should just return the /drive folder) // - the drive folders /drive/c, drive/e...etc. var search = SearchPattern.Parse(ref path, ref searchPattern); if (IsOnWindows) { if (IsWithinSpecialDirectory(path)) { if (!SpecialDirectoryExists(path)) { throw NewDirectoryNotFoundException(path); } var searchForDirectory = searchTarget == SearchTarget.Both || searchTarget == SearchTarget.Directory; // Only sub folder "/drive/" on root folder / if (path == UPath.Root) { if (searchForDirectory) { yield return(PathDrivePrefixOnWindows); if (searchOption == SearchOption.AllDirectories) { foreach (var subPath in EnumeratePathsImpl(PathDrivePrefixOnWindows, searchPattern, searchOption, searchTarget)) { yield return(subPath); } } } yield break; } // When listing for /drive, return the list of drives available if (path == PathDrivePrefixOnWindows) { var pathDrives = new List <UPath>(); foreach (var drive in DriveInfo.GetDrives()) { if (drive.Name.Length < 2 || drive.Name[1] != ':') { continue; } var pathDrive = PathDrivePrefixOnWindows / char.ToLowerInvariant(drive.Name[0]).ToString(); if (search.Match(pathDrive)) { pathDrives.Add(pathDrive); if (searchForDirectory) { yield return(pathDrive); } } } if (searchOption == SearchOption.AllDirectories) { foreach (var pathDrive in pathDrives) { foreach (var subPath in EnumeratePathsImpl(pathDrive, searchPattern, searchOption, searchTarget)) { yield return(subPath); } } } yield break; } } } switch (searchTarget) { case SearchTarget.File: foreach (var subPath in Directory.EnumerateFiles(ConvertPathToInternal(path), searchPattern, searchOption)) { yield return(ConvertPathFromInternal(subPath)); } break; case SearchTarget.Directory: foreach (var subPath in Directory.EnumerateDirectories(ConvertPathToInternal(path), searchPattern, searchOption)) { yield return(ConvertPathFromInternal(subPath)); } break; case SearchTarget.Both: foreach (var subPath in Directory.EnumerateFileSystemEntries(ConvertPathToInternal(path), searchPattern, searchOption)) { yield return(ConvertPathFromInternal(subPath)); } break; } }
// Token: 0x06001A2F RID: 6703 RVA: 0x0006FF0A File Offset: 0x0006E10A protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { this.UpdateDirectories(); SearchPattern search = SearchPattern.Parse(ref path, ref searchPattern); List <UPath> foldersToProcess = new List <UPath> { path }; SortedSet <UPath> entries = new SortedSet <UPath>(UPath.DefaultComparerIgnoreCase); while (foldersToProcess.Count > 0) { UPath upath = foldersToProcess[0]; foldersToProcess.RemoveAt(0); int num = 0; entries.Clear(); SteamworksRemoteStorageFileSystem.EnterFileSystemShared(); try { SteamworksRemoteStorageFileSystem.Node directoryNode = this.GetDirectoryNode(upath); if (upath == path) { this.AssertDirectory(directoryNode, upath); } else if (!(directoryNode is SteamworksRemoteStorageFileSystem.DirectoryNode)) { continue; } SteamworksRemoteStorageFileSystem.DirectoryNode directoryNode2 = (SteamworksRemoteStorageFileSystem.DirectoryNode)directoryNode; for (int i = 0; i < directoryNode2.childCount; i++) { SteamworksRemoteStorageFileSystem.Node child = directoryNode2.GetChild(i); if (!(child is SteamworksRemoteStorageFileSystem.FileNode) || searchTarget != SearchTarget.Directory) { bool flag = search.Match(child.path); bool flag2 = searchOption == SearchOption.AllDirectories && child is SteamworksRemoteStorageFileSystem.DirectoryNode; bool flag3 = (child is SteamworksRemoteStorageFileSystem.FileNode && searchTarget != SearchTarget.Directory && flag) || (child is SteamworksRemoteStorageFileSystem.DirectoryNode && searchTarget != SearchTarget.File && flag); UPath item = upath / child.path; if (flag2) { foldersToProcess.Insert(num++, item); } if (flag3) { entries.Add(item); } } } } finally { SteamworksRemoteStorageFileSystem.ExitFileSystemShared(); } foreach (UPath upath2 in entries) { yield return(upath2); } SortedSet <UPath> .Enumerator enumerator = default(SortedSet <UPath> .Enumerator); } yield break; yield break; }