From 78f8005de294c81202b95a75d68f9d5a88251c32 Mon Sep 17 00:00:00 2001 From: Gabe Stocco <98900+gfs@users.noreply.github.com> Date: Fri, 25 Aug 2023 14:24:40 -0700 Subject: [PATCH] Fix #696 Also retry reading signature and dllcharacteristics with readonly mode on encountering IOException. --- Lib/Collectors/WindowsFileSystemUtils.cs | 32 +++++++++++------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/Lib/Collectors/WindowsFileSystemUtils.cs b/Lib/Collectors/WindowsFileSystemUtils.cs index 3cbd3f9a..42c867d9 100644 --- a/Lib/Collectors/WindowsFileSystemUtils.cs +++ b/Lib/Collectors/WindowsFileSystemUtils.cs @@ -83,28 +83,19 @@ public static List GetDllCharacteristics(string Path) } // This can happen if the file is readable but not writable as MMFile tries to open the file with write privileges. // When we fail we can retry by reading the file to a Stream first, which cen be less performant but works with just Read access - // See #684 - catch (UnauthorizedAccessException) + // See #684 + #696 + catch (Exception e) when (e is UnauthorizedAccessException or IOException) { try { using var stream = File.OpenRead(Path); return GetDllCharacteristics(Path, stream); } - catch (Exception e) + catch (Exception ex) { - Log.Debug(e, "Failed to get DLL Characteristics for {0} ({1}:{2})", Path, e.GetType(), e.Message); + Log.Debug(ex, "Failed to get DLL Characteristics for {0} ({1}:{2})", Path, ex.GetType(), ex.Message); } } - catch (Exception e) when ( - e is IndexOutOfRangeException - || e is ArgumentNullException - || e is System.IO.IOException - || e is ArgumentException - || e is NullReferenceException) - { - Log.Debug("Failed to get DLL Characteristics for {0} ({1}:{2})", Path, e.GetType(), e.Message); - } catch (Exception e) { Log.Debug(e, "Failed to get DLL Characteristics for {0} ({1}:{2})", Path, e.GetType(), e.Message); @@ -185,18 +176,18 @@ private static List CharacteristicsTypeToListOfCharacteristi } } // This can happen if the file is readable but not writable as MMFile tries to open the file with write privileges. - // When we fail we can retry by reading the file to a Stream first, which cen be less performant but works with just Read access - // See #684 - catch (UnauthorizedAccessException) + // When we fail we can retry by reading the file to a Stream first, which can be less performant but works with just Read access + // See #684 + #696 + catch (Exception e) when (e is UnauthorizedAccessException or IOException) { try { using var stream = File.OpenRead(Path); return GetSignatureStatus(Path, stream); } - catch (Exception e) + catch (Exception ex) { - Log.Debug(e, "Failed to get signature for {0} ({1}:{2})", Path, e.GetType(), e.Message); + Log.Debug(ex, "Failed to get signature for {0} ({1}:{2})", Path, ex.GetType(), ex.Message); } } catch (Exception e) @@ -206,6 +197,11 @@ private static List CharacteristicsTypeToListOfCharacteristi return null; } + /// + /// Try to determine if the file is locally present to avoid triggering a downloading files that are cloud stubs + /// + /// Path to check + /// True when the file appears to be local based on its attributes public static bool IsLocal(string path) { NativeMethods.WIN32_FILE_ATTRIBUTE_DATA fileData;