Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
51803c3
Use File.Exists in GetFileSecurityZone
iSazonov Jun 7, 2018
c692d1c
Use File.Exists in CommandSearcher.cs
iSazonov Jun 9, 2018
9f8375f
Use File.Exists in AnalysisCache.cs
iSazonov Jun 9, 2018
5950a94
Use File.Exists in ModuleUtils.cs
iSazonov Jun 9, 2018
4f5db0e
Use File.Exists in UpdatableHelpSystem.cs
iSazonov Jun 9, 2018
358c14b
Use File.Exists in ModuleCmdletBase.cs
iSazonov Jun 9, 2018
1c1386b
Refactor FileSystemProvider.NormalizeThePath() method
iSazonov Jun 9, 2018
68448bb
Use File.Exists and Directory.Exists in GetFileSystemInfo
iSazonov Jun 9, 2018
3e0d086
Use File.Exists in CopyItemFromRemoteSession
iSazonov Jun 9, 2018
42b5629
Remove Utils.FileExists() method
iSazonov Jun 9, 2018
848d59b
Use Directory.Exists in ModuleCmdletBase.cs
iSazonov Jun 9, 2018
7de56f1
Use Directory.Exists in SessionStateDriveAPIs.cs
iSazonov Jun 9, 2018
e30b381
Use Directory.Exists in LocationGlobber.cs
iSazonov Jun 9, 2018
10f481e
Use Directory.Exists in FileSystemProvider.cs
iSazonov Jun 9, 2018
4e5bd0d
Remove Utils.DirectoryExists()
iSazonov Jun 9, 2018
bb22d3c
Exclude Utils.ItemExists() from FileSystemProvider.ItemExists()
iSazonov Jun 9, 2018
a99bd63
Exclude Utils.ItemExists() from FileSystemProvider.NewItem()
iSazonov Jun 9, 2018
abff189
Exclude Utils.ItemExists() from FileSystemProvider.RemoveDirectoryInf…
iSazonov Jun 9, 2018
666de17
Exclude Utils.ItemExists() from FileSystemProvider.GetProperty()
iSazonov Jun 9, 2018
f9f08c7
Exclude Utils.ItemExists() from FileSystemProvider.NewItem() - 2
iSazonov Jun 9, 2018
85a9865
Exclude Utils.ItemExists() from FileSystemProvider.NewItem() - 3
iSazonov Jun 9, 2018
b81b3c5
Exclude Utils.ItemExists() from FileSystemProvider.NewItem() - 4
iSazonov Jun 9, 2018
f678fe9
Exclude Utils.ItemExists() from FileSystemProvider.GetPathItems()
iSazonov Jun 9, 2018
f57ec8a
Exclude Utils.ItemExists() from FileSystemProvider.SetProperty()
iSazonov Jun 9, 2018
08b2e99
Exclude Utils.ItemExists() from FileSystemProvider.NormalizeRelativeP…
iSazonov Jun 9, 2018
4dc4838
Remove Utils.ItemExists()
iSazonov Jun 9, 2018
0172040
[Feature] Exclude Utils.ItemExists() from security\Utils.cs
iSazonov Jun 9, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Microsoft.PowerShell.Security/security/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ internal static string GetFilePathOfExistingFile(PSCmdlet cmdlet,
string path)
{
string resolvedProviderPath = cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath(path);
if (Utils.FileExists(resolvedProviderPath))
if (File.Exists(resolvedProviderPath))
{
return resolvedProviderPath;
}
Expand Down
2 changes: 1 addition & 1 deletion src/System.Management.Automation/engine/CommandSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ private CommandInfo GetInfoFromPath(string path)

do // false loop
{
if (!Utils.FileExists(path))
if (!File.Exists(path))
{
CommandDiscovery.discoveryTracer.TraceError("The path does not exist: {0}", path);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,9 @@ private void Cleanup()
var keys = Entries.Keys;
foreach (var key in keys)
{
if (!Utils.FileExists(key))
if (!File.Exists(key))
{
ModuleCacheEntry unused;
removedSomething |= Entries.TryRemove(key, out unused);
removedSomething |= Entries.TryRemove(key, out ModuleCacheEntry _);
}
}

Expand Down Expand Up @@ -690,7 +689,7 @@ private void Serialize(string filename)

try
{
if (Utils.FileExists(filename))
if (File.Exists(filename))
{
var fileLastWriteTime = new FileInfo(filename).LastWriteTime;
if (fileLastWriteTime > this.LastReadTime)
Expand Down Expand Up @@ -987,7 +986,7 @@ internal static AnalysisCacheData Get()
{
try
{
if (Utils.FileExists(s_cacheStoreLocation))
if (File.Exists(s_cacheStoreLocation))
{
return Deserialize(s_cacheStoreLocation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ internal bool LoadUsingModulePath(PSModuleInfo parentModule, bool found, IEnumer
{
qualifiedPath = Path.Combine(qualifiedPath, fileBaseName);
}
else if (Utils.DirectoryExists(qualifiedPath))
else if (Directory.Exists(qualifiedPath))
{
// if it points to a directory, add the basename back onto the path...
qualifiedPath = Path.Combine(qualifiedPath, Path.GetFileName(fileBaseName));
Expand Down Expand Up @@ -924,10 +924,9 @@ private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(string[] modulePaths,
foreach (string resolvedModulePath in modulePathCollection)
{
string moduleName = Path.GetFileName(resolvedModulePath);
bool isDirectory = Utils.DirectoryExists(resolvedModulePath);

// If the given path is a valid module file, we will load the specific file
if (!isDirectory && ModuleIntrinsics.IsPowerShellModuleExtension(Path.GetExtension(moduleName)))
if (!Directory.Exists(resolvedModulePath) && ModuleIntrinsics.IsPowerShellModuleExtension(Path.GetExtension(moduleName)))
{
PSModuleInfo module = CreateModuleInfoForGetModule(resolvedModulePath, refresh);
if (module != null)
Expand Down Expand Up @@ -1624,7 +1623,7 @@ internal PSModuleInfo LoadModuleManifest(
return loadedModule;
}
// remove the module if force is specified (and if module is already loaded)
else if (Utils.FileExists(rootedPath))
else if (File.Exists(rootedPath))
{
RemoveModule(loadedModule);
}
Expand Down Expand Up @@ -4278,7 +4277,7 @@ private bool GetListOfFilesFromData(
// which we can't really do b/c the file doesn't exist.
fixedFileName = psHome + "\\" + Path.GetFileName(s);
}
else if (verifyFilesExist && !Utils.FileExists(fixedFileName))
else if (verifyFilesExist && !File.Exists(fixedFileName))
{
string message = StringUtil.Format(SessionStateStrings.PathNotFound, fixedFileName);
throw new FileNotFoundException(message, fixedFileName);
Expand Down Expand Up @@ -5002,7 +5001,7 @@ internal PSModuleInfo IsModuleImportUnnecessaryBecauseModuleIsAlreadyLoaded(stri
else // reimport the module + return alreadyLoadedModule (alreadyLoadedModule = no need to proceed with regular import)
{
// If the module has already been loaded, then while loading it the second time, we should load it with the DefaultCommandPrefix specified in the module manifest. (If there is no Prefix from command line)
if (string.IsNullOrEmpty(prefix) && Utils.FileExists(alreadyLoadedModule.Path))
if (string.IsNullOrEmpty(prefix) && File.Exists(alreadyLoadedModule.Path))
{
string defaultPrefix = GetDefaultPrefix(alreadyLoadedModule);
if (!string.IsNullOrEmpty(defaultPrefix))
Expand Down Expand Up @@ -5178,7 +5177,7 @@ internal PSModuleInfo LoadUsingExtensions(PSModuleInfo parentModule,
found = true;
return module;
}
else if (Utils.FileExists(fileName))
else if (File.Exists(fileName))
{
moduleFileFound = true;
// Win8: 325243 - Added the version check so that we do not unload modules with the same name but different version
Expand Down Expand Up @@ -5328,7 +5327,7 @@ internal PSModuleInfo LoadModule(PSModuleInfo parentModule, string fileName, str
{
Dbg.Assert(fileName != null, "Filename argument to LoadModule() shouldn't be null");

if (!Utils.FileExists(fileName))
if (!File.Exists(fileName))
{
found = false;
moduleFileFound = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ internal static List<string> GetModuleVersionsFromAbsolutePath(string directory)
{
string moduleFile = Path.Combine(directory, fileName) + ext;

if (!Utils.FileExists(moduleFile))
if (!File.Exists(moduleFile))
{
continue;
}
Expand Down
20 changes: 2 additions & 18 deletions src/System.Management.Automation/engine/SessionStateDriveAPIs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -963,25 +963,9 @@ private bool IsAStaleVhdMountedDrive(PSDriveInfo drive)

if (systemDriveInfo.DriveType == DriveType.NoRootDirectory)
{
try
{
// Checking for the presence of mounted drive locally using Utils.DirectoryExists API as
// the calls to this API is faster than normal Directory.Exist API.
bool validDrive = Utils.DirectoryExists(drive.Root);
if (!validDrive)
{
result = true;
}
}
// We don't want to have automounting cause an exception. We
// rather it just fail silently as it wasn't a result of an
// explicit request by the user anyway.
// Following the same pattern as the Calling API.
catch (IOException)
{
}
catch (UnauthorizedAccessException)
if (!Directory.Exists(drive.Root))
{
result = true;
}
}
}
Expand Down
55 changes: 0 additions & 55 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -889,61 +889,6 @@ internal static bool IsAdministrator()
#endif
}

internal static bool ItemExists(string path)
{
try
{
return ItemExists(path, out bool _);
}
catch
{
}

return false;
}

internal static bool ItemExists(string path, out bool isDirectory)
{
isDirectory = false;

if (String.IsNullOrEmpty(path))
{
return false;
}

if (IsReservedDeviceName(path))
{
return false;
}

try
{
// Use 'File.GetAttributes()' because we want to get access exceptions.
FileAttributes attributes = File.GetAttributes(path);
isDirectory = attributes.HasFlag(FileAttributes.Directory);

return (int)attributes != -1;
}
catch (IOException)
{
return false;
}
}

internal static bool FileExists(string path)
{
bool itemExists = ItemExists(path, out bool isDirectory);

return (itemExists && (!isDirectory));
}

internal static bool DirectoryExists(string path)
{
bool itemExists = ItemExists(path, out bool isDirectory);

return (itemExists && isDirectory);
}

internal static void NativeEnumerateDirectory(string directory, out List<string> directories, out List<string> files)
{
IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
Expand Down
32 changes: 18 additions & 14 deletions src/System.Management.Automation/help/UpdatableHelpSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,32 +1627,36 @@ internal static string LoadStringFromPath(PSCmdlet cmdlet, string path, PSCreden
/// <returns></returns>
internal static string GetFilePath(string path)
{
FileInfo item = new FileInfo(path);

// We use 'FileInfo.Attributes' (not 'FileInfo.Exist')
// because we want to get exceptions
// like UnauthorizedAccessException or IOException.
if ((int)item.Attributes != -1)
{
return path;
}
#if UNIX
// On Linux, file paths are case sensitive.
// The user does not have control over the files (HelpInfo.xml, .zip, and cab) that are generated by the Publishing team.
// The logic below is to support updating help content via sourcepath parameter for case insensitive files.
FileInfo item = new FileInfo(path);
string directoryPath = item.Directory.FullName;
var dirInfo = item.Directory;
string fileName = item.Name;

// Prerequisite: The directory in the given path must exist and it is case sensitive.
if (Utils.DirectoryExists(directoryPath))
if (dirInfo.Exists)
{
// Get the list of files in the directory.
string[] fileList = Directory.GetFiles(directoryPath);
foreach (string filePath in fileList)
FileInfo[] fileList = dirInfo.GetFiles(searchPattern: fileName, new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive });

// We use 'FileInfo.Attributes' (not 'FileInfo.Exist')
// because we want to get exceptions
// like UnauthorizedAccessException or IOException.
if (fileList.Length > 0 && (int)fileList[0].Attributes != -1)
{
if (filePath.EndsWith(fileName, StringComparison.OrdinalIgnoreCase))
{
return filePath;
}
return fileList[0].FullName;
}
}
#else
if (Utils.FileExists(path))
{
return path;
}
#endif
return null;
}
Expand Down
Loading