0.2.0-dev Download ZIP
43 lines ·1.3 KB Raw
| 1 | using EvilEngine.Mounts; |
| 2 | |
| 3 | namespace EvilEngine.Conversion; |
| 4 | |
| 5 | public sealed class FileStamp |
| 6 | { |
| 7 | private readonly Lazy<string> _hash; |
| 8 | private volatile bool _forgotten; |
| 9 | |
| 10 | internal FileStamp(IFileSource files, VirtualPath path, long length) |
| 11 | { |
| 12 | Path = path; |
| 13 | Length = length; |
| 14 | _hash = new Lazy<string>(() => |
| 15 | { |
| 16 | if (_forgotten) |
| 17 | throw new InvalidOperationException( |
| 18 | $"'{path}' changed in its namespace before anything asked for the hash of the copy that was read."); |
| 19 | if (!files.TryOpenAccess(path, out var access)) |
| 20 | throw new FileNotFoundException($"'{path}' left its namespace before it was hashed.", path.Value); |
| 21 | using (access) |
| 22 | { |
| 23 | if (access.Length != length) |
| 24 | throw new InvalidOperationException( |
| 25 | $"'{path}' is {access.Length} bytes, not the {length} it was read at, so its hash isn't the one read."); |
| 26 | return ConversionContext.Hash(access); |
| 27 | } |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | public VirtualPath Path { get; } |
| 32 | |
| 33 | public long Length { get; } |
| 34 | |
| 35 | public bool Hashed => _hash.IsValueCreated; |
| 36 | |
| 37 | public string Hash => _hash.Value; |
| 38 | |
| 39 | internal void Forget() |
| 40 | { |
| 41 | _forgotten = true; |
| 42 | } |
| 43 | } |