From f308216bbcee55b860bd8087b54cc0591c7ef5e0 Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Sat, 10 Mar 2018 19:09:48 +0000 Subject: [PATCH 1/3] Always collect exceptions instead of swallowing them --- SparkleShare/Common/AboutController.cs | 3 ++- SparkleShare/Common/SetupController.cs | 4 ++-- SparkleShare/Linux/UserInterfaceHelpers.cs | 5 ++++- Sparkles/Git/GitRepository.cs | 17 +++++++++-------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/SparkleShare/Common/AboutController.cs b/SparkleShare/Common/AboutController.cs index 9f7436e4..134b1e62 100644 --- a/SparkleShare/Common/AboutController.cs +++ b/SparkleShare/Common/AboutController.cs @@ -73,7 +73,8 @@ namespace SparkleShare { else UpdateLabelEvent ("✓ You are running the latest version"); - } catch { + } catch (Exception e) { + Logger.LogInfo ("UI", "Failed to download " + uri , e); UpdateLabelEvent ("Couldn’t check for updates\t"); } } diff --git a/SparkleShare/Common/SetupController.cs b/SparkleShare/Common/SetupController.cs index 19b950ea..3e34173a 100644 --- a/SparkleShare/Common/SetupController.cs +++ b/SparkleShare/Common/SetupController.cs @@ -346,8 +346,8 @@ namespace SparkleShare { Logger.LogInfo ("Controller", "Added preset for " + uri.Host); } - } catch { - Logger.LogInfo ("Controller", "Failed adding preset for " + uri.Host); + } catch (Exception e) { + Logger.LogInfo ("Controller", "Failed adding preset for " + uri.Host, e); } } diff --git a/SparkleShare/Linux/UserInterfaceHelpers.cs b/SparkleShare/Linux/UserInterfaceHelpers.cs index 648bfbd4..34d760df 100755 --- a/SparkleShare/Linux/UserInterfaceHelpers.cs +++ b/SparkleShare/Linux/UserInterfaceHelpers.cs @@ -19,6 +19,7 @@ using System; using System.IO; using Gtk; +using Sparkles; namespace SparkleShare { @@ -35,7 +36,9 @@ namespace SparkleShare { try { return icon_theme.LoadIcon (name, size, IconLookupFlags.GenericFallback); - } catch { + } catch (Exception e) { + Logger.LogInfo ("UI", "Failed to load icon " + name + " of size " + size, e); + try { return icon_theme.LoadIcon ("gtk-missing-image", size, IconLookupFlags.GenericFallback); diff --git a/Sparkles/Git/GitRepository.cs b/Sparkles/Git/GitRepository.cs index 560a93e3..91f90f71 100644 --- a/Sparkles/Git/GitRepository.cs +++ b/Sparkles/Git/GitRepository.cs @@ -102,7 +102,8 @@ namespace Sparkles.Git { string size = File.ReadAllText (file_path); return double.Parse (size); - } catch { + } catch (Exception e) { + Logger.LogInfo ("Git", Name + " | Failed to parse " + file_path, e); return 0; } } @@ -117,7 +118,8 @@ namespace Sparkles.Git { string size = File.ReadAllText (file_path); return double.Parse (size); - } catch { + } catch (Exception e) { + Logger.LogInfo ("Git", Name + " | Failed to parse " + file_path, e); return 0; } } @@ -638,16 +640,15 @@ namespace Sparkles.Git { try { File.Move (local_file_path, target_file_path); - } catch { - Logger.LogInfo ("Git", - Name + " | Could not move \"" + local_file_path + "\" to \"" + target_file_path + "\""); + } catch (Exception e) { + string message = string.Format ("Failed to move \"{0}\" to \"{1}\"", local_file_path, target_file_path); + Logger.LogInfo ("Git", Name + " | " + message, e); } // ...and restore the most recent revision git = new GitCommand (LocalPath, "checkout " + CurrentRevision + " \"" + path + "\""); git.StartAndWaitForExit (); - if (target_file_path.StartsWith (LocalPath)) new Thread (() => OnFileActivity (null)).Start (); } @@ -966,8 +967,8 @@ namespace Sparkles.Git { File.WriteAllText (Path.Combine (path, ".empty"), "I'm a folder!"); File.SetAttributes (Path.Combine (path, ".empty"), FileAttributes.Hidden); - } catch { - Logger.LogInfo ("Git", Name + " | Failed adding empty folder " + path); + } catch (Exception e) { + Logger.LogInfo ("Git", Name + " | Failed adding empty folder " + path, e); } } } From 62bcad5563a36fe9ff73839c14f692839815e0d4 Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Wed, 14 Mar 2018 16:10:52 +0000 Subject: [PATCH 2/3] logger: Don't crash when not able to write to log --- Sparkles/Logger.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sparkles/Logger.cs b/Sparkles/Logger.cs index f6a4c896..80c70779 100644 --- a/Sparkles/Logger.cs +++ b/Sparkles/Logger.cs @@ -49,8 +49,14 @@ namespace Sparkles { Console.WriteLine (line); lock (log_writer_lock) { - log_writer.WriteLine (line); - log_writer.Flush (); + try { + log_writer.WriteLine (line); + log_writer.Flush (); + + } catch (Exception e) { + Console.WriteLine (string.Format ("Could not write to log {0}: {1} {2}", + (log_writer.BaseStream as FileStream).Name, e.Message, e.StackTrace)); + } } } From 1369ac07b45b8a6949fad6eea53b6998f68d4a47 Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Wed, 14 Mar 2018 16:11:49 +0000 Subject: [PATCH 3/3] More accurate/pretty OS detection --- SparkleShare/Common/BaseController.cs | 6 +--- SparkleShare/Common/SparkleShare.cs | 2 +- SparkleShare/Linux/meson.build | 3 +- Sparkles/Configuration.cs | 2 +- Sparkles/Git/GitFetcher.cs | 2 +- Sparkles/Git/GitRepository.cs | 2 +- Sparkles/InstallationInfo.cs | 48 +++++++++++++++------------ 7 files changed, 34 insertions(+), 31 deletions(-) diff --git a/SparkleShare/Common/BaseController.cs b/SparkleShare/Common/BaseController.cs index ec4d48c2..1afdb6cf 100644 --- a/SparkleShare/Common/BaseController.cs +++ b/SparkleShare/Common/BaseController.cs @@ -214,11 +214,7 @@ namespace SparkleShare { Logger.LogInfo ("Environment", "SparkleShare " + version); Logger.LogInfo ("Environment", "Git LFS " + Sparkles.Git.GitCommand.GitLFSVersion); Logger.LogInfo ("Environment", "Git " + Sparkles.Git.GitCommand.GitVersion); - - if (InstallationInfo.OperatingSystem == OS.Mac) - Logger.LogInfo ("Environment", InstallationInfo.MacOSVersion ()); - else - Logger.LogInfo ("Environment", InstallationInfo.OperatingSystem + " (" + Environment.OSVersion + ")"); + Logger.LogInfo ("Environment", InstallationInfo.OperatingSystem + " " + InstallationInfo.OperatingSystemVersion); UserAuthenticationInfo = new SSHAuthenticationInfo (); SSHAuthenticationInfo.DefaultAuthenticationInfo = UserAuthenticationInfo; diff --git a/SparkleShare/Common/SparkleShare.cs b/SparkleShare/Common/SparkleShare.cs index 73f0c2fc..5c7cce51 100644 --- a/SparkleShare/Common/SparkleShare.cs +++ b/SparkleShare/Common/SparkleShare.cs @@ -36,7 +36,7 @@ namespace SparkleShare { public static void Main (string [] args) { if (args.Length != 0 && (args [0].Equals ("help") || args [0].Equals ("version")) && - InstallationInfo.OperatingSystem != OS.Mac && + InstallationInfo.OperatingSystem != OS.macOS && InstallationInfo.OperatingSystem != OS.Windows) { string n = Environment.NewLine; diff --git a/SparkleShare/Linux/meson.build b/SparkleShare/Linux/meson.build index 54fcd4bd..a45f0c93 100644 --- a/SparkleShare/Linux/meson.build +++ b/SparkleShare/Linux/meson.build @@ -25,6 +25,7 @@ configure_file( output: 'sparkleshare', configuration: configuration, install_dir: get_option('bindir')) + # .desktop and .appdata files apps_dir = join_paths(get_option('prefix'), 'share', 'applications') install_data(sources: 'org.sparkleshare.SparkleShare.desktop', install_dir: apps_dir) @@ -56,6 +57,6 @@ sparkleshare = executable('SparkleShare', install: true, install_dir: install_dir) + subdir('Images') subdir('Images/icons') - diff --git a/Sparkles/Configuration.cs b/Sparkles/Configuration.cs index e0b46bae..c1e31edd 100644 --- a/Sparkles/Configuration.cs +++ b/Sparkles/Configuration.cs @@ -28,7 +28,7 @@ namespace Sparkles { private static Lazy ConfigLazy = new Lazy (() => { string app_data_path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData); - if (InstallationInfo.OperatingSystem != OS.Windows && InstallationInfo.OperatingSystem != OS.Mac) + if (InstallationInfo.OperatingSystem != OS.Windows && InstallationInfo.OperatingSystem != OS.macOS) app_data_path = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), ".config"); string config_path = Path.Combine (app_data_path, "org.sparkleshare.SparkleShare"); diff --git a/Sparkles/Git/GitFetcher.cs b/Sparkles/Git/GitFetcher.cs index a737763f..45809941 100644 --- a/Sparkles/Git/GitFetcher.cs +++ b/Sparkles/Git/GitFetcher.cs @@ -420,7 +420,7 @@ namespace Sparkles.Git { string smudge_command; string clean_command; - if (InstallationInfo.OperatingSystem == OS.Mac || InstallationInfo.OperatingSystem == OS.Windows) { + if (InstallationInfo.OperatingSystem == OS.macOS || InstallationInfo.OperatingSystem == OS.Windows) { smudge_command = "env GIT_SSH_COMMAND='" + GIT_SSH_COMMAND + "' " + Path.Combine (Configuration.DefaultConfiguration.BinPath, "git-lfs").Replace ("\\", "/") + " smudge %f"; diff --git a/Sparkles/Git/GitRepository.cs b/Sparkles/Git/GitRepository.cs index 91f90f71..5c024a52 100644 --- a/Sparkles/Git/GitRepository.cs +++ b/Sparkles/Git/GitRepository.cs @@ -209,7 +209,7 @@ namespace Sparkles.Git { string pre_push_hook_content; // The pre-push hook may have been changed by Git LFS, overwrite it to use our own configuration - if (InstallationInfo.OperatingSystem == OS.Mac || InstallationInfo.OperatingSystem == OS.Windows) { + if (InstallationInfo.OperatingSystem == OS.macOS || InstallationInfo.OperatingSystem == OS.Windows) { pre_push_hook_content = "#!/bin/sh" + Environment.NewLine + "env GIT_SSH_COMMAND='" + GitCommand.FormatGitSSHCommand (auth_info) + "' " + diff --git a/Sparkles/InstallationInfo.cs b/Sparkles/InstallationInfo.cs index 73679131..db8b0ce0 100644 --- a/Sparkles/InstallationInfo.cs +++ b/Sparkles/InstallationInfo.cs @@ -22,7 +22,7 @@ namespace Sparkles { public enum OS { Unknown, - Mac, + macOS, Windows, Ubuntu, GNOME @@ -49,7 +49,7 @@ namespace Sparkles { // Environment.OSVersion.Platform.PlatformID.MacOSX is broken in Mono // for historical reasons, so check manually if (output.StartsWith ("Darwin", StringComparison.InvariantCulture)) { - operating_system = OS.Mac; + operating_system = OS.macOS; } else if (output.Contains ("Ubuntu")) { operating_system = OS.Ubuntu; @@ -63,29 +63,35 @@ namespace Sparkles { } - public static string MacOSVersion () - { - var uname = new Command ("sw_vers", "-productVersion", false); - string output = uname.StartAndReadStandardOutput (); - string version = output; + public static string OperatingSystemVersion { + get { + if (OperatingSystem == OS.macOS) { + var uname = new Command ("sw_vers", "-productVersion", false); + string output = uname.StartAndReadStandardOutput (); + string version = output; - // Parse the version number between the periods (e.g. "10.12.1" -> 12) - output = output.Substring (output.IndexOf (".") + 1); - output = output.Substring (0, output.LastIndexOf (".")); + // Parse the version number between the periods (e.g. "10.12.1" -> 12) + output = output.Substring (output.IndexOf (".") + 1); + output = output.Substring (0, output.LastIndexOf (".")); - string release = "Unreleased Version"; + string release = "Unreleased Version"; - switch (int.Parse (output)) { - case 7: release = "Lion"; break; - case 8: release = "Mountain Lion"; break; - case 9: release = "Mavericks"; break; - case 10: release = "Yosemite"; break; - case 11: release = "El Capitan"; break; - case 12: release = "Sierra"; break; - case 13: release = "High Sierra"; break; + switch (int.Parse (output)) { + case 7: release = "Lion"; break; + case 8: release = "Mountain Lion"; break; + case 9: release = "Mavericks"; break; + case 10: release = "Yosemite"; break; + case 11: release = "El Capitan"; break; + case 12: release = "Sierra"; break; + case 13: release = "High Sierra"; break; + } + + return string.Format ("{0} ({1})", version, release); + } + + string os_version = Environment.OSVersion.ToString (); + return string.Format ("({0})", os_version.Replace ("Unix", "Linux")); } - - return string.Format ("macOS {0} ({1})", version, release); }