using System; using System.Diagnostics; using System.IO; using Pithos.Interfaces; using Pithos.Network; namespace Pithos.Core.Agents { [DebuggerDisplay("{FilePath} C:{C} L:{L} S:{S}")] public class StateTuple { public string FilePath { get; private set; } public string MD5 { get { Debug.Assert(EmptyOrLength(_md5, 32)); return _md5; } set { if (!EmptyOrLength(value, 32)) throw new ArgumentException("A proper MD5 hash value should have 32 characters"); _md5 = value; } } private bool EmptyOrLength(string value, int length) { return String.IsNullOrWhiteSpace(value) || value.Length == length; } public string L { get { //For the change to Merkle Hash, we need to return the final Merkle Hash var hash = FileState.NullSafe(f => f.Checksum); //var hash = FileState.NullSafe(f => f.ETag); //Ensure the hash size matches SHA256 Debug.Assert(EmptyOrLength(hash, 64)); return String.IsNullOrWhiteSpace(hash) ? null : hash; } } private string _c; public string C { get { Debug.Assert(EmptyOrLength(_c, 64)); return _c; } set { if (!EmptyOrLength(value, 64)) throw new ArgumentException("A proper hash value should have 64 characters"); _c = String.IsNullOrWhiteSpace(value) ? null : value; } } public string S { get { //The Server hash will be the X-Object-Hash, not the ETag var hash= ObjectInfo.NullSafe(o => o.X_Object_Hash); Debug.Assert(EmptyOrLength(hash, 64)); return String.IsNullOrWhiteSpace(hash) ? null : hash; } } private FileSystemInfo _fileInfo; private TreeHash _merkle; private string _md5; public FileSystemInfo FileInfo { get { return _fileInfo; } set { _fileInfo = value; FilePath = value.FullName; } } public FileState FileState { get; set; } public ObjectInfo ObjectInfo{ get; set; } public TreeHash Merkle { get { return _merkle; } set { _merkle = value; C = _merkle.TopHash.ToHashString(); } } public bool Locked { get; set; } public string NewFullPath { get; set; } public string OldMD5 { get; set; } public string OldFullPath { get; set; } public StateTuple() { } public StateTuple(FileSystemInfo info) { FileInfo = info; } public bool HashesValid() { return (EmptyOrLength(C, 64) && EmptyOrLength(L, 64) && EmptyOrLength(S, 64) ); } } }