From 24d8cc5b5c7d1477a3e75d2196a8b4c0e96d46fc Mon Sep 17 00:00:00 2001 From: David Moore Date: Sat, 31 Aug 2019 15:58:01 +1200 Subject: [PATCH] When detecting installed torrent clients, more gracefully handle empty paths, and make the logging a little less noisy when normalizing paths. --- Code/IPFilter/Apps/BitTorrentApplication.cs | 7 ++++--- Code/IPFilter/Apps/EmuleApplication.cs | 2 +- Code/IPFilter/Apps/QBitTorrent.cs | 2 +- Code/IPFilter/Apps/UTorrentApplication.cs | 4 ++-- Code/IPFilter/Native/PathHelper.cs | 16 ++++++---------- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/Code/IPFilter/Apps/BitTorrentApplication.cs b/Code/IPFilter/Apps/BitTorrentApplication.cs index d1b3e64..708101e 100644 --- a/Code/IPFilter/Apps/BitTorrentApplication.cs +++ b/Code/IPFilter/Apps/BitTorrentApplication.cs @@ -33,15 +33,16 @@ public Task DetectAsync() using (key) { var installLocation = (string)key.GetValue("InstallLocation"); - if (installLocation == null) return Task.FromResult(ApplicationDetectionResult.NotFound()); + if (string.IsNullOrWhiteSpace(installLocation)) return Task.FromResult(ApplicationDetectionResult.NotFound()); var displayName = (string)key.GetValue("DisplayName") ?? DefaultDisplayName; var version = (string)key.GetValue("DisplayVersion") ?? "Unknown"; var result = ApplicationDetectionResult.Create(this, displayName, installLocation); - result.Version = version; - if (!result.InstallLocation.Exists) result.IsPresent = false; + if (result.InstallLocation == null || !result.InstallLocation.Exists) return Task.FromResult(ApplicationDetectionResult.NotFound());; + + result.Version = version; return Task.FromResult(result); } diff --git a/Code/IPFilter/Apps/EmuleApplication.cs b/Code/IPFilter/Apps/EmuleApplication.cs index 7d0d9ad..7675545 100644 --- a/Code/IPFilter/Apps/EmuleApplication.cs +++ b/Code/IPFilter/Apps/EmuleApplication.cs @@ -24,7 +24,7 @@ public Task DetectAsync() if (key == null) return Task.FromResult(ApplicationDetectionResult.NotFound()); var installLocation = (string)key.GetValue("Install Path"); - if (installLocation == null) return Task.FromResult(ApplicationDetectionResult.NotFound()); + if (string.IsNullOrWhiteSpace(installLocation)) return Task.FromResult(ApplicationDetectionResult.NotFound()); var result = ApplicationDetectionResult.Create(this, DefaultDisplayName, installLocation); diff --git a/Code/IPFilter/Apps/QBitTorrent.cs b/Code/IPFilter/Apps/QBitTorrent.cs index ae38c1d..3a5be3a 100644 --- a/Code/IPFilter/Apps/QBitTorrent.cs +++ b/Code/IPFilter/Apps/QBitTorrent.cs @@ -33,7 +33,7 @@ public Task DetectAsync() var result = ApplicationDetectionResult.Create(this, "qBittorrent", installLocation); - if (!result.InstallLocation.Exists) return Task.FromResult(ApplicationDetectionResult.NotFound()); + if (result.InstallLocation == null || !result.InstallLocation.Exists) return Task.FromResult(ApplicationDetectionResult.NotFound()); var applicationPath = Path.Combine(result.InstallLocation.FullName, "qbittorrent.exe"); if (!File.Exists(applicationPath)) return Task.FromResult(ApplicationDetectionResult.NotFound()); diff --git a/Code/IPFilter/Apps/UTorrentApplication.cs b/Code/IPFilter/Apps/UTorrentApplication.cs index 519fb92..8d89a7b 100644 --- a/Code/IPFilter/Apps/UTorrentApplication.cs +++ b/Code/IPFilter/Apps/UTorrentApplication.cs @@ -2,7 +2,7 @@ namespace IPFilter.Apps { class UTorrentApplication : BitTorrentApplication { - protected override string DefaultDisplayName { get { return "µTorrent"; } } - protected override string FolderName { get { return "uTorrent"; } } + protected override string DefaultDisplayName => "µTorrent"; + protected override string FolderName => "uTorrent"; } } \ No newline at end of file diff --git a/Code/IPFilter/Native/PathHelper.cs b/Code/IPFilter/Native/PathHelper.cs index 12cf426..67f9161 100644 --- a/Code/IPFilter/Native/PathHelper.cs +++ b/Code/IPFilter/Native/PathHelper.cs @@ -16,9 +16,7 @@ public static DirectoryInfo GetDirectoryInfo(string path) public static DirectoryInfo GetDirectoryInfo([NotNull] string path, CultureInfo cultureInfo) { - Trace.WriteLine($"Parsing path: '{path}'"); - - if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Path is null or empty.", nameof(path)); + if (string.IsNullOrWhiteSpace(path)) return null; try { @@ -30,20 +28,18 @@ public static DirectoryInfo GetDirectoryInfo([NotNull] string path, CultureInfo // the yen character will actually get corrected to the backslash. // Get the culture's codepage - Trace.TraceInformation("Culture: " + cultureInfo); - Trace.TraceInformation("Codepage: " + cultureInfo.TextInfo.ANSICodePage); var encoding = Encoding.GetEncoding(cultureInfo.TextInfo.ANSICodePage); var normalizedPath = encoding.GetString(encoding.GetBytes(path)); - - Trace.WriteLine("Normalized path: " + path); - + return new DirectoryInfo(normalizedPath); } catch (Exception ex) { - Trace.TraceWarning("Couldn't normalize the path: " + ex); - Trace.TraceWarning("Falling back to just using the un-normalized path that was passed in as '" + path + "'"); + Trace.TraceWarning($"Couldn't normalize the path '{path}'. Culture: {cultureInfo}, CodePage: {cultureInfo.TextInfo.ANSICodePage}"); + Trace.TraceWarning("Error: " + ex); + Trace.TraceInformation($"Falling back to un-normalized path of '{path}'"); + return new DirectoryInfo(path); } }