From 3903e01076dbaa955ef60c11b5e1d308d6cbe0b1 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 11 May 2016 13:00:20 +0200 Subject: [PATCH] [mmp/mtouch] Add a Driver.WriteIfDifferent overload that takes a byte array. --- tools/common/Driver.cs | 66 +++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/tools/common/Driver.cs b/tools/common/Driver.cs index f0a0ba554e..f76b024c5f 100644 --- a/tools/common/Driver.cs +++ b/tools/common/Driver.cs @@ -196,6 +196,30 @@ namespace Xamarin.Bundler { File.Move (source, target); } + static void MoveIfDifferent (string path, string tmp) + { + // Don't read the entire file into memory, it can be quite big in certain cases. + + bool move = false; + + using (var fs1 = new FileStream (path, FileMode.Open, FileAccess.Read)) { + using (var fs2 = new FileStream (tmp, FileMode.Open, FileAccess.Read)) { + if (fs1.Length != fs2.Length) { + Log (3, "New file '{0}' has different length, writing new file.", path); + move = true; + } else { + move = !Cache.CompareStreams (fs1, fs2); + } + } + } + + if (move) { + FileMove (tmp, path); + } else { + Log (3, "Target {0} is up-to-date.", path); + } + } + public static void WriteIfDifferent (string path, string contents) { var tmp = path + ".tmp"; @@ -207,27 +231,8 @@ namespace Xamarin.Bundler { return; } - // Don't read the entire file into memory, it can be quite big in certain cases. - - bool move = false; File.WriteAllText (tmp, contents); - - using (var fs1 = new FileStream (path, FileMode.Open, FileAccess.Read)) { - using (var fs2 = new FileStream (tmp, FileMode.Open, FileAccess.Read)) { - if (fs1.Length != fs2.Length) { - Log (3, "New file '{0}' has different length, writing new file.", path); - move = true; - } else { - move = !Cache.CompareStreams (fs1, fs2); - } - } - } - - if (move) { - FileMove (tmp, path); - } else { - Log (3, "Target {0} is up-to-date.", path); - } + MoveIfDifferent (path, tmp); } catch (Exception e) { File.WriteAllText (path, contents); ErrorHelper.Warning (1014, e, "Failed to re-use cached version of '{0}': {1}.", path, e.Message); @@ -235,6 +240,27 @@ namespace Xamarin.Bundler { Application.TryDelete (tmp); } } + + public static void WriteIfDifferent (string path, byte[] contents) + { + var tmp = path + ".tmp"; + + try { + if (!File.Exists (path)) { + File.WriteAllBytes (path, contents); + Log (3, "File '{0}' does not exist, creating it.", path); + return; + } + + File.WriteAllBytes (tmp, contents); + MoveIfDifferent (path, tmp); + } catch (Exception e) { + File.WriteAllBytes (path, contents); + ErrorHelper.Warning (1014, e, "Failed to re-use cached version of '{0}': {1}.", path, e.Message); + } finally { + Application.TryDelete (tmp); + } + } #endif