SearchResult CreateSearchResult(SearchData localSearchData, FindData findData) { String userPathFinal = PathHelperMethods.InternalCombine(localSearchData.userPath, findData.FileName); String fullPathFinal = PathHelperMethods.InternalCombine(localSearchData.fullPath, findData.FileName); return(new SearchResult(fullPathFinal, userPathFinal, findData)); }
/// <summary> /// Initializes a new instance of the <see cref="SearchData"/> class. /// </summary> /// <param name="fullPath">The full path.</param> /// <param name="userPath">The user path.</param> /// <param name="searchOptions">The search options.</param> public SearchData(String fullPath, String userPath, SearchOption searchOptions) { Contract.Requires(fullPath != null); Contract.Requires(searchOptions == SearchOption.AllDirectories || searchOptions == SearchOption.TopDirectoryOnly); if (PathHelperMethods.IsDirectorySeparator(fullPath[fullPath.Length - 1])) { this.fullPath = fullPath; } else { this.fullPath = fullPath + Path.DirectorySeparatorChar; } if (string.IsNullOrEmpty(userPath) || PathHelperMethods.IsDirectorySeparator(userPath[userPath.Length - 1])) { this.userPath = userPath; } else { this.userPath = userPath + Path.DirectorySeparatorChar; } this.searchOptions = searchOptions; }
static string GetDisplayablePath(string path, bool isInvalidPath) { if (string.IsNullOrWhiteSpace(path)) { return(path); } // Is it a fully qualified path? var isFullyQualified = false; if (path.Length < 2) { return(path); } if ((PathHelperMethods.IsDirectorySeparator(path[0]) && PathHelperMethods.IsDirectorySeparator(path[1])) || path[1] == Path.VolumeSeparatorChar) { isFullyQualified = true; } if (!isFullyQualified && !isInvalidPath) { return(path); } try { if (!isInvalidPath) { new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new[] { path }).Demand(); return(path); } } catch (SecurityException) {} catch (ArgumentException) { // ? and * characters cause ArgumentException to be thrown from HasIllegalCharacters // inside FileIOPermission.AddPathList } catch (NotSupportedException) { // paths like "!Bogus\\dir:with/junk_.in it" can cause NotSupportedException to be thrown // from Security.Util.StringExpressionSet.CanonicalizePath when ':' is found in the path // beyond string index position 1. } if (PathHelperMethods.IsDirectorySeparator(path[path.Length - 1])) { throw new UnauthorizedAccessException($"No permission to directory name '{path}'"); } return(Path.GetFileName(path)); }
public FileSystemEnumerator(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler) { Contract.Requires(path != null); Contract.Requires(originalUserPath != null); Contract.Requires(searchPattern != null); Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly); Contract.Requires(resultHandler != null); oldMode = Win32Api.SetErrorMode(Win32Api.FailCriticalErrors); searchStack = new List <SearchData>(); String normalizedSearchPattern = NormalizeSearchPattern(searchPattern); if (normalizedSearchPattern.Length == 0) { isEmpty = true; } else { this.resultHandler = resultHandler; this.searchOption = searchOption; fullPath = PathHelperMethods.GetFullPathInternal(path); String fullSearchString = GetFullSearchString(fullPath, normalizedSearchPattern); normalizedSearchPath = Path.GetDirectoryName(fullSearchString); // permission demands var demandPaths = new String[2]; // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters demandPaths[0] = GetDemandDir(fullPath, true); // For filters like foo\*.cs we need to verify if the directory foo is not denied access. // Do a demand on the combined path so that we can fail early in case of deny demandPaths[1] = GetDemandDir(normalizedSearchPath, true); new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandPaths).Demand(); // normalize search criteria searchCriteria = GetNormalizedSearchCriteria(fullSearchString, normalizedSearchPath); // fix up user path String searchPatternDirName = Path.GetDirectoryName(normalizedSearchPattern); String userPathTemp = originalUserPath; if (searchPatternDirName != null && searchPatternDirName.Length != 0) { userPathTemp = Path.Combine(userPathTemp, searchPatternDirName); } userPath = userPathTemp; searchData = new SearchData(normalizedSearchPath, userPath, searchOption); CommonInit(); } }
static String NormalizeSearchPattern(String searchPattern) { Contract.Requires(searchPattern != null); // Win32 normalization trims only U+0020. String tempSearchPattern = searchPattern.TrimEnd(PathHelperMethods.TrimEndChars); // Make this corner case more useful, like dir if (tempSearchPattern.Equals(".")) { tempSearchPattern = "*"; } PathHelperMethods.CheckSearchPattern(tempSearchPattern); return(tempSearchPattern); }
static String GetFullSearchString(String fullPath, String searchPattern) { Contract.Requires(fullPath != null); Contract.Requires(searchPattern != null); String tempStr = PathHelperMethods.InternalCombine(fullPath, searchPattern); // If path ends in a trailing slash (\), append a * or we'll get a "Cannot find the file specified" exception char lastChar = tempStr[tempStr.Length - 1]; if (PathHelperMethods.IsDirectorySeparator(lastChar) || lastChar == Path.VolumeSeparatorChar) { tempStr = tempStr + '*'; } return(tempStr); }
static String GetNormalizedSearchCriteria(String fullSearchString, String fullPathMod) { Contract.Requires(fullSearchString != null); Contract.Requires(fullPathMod != null); Contract.Requires(fullSearchString.Length >= fullPathMod.Length); String searchCriteria = null; char lastChar = fullPathMod[fullPathMod.Length - 1]; if (PathHelperMethods.IsDirectorySeparator(lastChar)) { // Can happen if the path is C:\temp, in which case GetDirectoryName would return C:\ searchCriteria = fullSearchString.Substring(fullPathMod.Length); } else { Contract.Assert(fullSearchString.Length > fullPathMod.Length); searchCriteria = fullSearchString.Substring(fullPathMod.Length + 1); } return(searchCriteria); }