// </copyright>
// -----------------------------------------------------------------------
+using System.Diagnostics.Contracts;
using System.IO;
using System.Threading.Tasks;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
+using Castle.ActiveRecord.Queries;
using NHibernate.Engine;
+using Pithos.Core.Agents;
using Pithos.Interfaces;
using Pithos.Network;
[Property]
public string Checksum { get; set; }
+/*
[Property]
public string TopHash { get; set; }
+*/
+
+ [Property]
+ public long? Version { get; set; }
+
+ [Property]
+ public DateTime? VersionTimeStamp { get; set; }
+
+
+ [Property]
+ public bool IsShared { get; set; }
+
+ [Property]
+ public string SharedBy { get; set; }
+
+ [Property]
+ public bool ShareWrite { get; set; }
+
[HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true,Inverse=true)]
public IList<FileTag> Tags
// [Property]
// public byte[] HashmapHash { get; set; }
- public static Task<FileState> CreateForAsync(string filePath)
+ public static FileState FindByFilePath(string absolutePath)
{
+ if (string.IsNullOrWhiteSpace(absolutePath))
+ throw new ArgumentNullException("absolutePath");
+ Contract.EndContractBlock();
+ return Queryable.FirstOrDefault(s => s.FilePath == absolutePath.ToLower());
+ }
+
+ public static void DeleteByFilePath(string absolutePath)
+ {
+ if(string.IsNullOrWhiteSpace(absolutePath))
+ throw new ArgumentNullException("absolutePath");
+ Contract.EndContractBlock();
+
+ FileState.Execute((session, instance) =>
+ {
+ const string hqlDelete = "delete FileState where FilePath = :path";
+ var deletedEntities = session.CreateQuery(hqlDelete)
+ .SetString("path", absolutePath.ToLower())
+ .ExecuteUpdate();
+ return null;
+ },null);
+
+ }
+
+ public static void ChangeRootPath(string oldPath,string newPath)
+ {
+ if (String.IsNullOrWhiteSpace(oldPath))
+ throw new ArgumentNullException("oldPath");
+ if (!Path.IsPathRooted(oldPath))
+ throw new ArgumentException("oldPath must be an absolute path", "oldPath");
+ if (string.IsNullOrWhiteSpace(newPath))
+ throw new ArgumentNullException("newPath");
+ if (!Path.IsPathRooted(newPath))
+ throw new ArgumentException("newPath must be an absolute path", "newPath");
+ Contract.EndContractBlock();
+
+ //Ensure the paths end with the same character
+ if (!oldPath.EndsWith("\\"))
+ oldPath = oldPath + "\\";
+ if (!newPath.EndsWith("\\"))
+ newPath = newPath + "\\";
+
+ using (new TransactionScope())
+ {
+ Execute((session, instance) =>
+ {
+ const string hqlUpdate =
+ "update FileState set FilePath = replace(FilePath,:oldPath,:newPath) where FilePath like :oldPath || '%' ";
+ var result=session.CreateQuery(hqlUpdate)
+ .SetString("oldPath", oldPath.ToLower())
+ .SetString("newPath", newPath.ToLower())
+ .ExecuteUpdate();
+ return null;
+ }, null);
+ }
+ }
+
+ public static Task<FileState> CreateForAsync(string filePath,int blockSize,string algorithm)
+ {
+ if (blockSize <= 0)
+ throw new ArgumentOutOfRangeException("blockSize");
+ if (String.IsNullOrWhiteSpace(algorithm))
+ throw new ArgumentNullException("algorithm");
+ Contract.EndContractBlock();
+
+
var fileState = new FileState
{
FilePath = filePath,
};
- return fileState.UpdateHashesAsync();
+ return fileState.UpdateHashesAsync(blockSize,algorithm);
}
- public Task<FileState> UpdateHashesAsync()
+ public Task<FileState> UpdateHashesAsync(int blockSize,string algorithm)
{
+ if (blockSize<=0)
+ throw new ArgumentOutOfRangeException("blockSize");
+ if (String.IsNullOrWhiteSpace(algorithm))
+ throw new ArgumentNullException("algorithm");
+ Contract.EndContractBlock();
+
//Skip updating the hash for folders
if (Directory.Exists(FilePath))
- return Task.Factory.StartNew(() => this);
-
- return Task.Factory.StartNew(() => { Checksum = Signature.CalculateMD5(FilePath); })
- .ContinueWith(
- t => this);
+ return Task.Factory.FromResult(this);
+
+ var results = Task.Factory.StartNew(() =>
+ {
+ var info = new FileInfo(FilePath);
+ return info.CalculateHash(blockSize, algorithm);
+ });
+
+ var state=results.Then(hash =>
+ {
+ Checksum = hash;
+ return Task.Factory.FromResult(this);
+ });
+
+ return state;
}
}