using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.Data.SQLite; using System.Diagnostics; using System.Diagnostics.Contracts; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Castle.ActiveRecord; using Castle.ActiveRecord.Framework.Config; using Pithos.Interfaces; using Pithos.Network; using log4net; namespace Pithos.Core.Agents { [Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))] public class StatusAgent:IStatusChecker,IStatusKeeper { [System.ComponentModel.Composition.Import] public IPithosSettings Settings { get; set; } private Agent _persistenceAgent; private static readonly ILog Log = LogManager.GetLogger("StatusAgent"); public StatusAgent() { var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); _pithosDataPath = Path.Combine(appDataPath , "Pithos"); if (!Directory.Exists(_pithosDataPath)) Directory.CreateDirectory(_pithosDataPath); var source = GetConfiguration(_pithosDataPath); ActiveRecordStarter.Initialize(source,typeof(FileState),typeof(FileTag)); ActiveRecordStarter.UpdateSchema(); if (!File.Exists(Path.Combine(_pithosDataPath ,"pithos.db"))) ActiveRecordStarter.CreateSchema(); CreateTrigger(); } private void CreateTrigger() { using (var connection = GetConnection()) using (var triggerCommand = connection.CreateCommand()) { var cmdText = new StringBuilder() .AppendLine("CREATE TRIGGER IF NOT EXISTS update_last_modified UPDATE ON FileState FOR EACH ROW") .AppendLine("BEGIN") .AppendLine("UPDATE FileState SET Modified=datetime('now') WHERE Id=old.Id;") .AppendLine("END;") .AppendLine("CREATE TRIGGER IF NOT EXISTS insert_last_modified INSERT ON FileState FOR EACH ROW") .AppendLine("BEGIN") .AppendLine("UPDATE FileState SET Modified=datetime('now') WHERE Id=new.Id;") .AppendLine("END;") .ToString(); triggerCommand.CommandText = cmdText; triggerCommand.ExecuteNonQuery(); } } private static InPlaceConfigurationSource GetConfiguration(string pithosDbPath) { if (String.IsNullOrWhiteSpace(pithosDbPath)) throw new ArgumentNullException("pithosDbPath"); if (!Path.IsPathRooted(pithosDbPath)) throw new ArgumentException("path must be a rooted path", "pithosDbPath"); Contract.EndContractBlock(); var properties = new Dictionary { {"connection.driver_class", "NHibernate.Driver.SQLite20Driver"}, {"dialect", "NHibernate.Dialect.SQLiteDialect"}, {"connection.provider", "NHibernate.Connection.DriverConnectionProvider"}, { "proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" }, }; var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3;Enlist=N", pithosDbPath); properties.Add("connection.connection_string", connectionString); var source = new InPlaceConfigurationSource(); source.Add(typeof (ActiveRecordBase), properties); source.SetDebugFlag(false); return source; } public void StartProcessing(CancellationToken token) { _persistenceAgent = Agent.Start(queue => { Action loop = null; loop = () => { var job = queue.Receive(); job.ContinueWith(t => { var action = job.Result; try { action(); } catch (SQLiteException ex) { Log.ErrorFormat("[ERROR] SQL \n{0}", ex); } catch (Exception ex) { Log.ErrorFormat("[ERROR] STATE \n{0}", ex); } // ReSharper disable AccessToModifiedClosure queue.DoAsync(loop); // ReSharper restore AccessToModifiedClosure }); }; loop(); }); } public void Stop() { _persistenceAgent.Stop(); } public void ProcessExistingFiles(IEnumerable existingFiles) { if(existingFiles ==null) throw new ArgumentNullException("existingFiles"); Contract.EndContractBlock(); //Find new or matching files with a left join to the stored states var fileStates = FileState.Queryable; var currentFiles=from file in existingFiles join state in fileStates on file.FullName.ToLower() equals state.FilePath.ToLower() into gs from substate in gs.DefaultIfEmpty() select new {File = file, State = substate}; //To get the deleted files we must get the states that have no corresponding //files. //We can't use the File.Exists method inside a query, so we get all file paths from the states var statePaths = (from state in fileStates select new {state.Id, state.FilePath}).ToList(); //and check each one var missingStates= (from path in statePaths where !File.Exists(path.FilePath) && !Directory.Exists(path.FilePath) select path.Id).ToList(); //Finally, retrieve the states that correspond to the deleted files var deletedFiles = from state in fileStates where missingStates.Contains(state.Id) select new { File = default(FileInfo), State = state }; var pairs = currentFiles.Union(deletedFiles); foreach(var pair in pairs) { var fileState = pair.State; var file = pair.File; if (fileState == null) { //This is a new file var fullPath = pair.File.FullName; var createState = FileState.CreateForAsync(fullPath, BlockSize, BlockHash); createState.ContinueWith(state => _persistenceAgent.Post(state.Result.Create)); } else if (file == null) { //This file was deleted while we were down. We should mark it as deleted //We have to go through UpdateStatus here because the state object we are using //was created by a different ORM session. _persistenceAgent.Post(()=> UpdateStatusDirect(fileState.Id, FileStatus.Deleted)); } else { //This file has a matching state. Need to check for possible changes var hashString = file.CalculateHash(BlockSize,BlockHash); //If the hashes don't match the file was changed if (fileState.Checksum != hashString) { _persistenceAgent.Post(() => UpdateStatusDirect(fileState.Id, FileStatus.Modified)); } } }; } private int UpdateStatusDirect(Guid id, FileStatus status) { using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect")) { try { using (var connection = GetConnection()) using ( var command = new SQLiteCommand("update FileState set FileStatus= :fileStatus where Id = :id ", connection)) { command.Parameters.AddWithValue("fileStatus", status); command.Parameters.AddWithValue("id", id); var affected = command.ExecuteNonQuery(); return affected; } } catch (Exception exc) { Log.Error(exc.ToString()); throw; } } } private int UpdateStatusDirect(string path, FileStatus status) { using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect")) { try { using (var connection = GetConnection()) using ( var command = new SQLiteCommand("update FileState set FileStatus= :fileStatus where FilePath = :path COLLATE NOCASE", connection)) { command.Parameters.AddWithValue("fileStatus", status); command.Parameters.AddWithValue("path", path); var affected = command.ExecuteNonQuery(); return affected; } } catch (Exception exc) { Log.Error(exc.ToString()); throw; } } } private int UpdateStatusDirect(string absolutePath, FileStatus fileStatus, FileOverlayStatus overlayStatus) { using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect")) { try { using (var connection = GetConnection()) using ( var command = new SQLiteCommand( "update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus where FilePath = :path COLLATE NOCASE ", connection)) { command.Parameters.AddWithValue("path", absolutePath); command.Parameters.AddWithValue("fileStatus", fileStatus); command.Parameters.AddWithValue("overlayStatus", overlayStatus); var affected = command.ExecuteNonQuery(); return affected; } } catch (Exception exc) { Log.Error(exc.ToString()); throw; } } } public string BlockHash { get; set; } public int BlockSize { get; set; } public void ChangeRoots(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(); FileState.ChangeRootPath(oldPath,newPath); } private PithosStatus _pithosStatus=PithosStatus.InSynch; public void SetPithosStatus(PithosStatus status) { _pithosStatus = status; } public PithosStatus GetPithosStatus() { return _pithosStatus; } private readonly string _pithosDataPath; public FileState GetStateByFilePath(string path) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted", "path"); Contract.EndContractBlock(); try { using (var connection = GetConnection()) using (var command = new SQLiteCommand("select Id, FilePath, OverlayStatus,FileStatus ,Checksum ,Version ,VersionTimeStamp,IsShared ,SharedBy ,ShareWrite from FileState where FilePath=:path COLLATE NOCASE", connection)) { command.Parameters.AddWithValue("path", path); using (var reader = command.ExecuteReader()) { if (reader.Read()) { //var values = new object[reader.FieldCount]; //reader.GetValues(values); var state = new FileState { Id = reader.GetGuid(0), FilePath = reader.IsDBNull(1)?"":reader.GetString(1), OverlayStatus =reader.IsDBNull(2)?FileOverlayStatus.Unversioned: (FileOverlayStatus) reader.GetInt64(2), FileStatus = reader.IsDBNull(3)?FileStatus.Missing:(FileStatus) reader.GetInt64(3), Checksum = reader.IsDBNull(4)?"":reader.GetString(4), Version = reader.IsDBNull(5)?default(long):reader.GetInt64(5), VersionTimeStamp = reader.IsDBNull(6)?default(DateTime):reader.GetDateTime(6), IsShared = !reader.IsDBNull(7) && reader.GetBoolean(7), SharedBy = reader.IsDBNull(8)?"":reader.GetString(8), ShareWrite = !reader.IsDBNull(9) && reader.GetBoolean(9) }; /* var state = new FileState { Id = (Guid) values[0], FilePath = (string) values[1], OverlayStatus = (FileOverlayStatus) (long)values[2], FileStatus = (FileStatus) (long)values[3], Checksum = (string) values[4], Version = (long?) values[5], VersionTimeStamp = (DateTime?) values[6], IsShared = (long)values[7] == 1, SharedBy = (string) values[8], ShareWrite = (long)values[9] == 1 }; */ return state; } else { return null; } } } } catch (Exception exc) { Log.ErrorFormat(exc.ToString()); throw; } } public FileOverlayStatus GetFileOverlayStatus(string path) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted", "path"); Contract.EndContractBlock(); try { using (var connection = GetConnection()) using (var command = new SQLiteCommand("select OverlayStatus from FileState where FilePath=:path COLLATE NOCASE", connection)) { command.Parameters.AddWithValue("path", path); var s = command.ExecuteScalar(); return (FileOverlayStatus) Convert.ToInt32(s); } } catch (Exception exc) { Log.ErrorFormat(exc.ToString()); return FileOverlayStatus.Unversioned; } } private string GetConnectionString() { var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3;Enlist=N;Pooling=True", _pithosDataPath); return connectionString; } private SQLiteConnection GetConnection() { var connectionString = GetConnectionString(); var connection = new SQLiteConnection(connectionString); connection.Open(); using(var cmd =connection.CreateCommand()) { cmd.CommandText = "PRAGMA journal_mode=WAL"; cmd.ExecuteNonQuery(); } return connection; } public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted","path"); Contract.EndContractBlock(); _persistenceAgent.Post(() => FileState.StoreOverlayStatus(path,overlayStatus)); } /* public void RenameFileOverlayStatus(string oldPath, string newPath) { if (String.IsNullOrWhiteSpace(oldPath)) throw new ArgumentNullException("oldPath"); if (!Path.IsPathRooted(oldPath)) throw new ArgumentException("The oldPath must be rooted", "oldPath"); if (String.IsNullOrWhiteSpace(newPath)) throw new ArgumentNullException("newPath"); if (!Path.IsPathRooted(newPath)) throw new ArgumentException("The newPath must be rooted", "newPath"); Contract.EndContractBlock(); _persistenceAgent.Post(() =>FileState.RenameState(oldPath, newPath)); }*/ public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted", "path"); Contract.EndContractBlock(); Debug.Assert(!path.Contains(FolderConstants.CacheFolder)); Debug.Assert(!path.EndsWith(".ignore")); _persistenceAgent.Post(() => UpdateStatusDirect(path, fileStatus, overlayStatus)); } /* public void StoreInfo(string path,ObjectInfo objectInfo) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted", "path"); if (objectInfo == null) throw new ArgumentNullException("objectInfo", "objectInfo can't be empty"); Contract.EndContractBlock(); _persistenceAgent.Post(() => { var filePath = path.ToLower(); //Load the existing files state and set its properties in one session using (new SessionScope()) { //Forgetting to use a sessionscope results in two sessions being created, one by //FirstOrDefault and one by Save() var state =FileState.FindByFilePath(filePath); //Create a new empty state object if this is a new file state = state ?? new FileState(); state.FilePath = filePath; state.Checksum = objectInfo.Hash; state.Version = objectInfo.Version; state.VersionTimeStamp = objectInfo.VersionTimestamp; state.FileStatus = FileStatus.Unchanged; state.OverlayStatus = FileOverlayStatus.Normal; //Do the save state.Save(); } }); } */ public void StoreInfo(string path, ObjectInfo objectInfo) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted", "path"); if (objectInfo == null) throw new ArgumentNullException("objectInfo", "objectInfo can't be empty"); Contract.EndContractBlock(); _persistenceAgent.Post(() => StoreInfoDirect(path, objectInfo)); } private void StoreInfoDirect(string path, ObjectInfo objectInfo) { try { using (var connection = GetConnection()) using (var command = new SQLiteCommand(connection)) { if (StateExists(path, connection)) command.CommandText = "update FileState set FileStatus= :fileStatus where FilePath = :path COLLATE NOCASE "; else { command.CommandText = "INSERT INTO FileState (Id,FilePath,Checksum,Version,VersionTimeStamp,FileStatus,OverlayStatus) VALUES (:id,:path,:checksum,:version,:versionTimeStamp,:fileStatus,:overlayStatus)"; command.Parameters.AddWithValue("id", Guid.NewGuid()); } command.Parameters.AddWithValue("path", path); command.Parameters.AddWithValue("checksum", objectInfo.Hash); command.Parameters.AddWithValue("version", objectInfo.Version); command.Parameters.AddWithValue("versionTimeStamp", objectInfo.VersionTimestamp); command.Parameters.AddWithValue("fileStatus", FileStatus.Unchanged); command.Parameters.AddWithValue("overlayStatus", FileOverlayStatus.Normal); var affected = command.ExecuteNonQuery(); return; } } catch (Exception exc) { Log.Error(exc.ToString()); throw; } } private bool StateExists(string filePath,SQLiteConnection connection) { using (var command = new SQLiteCommand("Select count(*) from FileState where FilePath=:path COLLATE NOCASE", connection)) { command.Parameters.AddWithValue("path", filePath); var result = command.ExecuteScalar(); return ((long)result >= 1); } } public void SetFileStatus(string path, FileStatus status) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted", "path"); Contract.EndContractBlock(); _persistenceAgent.Post(() => UpdateStatusDirect(path, status)); } public FileStatus GetFileStatus(string path) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted", "path"); Contract.EndContractBlock(); using (var connection = GetConnection()) { var command = new SQLiteCommand("select FileStatus from FileState where FilePath=:path COLLATE NOCASE", connection); command.Parameters.AddWithValue("path", path); var statusValue = command.ExecuteScalar(); if (statusValue==null) return FileStatus.Missing; return (FileStatus)Convert.ToInt32(statusValue); } } /// /// Deletes the status of the specified file /// /// public void ClearFileStatus(string path) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted", "path"); Contract.EndContractBlock(); _persistenceAgent.Post(() => DeleteDirect(path)); } /// /// Deletes the status of the specified folder and all its contents /// /// public void ClearFolderStatus(string path) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted", "path"); Contract.EndContractBlock(); _persistenceAgent.Post(() => DeleteFolderDirect(path)); } public IEnumerable GetChildren(FileState fileState) { if (fileState == null) throw new ArgumentNullException("fileState"); Contract.EndContractBlock(); var children = from state in FileState.Queryable where state.FilePath.StartsWith(fileState.FilePath + "\\") select state; return children; } private int DeleteDirect(string filePath) { using (log4net.ThreadContext.Stacks["StatusAgent"].Push("DeleteDirect")) { try { using (var connection = GetConnection()) { var command = new SQLiteCommand("delete from FileState where FilePath = :path COLLATE NOCASE", connection); command.Parameters.AddWithValue("path", filePath); var affected = command.ExecuteNonQuery(); return affected; } } catch (Exception exc) { Log.Error(exc.ToString()); throw; } } } private int DeleteFolderDirect(string filePath) { using (log4net.ThreadContext.Stacks["StatusAgent"].Push("DeleteDirect")) { try { using (var connection = GetConnection()) { var command = new SQLiteCommand("delete from FileState where FilePath = :path or FilePath like :path + '/%' COLLATE NOCASE", connection); command.Parameters.AddWithValue("path", filePath); var affected = command.ExecuteNonQuery(); return affected; } } catch (Exception exc) { Log.Error(exc.ToString()); throw; } } } public void UpdateFileChecksum(string path, string checksum) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); if (!Path.IsPathRooted(path)) throw new ArgumentException("The path must be rooted", "path"); Contract.EndContractBlock(); _persistenceAgent.Post(() => FileState.UpdateChecksum(path, checksum)); } } }