Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9e4faba
[Feature] Exclude Utils.ItemExists() from security\Utils.cs
iSazonov Jun 9, 2018
0305aee
[Feature] Use File.Exists in CommandSearcher.cs
iSazonov Jun 9, 2018
24d0bac
[Feature] Use File.Exists in AnalysisCache.cs
iSazonov Jun 9, 2018
00ac7e2
[Feature] Use File.Exists in UpdatableHelpSystem.cs
iSazonov Jun 9, 2018
c05bf39
[Feature] Use File.Exists in ModuleCmdletBase.cs
iSazonov Jun 9, 2018
71b7e8f
[Feature] Refactor FileSystemProvider.NormalizeThePath() method
iSazonov Jun 9, 2018
469d7c0
[Feature] Use File.Exists and Directory.Exists in GetFileSystemInfo
iSazonov Jun 9, 2018
08f3c5a
[Feature] Use File.Exists in CopyItemFromRemoteSession
iSazonov Jun 9, 2018
519474a
[Feature] Remove Utils.FileExists() method
iSazonov Jun 9, 2018
cb237d2
[Feature] Use File.Exists in ClrFacade.cs
iSazonov Jul 2, 2018
53211d1
[Feature] Use Directory.Exists in ModuleCmdletBase.cs
iSazonov Jun 9, 2018
f38b900
[Feature] Use Directory.Exists in SessionStateDriveAPIs.cs
iSazonov Jun 9, 2018
e51e85a
[Feature] Use Directory.Exists in LocationGlobber.cs
iSazonov Jun 9, 2018
a6dfa41
[Feature] Use Directory.Exists in FileSystemProvider.cs
iSazonov Jun 9, 2018
d4da5f0
[Feature] Remove Utils.DirectoryExists()
iSazonov Jun 9, 2018
3ec7825
[Feature] Exclude Utils.ItemExists() from FileSystemProvider.ItemExis…
iSazonov Jun 21, 2018
adbdb0a
[Feature] Exclude Utils.ItemExists() from FileSystemProvider.NewItem()
iSazonov Jun 21, 2018
59b0956
[Feature] Exclude Utils.ItemExists() from FileSystemProvider.GetPrope…
iSazonov Jun 9, 2018
201748f
[Feature] Exclude Utils.ItemExists() from FileSystemProvider.GetPathI…
iSazonov Jun 22, 2018
19f1c17
[Feature] Exclude Utils.ItemExists() from FileSystemProvider.SetPrope…
iSazonov Jun 22, 2018
a412981
[Feature] Exclude Utils.ItemExists() from FileSystemProvider.NewItem(…
iSazonov Jun 22, 2018
97bedc7
[Feature] Exclude Utils.ItemExists() from FileSystemProvider.NewItem(…
iSazonov Jun 22, 2018
41cda4b
[Feature] Exclude Utils.ItemExists() from FileSystemProvider.NewItem(…
iSazonov Jun 22, 2018
1075f9f
[Feature] Remove Utils.ItemExists()
iSazonov Jun 9, 2018
c53aaa3
[Feature] Use File.Exists and Directory.Exists in FileSystemProvider.…
iSazonov Jul 2, 2018
ff80c28
[Feature] Address feedback
iSazonov Jul 12, 2018
b975731
Remove unneeded casts
iSazonov Jul 13, 2018
92eaca0
Move var declaration
iSazonov Jul 13, 2018
1f065fd
Remove new code
iSazonov Jul 13, 2018
5b04c07
Use GetFileSystemInfo() in NormalizeThePath()
iSazonov Jul 13, 2018
558cd50
Remove unneeded file existence check
iSazonov Jul 24, 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 @@ -906,10 +906,9 @@ private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(List<string> modulePat
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 @@ -1578,7 +1577,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 @@ -4228,7 +4227,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 @@ -4952,7 +4951,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 @@ -5128,7 +5127,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 @@ -5278,7 +5277,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
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be

return !DirectoryExists(drive.Root)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems original code is easier to understand.

{
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
29 changes: 15 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,33 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain how do you decide which Utils.DirectoryExists or Utils.FileExists can be replaced with Directory.Exists and File.Exists (swallow all exceptions), and which ones need to continue throw exceptions? I wonder why we want to get exceptions in this method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to keep the logic of the previous code. And I tried to make as few changes as possible. I'm afraid that this PR will grow many times if we try to correct the previous logic in all places where Utils.DirectoryExists or Utils.FileExists exist.

Here I'm afraid that we'll get issues in LoadStringFromPath() .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you think this method GetFilePath(string path) should throw the exception from checking the file existence?

I tried to keep the logic of the previous code

The previous code just used Utils.FileExists and Utils.DirectoryExists like everywhere else. Here, you didn't replace them to File.Exists and Directory.Exists but check the existence in a way that may throw exception. I want to know what your decision is based on.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utils.FileExists and Utils.DirectoryExists can throw but File.Exists and Directory.Exists don't throw.
If calling code catch exceptions and ignore them we can safely replace Utils.FileExists and Utils.DirectoryExists with File.Exists and Directory.Exists.
Here calling code don't catch and ignore exceptions so I used item.Attributes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If calling code catch exceptions and ignore them we can safely replace Utils.FileExists and Utils.DirectoryExists with File.Exists and Directory.Exists.
Here calling code don't catch and ignore exceptions so I used item.Attributes.

Does this mean for all the places you changed to File.Exists or Directory.Exists, you have checked that the calling code actually caches exceptions and ignore them?

Copy link
Collaborator Author

@iSazonov iSazonov Jul 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I did this commit by commit.

[daxian-dbw] #Closed

{
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 System.IO.EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive });

if (fileList.Length > 0)
{
if (filePath.EndsWith(fileName, StringComparison.OrdinalIgnoreCase))
{
return filePath;
}
return fileList[0].FullName;
}
}
#else
if (Utils.FileExists(path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was in windows-only section. You moved it above at the beginning of the method, but that's not made "compile-only-windows"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that we will always receive the path with the right case. (Seems we get the path in code not from user). So the first block will always return a value on all platforms. The code below will only search the file on Unix with ignoring case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it makes sense to to always have that check on all plats. #closed

{
return path;
}
#endif
return null;
}
Expand Down
Loading