From 711811f13dc8aea5c3e795c2442cd21c6c3b4708 Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Thu, 20 Jun 2013 21:01:03 +0100 Subject: [PATCH] fetcher git: Catch stream errors when process has been killed by user. Fixes #1187 --- SparkleLib/Git/SparkleFetcherGit.cs | 77 +++++++++++++++-------------- SparkleLib/SparkleFetcherBase.cs | 12 +++-- 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/SparkleLib/Git/SparkleFetcherGit.cs b/SparkleLib/Git/SparkleFetcherGit.cs index fc36646c..d5982fb0 100755 --- a/SparkleLib/Git/SparkleFetcherGit.cs +++ b/SparkleLib/Git/SparkleFetcherGit.cs @@ -128,49 +128,55 @@ namespace SparkleLib.Git { DateTime last_change = DateTime.Now; TimeSpan change_interval = new TimeSpan (0, 0, 0, 1); - while (!this.git.StandardError.EndOfStream) { - string line = this.git.StandardError.ReadLine (); - Match match = progress_regex.Match (line); - - double number = 0.0; - if (match.Success) { - number = double.Parse (match.Groups [1].Value, new CultureInfo ("en-US")); + try { + while (!this.git.StandardError.EndOfStream) { + string line = this.git.StandardError.ReadLine (); + Match match = progress_regex.Match (line); - // The cloning progress consists of two stages: the "Compressing - // objects" stage which we count as 20% of the total progress, and - // the "Receiving objects" stage which we count as the last 80% - if (line.Contains ("|")) - number = (number / 100 * 80 + 20); // "Receiving objects" stage - else - number = (number / 100 * 20); // "Compressing objects" stage + double number = 0.0; + if (match.Success) { + number = double.Parse (match.Groups [1].Value, new CultureInfo ("en-US")); + + // The cloning progress consists of two stages: the "Compressing + // objects" stage which we count as 20% of the total progress, and + // the "Receiving objects" stage which we count as the last 80% + if (line.Contains ("|")) + number = (number / 100 * 80 + 20); // "Receiving objects" stage + else + number = (number / 100 * 20); // "Compressing objects" stage - } else { - SparkleLogger.LogInfo ("Fetcher", line); - line = line.Trim (new char [] {' ', '@'}); + } else { + SparkleLogger.LogInfo ("Fetcher", line); + line = line.Trim (new char [] {' ', '@'}); - if (line.StartsWith ("fatal:", StringComparison.InvariantCultureIgnoreCase) || - line.StartsWith ("error:", StringComparison.InvariantCultureIgnoreCase)) { + if (line.StartsWith ("fatal:", StringComparison.InvariantCultureIgnoreCase) || + line.StartsWith ("error:", StringComparison.InvariantCultureIgnoreCase)) { - base.errors.Add (line); + base.errors.Add (line); - } else if (line.StartsWith ("WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!")) { - base.errors.Add ("warning: Remote host identification has changed!"); + } else if (line.StartsWith ("WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!")) { + base.errors.Add ("warning: Remote host identification has changed!"); - } else if (line.StartsWith ("WARNING: POSSIBLE DNS SPOOFING DETECTED!")) { - base.errors.Add ("warning: Possible DNS spoofing detected!"); + } else if (line.StartsWith ("WARNING: POSSIBLE DNS SPOOFING DETECTED!")) { + base.errors.Add ("warning: Possible DNS spoofing detected!"); + } + } + + if (number >= percentage) { + percentage = number; + + if (DateTime.Compare (last_change, DateTime.Now.Subtract (change_interval)) < 0) { + base.OnProgressChanged (percentage); + last_change = DateTime.Now; + } } } - - if (number >= percentage) { - percentage = number; - - if (DateTime.Compare (last_change, DateTime.Now.Subtract (change_interval)) < 0) { - base.OnProgressChanged (percentage); - last_change = DateTime.Now; - } - } - } + } catch (Exception e) { + IsActive = false; + return false; + } + this.git.WaitForExit (); if (this.git.ExitCode == 0) { @@ -277,8 +283,7 @@ namespace SparkleLib.Git { public override void Stop () { try { - if (this.git != null) { - this.git.Close (); + if (this.git != null && !this.git.HasExited) { this.git.Kill (); this.git.Dispose (); } diff --git a/SparkleLib/SparkleFetcherBase.cs b/SparkleLib/SparkleFetcherBase.cs index 6c575997..e211ff89 100755 --- a/SparkleLib/SparkleFetcherBase.cs +++ b/SparkleLib/SparkleFetcherBase.cs @@ -56,7 +56,7 @@ namespace SparkleLib { public string RequiredFingerprint { get; protected set; } public readonly bool FetchPriorHistory = false; public string TargetFolder { get; protected set; } - public bool IsActive { get; private set; } + public bool IsActive { get; protected set; } public string Identifier; public SparkleFetcherInfo OriginalFetcherInfo; @@ -150,10 +150,16 @@ namespace SparkleLib { } else { Thread.Sleep (500); - SparkleLogger.LogInfo ("Fetcher", "Failed"); + + if (IsActive) { + SparkleLogger.LogInfo ("Fetcher", "Failed"); + Failed (); + + } else { + SparkleLogger.LogInfo ("Fetcher", "Failed: cancelled by user"); + } IsActive = false; - Failed (); } });