private FileSystemEnumerableIterator(string fullPath, string normalizedSearchPath, string searchCriteria, string userPath, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost) { this.fullPath = fullPath; this.normalizedSearchPath = normalizedSearchPath; this.searchCriteria = searchCriteria; this._resultHandler = resultHandler; this.userPath = userPath; this.searchOption = searchOption; this._checkHost = checkHost; this.searchStack = new List <Directory.SearchData>(); if (searchCriteria != null) { new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[2] { Directory.GetDemandDir(fullPath, true), Directory.GetDemandDir(normalizedSearchPath, true) }, false, false).Demand(); this.searchData = new Directory.SearchData(normalizedSearchPath, userPath, searchOption); this.CommonInit(); } else { this.empty = true; } }
internal FileSystemEnumerableIterator(string path, string originalUserPath, string searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler) { this.oldMode = Win32Native.SetErrorMode(1); this.searchStack = new List <Directory.SearchData>(); string str = FileSystemEnumerableIterator <TSource> .NormalizeSearchPattern(searchPattern); if (str.Length == 0) { this.empty = true; } else { this._resultHandler = resultHandler; this.searchOption = searchOption; this.fullPath = Path.GetFullPathInternal(path); string fullSearchString = FileSystemEnumerableIterator <TSource> .GetFullSearchString(this.fullPath, str); this.normalizedSearchPath = Path.GetDirectoryName(fullSearchString); string[] pathList = new string[] { Directory.GetDemandDir(this.fullPath, true), Directory.GetDemandDir(this.normalizedSearchPath, true) }; new FileIOPermission(FileIOPermissionAccess.PathDiscovery, pathList, false, false).Demand(); this.searchCriteria = FileSystemEnumerableIterator <TSource> .GetNormalizedSearchCriteria(fullSearchString, this.normalizedSearchPath); string directoryName = Path.GetDirectoryName(str); string str4 = originalUserPath; if ((directoryName != null) && (directoryName.Length != 0)) { str4 = Path.Combine(str4, directoryName); } this.userPath = str4; this.searchData = new Directory.SearchData(this.normalizedSearchPath, this.userPath, searchOption); this.CommonInit(); } }
private SearchResult CreateSearchResult(Directory.SearchData localSearchData, Win32Native.WIN32_FIND_DATA findData) { String userPathFinal = Path.InternalCombine(localSearchData.userPath, findData.cFileName); String fullPathFinal = Path.InternalCombine(localSearchData.fullPath, findData.cFileName); return(new SearchResult(fullPathFinal, userPathFinal, findData)); }
internal override FileSystemInfo CreateObject(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData) { if (!findData.IsFile) { return(DirectoryInfoResultHandler.CreateDirectoryInfo(searchData, ref findData)); } return(FileInfoResultHandler.CreateFileInfo(searchData, ref findData)); }
[System.Security.SecurityCritical] // auto-generated private void AddSearchableDirsToStack(Directory.SearchData localSearchData) { Contract.Requires(localSearchData != null); String searchPath = Path.Combine(localSearchData.fullPath, "*"); SafeFindHandle hnd = null; Interop.WIN32_FIND_DATA data = new Interop.WIN32_FIND_DATA(); try { // Get all files and dirs hnd = Interop.mincore.FindFirstFile(searchPath, ref data); if (hnd.IsInvalid) { int errorCode = Marshal.GetLastWin32Error(); // This could happen if the dir doesn't contain any files. // Continue with the recursive search though, eventually // searchStack will become empty if (errorCode == Interop.ERROR_FILE_NOT_FOUND || errorCode == Interop.ERROR_NO_MORE_FILES || errorCode == Interop.ERROR_PATH_NOT_FOUND) { return; } HandleError(errorCode, localSearchData.fullPath); } // Add subdirs to searchStack. Exempt ReparsePoints as appropriate int incr = 0; do { if (Win32FileSystemEnumerableHelpers.IsDir(data)) { Contract.Assert(data.cFileName.Length != 0 && !Path.IsPathRooted(data.cFileName), "Expected file system enumeration to not have empty file/directory name and not have rooted name"); String tempFullPath = Path.Combine(localSearchData.fullPath, data.cFileName); String tempUserPath = Path.Combine(localSearchData.userPath, data.cFileName); SearchOption option = localSearchData.searchOption; // Setup search data for the sub directory and push it into the stack Directory.SearchData searchDataSubDir = new Directory.SearchData(tempFullPath, tempUserPath, option); _searchStack.Insert(incr++, searchDataSubDir); } } while (Interop.mincore.FindNextFile(hnd, ref data)); // We don't care about errors here } finally { if (hnd != null) { hnd.Dispose(); } } }
private SearchResult CreateSearchResult(Directory.SearchData localSearchData, Interop.WIN32_FIND_DATA findData) { string findData_fileName = findData.cFileName; Contract.Requires(findData_fileName.Length != 0 && !Path.IsPathRooted(findData_fileName), "Expected file system enumeration to not have empty file/directory name and not have rooted name"); String userPathFinal = Path.Combine(localSearchData.userPath, findData_fileName); String fullPathFinal = Path.Combine(localSearchData.fullPath, findData_fileName); return(new SearchResult(fullPathFinal, userPathFinal, findData)); }
private void AddSearchableDirsToStack(Directory.SearchData localSearchData) { string fileName = Path.InternalCombine(localSearchData.fullPath, "*"); SafeFindHandle hndFindFile = (SafeFindHandle)null; Win32Native.WIN32_FIND_DATA wiN32FindData = new Win32Native.WIN32_FIND_DATA(); try { hndFindFile = Win32Native.FindFirstFile(fileName, wiN32FindData); if (hndFindFile.IsInvalid) { int lastWin32Error = Marshal.GetLastWin32Error(); switch (lastWin32Error) { case 2: return; case 18: return; case 3: return; default: this.HandleError(lastWin32Error, localSearchData.fullPath); break; } } int num1 = 0; do { if (FileSystemEnumerableHelpers.IsDir(wiN32FindData)) { string fullPath = Path.InternalCombine(localSearchData.fullPath, wiN32FindData.cFileName); string str = Path.InternalCombine(localSearchData.userPath, wiN32FindData.cFileName); SearchOption searchOption = localSearchData.searchOption; string userPath = str; int num2 = (int)searchOption; Directory.SearchData searchData = new Directory.SearchData(fullPath, userPath, (SearchOption)num2); this.searchStack.Insert(num1++, searchData); } }while (Win32Native.FindNextFile(hndFindFile, wiN32FindData)); } finally { if (hndFindFile != null) { hndFindFile.Dispose(); } } }
internal static DirectoryInfo CreateDirectoryInfo(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData) { string fileName = findData.cFileName; string fullPath = Path.CombineNoChecks(searchData.fullPath, fileName); if (!CodeAccessSecurityEngine.QuickCheckForAllDemands()) { // There is no need to emulate checks that FileIOPermission does if we aren't in full trust. // The paths we're getting are already tested and/or coming straight from the OS. new FileIOPermission(FileIOPermissionAccess.Read, new string[] { fullPath + "\\." }, false, false).Demand(); } DirectoryInfo di = new DirectoryInfo(fullPath, fileName); di.InitializeFrom(ref findData); return(di); }
internal static FileInfo CreateFileInfo(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData) { string cFileName = findData.cFileName; string text = Path.CombineNoChecks(searchData.fullPath, cFileName); if (!CodeAccessSecurityEngine.QuickCheckForAllDemands()) { new FileIOPermission(FileIOPermissionAccess.Read, new string[] { text }, false, false).Demand(); } FileInfo fileInfo = new FileInfo(text, cFileName); fileInfo.InitializeFrom(ref findData); return(fileInfo); }
internal FileSystemEnumerableIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost) { Contract.Requires(path != null); Contract.Requires(originalUserPath != null); Contract.Requires(searchPattern != null); Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly); Contract.Requires(resultHandler != null); #if !PLATFORM_UNIX _setBackOldMode = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out _oldMode); #endif searchStack = new List <Directory.SearchData>(); String normalizedSearchPattern = NormalizeSearchPattern(searchPattern); if (normalizedSearchPattern.Length == 0) { empty = true; } else { _resultHandler = resultHandler; this.searchOption = searchOption; fullPath = Path.GetFullPath(path); String fullSearchString = GetFullSearchString(fullPath, normalizedSearchPattern); normalizedSearchPath = Path.GetDirectoryName(fullSearchString); // 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); } this.userPath = userPathTemp; searchData = new Directory.SearchData(normalizedSearchPath, this.userPath, searchOption); CommonInit(); } }
internal Win32FileSystemEnumerableIterator(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 = Interop.mincore.SetErrorMode(Interop.SEM_FAILCRITICALERRORS); _searchStack = new List <Directory.SearchData>(); String normalizedSearchPattern = NormalizeSearchPattern(searchPattern); if (normalizedSearchPattern.Length == 0) { _empty = true; } else { _resultHandler = resultHandler; this._searchOption = searchOption; _fullPath = Path.GetFullPath(path); String fullSearchString = GetFullSearchString(_fullPath, normalizedSearchPattern); _normalizedSearchPath = Path.GetDirectoryName(fullSearchString); // 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); } this._userPath = userPathTemp; _searchData = new Directory.SearchData(_normalizedSearchPath, this._userPath, searchOption); CommonInit(); } }
private FileSystemEnumerableIterator(String fullPath, String normalizedSearchPath, String searchCriteria, String userPath, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost) { this.fullPath = fullPath; this.normalizedSearchPath = normalizedSearchPath; this.searchCriteria = searchCriteria; this._resultHandler = resultHandler; this.userPath = userPath; this.searchOption = searchOption; this._checkHost = checkHost; searchStack = new List <Directory.SearchData>(); if (searchCriteria != null) { if (CodeAccessSecurityEngine.QuickCheckForAllDemands()) { // Full trust, just need to validate incoming paths // (we don't need to get the demand directory as it has no impact) FileIOPermission.EmulateFileIOPermissionChecks(fullPath); FileIOPermission.EmulateFileIOPermissionChecks(normalizedSearchPath); } else { // Not full trust, need to check for rights string[] demandPaths = new string[2]; // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters demandPaths[0] = Directory.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] = Directory.GetDemandDir(normalizedSearchPath, true); new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false).Demand(); } searchData = new Directory.SearchData(normalizedSearchPath, userPath, searchOption); CommonInit(); } else { empty = true; } }
private FileSystemEnumerableIterator(String fullPath, String normalizedSearchPath, String searchCriteria, String userPath, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost) { this.fullPath = fullPath; this.normalizedSearchPath = normalizedSearchPath; this.searchCriteria = searchCriteria; this._resultHandler = resultHandler; this.userPath = userPath; this.searchOption = searchOption; this._checkHost = checkHost; searchStack = new List <Directory.SearchData>(); if (searchCriteria != null) { // permission demands String[] demandPaths = new String[2]; // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters demandPaths[0] = Directory.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] = Directory.GetDemandDir(normalizedSearchPath, true); #if MONO_FEATURE_CAS #if FEATURE_CORECLR if (checkHost) { FileSecurityState state1 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[0]); state1.EnsureState(); FileSecurityState state2 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[1]); state2.EnsureState(); } #else FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false); #endif #endif searchData = new Directory.SearchData(normalizedSearchPath, userPath, searchOption); CommonInit(); } else { empty = true; } }
private FileSystemEnumerableIterator(String fullPath, String normalizedSearchPath, String searchCriteria, String userPath, SearchOption searchOption, SearchResultHandler <TSource> resultHandler) { this.fullPath = fullPath; this.normalizedSearchPath = normalizedSearchPath; this.searchCriteria = searchCriteria; this._resultHandler = resultHandler; this.userPath = userPath; this.searchOption = searchOption; searchStack = new List <Directory.SearchData>(); if (searchCriteria != null) { searchData = new Directory.SearchData(normalizedSearchPath, userPath, searchOption); CommonInit(); } else { empty = true; } }
private void AddSearchableDirsToStack(Directory.SearchData localSearchData) { string fileName = Path.InternalCombine(localSearchData.fullPath, "*"); SafeFindHandle safeFindHandle = null; Win32Native.WIN32_FIND_DATA win32_FIND_DATA = default(Win32Native.WIN32_FIND_DATA); try { safeFindHandle = Win32Native.FindFirstFile(fileName, ref win32_FIND_DATA); if (safeFindHandle.IsInvalid) { int lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error == 2 || lastWin32Error == 18 || lastWin32Error == 3) { return; } this.HandleError(lastWin32Error, localSearchData.fullPath); } int num = 0; do { if (win32_FIND_DATA.IsNormalDirectory) { string cFileName = win32_FIND_DATA.cFileName; string text = Path.CombineNoChecks(localSearchData.fullPath, cFileName); string text2 = Path.CombineNoChecks(localSearchData.userPath, cFileName); SearchOption searchOption = localSearchData.searchOption; Directory.SearchData item = new Directory.SearchData(text, text2, searchOption); this.searchStack.Insert(num++, item); } }while (Win32Native.FindNextFile(safeFindHandle, ref win32_FIND_DATA)); } finally { if (safeFindHandle != null) { safeFindHandle.Dispose(); } } }
private void AddSearchableDirsToStack(Directory.SearchData localSearchData) { string fileName = localSearchData.fullPath + "*"; SafeFindHandle hndFindFile = null; Win32Native.WIN32_FIND_DATA win_find_data = new Win32Native.WIN32_FIND_DATA(); using (hndFindFile = Win32Native.FindFirstFile(fileName, win_find_data)) { if (hndFindFile.IsInvalid) { int hr = Marshal.GetLastWin32Error(); switch (hr) { case 2: case 0x12: case 3: return; } this.HandleError(hr, localSearchData.fullPath); } int num2 = 0; do { if (FileSystemEnumerableHelpers.IsDir(win_find_data)) { StringBuilder builder = new StringBuilder(localSearchData.fullPath); builder.Append(win_find_data.cFileName); string fullPath = builder.ToString(); builder.Length = 0; builder.Append(localSearchData.userPath); builder.Append(win_find_data.cFileName); SearchOption searchOption = localSearchData.searchOption; Directory.SearchData item = new Directory.SearchData(fullPath, builder.ToString(), searchOption); this.searchStack.Insert(num2++, item); } }while (Win32Native.FindNextFile(hndFindFile, win_find_data)); } }
private Win32FileSystemEnumerableIterator(String fullPath, String normalizedSearchPath, String searchCriteria, String userPath, SearchOption searchOption, SearchResultHandler <TSource> resultHandler) { this._fullPath = fullPath; this._normalizedSearchPath = normalizedSearchPath; this._searchCriteria = searchCriteria; this._resultHandler = resultHandler; this._userPath = userPath; this._searchOption = searchOption; _searchStack = new List <Directory.SearchData>(); if (searchCriteria != null) { PathHelpers.CheckInvalidPathChars(fullPath, true); _searchData = new Directory.SearchData(normalizedSearchPath, userPath, searchOption); CommonInit(); } else { _empty = true; } }
internal abstract TSource CreateObject(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData);
public override bool MoveNext() { Win32Native.WIN32_FIND_DATA wiN32FindData = new Win32Native.WIN32_FIND_DATA(); switch (this.state) { case 1: if (this.empty) { this.state = 4; goto case 4; } else if (this.searchData.searchOption == SearchOption.TopDirectoryOnly) { this.state = 3; if ((object)this.current != null) { return(true); } goto case 3; } else { this.state = 2; goto case 2; } case 2: while (this.searchStack.Count > 0) { this.searchData = this.searchStack[0]; this.searchStack.RemoveAt(0); this.AddSearchableDirsToStack(this.searchData); this._hnd = Win32Native.FindFirstFile(Path.InternalCombine(this.searchData.fullPath, this.searchCriteria), wiN32FindData); if (this._hnd.IsInvalid) { int lastWin32Error = Marshal.GetLastWin32Error(); switch (lastWin32Error) { case 2: case 18: case 3: continue; default: this._hnd.Dispose(); this.HandleError(lastWin32Error, this.searchData.fullPath); break; } } this.state = 3; this.needsParentPathDiscoveryDemand = true; SearchResult searchResult = this.CreateSearchResult(this.searchData, wiN32FindData); if (this._resultHandler.IsResultIncluded(searchResult)) { if (this.needsParentPathDiscoveryDemand) { this.DoDemand(this.searchData.fullPath); this.needsParentPathDiscoveryDemand = false; } this.current = this._resultHandler.CreateObject(searchResult); return(true); } goto case 3; } this.state = 4; goto case 4; case 3: if (this.searchData != null && this._hnd != null) { while (Win32Native.FindNextFile(this._hnd, wiN32FindData)) { SearchResult searchResult = this.CreateSearchResult(this.searchData, wiN32FindData); if (this._resultHandler.IsResultIncluded(searchResult)) { if (this.needsParentPathDiscoveryDemand) { this.DoDemand(this.searchData.fullPath); this.needsParentPathDiscoveryDemand = false; } this.current = this._resultHandler.CreateObject(searchResult); return(true); } } int lastWin32Error = Marshal.GetLastWin32Error(); if (this._hnd != null) { this._hnd.Dispose(); } if (lastWin32Error != 0 && lastWin32Error != 18 && lastWin32Error != 2) { this.HandleError(lastWin32Error, this.searchData.fullPath); } } if (this.searchData.searchOption == SearchOption.TopDirectoryOnly) { this.state = 4; goto case 4; } else { this.state = 2; goto case 2; } case 4: this.Dispose(); break; } return(false); }
public override bool MoveNext() { Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA(); switch (base.state) { case 1: if (!this.empty) { if (this.searchData.searchOption == SearchOption.TopDirectoryOnly) { base.state = 3; if (base.current != null) { return(true); } goto Label_017A; } base.state = 2; break; } base.state = 4; goto Label_0250; case 2: break; case 3: goto Label_017A; case 4: goto Label_0250; default: goto Label_0256; } Label_015D: while (this.searchStack.Count > 0) { this.searchData = this.searchStack[0]; this.searchStack.RemoveAt(0); this.AddSearchableDirsToStack(this.searchData); string fileName = this.searchData.fullPath + this.searchCriteria; this._hnd = Win32Native.FindFirstFile(fileName, data); if (this._hnd.IsInvalid) { int hr = Marshal.GetLastWin32Error(); switch (hr) { case 2: case 0x12: case 3: { continue; } } this._hnd.Dispose(); this.HandleError(hr, this.searchData.fullPath); } base.state = 3; this.needsParentPathDiscoveryDemand = true; SearchResult result = this.CreateSearchResult(this.searchData, data); if (!this._resultHandler.IsResultIncluded(result)) { goto Label_017A; } if (this.needsParentPathDiscoveryDemand) { FileSystemEnumerableIterator <TSource> .DoDemand(this.searchData.fullPath); this.needsParentPathDiscoveryDemand = false; } base.current = this._resultHandler.CreateObject(result); return(true); } base.state = 4; goto Label_0250; Label_017A: if ((this.searchData != null) && (this._hnd != null)) { while (Win32Native.FindNextFile(this._hnd, data)) { SearchResult result2 = this.CreateSearchResult(this.searchData, data); if (this._resultHandler.IsResultIncluded(result2)) { if (this.needsParentPathDiscoveryDemand) { FileSystemEnumerableIterator <TSource> .DoDemand(this.searchData.fullPath); this.needsParentPathDiscoveryDemand = false; } base.current = this._resultHandler.CreateObject(result2); return(true); } } int num2 = Marshal.GetLastWin32Error(); if (this._hnd != null) { this._hnd.Dispose(); } if (((num2 != 0) && (num2 != 0x12)) && (num2 != 2)) { this.HandleError(num2, this.searchData.fullPath); } } if (this.searchData.searchOption == SearchOption.TopDirectoryOnly) { base.state = 4; } else { base.state = 2; goto Label_015D; } Label_0250: base.Dispose(); Label_0256: return(false); }
internal FileSystemEnumerableIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost) { 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 = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS); searchStack = new List <Directory.SearchData>(); String normalizedSearchPattern = NormalizeSearchPattern(searchPattern); if (normalizedSearchPattern.Length == 0) { empty = true; } else { _resultHandler = resultHandler; this.searchOption = searchOption; fullPath = Path.GetFullPathInternal(path); String fullSearchString = GetFullSearchString(fullPath, normalizedSearchPattern); normalizedSearchPath = Path.GetDirectoryName(fullSearchString); if (CodeAccessSecurityEngine.QuickCheckForAllDemands()) { // Full trust, just need to validate incoming paths // (we don't need to get the demand directory as it has no impact) FileIOPermission.EmulateFileIOPermissionChecks(fullPath); FileIOPermission.EmulateFileIOPermissionChecks(normalizedSearchPath); } else { // Not full trust, need to check for rights string[] demandPaths = new string[2]; // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters demandPaths[0] = Directory.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] = Directory.GetDemandDir(normalizedSearchPath, true); new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false).Demand(); } _checkHost = checkHost; // 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.CombineNoChecks(userPathTemp, searchPatternDirName); } this.userPath = userPathTemp; searchData = new Directory.SearchData(normalizedSearchPath, this.userPath, searchOption); CommonInit(); } }
internal FileSystemEnumerableIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost) { 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 = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS); searchStack = new List <Directory.SearchData>(); String normalizedSearchPattern = NormalizeSearchPattern(searchPattern); if (normalizedSearchPattern.Length == 0) { empty = true; } else { _resultHandler = resultHandler; this.searchOption = searchOption; fullPath = Path.GetFullPathInternal(path); String fullSearchString = GetFullSearchString(fullPath, normalizedSearchPattern); normalizedSearchPath = Path.GetDirectoryName(fullSearchString); // permission demands String[] demandPaths = new String[2]; // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters demandPaths[0] = Directory.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] = Directory.GetDemandDir(normalizedSearchPath, true); _checkHost = checkHost; #if FEATURE_CORECLR if (checkHost) { FileSecurityState state1 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[0]); state1.EnsureState(); FileSecurityState state2 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[1]); state2.EnsureState(); } #else new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false).Demand(); #endif // 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); } this.userPath = userPathTemp; searchData = new Directory.SearchData(normalizedSearchPath, this.userPath, searchOption); CommonInit(); } }
internal override DirectoryInfo CreateObject(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData) { return(CreateDirectoryInfo(searchData, ref findData)); }
public override bool MoveNext() { Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA(); switch (state) { case STATE_INIT: { if (empty) { state = STATE_FINISH; goto case STATE_FINISH; } if (searchData.searchOption == SearchOption.TopDirectoryOnly) { state = STATE_FIND_NEXT_FILE; if (current != null) { return(true); } else { goto case STATE_FIND_NEXT_FILE; } } else { state = STATE_SEARCH_NEXT_DIR; goto case STATE_SEARCH_NEXT_DIR; } } case STATE_SEARCH_NEXT_DIR: { Contract.Assert(searchData.searchOption != SearchOption.TopDirectoryOnly, "should not reach this code path if searchOption == TopDirectoryOnly"); // Traverse directory structure. We need to get '*' while (searchStack.Count > 0) { searchData = searchStack[0]; Contract.Assert((searchData.fullPath != null), "fullpath can't be null!"); searchStack.RemoveAt(0); // Traverse the subdirs AddSearchableDirsToStack(searchData); // Execute searchCriteria against the current directory String searchPath = Path.InternalCombine(searchData.fullPath, searchCriteria); // Open a Find handle _hnd = Win32Native.FindFirstFile(searchPath, data); if (_hnd.IsInvalid) { int hr = Marshal.GetLastWin32Error(); if (hr == Win32Native.ERROR_FILE_NOT_FOUND || hr == Win32Native.ERROR_NO_MORE_FILES || hr == Win32Native.ERROR_PATH_NOT_FOUND) { continue; } _hnd.Dispose(); HandleError(hr, searchData.fullPath); } state = STATE_FIND_NEXT_FILE; needsParentPathDiscoveryDemand = true; SearchResult searchResult = CreateSearchResult(searchData, data); if (_resultHandler.IsResultIncluded(searchResult)) { if (needsParentPathDiscoveryDemand) { DoDemand(searchData.fullPath); needsParentPathDiscoveryDemand = false; } current = _resultHandler.CreateObject(searchResult); return(true); } else { goto case STATE_FIND_NEXT_FILE; } } state = STATE_FINISH; goto case STATE_FINISH; } case STATE_FIND_NEXT_FILE: { if (searchData != null && _hnd != null) { // Keep asking for more matching files/dirs, add it to the list while (Win32Native.FindNextFile(_hnd, data)) { SearchResult searchResult = CreateSearchResult(searchData, data); if (_resultHandler.IsResultIncluded(searchResult)) { if (needsParentPathDiscoveryDemand) { DoDemand(searchData.fullPath); needsParentPathDiscoveryDemand = false; } current = _resultHandler.CreateObject(searchResult); return(true); } } // Make sure we quit with a sensible error. int hr = Marshal.GetLastWin32Error(); if (_hnd != null) { _hnd.Dispose(); } // ERROR_FILE_NOT_FOUND is valid here because if the top level // dir doen't contain any subdirs and matching files then // we will get here with this errorcode from the searchStack walk if ((hr != 0) && (hr != Win32Native.ERROR_NO_MORE_FILES) && (hr != Win32Native.ERROR_FILE_NOT_FOUND)) { HandleError(hr, searchData.fullPath); } } if (searchData.searchOption == SearchOption.TopDirectoryOnly) { state = STATE_FINISH; goto case STATE_FINISH; } else { state = STATE_SEARCH_NEXT_DIR; goto case STATE_SEARCH_NEXT_DIR; } } case STATE_FINISH: { Dispose(); break; } } return(false); }
internal override string CreateObject(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData) { return(Path.CombineNoChecks(searchData.userPath, findData.cFileName)); }
[System.Security.SecurityCritical] // auto-generated private void AddSearchableDirsToStack(Directory.SearchData localSearchData) { Contract.Requires(localSearchData != null); String searchPath = Path.InternalCombine(localSearchData.fullPath, "*"); SafeFindHandle hnd = null; Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA(); try { // Get all files and dirs hnd = Win32Native.FindFirstFile(searchPath, data); if (hnd.IsInvalid) { int hr = Marshal.GetLastWin32Error(); // This could happen if the dir doesn't contain any files. // Continue with the recursive search though, eventually // searchStack will become empty if (hr == Win32Native.ERROR_FILE_NOT_FOUND || hr == Win32Native.ERROR_NO_MORE_FILES || hr == Win32Native.ERROR_PATH_NOT_FOUND) { return; } HandleError(hr, localSearchData.fullPath); } // Add subdirs to searchStack. Exempt ReparsePoints as appropriate int incr = 0; do { if (FileSystemEnumerableHelpers.IsDir(data)) { String tempFullPath = Path.InternalCombine(localSearchData.fullPath, data.cFileName); String tempUserPath = Path.InternalCombine(localSearchData.userPath, data.cFileName); SearchOption option = localSearchData.searchOption; #if EXCLUDE_REPARSEPOINTS // Traverse reparse points depending on the searchoption specified if ((searchDataSubDir.searchOption == SearchOption.AllDirectories) && (0 != (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_REPARSE_POINT))) { option = SearchOption.TopDirectoryOnly; } #endif // Setup search data for the sub directory and push it into the stack Directory.SearchData searchDataSubDir = new Directory.SearchData(tempFullPath, tempUserPath, option); searchStack.Insert(incr++, searchDataSubDir); } } while (Win32Native.FindNextFile(hnd, data)); // We don't care about errors here } finally { if (hnd != null) { hnd.Dispose(); } } }
private SearchResult CreateSearchResult(Directory.SearchData localSearchData, Win32Native.WIN32_FIND_DATA findData) { return(new SearchResult(Path.InternalCombine(localSearchData.fullPath, findData.cFileName), Path.InternalCombine(localSearchData.userPath, findData.cFileName), findData)); }