root / trunk / Pithos.Core / FileState.cs @ e0f69809
History | View | Annotate | Download (20.7 kB)
1 | 255f5f86 | Panagiotis Kanavos | #region |
---|---|---|---|
2 | 255f5f86 | Panagiotis Kanavos | /* ----------------------------------------------------------------------- |
3 | 255f5f86 | Panagiotis Kanavos | * <copyright file="FileState.cs" company="GRNet"> |
4 | 255f5f86 | Panagiotis Kanavos | * |
5 | 255f5f86 | Panagiotis Kanavos | * Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 | 255f5f86 | Panagiotis Kanavos | * |
7 | 255f5f86 | Panagiotis Kanavos | * Redistribution and use in source and binary forms, with or |
8 | 255f5f86 | Panagiotis Kanavos | * without modification, are permitted provided that the following |
9 | 255f5f86 | Panagiotis Kanavos | * conditions are met: |
10 | 255f5f86 | Panagiotis Kanavos | * |
11 | 255f5f86 | Panagiotis Kanavos | * 1. Redistributions of source code must retain the above |
12 | 255f5f86 | Panagiotis Kanavos | * copyright notice, this list of conditions and the following |
13 | 255f5f86 | Panagiotis Kanavos | * disclaimer. |
14 | 255f5f86 | Panagiotis Kanavos | * |
15 | 255f5f86 | Panagiotis Kanavos | * 2. Redistributions in binary form must reproduce the above |
16 | 255f5f86 | Panagiotis Kanavos | * copyright notice, this list of conditions and the following |
17 | 255f5f86 | Panagiotis Kanavos | * disclaimer in the documentation and/or other materials |
18 | 255f5f86 | Panagiotis Kanavos | * provided with the distribution. |
19 | 255f5f86 | Panagiotis Kanavos | * |
20 | 255f5f86 | Panagiotis Kanavos | * |
21 | 255f5f86 | Panagiotis Kanavos | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 | 255f5f86 | Panagiotis Kanavos | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | 255f5f86 | Panagiotis Kanavos | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 | 255f5f86 | Panagiotis Kanavos | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 | 255f5f86 | Panagiotis Kanavos | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | 255f5f86 | Panagiotis Kanavos | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | 255f5f86 | Panagiotis Kanavos | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 | 255f5f86 | Panagiotis Kanavos | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 | 255f5f86 | Panagiotis Kanavos | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | 255f5f86 | Panagiotis Kanavos | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 | 255f5f86 | Panagiotis Kanavos | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 | 255f5f86 | Panagiotis Kanavos | * POSSIBILITY OF SUCH DAMAGE. |
33 | 255f5f86 | Panagiotis Kanavos | * |
34 | 255f5f86 | Panagiotis Kanavos | * The views and conclusions contained in the software and |
35 | 255f5f86 | Panagiotis Kanavos | * documentation are those of the authors and should not be |
36 | 255f5f86 | Panagiotis Kanavos | * interpreted as representing official policies, either expressed |
37 | 255f5f86 | Panagiotis Kanavos | * or implied, of GRNET S.A. |
38 | 255f5f86 | Panagiotis Kanavos | * </copyright> |
39 | 255f5f86 | Panagiotis Kanavos | * ----------------------------------------------------------------------- |
40 | 255f5f86 | Panagiotis Kanavos | */ |
41 | 255f5f86 | Panagiotis Kanavos | #endregion |
42 | bfc13ed8 | Panagiotis Kanavos | using System.Diagnostics.Contracts; |
43 | 5ce54458 | Panagiotis Kanavos | using System.IO; |
44 | db8a9589 | Panagiotis Kanavos | using System.Reflection; |
45 | 0c02aa65 | Panagiotis Kanavos | using System.Threading.Tasks; |
46 | eeee29e3 | Panagiotis Kanavos | using Castle.ActiveRecord; |
47 | b5061ac8 | Panagiotis Kanavos | using Castle.ActiveRecord.Framework; |
48 | 759bd3c4 | Panagiotis Kanavos | using Castle.ActiveRecord.Queries; |
49 | 759bd3c4 | Panagiotis Kanavos | using NHibernate.Criterion; |
50 | 0af3141d | Panagiotis Kanavos | using Pithos.Core.Agents; |
51 | eeee29e3 | Panagiotis Kanavos | using Pithos.Interfaces; |
52 | 540b8cf8 | Panagiotis Kanavos | using Pithos.Network; |
53 | 4ec636f6 | Panagiotis Kanavos | using log4net; |
54 | eeee29e3 | Panagiotis Kanavos | |
55 | eeee29e3 | Panagiotis Kanavos | namespace Pithos.Core |
56 | eeee29e3 | Panagiotis Kanavos | { |
57 | eeee29e3 | Panagiotis Kanavos | using System; |
58 | fec5da06 | Panagiotis Kanavos | using System.Collections.Generic; |
59 | eeee29e3 | Panagiotis Kanavos | |
60 | eeee29e3 | Panagiotis Kanavos | /// <summary> |
61 | eeee29e3 | Panagiotis Kanavos | /// TODO: Update summary. |
62 | eeee29e3 | Panagiotis Kanavos | /// </summary> |
63 | eeee29e3 | Panagiotis Kanavos | [ActiveRecord] |
64 | e81dd1f6 | Panagiotis Kanavos | public class FileState : ActiveRecordLinqBase<FileState> |
65 | eeee29e3 | Panagiotis Kanavos | { |
66 | db8a9589 | Panagiotis Kanavos | private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
67 | db8a9589 | Panagiotis Kanavos | |
68 | e81dd1f6 | Panagiotis Kanavos | |
69 | e81dd1f6 | Panagiotis Kanavos | private IList<FileTag> _tags = new List<FileTag>(); |
70 | 3c43ec9b | Panagiotis Kanavos | |
71 | 9c4346c9 | Panagiotis Kanavos | [PrimaryKey(PrimaryKeyType.Guid)] |
72 | 9c4346c9 | Panagiotis Kanavos | public Guid Id { get; set; } |
73 | 0eea575a | Panagiotis Kanavos | |
74 | 3c76f045 | Panagiotis Kanavos | |
75 | e81dd1f6 | Panagiotis Kanavos | [Property(Unique = true, UniqueKey = "IX_FileState_FilePath")] |
76 | 3c76f045 | Panagiotis Kanavos | public string FilePath { get; set; } |
77 | eeee29e3 | Panagiotis Kanavos | |
78 | eeee29e3 | Panagiotis Kanavos | [Property] |
79 | eeee29e3 | Panagiotis Kanavos | public FileOverlayStatus OverlayStatus { get; set; } |
80 | eeee29e3 | Panagiotis Kanavos | |
81 | eeee29e3 | Panagiotis Kanavos | [Property] |
82 | eeee29e3 | Panagiotis Kanavos | public FileStatus FileStatus { get; set; } |
83 | eeee29e3 | Panagiotis Kanavos | |
84 | e3bc51ba | pkanavos | [Property] |
85 | e3bc51ba | pkanavos | public string ConflictReason { get; set; } |
86 | e3bc51ba | pkanavos | |
87 | 174bbb6e | Panagiotis Kanavos | private string _checksum; |
88 | 174bbb6e | Panagiotis Kanavos | |
89 | 174bbb6e | Panagiotis Kanavos | /// <summary> |
90 | 174bbb6e | Panagiotis Kanavos | /// The tophash value of the file, calculated by using a Merkle hash with the SHA256 algorithm |
91 | 174bbb6e | Panagiotis Kanavos | /// </summary> |
92 | 174bbb6e | Panagiotis Kanavos | /// <remarks> |
93 | 174bbb6e | Panagiotis Kanavos | /// The SHA256 algorithm is substantially more expenive than other algorithms. |
94 | 174bbb6e | Panagiotis Kanavos | /// Recalculating the Checksum should be avoided whenever possible. |
95 | 174bbb6e | Panagiotis Kanavos | /// </remarks> |
96 | eeee29e3 | Panagiotis Kanavos | [Property] |
97 | 174bbb6e | Panagiotis Kanavos | public string Checksum |
98 | 174bbb6e | Panagiotis Kanavos | { |
99 | 174bbb6e | Panagiotis Kanavos | get |
100 | 174bbb6e | Panagiotis Kanavos | { |
101 | 174bbb6e | Panagiotis Kanavos | return _checksum; |
102 | 174bbb6e | Panagiotis Kanavos | } |
103 | 174bbb6e | Panagiotis Kanavos | set |
104 | 174bbb6e | Panagiotis Kanavos | { |
105 | 174bbb6e | Panagiotis Kanavos | _checksum = value; |
106 | 174bbb6e | Panagiotis Kanavos | } |
107 | 174bbb6e | Panagiotis Kanavos | } |
108 | 174bbb6e | Panagiotis Kanavos | |
109 | 174bbb6e | Panagiotis Kanavos | private string _shortHash; |
110 | 174bbb6e | Panagiotis Kanavos | |
111 | 174bbb6e | Panagiotis Kanavos | /// <summary> |
112 | 174bbb6e | Panagiotis Kanavos | /// An easy to calcualte hash over the entire file, used to detect file changes |
113 | 174bbb6e | Panagiotis Kanavos | /// </summary> |
114 | 174bbb6e | Panagiotis Kanavos | /// <remarks>The algorithm used to calculate this hash should be cheap</remarks> |
115 | 174bbb6e | Panagiotis Kanavos | [Property(NotNull=true,Default="")] |
116 | 174bbb6e | Panagiotis Kanavos | public string ShortHash |
117 | 174bbb6e | Panagiotis Kanavos | { |
118 | 174bbb6e | Panagiotis Kanavos | get |
119 | 174bbb6e | Panagiotis Kanavos | { |
120 | 174bbb6e | Panagiotis Kanavos | return _shortHash; |
121 | 174bbb6e | Panagiotis Kanavos | } |
122 | 174bbb6e | Panagiotis Kanavos | set |
123 | 174bbb6e | Panagiotis Kanavos | { |
124 | 174bbb6e | Panagiotis Kanavos | _shortHash = value; |
125 | 174bbb6e | Panagiotis Kanavos | } |
126 | 174bbb6e | Panagiotis Kanavos | } |
127 | 283809f3 | Panagiotis Kanavos | |
128 | 9c4346c9 | Panagiotis Kanavos | |
129 | bfc13ed8 | Panagiotis Kanavos | [Property] |
130 | bfc13ed8 | Panagiotis Kanavos | public long? Version { get; set; } |
131 | bfc13ed8 | Panagiotis Kanavos | |
132 | bfc13ed8 | Panagiotis Kanavos | [Property] |
133 | bfc13ed8 | Panagiotis Kanavos | public DateTime? VersionTimeStamp { get; set; } |
134 | cfed7823 | Panagiotis Kanavos | |
135 | cfed7823 | Panagiotis Kanavos | |
136 | cfed7823 | Panagiotis Kanavos | [Property] |
137 | cfed7823 | Panagiotis Kanavos | public bool IsShared { get; set; } |
138 | cfed7823 | Panagiotis Kanavos | |
139 | cfed7823 | Panagiotis Kanavos | [Property] |
140 | cfed7823 | Panagiotis Kanavos | public string SharedBy { get; set; } |
141 | cfed7823 | Panagiotis Kanavos | |
142 | cfed7823 | Panagiotis Kanavos | [Property] |
143 | cfed7823 | Panagiotis Kanavos | public bool ShareWrite { get; set; } |
144 | cfed7823 | Panagiotis Kanavos | |
145 | 3c76f045 | Panagiotis Kanavos | [Property] |
146 | 3c76f045 | Panagiotis Kanavos | public bool IsFolder{ get; set; } |
147 | bfc13ed8 | Panagiotis Kanavos | |
148 | e81dd1f6 | Panagiotis Kanavos | [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true, Inverse = true)] |
149 | 3c43ec9b | Panagiotis Kanavos | public IList<FileTag> Tags |
150 | 3c43ec9b | Panagiotis Kanavos | { |
151 | e81dd1f6 | Panagiotis Kanavos | get { return _tags; } |
152 | e81dd1f6 | Panagiotis Kanavos | set { _tags = value; } |
153 | 3c43ec9b | Panagiotis Kanavos | } |
154 | 0c02aa65 | Panagiotis Kanavos | |
155 | 540b8cf8 | Panagiotis Kanavos | [Property] |
156 | 540b8cf8 | Panagiotis Kanavos | public DateTime Modified { get; set; } |
157 | 540b8cf8 | Panagiotis Kanavos | |
158 | 540b8cf8 | Panagiotis Kanavos | |
159 | 540b8cf8 | Panagiotis Kanavos | public FileSystemInfo GetFileSystemInfo() |
160 | 540b8cf8 | Panagiotis Kanavos | { |
161 | 540b8cf8 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(FilePath)) |
162 | 540b8cf8 | Panagiotis Kanavos | throw new InvalidOperationException(); |
163 | 540b8cf8 | Panagiotis Kanavos | Contract.EndContractBlock(); |
164 | e81dd1f6 | Panagiotis Kanavos | |
165 | 540b8cf8 | Panagiotis Kanavos | return Directory.Exists(FilePath) ? |
166 | 540b8cf8 | Panagiotis Kanavos | (FileSystemInfo)new DirectoryInfo(FilePath) |
167 | 540b8cf8 | Panagiotis Kanavos | : new FileInfo(FilePath); |
168 | 540b8cf8 | Panagiotis Kanavos | } |
169 | 540b8cf8 | Panagiotis Kanavos | |
170 | 540b8cf8 | Panagiotis Kanavos | public string GetRelativeUrl(AccountInfo accountInfo) |
171 | 540b8cf8 | Panagiotis Kanavos | { |
172 | 540b8cf8 | Panagiotis Kanavos | if (accountInfo==null) |
173 | 540b8cf8 | Panagiotis Kanavos | throw new ArgumentNullException("accountInfo"); |
174 | 540b8cf8 | Panagiotis Kanavos | Contract.EndContractBlock(); |
175 | 540b8cf8 | Panagiotis Kanavos | |
176 | 540b8cf8 | Panagiotis Kanavos | var fsi=GetFileSystemInfo(); |
177 | 540b8cf8 | Panagiotis Kanavos | return fsi.AsRelativeUrlTo(accountInfo.AccountPath); |
178 | 540b8cf8 | Panagiotis Kanavos | } |
179 | 039a89e5 | Panagiotis Kanavos | /*public static FileState FindByFilePath(string absolutePath) |
180 | bfc13ed8 | Panagiotis Kanavos | { |
181 | 0af3141d | Panagiotis Kanavos | if (string.IsNullOrWhiteSpace(absolutePath)) |
182 | 0af3141d | Panagiotis Kanavos | throw new ArgumentNullException("absolutePath"); |
183 | 0af3141d | Panagiotis Kanavos | Contract.EndContractBlock(); |
184 | 4ec636f6 | Panagiotis Kanavos | try |
185 | 4ec636f6 | Panagiotis Kanavos | { |
186 | e81dd1f6 | Panagiotis Kanavos | |
187 | e81dd1f6 | Panagiotis Kanavos | |
188 | e81dd1f6 | Panagiotis Kanavos | |
189 | 3c76f045 | Panagiotis Kanavos | return Queryable.FirstOrDefault(s => s.FilePath == absolutePath); |
190 | 4ec636f6 | Panagiotis Kanavos | } |
191 | 4ec636f6 | Panagiotis Kanavos | catch (Exception ex) |
192 | 4ec636f6 | Panagiotis Kanavos | { |
193 | 4ec636f6 | Panagiotis Kanavos | Log.Error(ex.ToString()); |
194 | 4ec636f6 | Panagiotis Kanavos | throw; |
195 | 4ec636f6 | Panagiotis Kanavos | } |
196 | e81dd1f6 | Panagiotis Kanavos | |
197 | 4ec636f6 | Panagiotis Kanavos | |
198 | 039a89e5 | Panagiotis Kanavos | }*/ |
199 | bfc13ed8 | Panagiotis Kanavos | |
200 | e81dd1f6 | Panagiotis Kanavos | /* public static void DeleteByFilePath(string absolutePath) |
201 | 0af3141d | Panagiotis Kanavos | { |
202 | e81dd1f6 | Panagiotis Kanavos | if (string.IsNullOrWhiteSpace(absolutePath)) |
203 | 0af3141d | Panagiotis Kanavos | throw new ArgumentNullException("absolutePath"); |
204 | 0af3141d | Panagiotis Kanavos | Contract.EndContractBlock(); |
205 | e81dd1f6 | Panagiotis Kanavos | |
206 | e81dd1f6 | Panagiotis Kanavos | ExecuteWithRetry((session, instance) => |
207 | e81dd1f6 | Panagiotis Kanavos | { |
208 | e81dd1f6 | Panagiotis Kanavos | const string hqlDelete = "delete FileState where FilePath = :path"; |
209 | e81dd1f6 | Panagiotis Kanavos | var deletedEntities = session.CreateQuery(hqlDelete) |
210 | 3c76f045 | Panagiotis Kanavos | .SetString("path", absolutePath) |
211 | e81dd1f6 | Panagiotis Kanavos | .ExecuteUpdate(); |
212 | e81dd1f6 | Panagiotis Kanavos | return deletedEntities; |
213 | e81dd1f6 | Panagiotis Kanavos | }, null); |
214 | e81dd1f6 | Panagiotis Kanavos | |
215 | e81dd1f6 | Panagiotis Kanavos | }*/ |
216 | 0af3141d | Panagiotis Kanavos | |
217 | ab2f6f79 | Panagiotis Kanavos | public static void StoreFileStatus(string absolutePath, FileStatus newStatus) |
218 | ab2f6f79 | Panagiotis Kanavos | { |
219 | ab2f6f79 | Panagiotis Kanavos | if (string.IsNullOrWhiteSpace(absolutePath)) |
220 | ab2f6f79 | Panagiotis Kanavos | throw new ArgumentNullException("absolutePath"); |
221 | ab2f6f79 | Panagiotis Kanavos | Contract.EndContractBlock(); |
222 | ab2f6f79 | Panagiotis Kanavos | |
223 | e81dd1f6 | Panagiotis Kanavos | ExecuteWithRetry((session, instance) => |
224 | e81dd1f6 | Panagiotis Kanavos | { |
225 | 3c76f045 | Panagiotis Kanavos | const string hqlUpdate = "update FileState set FileStatus= :status where FilePath = :path "; |
226 | e81dd1f6 | Panagiotis Kanavos | var updatedEntities = session.CreateQuery(hqlUpdate) |
227 | 3c76f045 | Panagiotis Kanavos | .SetString("path", absolutePath) |
228 | e81dd1f6 | Panagiotis Kanavos | .SetEnum("status", newStatus) |
229 | e81dd1f6 | Panagiotis Kanavos | .ExecuteUpdate(); |
230 | e81dd1f6 | Panagiotis Kanavos | if (updatedEntities == 0) |
231 | e81dd1f6 | Panagiotis Kanavos | { |
232 | e81dd1f6 | Panagiotis Kanavos | var newState = new FileState |
233 | e81dd1f6 | Panagiotis Kanavos | { |
234 | 3c76f045 | Panagiotis Kanavos | FilePath = absolutePath, |
235 | e81dd1f6 | Panagiotis Kanavos | Id = Guid.NewGuid(), |
236 | 3c76f045 | Panagiotis Kanavos | FileStatus = newStatus, |
237 | 3c76f045 | Panagiotis Kanavos | IsFolder=Directory.Exists(absolutePath) |
238 | e81dd1f6 | Panagiotis Kanavos | }; |
239 | e81dd1f6 | Panagiotis Kanavos | newState.CreateAndFlush(); |
240 | e81dd1f6 | Panagiotis Kanavos | } |
241 | e81dd1f6 | Panagiotis Kanavos | return null; |
242 | e81dd1f6 | Panagiotis Kanavos | }, null); |
243 | ab2f6f79 | Panagiotis Kanavos | |
244 | ab2f6f79 | Panagiotis Kanavos | } |
245 | ab2f6f79 | Panagiotis Kanavos | |
246 | 174bbb6e | Panagiotis Kanavos | /*public static void StoreOverlayStatus(string absolutePath, FileOverlayStatus newStatus) |
247 | 174bbb6e | Panagiotis Kanavos | { |
248 | 174bbb6e | Panagiotis Kanavos | if (string.IsNullOrWhiteSpace(absolutePath)) |
249 | 174bbb6e | Panagiotis Kanavos | throw new ArgumentNullException("absolutePath"); |
250 | 174bbb6e | Panagiotis Kanavos | Contract.EndContractBlock(); |
251 | 174bbb6e | Panagiotis Kanavos | |
252 | 174bbb6e | Panagiotis Kanavos | ExecuteWithRetry((session, instance) => |
253 | 174bbb6e | Panagiotis Kanavos | { |
254 | 174bbb6e | Panagiotis Kanavos | const string hqlUpdate = |
255 | 174bbb6e | Panagiotis Kanavos | "update FileState set OverlayStatus= :status where FilePath = :path "; |
256 | 174bbb6e | Panagiotis Kanavos | var updatedEntities = session.CreateQuery(hqlUpdate) |
257 | 174bbb6e | Panagiotis Kanavos | .SetString("path", absolutePath) |
258 | 174bbb6e | Panagiotis Kanavos | .SetEnum("status", newStatus) |
259 | 174bbb6e | Panagiotis Kanavos | .ExecuteUpdate(); |
260 | 174bbb6e | Panagiotis Kanavos | if (updatedEntities == 0) |
261 | 174bbb6e | Panagiotis Kanavos | { |
262 | 174bbb6e | Panagiotis Kanavos | var newState = new FileState |
263 | 174bbb6e | Panagiotis Kanavos | { |
264 | 174bbb6e | Panagiotis Kanavos | FilePath = absolutePath, |
265 | 174bbb6e | Panagiotis Kanavos | Id = Guid.NewGuid(), |
266 | 174bbb6e | Panagiotis Kanavos | OverlayStatus = newStatus, |
267 | 174bbb6e | Panagiotis Kanavos | ShortHash = String.Empty, |
268 | 174bbb6e | Panagiotis Kanavos | IsFolder=Directory.Exists(absolutePath) |
269 | 174bbb6e | Panagiotis Kanavos | }; |
270 | 174bbb6e | Panagiotis Kanavos | newState.CreateAndFlush(); |
271 | 174bbb6e | Panagiotis Kanavos | } |
272 | 174bbb6e | Panagiotis Kanavos | return null; |
273 | 174bbb6e | Panagiotis Kanavos | }, null); |
274 | 174bbb6e | Panagiotis Kanavos | |
275 | 174bbb6e | Panagiotis Kanavos | } |
276 | 174bbb6e | Panagiotis Kanavos | */ |
277 | 174bbb6e | Panagiotis Kanavos | public static void StoreOverlayStatus(string absolutePath, FileOverlayStatus newStatus,string shortHash) |
278 | ab2f6f79 | Panagiotis Kanavos | { |
279 | ab2f6f79 | Panagiotis Kanavos | if (string.IsNullOrWhiteSpace(absolutePath)) |
280 | ab2f6f79 | Panagiotis Kanavos | throw new ArgumentNullException("absolutePath"); |
281 | ab2f6f79 | Panagiotis Kanavos | Contract.EndContractBlock(); |
282 | ab2f6f79 | Panagiotis Kanavos | |
283 | e81dd1f6 | Panagiotis Kanavos | ExecuteWithRetry((session, instance) => |
284 | e81dd1f6 | Panagiotis Kanavos | { |
285 | e81dd1f6 | Panagiotis Kanavos | const string hqlUpdate = |
286 | 3c76f045 | Panagiotis Kanavos | "update FileState set OverlayStatus= :status where FilePath = :path "; |
287 | 3c76f045 | Panagiotis Kanavos | var updatedEntities = session.CreateQuery(hqlUpdate) |
288 | 3c76f045 | Panagiotis Kanavos | .SetString("path", absolutePath) |
289 | 3c76f045 | Panagiotis Kanavos | .SetEnum("status", newStatus) |
290 | e81dd1f6 | Panagiotis Kanavos | .ExecuteUpdate(); |
291 | e81dd1f6 | Panagiotis Kanavos | if (updatedEntities == 0) |
292 | e81dd1f6 | Panagiotis Kanavos | { |
293 | e81dd1f6 | Panagiotis Kanavos | var newState = new FileState |
294 | e81dd1f6 | Panagiotis Kanavos | { |
295 | e81dd1f6 | Panagiotis Kanavos | FilePath = absolutePath, |
296 | e81dd1f6 | Panagiotis Kanavos | Id = Guid.NewGuid(), |
297 | 3c76f045 | Panagiotis Kanavos | OverlayStatus = newStatus, |
298 | 174bbb6e | Panagiotis Kanavos | ShortHash = shortHash??String.Empty, |
299 | 3c76f045 | Panagiotis Kanavos | IsFolder=Directory.Exists(absolutePath) |
300 | e81dd1f6 | Panagiotis Kanavos | }; |
301 | e81dd1f6 | Panagiotis Kanavos | newState.CreateAndFlush(); |
302 | e81dd1f6 | Panagiotis Kanavos | } |
303 | e81dd1f6 | Panagiotis Kanavos | return null; |
304 | e81dd1f6 | Panagiotis Kanavos | }, null); |
305 | ab2f6f79 | Panagiotis Kanavos | |
306 | ab2f6f79 | Panagiotis Kanavos | } |
307 | ab2f6f79 | Panagiotis Kanavos | |
308 | e81dd1f6 | Panagiotis Kanavos | /* |
309 | ab2f6f79 | Panagiotis Kanavos | public static void UpdateStatus(string absolutePath, FileStatus fileStatus, FileOverlayStatus overlayStatus) |
310 | ab2f6f79 | Panagiotis Kanavos | { |
311 | ab2f6f79 | Panagiotis Kanavos | if (string.IsNullOrWhiteSpace(absolutePath)) |
312 | ab2f6f79 | Panagiotis Kanavos | throw new ArgumentNullException("absolutePath"); |
313 | ab2f6f79 | Panagiotis Kanavos | Contract.EndContractBlock(); |
314 | ab2f6f79 | Panagiotis Kanavos | |
315 | e81dd1f6 | Panagiotis Kanavos | ExecuteWithRetry((session, instance) => |
316 | e81dd1f6 | Panagiotis Kanavos | { |
317 | e81dd1f6 | Panagiotis Kanavos | const string hqlUpdate = |
318 | e81dd1f6 | Panagiotis Kanavos | "update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus where FilePath = :path "; |
319 | e81dd1f6 | Panagiotis Kanavos | var updatedEntities = session.CreateQuery(hqlUpdate) |
320 | 3c76f045 | Panagiotis Kanavos | .SetString("path", absolutePath) |
321 | e81dd1f6 | Panagiotis Kanavos | .SetEnum("fileStatus", fileStatus) |
322 | e81dd1f6 | Panagiotis Kanavos | .SetEnum("overlayStatus", overlayStatus) |
323 | e81dd1f6 | Panagiotis Kanavos | .ExecuteUpdate(); |
324 | e81dd1f6 | Panagiotis Kanavos | return updatedEntities; |
325 | e81dd1f6 | Panagiotis Kanavos | }, null); |
326 | ab2f6f79 | Panagiotis Kanavos | |
327 | ab2f6f79 | Panagiotis Kanavos | } |
328 | e81dd1f6 | Panagiotis Kanavos | */ |
329 | e81dd1f6 | Panagiotis Kanavos | |
330 | e81dd1f6 | Panagiotis Kanavos | /* |
331 | ab2f6f79 | Panagiotis Kanavos | public static void UpdateStatus(string absolutePath, FileStatus fileStatus) |
332 | ab2f6f79 | Panagiotis Kanavos | { |
333 | ab2f6f79 | Panagiotis Kanavos | if (string.IsNullOrWhiteSpace(absolutePath)) |
334 | ab2f6f79 | Panagiotis Kanavos | throw new ArgumentNullException("absolutePath"); |
335 | ab2f6f79 | Panagiotis Kanavos | Contract.EndContractBlock(); |
336 | ab2f6f79 | Panagiotis Kanavos | |
337 | e81dd1f6 | Panagiotis Kanavos | ExecuteWithRetry((session, instance) => |
338 | e81dd1f6 | Panagiotis Kanavos | { |
339 | e81dd1f6 | Panagiotis Kanavos | const string hqlUpdate = |
340 | e81dd1f6 | Panagiotis Kanavos | "update FileState set FileStatus= :fileStatus where FilePath = :path "; |
341 | e81dd1f6 | Panagiotis Kanavos | var updatedEntities = session.CreateQuery(hqlUpdate) |
342 | 3c76f045 | Panagiotis Kanavos | .SetString("path", absolutePath) |
343 | e81dd1f6 | Panagiotis Kanavos | .SetEnum("fileStatus", fileStatus) |
344 | e81dd1f6 | Panagiotis Kanavos | .ExecuteUpdate(); |
345 | e81dd1f6 | Panagiotis Kanavos | return updatedEntities; |
346 | e81dd1f6 | Panagiotis Kanavos | }, null); |
347 | ab2f6f79 | Panagiotis Kanavos | |
348 | ab2f6f79 | Panagiotis Kanavos | } |
349 | ab2f6f79 | Panagiotis Kanavos | |
350 | e81dd1f6 | Panagiotis Kanavos | */ |
351 | ab2f6f79 | Panagiotis Kanavos | public static void RenameState(string oldPath, string newPath) |
352 | ab2f6f79 | Panagiotis Kanavos | { |
353 | ab2f6f79 | Panagiotis Kanavos | if (string.IsNullOrWhiteSpace(oldPath)) |
354 | ab2f6f79 | Panagiotis Kanavos | throw new ArgumentNullException("oldPath"); |
355 | ab2f6f79 | Panagiotis Kanavos | Contract.EndContractBlock(); |
356 | ab2f6f79 | Panagiotis Kanavos | |
357 | e81dd1f6 | Panagiotis Kanavos | ExecuteWithRetry((session, instance) => |
358 | e81dd1f6 | Panagiotis Kanavos | { |
359 | e81dd1f6 | Panagiotis Kanavos | const string hqlUpdate = |
360 | 3c76f045 | Panagiotis Kanavos | "update FileState set FilePath= :newPath where FilePath = :oldPath "; |
361 | e81dd1f6 | Panagiotis Kanavos | var updatedEntities = session.CreateQuery(hqlUpdate) |
362 | 3c76f045 | Panagiotis Kanavos | .SetString("oldPath", oldPath) |
363 | 3c76f045 | Panagiotis Kanavos | .SetString("newPath", newPath) |
364 | e81dd1f6 | Panagiotis Kanavos | .ExecuteUpdate(); |
365 | e81dd1f6 | Panagiotis Kanavos | return updatedEntities; |
366 | e81dd1f6 | Panagiotis Kanavos | }, null); |
367 | ab2f6f79 | Panagiotis Kanavos | |
368 | ab2f6f79 | Panagiotis Kanavos | } |
369 | ab2f6f79 | Panagiotis Kanavos | |
370 | e81dd1f6 | Panagiotis Kanavos | /* public static void UpdateStatus(Guid id, FileStatus fileStatus) |
371 | ab2f6f79 | Panagiotis Kanavos | { |
372 | ab2f6f79 | Panagiotis Kanavos | |
373 | e81dd1f6 | Panagiotis Kanavos | ExecuteWithRetry((session, instance) => |
374 | ab2f6f79 | Panagiotis Kanavos | { |
375 | e81dd1f6 | Panagiotis Kanavos | const string hqlUpdate = |
376 | e81dd1f6 | Panagiotis Kanavos | "update FileState set FileStatus= :fileStatus where Id = :id "; |
377 | ab2f6f79 | Panagiotis Kanavos | var updatedEntities = session.CreateQuery(hqlUpdate) |
378 | e81dd1f6 | Panagiotis Kanavos | .SetGuid("id", id) |
379 | e81dd1f6 | Panagiotis Kanavos | .SetEnum("fileStatus", fileStatus) |
380 | e81dd1f6 | Panagiotis Kanavos | .ExecuteUpdate(); |
381 | a0dcfcc9 | Panagiotis Kanavos | return updatedEntities; |
382 | ab2f6f79 | Panagiotis Kanavos | }, null); |
383 | e81dd1f6 | Panagiotis Kanavos | }*/ |
384 | ab2f6f79 | Panagiotis Kanavos | |
385 | 174bbb6e | Panagiotis Kanavos | public static void UpdateChecksum(string absolutePath, string shortHash, string checksum) |
386 | ab2f6f79 | Panagiotis Kanavos | { |
387 | ab2f6f79 | Panagiotis Kanavos | if (string.IsNullOrWhiteSpace(absolutePath)) |
388 | ab2f6f79 | Panagiotis Kanavos | throw new ArgumentNullException("absolutePath"); |
389 | ab2f6f79 | Panagiotis Kanavos | Contract.EndContractBlock(); |
390 | ab2f6f79 | Panagiotis Kanavos | |
391 | e81dd1f6 | Panagiotis Kanavos | ExecuteWithRetry((session, instance) => |
392 | e81dd1f6 | Panagiotis Kanavos | { |
393 | 174bbb6e | Panagiotis Kanavos | const string hqlUpdate = "update FileState set Checksum= :checksum,ShortHash=:shortHash where FilePath = :path "; |
394 | e81dd1f6 | Panagiotis Kanavos | var updatedEntities = session.CreateQuery(hqlUpdate) |
395 | 3c76f045 | Panagiotis Kanavos | .SetString("path", absolutePath) |
396 | e81dd1f6 | Panagiotis Kanavos | .SetString("checksum", checksum) |
397 | 174bbb6e | Panagiotis Kanavos | .SetString("shortHash", shortHash) |
398 | e81dd1f6 | Panagiotis Kanavos | .ExecuteUpdate(); |
399 | e81dd1f6 | Panagiotis Kanavos | return updatedEntities; |
400 | e81dd1f6 | Panagiotis Kanavos | }, null); |
401 | ab2f6f79 | Panagiotis Kanavos | |
402 | ab2f6f79 | Panagiotis Kanavos | } |
403 | 0af3141d | Panagiotis Kanavos | |
404 | e81dd1f6 | Panagiotis Kanavos | public static void ChangeRootPath(string oldPath, string newPath) |
405 | 5120f3cb | Panagiotis Kanavos | { |
406 | 5120f3cb | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(oldPath)) |
407 | 5120f3cb | Panagiotis Kanavos | throw new ArgumentNullException("oldPath"); |
408 | 5120f3cb | Panagiotis Kanavos | if (!Path.IsPathRooted(oldPath)) |
409 | 5120f3cb | Panagiotis Kanavos | throw new ArgumentException("oldPath must be an absolute path", "oldPath"); |
410 | 5120f3cb | Panagiotis Kanavos | if (string.IsNullOrWhiteSpace(newPath)) |
411 | 5120f3cb | Panagiotis Kanavos | throw new ArgumentNullException("newPath"); |
412 | 5120f3cb | Panagiotis Kanavos | if (!Path.IsPathRooted(newPath)) |
413 | 5120f3cb | Panagiotis Kanavos | throw new ArgumentException("newPath must be an absolute path", "newPath"); |
414 | 5120f3cb | Panagiotis Kanavos | Contract.EndContractBlock(); |
415 | 5120f3cb | Panagiotis Kanavos | |
416 | 5120f3cb | Panagiotis Kanavos | //Ensure the paths end with the same character |
417 | 5120f3cb | Panagiotis Kanavos | if (!oldPath.EndsWith("\\")) |
418 | 5120f3cb | Panagiotis Kanavos | oldPath = oldPath + "\\"; |
419 | 5120f3cb | Panagiotis Kanavos | if (!newPath.EndsWith("\\")) |
420 | 5120f3cb | Panagiotis Kanavos | newPath = newPath + "\\"; |
421 | 5120f3cb | Panagiotis Kanavos | |
422 | e81dd1f6 | Panagiotis Kanavos | ExecuteWithRetry((session, instance) => |
423 | 5120f3cb | Panagiotis Kanavos | { |
424 | 5120f3cb | Panagiotis Kanavos | const string hqlUpdate = |
425 | 5120f3cb | Panagiotis Kanavos | "update FileState set FilePath = replace(FilePath,:oldPath,:newPath) where FilePath like :oldPath || '%' "; |
426 | e81dd1f6 | Panagiotis Kanavos | var renames = session.CreateQuery(hqlUpdate) |
427 | 3c76f045 | Panagiotis Kanavos | .SetString("oldPath", oldPath) |
428 | 3c76f045 | Panagiotis Kanavos | .SetString("newPath", newPath) |
429 | 5120f3cb | Panagiotis Kanavos | .ExecuteUpdate(); |
430 | a0dcfcc9 | Panagiotis Kanavos | return renames; |
431 | 5120f3cb | Panagiotis Kanavos | }, null); |
432 | 5120f3cb | Panagiotis Kanavos | } |
433 | 5120f3cb | Panagiotis Kanavos | |
434 | 174bbb6e | Panagiotis Kanavos | public static FileState CreateFor(FileSystemInfo info) |
435 | 0c02aa65 | Panagiotis Kanavos | { |
436 | d78d765c | pkanavos | if(info==null) |
437 | d78d765c | pkanavos | throw new ArgumentNullException("info"); |
438 | bfc13ed8 | Panagiotis Kanavos | Contract.EndContractBlock(); |
439 | 174bbb6e | Panagiotis Kanavos | |
440 | 174bbb6e | Panagiotis Kanavos | if (info is DirectoryInfo) |
441 | 174bbb6e | Panagiotis Kanavos | return new FileState |
442 | 174bbb6e | Panagiotis Kanavos | { |
443 | 174bbb6e | Panagiotis Kanavos | FilePath = info.FullName, |
444 | 174bbb6e | Panagiotis Kanavos | OverlayStatus = FileOverlayStatus.Unversioned, |
445 | 174bbb6e | Panagiotis Kanavos | FileStatus = FileStatus.Created, |
446 | 174bbb6e | Panagiotis Kanavos | ShortHash=String.Empty, |
447 | 174bbb6e | Panagiotis Kanavos | Id = Guid.NewGuid() |
448 | 174bbb6e | Panagiotis Kanavos | }; |
449 | bfc13ed8 | Panagiotis Kanavos | |
450 | bfc13ed8 | Panagiotis Kanavos | |
451 | 174bbb6e | Panagiotis Kanavos | var shortHash = ((FileInfo)info).ComputeShortHash(); |
452 | 0c02aa65 | Panagiotis Kanavos | var fileState = new FileState |
453 | 0c02aa65 | Panagiotis Kanavos | { |
454 | 174bbb6e | Panagiotis Kanavos | FilePath = info.FullName, |
455 | e81dd1f6 | Panagiotis Kanavos | OverlayStatus = FileOverlayStatus.Unversioned, |
456 | 174bbb6e | Panagiotis Kanavos | FileStatus = FileStatus.Created, |
457 | 174bbb6e | Panagiotis Kanavos | ShortHash=shortHash, |
458 | e81dd1f6 | Panagiotis Kanavos | Id = Guid.NewGuid() |
459 | 0c02aa65 | Panagiotis Kanavos | }; |
460 | 174bbb6e | Panagiotis Kanavos | return fileState; |
461 | 0c02aa65 | Panagiotis Kanavos | } |
462 | 0c02aa65 | Panagiotis Kanavos | |
463 | e81dd1f6 | Panagiotis Kanavos | |
464 | e81dd1f6 | Panagiotis Kanavos | private static void ExecuteWithRetry(NHibernateDelegate call, object state) |
465 | e81dd1f6 | Panagiotis Kanavos | { |
466 | e81dd1f6 | Panagiotis Kanavos | int retries = 3; |
467 | e81dd1f6 | Panagiotis Kanavos | while (retries > 0) |
468 | e81dd1f6 | Panagiotis Kanavos | try |
469 | e81dd1f6 | Panagiotis Kanavos | { |
470 | e81dd1f6 | Panagiotis Kanavos | using (new SessionScope()) |
471 | e81dd1f6 | Panagiotis Kanavos | { |
472 | e81dd1f6 | Panagiotis Kanavos | Execute(call, state); |
473 | 039a89e5 | Panagiotis Kanavos | return; |
474 | e81dd1f6 | Panagiotis Kanavos | } |
475 | e81dd1f6 | Panagiotis Kanavos | } |
476 | 1b4a7550 | Panagiotis Kanavos | catch (ActiveRecordException ) |
477 | e81dd1f6 | Panagiotis Kanavos | { |
478 | e81dd1f6 | Panagiotis Kanavos | retries--; |
479 | e81dd1f6 | Panagiotis Kanavos | if (retries <= 0) |
480 | e81dd1f6 | Panagiotis Kanavos | throw; |
481 | e81dd1f6 | Panagiotis Kanavos | } |
482 | e81dd1f6 | Panagiotis Kanavos | } |
483 | 759bd3c4 | Panagiotis Kanavos | |
484 | fec5da06 | Panagiotis Kanavos | /// <summary> |
485 | 4147814e | Panagiotis Kanavos | /// Mark Unversioned all FileState rows from the database whose path |
486 | fec5da06 | Panagiotis Kanavos | /// starts with one of the removed paths |
487 | fec5da06 | Panagiotis Kanavos | /// </summary> |
488 | fec5da06 | Panagiotis Kanavos | /// <param name="removed"></param> |
489 | 4147814e | Panagiotis Kanavos | public static void UnversionPaths(List<string> removed) |
490 | 759bd3c4 | Panagiotis Kanavos | { |
491 | fec5da06 | Panagiotis Kanavos | if (removed == null) |
492 | fec5da06 | Panagiotis Kanavos | return; |
493 | fec5da06 | Panagiotis Kanavos | if (removed.Count == 0) |
494 | fec5da06 | Panagiotis Kanavos | return; |
495 | 759bd3c4 | Panagiotis Kanavos | |
496 | fec5da06 | Panagiotis Kanavos | //Create a disjunction (list of OR statements |
497 | fec5da06 | Panagiotis Kanavos | var disjunction = new Disjunction(); |
498 | 759bd3c4 | Panagiotis Kanavos | foreach (var path in removed) |
499 | 759bd3c4 | Panagiotis Kanavos | { |
500 | fec5da06 | Panagiotis Kanavos | //with the restriction FileState.FilePath like '@path%' |
501 | fec5da06 | Panagiotis Kanavos | disjunction.Add(Restrictions.On<FileState>(s => s.FilePath) |
502 | fec5da06 | Panagiotis Kanavos | .IsLike(path, MatchMode.Start)); |
503 | 759bd3c4 | Panagiotis Kanavos | } |
504 | 759bd3c4 | Panagiotis Kanavos | |
505 | fec5da06 | Panagiotis Kanavos | //Generate a query from the disjunction |
506 | 759bd3c4 | Panagiotis Kanavos | var query=QueryOver.Of<FileState>().Where(disjunction); |
507 | 4147814e | Panagiotis Kanavos | |
508 | 4147814e | Panagiotis Kanavos | ExecuteWithRetry((session,instance)=> |
509 | 4147814e | Panagiotis Kanavos | { |
510 | 4147814e | Panagiotis Kanavos | using (var t=session.BeginTransaction()) |
511 | 4147814e | Panagiotis Kanavos | { |
512 | 4147814e | Panagiotis Kanavos | var states = query.GetExecutableQueryOver(session).List(); |
513 | 4147814e | Panagiotis Kanavos | foreach (var state in states) |
514 | 4147814e | Panagiotis Kanavos | { |
515 | 4147814e | Panagiotis Kanavos | state.FileStatus = FileStatus.Unversioned; |
516 | 4147814e | Panagiotis Kanavos | state.OverlayStatus = FileOverlayStatus.Unversioned; |
517 | 4147814e | Panagiotis Kanavos | state.Update(); |
518 | 4147814e | Panagiotis Kanavos | } |
519 | 4147814e | Panagiotis Kanavos | t.Commit(); |
520 | 4147814e | Panagiotis Kanavos | } |
521 | 4147814e | Panagiotis Kanavos | return null; |
522 | 4147814e | Panagiotis Kanavos | },null); |
523 | 759bd3c4 | Panagiotis Kanavos | } |
524 | 4147814e | Panagiotis Kanavos | |
525 | eeee29e3 | Panagiotis Kanavos | } |
526 | 283809f3 | Panagiotis Kanavos | |
527 | 9c4346c9 | Panagiotis Kanavos | [ActiveRecord("Tags")] |
528 | 3c43ec9b | Panagiotis Kanavos | public class FileTag : ActiveRecordLinqBase<FileTag> |
529 | 3c43ec9b | Panagiotis Kanavos | { |
530 | 3c43ec9b | Panagiotis Kanavos | [PrimaryKey] |
531 | 9c4346c9 | Panagiotis Kanavos | public int Id { get; set; } |
532 | 9c4346c9 | Panagiotis Kanavos | |
533 | 9c4346c9 | Panagiotis Kanavos | [Property] |
534 | 9c4346c9 | Panagiotis Kanavos | public string Name { get; set; } |
535 | 3c43ec9b | Panagiotis Kanavos | |
536 | 3c43ec9b | Panagiotis Kanavos | [Property] |
537 | 3c43ec9b | Panagiotis Kanavos | public string Value { get; set; } |
538 | 3c43ec9b | Panagiotis Kanavos | |
539 | 9c4346c9 | Panagiotis Kanavos | [BelongsTo("FileStateId")] |
540 | 3c43ec9b | Panagiotis Kanavos | public FileState FileState { get; set; } |
541 | 3c43ec9b | Panagiotis Kanavos | |
542 | 3c43ec9b | Panagiotis Kanavos | } |
543 | a0dcfcc9 | Panagiotis Kanavos | |
544 | eeee29e3 | Panagiotis Kanavos | } |