4 using System.Collections.Generic;
6 using System.Threading;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Diagnostics;
9 using SiliconStudio.Core.Storage;
11 namespace SiliconStudio.BuildEngine
18 private const string DefaultFileVersionTrackerFile =
@"Silicon Studio Corp\FileVersionTracker.cache";
20 private readonly Dictionary<FileVersionKey, object> locks;
21 private static readonly
Logger log = GlobalLogger.GetLogger(
"FileVersionTracker");
22 private static readonly
object lockDefaultTracker =
new object();
32 locks =
new Dictionary<FileVersionKey, object>();
41 lock (lockDefaultTracker)
43 if (defaultFileVersionTracker == null)
45 var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), DefaultFileVersionTrackerFile);
46 var directory = Path.GetDirectoryName(filePath);
47 if (directory != null && !Directory.Exists(directory))
49 Directory.CreateDirectory(directory);
53 defaultFileVersionTracker = Load(filePath);
56 return defaultFileVersionTracker;
67 FileVersionStorage.Compact(filePath);
69 bool isFirstPass =
true;
72 FileStream fileStream = null;
76 const int RetryOpenFileStream = 20;
77 var random =
new Random();
78 for (
int i = 0; i < RetryOpenFileStream; i++)
82 fileStream =
new FileStream(filePath,
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
87 if ((i + 1) == RetryOpenFileStream)
90 Thread.Sleep(50 + random.Next(100));
97 tracker.storage.LoadNewValues();
104 fileStream.SetLength(0);
118 storage.LoadNewValues();
125 if (!locks.TryGetValue(inputVersionKey, out versionLock))
127 versionLock =
new object();
128 locks.Add(inputVersionKey, versionLock);
132 var hash = ObjectId.Empty;
135 if (!storage.TryGetValue(inputVersionKey, out hash))
139 using (var fileStream = File.OpenRead(filePath))
142 fileStream.CopyTo(stream);
143 hash = stream.CurrentHash;
148 log.Debug(
"Cannot calculate hash for file [{0}]", ex, filePath);
150 storage[inputVersionKey] = hash;
174 if (path == null)
throw new ArgumentNullException(
"path");
176 LastModifiedDate = DateTime.MinValue;
179 if (
File.Exists(path))
181 LastModifiedDate = File.GetLastWriteTime(path);
182 FileSize =
new FileInfo(path).Length;
188 return string.Equals(Path, other.Path) && LastModifiedDate.Equals(other.
LastModifiedDate) && FileSize == other.FileSize;
193 if (ReferenceEquals(null, obj))
return false;
201 int hashCode = (Path != null ? Path.GetHashCode() : 0);
202 hashCode = (hashCode * 397) ^ LastModifiedDate.GetHashCode();
203 hashCode = (hashCode * 397) ^ FileSize.GetHashCode();
210 return left.Equals(right);
215 return !left.Equals(right);
Storage used for FileVersionKey associated with an ObjectId.
override bool Equals(object obj)
static FileVersionTracker GetDefault()
Gets the default file version tracker for this machine.
DateTime LastModifiedDate
System.IO.FileMode FileMode
FileVersionTracker(Stream stream)
Initializes a new instance of the FileVersionTracker class.
override int GetHashCode()
Base implementation for ILogger.
bool Equals(FileVersionKey other)
FileVersionKey(string path)
ObjectId ComputeFileHash(string filePath)
static FileVersionTracker Load(string filePath)
Loads previous versions stored from the specified file path.
A hash to uniquely identify data.