From 34399f0edf85ad6e929a154296c548b072deb045 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 29 Aug 2018 17:19:33 +0500 Subject: [PATCH 1/8] Cleanup FileSystemProvider from runtime checks Replace runtime checks with compile time checks. Remove unneeded methods. --- .../CoreCLR/CorePsPlatform.cs | 6 ---- .../namespaces/FileSystemProvider.cs | 34 ++++++------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index 32585530ac7..becede6497c 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -497,12 +497,6 @@ internal static bool NonWindowsIsSymLink(FileSystemInfo fileInfo) return Unix.NativeMethods.IsSymLink(fileInfo.FullName); } - internal static string NonWindowsInternalGetTarget(SafeFileHandle handle) - { - // SafeHandle is a Windows concept. Use the string version instead. - throw new PlatformNotSupportedException(); - } - internal static string NonWindowsInternalGetTarget(string path) { return Unix.NativeMethods.FollowSymLink(path); diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index d8a2a9151d0..c15f2842818 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7618,25 +7618,23 @@ internal static extern IntPtr CreateFile( /// The target of the reparse point public static IEnumerable GetTarget(PSObject instance) { - FileSystemInfo fileSysInfo = instance.BaseObject as FileSystemInfo; - - if (fileSysInfo != null) + if (instance.BaseObject is FileSystemInfo fileSysInfo) { - if (Platform.IsWindows) +#if !Unix + using (SafeFileHandle handle = OpenReparsePoint(fileSysInfo.FullName, FileDesiredAccess.GenericRead)) { - using (SafeFileHandle handle = OpenReparsePoint(fileSysInfo.FullName, FileDesiredAccess.GenericRead)) - { - string linkTarget = InternalGetTarget(handle); + string linkTarget = WinInternalGetTarget(handle); - if (linkTarget != null) - return (new string[] { linkTarget }); + if (linkTarget != null) + { + return (new string[] { linkTarget }); } } - +#endif return InternalGetTarget(fileSysInfo.FullName); } - else - return null; + + return null; } /// @@ -7964,18 +7962,6 @@ internal static bool WinIsHardLink(ref IntPtr handle) return succeeded && (handleInfo.NumberOfLinks > 1); } - private static string InternalGetTarget(SafeFileHandle handle) - { - if (Platform.IsWindows) - { - return WinInternalGetTarget(handle); - } - else - { - return Platform.NonWindowsInternalGetTarget(handle); - } - } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods")] private static string WinInternalGetTarget(SafeFileHandle handle) { From 651e1b525d62f6fd4a8a13fe09b5b1ecf9b63991 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 29 Aug 2018 18:12:36 +0500 Subject: [PATCH 2/8] Revert "Cleanup FileSystemProvider from runtime checks" This reverts commit 34399f0edf85ad6e929a154296c548b072deb045. --- .../CoreCLR/CorePsPlatform.cs | 6 ++++ .../namespaces/FileSystemProvider.cs | 34 +++++++++++++------ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index becede6497c..32585530ac7 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -497,6 +497,12 @@ internal static bool NonWindowsIsSymLink(FileSystemInfo fileInfo) return Unix.NativeMethods.IsSymLink(fileInfo.FullName); } + internal static string NonWindowsInternalGetTarget(SafeFileHandle handle) + { + // SafeHandle is a Windows concept. Use the string version instead. + throw new PlatformNotSupportedException(); + } + internal static string NonWindowsInternalGetTarget(string path) { return Unix.NativeMethods.FollowSymLink(path); diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index c15f2842818..d8a2a9151d0 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7618,23 +7618,25 @@ internal static extern IntPtr CreateFile( /// The target of the reparse point public static IEnumerable GetTarget(PSObject instance) { - if (instance.BaseObject is FileSystemInfo fileSysInfo) + FileSystemInfo fileSysInfo = instance.BaseObject as FileSystemInfo; + + if (fileSysInfo != null) { -#if !Unix - using (SafeFileHandle handle = OpenReparsePoint(fileSysInfo.FullName, FileDesiredAccess.GenericRead)) + if (Platform.IsWindows) { - string linkTarget = WinInternalGetTarget(handle); - - if (linkTarget != null) + using (SafeFileHandle handle = OpenReparsePoint(fileSysInfo.FullName, FileDesiredAccess.GenericRead)) { - return (new string[] { linkTarget }); + string linkTarget = InternalGetTarget(handle); + + if (linkTarget != null) + return (new string[] { linkTarget }); } } -#endif + return InternalGetTarget(fileSysInfo.FullName); } - - return null; + else + return null; } /// @@ -7962,6 +7964,18 @@ internal static bool WinIsHardLink(ref IntPtr handle) return succeeded && (handleInfo.NumberOfLinks > 1); } + private static string InternalGetTarget(SafeFileHandle handle) + { + if (Platform.IsWindows) + { + return WinInternalGetTarget(handle); + } + else + { + return Platform.NonWindowsInternalGetTarget(handle); + } + } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods")] private static string WinInternalGetTarget(SafeFileHandle handle) { From 125e4be30701479120d4c0b666515ea556d08784 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 29 Aug 2018 18:17:31 +0500 Subject: [PATCH 3/8] Step 1 --- .../namespaces/FileSystemProvider.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index d8a2a9151d0..5550d47a5f5 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7622,17 +7622,17 @@ public static IEnumerable GetTarget(PSObject instance) if (fileSysInfo != null) { - if (Platform.IsWindows) +#if !UNIX + using (SafeFileHandle handle = OpenReparsePoint(fileSysInfo.FullName, FileDesiredAccess.GenericRead)) { - using (SafeFileHandle handle = OpenReparsePoint(fileSysInfo.FullName, FileDesiredAccess.GenericRead)) - { - string linkTarget = InternalGetTarget(handle); + string linkTarget = InternalGetTarget(handle); - if (linkTarget != null) - return (new string[] { linkTarget }); + if (linkTarget != null) + { + return (new string[] { linkTarget }); } } - +#endif return InternalGetTarget(fileSysInfo.FullName); } else From 6a06f1c73b4faec2cc9bcc03e7e0e9e23b1e07f6 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 29 Aug 2018 18:45:30 +0500 Subject: [PATCH 4/8] Step 2 --- .../namespaces/FileSystemProvider.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 5550d47a5f5..886bcd9394f 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7618,9 +7618,7 @@ internal static extern IntPtr CreateFile( /// The target of the reparse point public static IEnumerable GetTarget(PSObject instance) { - FileSystemInfo fileSysInfo = instance.BaseObject as FileSystemInfo; - - if (fileSysInfo != null) + if (instance.BaseObject is FileSystemInfo fileSysInfo) { #if !UNIX using (SafeFileHandle handle = OpenReparsePoint(fileSysInfo.FullName, FileDesiredAccess.GenericRead)) @@ -7635,8 +7633,8 @@ public static IEnumerable GetTarget(PSObject instance) #endif return InternalGetTarget(fileSysInfo.FullName); } - else - return null; + + return null; } /// From 10e81eb3d04659bfd020387e6cdbb67d420a8061 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 29 Aug 2018 19:15:27 +0500 Subject: [PATCH 5/8] Step 3 --- .../namespaces/FileSystemProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 886bcd9394f..b27e88984a3 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7623,7 +7623,7 @@ public static IEnumerable GetTarget(PSObject instance) #if !UNIX using (SafeFileHandle handle = OpenReparsePoint(fileSysInfo.FullName, FileDesiredAccess.GenericRead)) { - string linkTarget = InternalGetTarget(handle); + string linkTarget = WinInternalGetTarget(handle); if (linkTarget != null) { From 8b881d2eac247f8c7b7b6bd2a18044b03e9beb9e Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 29 Aug 2018 19:39:21 +0500 Subject: [PATCH 6/8] Step 4 --- .../namespaces/FileSystemProvider.cs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index b27e88984a3..5571c2ef647 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7962,18 +7962,6 @@ internal static bool WinIsHardLink(ref IntPtr handle) return succeeded && (handleInfo.NumberOfLinks > 1); } - private static string InternalGetTarget(SafeFileHandle handle) - { - if (Platform.IsWindows) - { - return WinInternalGetTarget(handle); - } - else - { - return Platform.NonWindowsInternalGetTarget(handle); - } - } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods")] private static string WinInternalGetTarget(SafeFileHandle handle) { From b8e53221b54405156d7c773246fa8c36ad20573d Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 29 Aug 2018 20:04:11 +0500 Subject: [PATCH 7/8] Step 5 --- src/System.Management.Automation/CoreCLR/CorePsPlatform.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index 32585530ac7..becede6497c 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -497,12 +497,6 @@ internal static bool NonWindowsIsSymLink(FileSystemInfo fileInfo) return Unix.NativeMethods.IsSymLink(fileInfo.FullName); } - internal static string NonWindowsInternalGetTarget(SafeFileHandle handle) - { - // SafeHandle is a Windows concept. Use the string version instead. - throw new PlatformNotSupportedException(); - } - internal static string NonWindowsInternalGetTarget(string path) { return Unix.NativeMethods.FollowSymLink(path); From 08e3d7ff5a1f0f56705b27c815a2fcb1c2d7ed7a Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 6 Sep 2018 10:26:04 +0500 Subject: [PATCH 8/8] [Feature]