2 /* -----------------------------------------------------------------------
3 * <copyright file="FileState.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * The views and conclusions contained in the software and
35 * documentation are those of the authors and should not be
36 * interpreted as representing official policies, either expressed
37 * or implied, of GRNET S.A.
39 * -----------------------------------------------------------------------
42 using System.Diagnostics.Contracts;
44 using System.Reflection;
45 using System.Threading.Tasks;
46 using Castle.ActiveRecord;
47 using Castle.ActiveRecord.Framework;
48 using Castle.ActiveRecord.Queries;
49 using NHibernate.Criterion;
50 using Pithos.Core.Agents;
51 using Pithos.Interfaces;
58 using System.Collections.Generic;
61 /// TODO: Update summary.
64 public class FileState : ActiveRecordLinqBase<FileState>
66 private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
69 private IList<FileTag> _tags = new List<FileTag>();
71 [PrimaryKey(PrimaryKeyType.Guid)]
72 public Guid Id { get; set; }
75 [Property(Unique = true, UniqueKey = "IX_FileState_FilePath")]
76 public string FilePath { get; set; }
79 public FileOverlayStatus OverlayStatus { get; set; }
82 public FileStatus FileStatus { get; set; }
85 public string ConflictReason { get; set; }
87 private string _checksum;
90 /// The tophash value of the file, calculated by using a Merkle hash with the SHA256 algorithm
93 /// The SHA256 algorithm is substantially more expenive than other algorithms.
94 /// Recalculating the Checksum should be avoided whenever possible.
97 public string Checksum
109 private string _shortHash;
112 /// An easy to calcualte hash over the entire file, used to detect file changes
114 /// <remarks>The algorithm used to calculate this hash should be cheap</remarks>
115 [Property(NotNull=true,Default="")]
116 public string ShortHash
130 public long? Version { get; set; }
133 public DateTime? VersionTimeStamp { get; set; }
137 public bool IsShared { get; set; }
140 public string SharedBy { get; set; }
143 public bool ShareWrite { get; set; }
146 public bool IsFolder{ get; set; }
148 [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true, Inverse = true)]
149 public IList<FileTag> Tags
151 get { return _tags; }
152 set { _tags = value; }
156 public DateTime Modified { get; set; }
159 public FileSystemInfo GetFileSystemInfo()
161 if (String.IsNullOrWhiteSpace(FilePath))
162 throw new InvalidOperationException();
163 Contract.EndContractBlock();
165 return Directory.Exists(FilePath) ?
166 (FileSystemInfo)new DirectoryInfo(FilePath)
167 : new FileInfo(FilePath);
170 public string GetRelativeUrl(AccountInfo accountInfo)
172 if (accountInfo==null)
173 throw new ArgumentNullException("accountInfo");
174 Contract.EndContractBlock();
176 var fsi=GetFileSystemInfo();
177 return fsi.AsRelativeUrlTo(accountInfo.AccountPath);
179 /*public static FileState FindByFilePath(string absolutePath)
181 if (string.IsNullOrWhiteSpace(absolutePath))
182 throw new ArgumentNullException("absolutePath");
183 Contract.EndContractBlock();
189 return Queryable.FirstOrDefault(s => s.FilePath == absolutePath);
193 Log.Error(ex.ToString());
200 /* public static void DeleteByFilePath(string absolutePath)
202 if (string.IsNullOrWhiteSpace(absolutePath))
203 throw new ArgumentNullException("absolutePath");
204 Contract.EndContractBlock();
206 ExecuteWithRetry((session, instance) =>
208 const string hqlDelete = "delete FileState where FilePath = :path";
209 var deletedEntities = session.CreateQuery(hqlDelete)
210 .SetString("path", absolutePath)
212 return deletedEntities;
217 public static void StoreFileStatus(string absolutePath, FileStatus newStatus)
219 if (string.IsNullOrWhiteSpace(absolutePath))
220 throw new ArgumentNullException("absolutePath");
221 Contract.EndContractBlock();
223 ExecuteWithRetry((session, instance) =>
225 const string hqlUpdate = "update FileState set FileStatus= :status where FilePath = :path ";
226 var updatedEntities = session.CreateQuery(hqlUpdate)
227 .SetString("path", absolutePath)
228 .SetEnum("status", newStatus)
230 if (updatedEntities == 0)
232 var newState = new FileState
234 FilePath = absolutePath,
236 FileStatus = newStatus,
237 IsFolder=Directory.Exists(absolutePath)
239 newState.CreateAndFlush();
246 /*public static void StoreOverlayStatus(string absolutePath, FileOverlayStatus newStatus)
248 if (string.IsNullOrWhiteSpace(absolutePath))
249 throw new ArgumentNullException("absolutePath");
250 Contract.EndContractBlock();
252 ExecuteWithRetry((session, instance) =>
254 const string hqlUpdate =
255 "update FileState set OverlayStatus= :status where FilePath = :path ";
256 var updatedEntities = session.CreateQuery(hqlUpdate)
257 .SetString("path", absolutePath)
258 .SetEnum("status", newStatus)
260 if (updatedEntities == 0)
262 var newState = new FileState
264 FilePath = absolutePath,
266 OverlayStatus = newStatus,
267 ShortHash = String.Empty,
268 IsFolder=Directory.Exists(absolutePath)
270 newState.CreateAndFlush();
277 public static void StoreOverlayStatus(string absolutePath, FileOverlayStatus newStatus,string shortHash)
279 if (string.IsNullOrWhiteSpace(absolutePath))
280 throw new ArgumentNullException("absolutePath");
281 Contract.EndContractBlock();
283 ExecuteWithRetry((session, instance) =>
285 const string hqlUpdate =
286 "update FileState set OverlayStatus= :status where FilePath = :path ";
287 var updatedEntities = session.CreateQuery(hqlUpdate)
288 .SetString("path", absolutePath)
289 .SetEnum("status", newStatus)
291 if (updatedEntities == 0)
293 var newState = new FileState
295 FilePath = absolutePath,
297 OverlayStatus = newStatus,
298 ShortHash = shortHash??String.Empty,
299 IsFolder=Directory.Exists(absolutePath)
301 newState.CreateAndFlush();
309 public static void UpdateStatus(string absolutePath, FileStatus fileStatus, FileOverlayStatus overlayStatus)
311 if (string.IsNullOrWhiteSpace(absolutePath))
312 throw new ArgumentNullException("absolutePath");
313 Contract.EndContractBlock();
315 ExecuteWithRetry((session, instance) =>
317 const string hqlUpdate =
318 "update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus where FilePath = :path ";
319 var updatedEntities = session.CreateQuery(hqlUpdate)
320 .SetString("path", absolutePath)
321 .SetEnum("fileStatus", fileStatus)
322 .SetEnum("overlayStatus", overlayStatus)
324 return updatedEntities;
331 public static void UpdateStatus(string absolutePath, FileStatus fileStatus)
333 if (string.IsNullOrWhiteSpace(absolutePath))
334 throw new ArgumentNullException("absolutePath");
335 Contract.EndContractBlock();
337 ExecuteWithRetry((session, instance) =>
339 const string hqlUpdate =
340 "update FileState set FileStatus= :fileStatus where FilePath = :path ";
341 var updatedEntities = session.CreateQuery(hqlUpdate)
342 .SetString("path", absolutePath)
343 .SetEnum("fileStatus", fileStatus)
345 return updatedEntities;
351 public static void RenameState(string oldPath, string newPath)
353 if (string.IsNullOrWhiteSpace(oldPath))
354 throw new ArgumentNullException("oldPath");
355 Contract.EndContractBlock();
357 ExecuteWithRetry((session, instance) =>
359 const string hqlUpdate =
360 "update FileState set FilePath= :newPath where FilePath = :oldPath ";
361 var updatedEntities = session.CreateQuery(hqlUpdate)
362 .SetString("oldPath", oldPath)
363 .SetString("newPath", newPath)
365 return updatedEntities;
370 /* public static void UpdateStatus(Guid id, FileStatus fileStatus)
373 ExecuteWithRetry((session, instance) =>
375 const string hqlUpdate =
376 "update FileState set FileStatus= :fileStatus where Id = :id ";
377 var updatedEntities = session.CreateQuery(hqlUpdate)
379 .SetEnum("fileStatus", fileStatus)
381 return updatedEntities;
385 public static void UpdateChecksum(string absolutePath, string shortHash, string checksum)
387 if (string.IsNullOrWhiteSpace(absolutePath))
388 throw new ArgumentNullException("absolutePath");
389 Contract.EndContractBlock();
391 ExecuteWithRetry((session, instance) =>
393 const string hqlUpdate = "update FileState set Checksum= :checksum,ShortHash=:shortHash where FilePath = :path ";
394 var updatedEntities = session.CreateQuery(hqlUpdate)
395 .SetString("path", absolutePath)
396 .SetString("checksum", checksum)
397 .SetString("shortHash", shortHash)
399 return updatedEntities;
404 public static void ChangeRootPath(string oldPath, string newPath)
406 if (String.IsNullOrWhiteSpace(oldPath))
407 throw new ArgumentNullException("oldPath");
408 if (!Path.IsPathRooted(oldPath))
409 throw new ArgumentException("oldPath must be an absolute path", "oldPath");
410 if (string.IsNullOrWhiteSpace(newPath))
411 throw new ArgumentNullException("newPath");
412 if (!Path.IsPathRooted(newPath))
413 throw new ArgumentException("newPath must be an absolute path", "newPath");
414 Contract.EndContractBlock();
416 //Ensure the paths end with the same character
417 if (!oldPath.EndsWith("\\"))
418 oldPath = oldPath + "\\";
419 if (!newPath.EndsWith("\\"))
420 newPath = newPath + "\\";
422 ExecuteWithRetry((session, instance) =>
424 const string hqlUpdate =
425 "update FileState set FilePath = replace(FilePath,:oldPath,:newPath) where FilePath like :oldPath || '%' ";
426 var renames = session.CreateQuery(hqlUpdate)
427 .SetString("oldPath", oldPath)
428 .SetString("newPath", newPath)
434 public static FileState CreateFor(FileSystemInfo info)
437 throw new ArgumentNullException("info");
438 Contract.EndContractBlock();
440 if (info is DirectoryInfo)
443 FilePath = info.FullName,
444 OverlayStatus = FileOverlayStatus.Unversioned,
445 FileStatus = FileStatus.Created,
446 ShortHash=String.Empty,
451 var shortHash = ((FileInfo)info).ComputeShortHash();
452 var fileState = new FileState
454 FilePath = info.FullName,
455 OverlayStatus = FileOverlayStatus.Unversioned,
456 FileStatus = FileStatus.Created,
464 private static void ExecuteWithRetry(NHibernateDelegate call, object state)
470 using (new SessionScope())
472 Execute(call, state);
476 catch (ActiveRecordException )
485 /// Mark Unversioned all FileState rows from the database whose path
486 /// starts with one of the removed paths
488 /// <param name="removed"></param>
489 public static void UnversionPaths(List<string> removed)
493 if (removed.Count == 0)
496 //Create a disjunction (list of OR statements
497 var disjunction = new Disjunction();
498 foreach (var path in removed)
500 //with the restriction FileState.FilePath like '@path%'
501 disjunction.Add(Restrictions.On<FileState>(s => s.FilePath)
502 .IsLike(path, MatchMode.Start));
505 //Generate a query from the disjunction
506 var query=QueryOver.Of<FileState>().Where(disjunction);
508 ExecuteWithRetry((session,instance)=>
510 using (var t=session.BeginTransaction())
512 var states = query.GetExecutableQueryOver(session).List();
513 foreach (var state in states)
515 state.FileStatus = FileStatus.Unversioned;
516 state.OverlayStatus = FileOverlayStatus.Unversioned;
527 [ActiveRecord("Tags")]
528 public class FileTag : ActiveRecordLinqBase<FileTag>
531 public int Id { get; set; }
534 public string Name { get; set; }
537 public string Value { get; set; }
539 [BelongsTo("FileStateId")]
540 public FileState FileState { get; set; }