Modified merging code to detect local moves and create the appropriate tuples
[pithos-ms-client] / trunk / Pithos.Core / Agents / StateTuple.cs
1 using System;\r
2 using System.Diagnostics;\r
3 using System.IO;\r
4 using Pithos.Interfaces;\r
5 using Pithos.Network;\r
6 \r
7 namespace Pithos.Core.Agents\r
8 {\r
9     [DebuggerDisplay("{FilePath} C:{C} L:{L} S:{S}")]\r
10     public class StateTuple\r
11     {\r
12         public string FilePath { get; private set; }\r
13 \r
14         //public string MD5\r
15         //{\r
16         //    get\r
17         //    {\r
18         //        Debug.Assert(EmptyOrLength(_md5, 32));\r
19         //        return _md5;\r
20         //    }\r
21         //    set\r
22         //    {\r
23         //        if (!EmptyOrLength(value, 32))\r
24         //            throw new ArgumentException("A proper MD5 hash value should have 32 characters");\r
25         //        _md5 = value;\r
26         //    }\r
27         //}\r
28 \r
29         private bool EmptyOrLength(string value, int length)\r
30         {\r
31             return String.IsNullOrWhiteSpace(value) || value.Length == length;\r
32         }\r
33 \r
34         /// <summary>\r
35         /// Last encountered server hash, stored in the State\r
36         /// </summary>\r
37         public string L\r
38         {\r
39             get\r
40             {\r
41                 //For the change to Merkle Hash, we need to return the final Merkle Hash\r
42                 var hash = FileState.NullSafe(f => f.Checksum);\r
43 \r
44                // Debug.Assert(FileState==null || hash != null,"FileState without a checksum encountered");\r
45 \r
46                 //var hash = FileState.NullSafe(f => f.ETag);\r
47                 \r
48                 //Ensure the hash size matches SHA256\r
49                 //Debug.Assert(EmptyOrLength(hash, 64));\r
50                 \r
51                 return String.IsNullOrWhiteSpace(hash) ? null : hash;\r
52             }\r
53         }\r
54 \r
55         private string _c;\r
56         /// <summary>\r
57         /// Current local file Hash\r
58         /// </summary>\r
59         public string C\r
60         {\r
61             get\r
62             {\r
63                 Debug.Assert(EmptyOrLength(_c, 64));\r
64                 return _c;\r
65             }\r
66             set {\r
67                 if (!EmptyOrLength(value, 64))\r
68                     throw new ArgumentException("A proper hash value should have 64 characters");\r
69                 _c = String.IsNullOrWhiteSpace(value) ? null : value;\r
70             }\r
71         }\r
72 \r
73         /// <summary>\r
74         /// Server file hash\r
75         /// </summary>\r
76         public string S\r
77         {\r
78             get\r
79             {\r
80                 //The Server hash will be the X-Object-Hash, not the ETag\r
81                 var hash= ObjectInfo.NullSafe(o => o.X_Object_Hash);\r
82 \r
83                 Debug.Assert(EmptyOrLength(hash, 64));\r
84                 \r
85                 return String.IsNullOrWhiteSpace(hash) ? null : hash;\r
86             }\r
87         }\r
88 \r
89         private FileSystemInfo _fileInfo;\r
90         private TreeHash _merkle;\r
91         //private string _md5;\r
92 \r
93         public FileSystemInfo FileInfo\r
94         {\r
95             get { return _fileInfo; }\r
96             set\r
97             {\r
98                 _fileInfo = value;\r
99                 FilePath = value.FullName;\r
100             }\r
101         }\r
102 \r
103         public FileState FileState { get; set; }\r
104         public ObjectInfo ObjectInfo{ get; set; }\r
105 \r
106 \r
107         public TreeHash Merkle\r
108         {\r
109             get {\r
110                 return _merkle;\r
111             }\r
112             set {\r
113                 _merkle = value;\r
114                 C = _merkle.TopHash.ToHashString();\r
115             }\r
116         }\r
117 \r
118         public bool Locked { get; set; }\r
119 \r
120         public string NewFullPath { get; set; }\r
121 \r
122         public string OldChecksum { get; set; }\r
123 \r
124         public string OldFullPath { get; set; }\r
125 \r
126         public StateTuple() { }\r
127 \r
128         public StateTuple(FileSystemInfo info)\r
129         {\r
130             FileInfo = info;\r
131         }\r
132 \r
133         public bool HashesValid()\r
134         {\r
135             return (EmptyOrLength(C, 64)\r
136                   && EmptyOrLength(L, 64)\r
137                   && EmptyOrLength(S, 64)\r
138                 );\r
139         }\r
140     }\r
141 }