1- using MatthiWare . UpdateLib . Utils ;
1+ using MatthiWare . UpdateLib . Security ;
2+ using MatthiWare . UpdateLib . Utils ;
23using System ;
34using System . Collections . Generic ;
45using System . IO ;
56using System . Reflection ;
67using System . Xml . Serialization ;
8+ using System . Linq ;
79
810namespace MatthiWare . UpdateLib . Files
911{
@@ -17,29 +19,53 @@ public class HashCacheFile
1719 [ XmlArrayItem ( "Entry" ) ]
1820 public List < HashCacheEntry > Items { get ; set ; }
1921
20- private static Lazy < string > storagePath = new Lazy < string > ( GetStoragePath ) ;
22+ private readonly object sync = new object ( ) ;
2123
2224 public HashCacheFile ( )
2325 {
2426 Items = new List < HashCacheEntry > ( ) ;
2527 }
2628
29+ public void AddOrUpdateEntry ( string fullPath , string hash = "" )
30+ {
31+ lock ( sync )
32+ {
33+ long ticks = File . GetLastWriteTime ( fullPath ) . Ticks ;
34+ hash = string . IsNullOrEmpty ( hash ) ? HashUtil . GetHash ( fullPath ) : hash ;
35+
36+ HashCacheEntry entry = Items . FirstOrDefault ( f => f . FilePath == fullPath ) ;
37+
38+ if ( entry == null )
39+ {
40+ entry = new HashCacheEntry ( ) ;
41+ entry . FilePath = fullPath ;
42+ entry . Hash = hash ;
43+ entry . Ticks = ticks ;
44+
45+ Items . Add ( entry ) ;
46+ }
47+ else
48+ {
49+ entry . Ticks = ticks ;
50+ entry . Hash = hash ;
51+ }
52+ }
53+ }
54+
2755 #region Save/Load
2856 private static string GetStoragePath ( )
2957 {
30- string path = IOUtils . GetAppDataPath ( ) ;
31- string productName = Updater . ProductName ;
32- string name = Assembly . GetEntryAssembly ( ) . GetName ( ) . Name ;
58+ string path = IOUtils . AppDataPath ;
3359
34- return $@ "{ path } \{ name } \ { productName } \ { CACHE_FOLDER_NAME } \{ FILE_NAME } ";
60+ return $@ "{ path } \{ CACHE_FOLDER_NAME } \{ FILE_NAME } ";
3561 }
3662
3763 public static HashCacheFile Load ( )
3864 {
39- if ( ! File . Exists ( storagePath . Value ) )
65+ if ( ! File . Exists ( GetStoragePath ( ) ) )
4066 return null ;
4167
42- using ( Stream stream = File . Open ( storagePath . Value , FileMode . Open , FileAccess . Read ) )
68+ using ( Stream stream = File . Open ( GetStoragePath ( ) , FileMode . Open , FileAccess . Read ) )
4369 {
4470 XmlSerializer serializer = new XmlSerializer ( typeof ( HashCacheFile ) ) ;
4571 return ( HashCacheFile ) serializer . Deserialize ( stream ) ;
@@ -48,7 +74,7 @@ public static HashCacheFile Load()
4874
4975 public void Save ( )
5076 {
51- FileInfo fi = new FileInfo ( storagePath . Value ) ;
77+ FileInfo fi = new FileInfo ( GetStoragePath ( ) ) ;
5278
5379 if ( ! fi . Directory . Exists )
5480 fi . Directory . Create ( ) ;
0 commit comments