From e6ae88c016fed481746fc82fdfa2e5a33c3e2555 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 21 Nov 2013 14:45:52 +1100 Subject: [PATCH] Fixes hash generation and checking with the trees.config file - since this is changed during startup our plugin cache was never active :( --- src/Umbraco.Core/PluginManager.cs | 39 ++++++++++++++++++------- src/Umbraco.Tests/PluginManagerTests.cs | 8 ++--- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/Umbraco.Core/PluginManager.cs b/src/Umbraco.Core/PluginManager.cs index 485602f23f..84d45dc220 100644 --- a/src/Umbraco.Core/PluginManager.cs +++ b/src/Umbraco.Core/PluginManager.cs @@ -171,17 +171,18 @@ namespace Umbraco.Core if (_currentAssembliesHash != -1) return _currentAssembliesHash; - _currentAssembliesHash = GetAssembliesHash( - new FileSystemInfo[] + _currentAssembliesHash = GetFileHash( + new List> { //add the bin folder and everything in it - new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Bin)), + new Tuple(new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Bin)), false), //add the app code folder and everything in it - new DirectoryInfo(IOHelper.MapPath("~/App_Code")), + new Tuple(new DirectoryInfo(IOHelper.MapPath("~/App_Code")), false), //add the global.asax (the app domain also monitors this, if it changes will do a full restart) - new FileInfo(IOHelper.MapPath("~/global.asax")), - //add the trees.config - new FileInfo(IOHelper.MapPath(SystemDirectories.Config + "/trees.config")) + new Tuple(new FileInfo(IOHelper.MapPath("~/global.asax")), false), + + //add the trees.config - use the contents to create the has since this gets resaved on every app startup! + new Tuple(new FileInfo(IOHelper.MapPath(SystemDirectories.Config + "/trees.config")), true) } ); return _currentAssembliesHash; @@ -200,18 +201,34 @@ namespace Umbraco.Core /// /// Returns a unique hash for the combination of FileInfo objects passed in /// - /// + /// + /// A collection of files and whether or not to use their file contents to determine the hash or the file's properties + /// (true will make a hash based on it's contents) + /// /// - internal static long GetAssembliesHash(IEnumerable filesAndFolders) + internal static long GetFileHash(IEnumerable> filesAndFolders) { using (DisposableTimer.TraceDuration("Determining hash of code files on disk", "Hash determined")) { var hashCombiner = new HashCodeCombiner(); - //add each unique folder to the hash - foreach (var i in filesAndFolders.DistinctBy(x => x.FullName)) + + //get the file info's to check + var fileInfos = filesAndFolders.Where(x => x.Item2 == false).ToArray(); + var fileContents = filesAndFolders.Except(fileInfos); + + //add each unique folder/file to the hash + foreach (var i in fileInfos.Select(x => x.Item1).DistinctBy(x => x.FullName)) { hashCombiner.AddFileSystemItem(i); } + + //add each unique file's contents to the hash + foreach (var i in fileContents.Select(x => x.Item1).DistinctBy(x => x.FullName)) + { + var content = File.ReadAllText(i.FullName).Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty); + hashCombiner.AddCaseInsensitiveString(content); + } + return ConvertPluginsHashFromHex(hashCombiner.GetCombinedHashCode()); } } diff --git a/src/Umbraco.Tests/PluginManagerTests.cs b/src/Umbraco.Tests/PluginManagerTests.cs index 861b598c1e..a67ce74627 100644 --- a/src/Umbraco.Tests/PluginManagerTests.cs +++ b/src/Umbraco.Tests/PluginManagerTests.cs @@ -242,16 +242,16 @@ namespace Umbraco.Tests var list3 = new[] { f1, f3, f5, f7 }; //Act - var hash1 = PluginManager.GetAssembliesHash(list1); - var hash2 = PluginManager.GetAssembliesHash(list2); - var hash3 = PluginManager.GetAssembliesHash(list3); + var hash1 = PluginManager.GetFileHash(list1); + var hash2 = PluginManager.GetFileHash(list2); + var hash3 = PluginManager.GetFileHash(list3); //Assert Assert.AreNotEqual(hash1, hash2); Assert.AreNotEqual(hash1, hash3); Assert.AreNotEqual(hash2, hash3); - Assert.AreEqual(hash1, PluginManager.GetAssembliesHash(list1)); + Assert.AreEqual(hash1, PluginManager.GetFileHash(list1)); } [Test]