2 using System.Collections.Generic;
3 using System.ComponentModel.Composition;
4 using System.Data.SQLite;
5 using System.Diagnostics;
6 using System.Diagnostics.Contracts;
10 using System.Threading;
11 using System.Threading.Tasks;
12 using Castle.ActiveRecord;
13 using Castle.ActiveRecord.Framework.Config;
14 using Pithos.Interfaces;
18 namespace Pithos.Core.Agents
20 [Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))]
21 public class StatusAgent:IStatusChecker,IStatusKeeper
23 [System.ComponentModel.Composition.Import]
24 public IPithosSettings Settings { get; set; }
26 private Agent<Action> _persistenceAgent;
29 private static readonly ILog Log = LogManager.GetLogger("StatusAgent");
33 var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
34 _pithosDataPath = Path.Combine(appDataPath , "Pithos");
36 if (!Directory.Exists(_pithosDataPath))
37 Directory.CreateDirectory(_pithosDataPath);
38 var source = GetConfiguration(_pithosDataPath);
39 ActiveRecordStarter.Initialize(source,typeof(FileState),typeof(FileTag));
40 ActiveRecordStarter.UpdateSchema();
43 if (!File.Exists(Path.Combine(_pithosDataPath ,"pithos.db")))
44 ActiveRecordStarter.CreateSchema();
50 private void CreateTrigger()
52 using (var connection = GetConnection())
53 using (var triggerCommand = connection.CreateCommand())
55 var cmdText = new StringBuilder()
56 .AppendLine("CREATE TRIGGER IF NOT EXISTS update_last_modified UPDATE ON FileState FOR EACH ROW")
58 .AppendLine("UPDATE FileState SET Modified=datetime('now') WHERE Id=old.Id;")
60 .AppendLine("CREATE TRIGGER IF NOT EXISTS insert_last_modified INSERT ON FileState FOR EACH ROW")
62 .AppendLine("UPDATE FileState SET Modified=datetime('now') WHERE Id=new.Id;")
65 triggerCommand.CommandText = cmdText;
66 triggerCommand.ExecuteNonQuery();
71 private static InPlaceConfigurationSource GetConfiguration(string pithosDbPath)
73 if (String.IsNullOrWhiteSpace(pithosDbPath))
74 throw new ArgumentNullException("pithosDbPath");
75 if (!Path.IsPathRooted(pithosDbPath))
76 throw new ArgumentException("path must be a rooted path", "pithosDbPath");
77 Contract.EndContractBlock();
79 var properties = new Dictionary<string, string>
81 {"connection.driver_class", "NHibernate.Driver.SQLite20Driver"},
82 {"dialect", "NHibernate.Dialect.SQLiteDialect"},
83 {"connection.provider", "NHibernate.Connection.DriverConnectionProvider"},
85 "proxyfactory.factory_class",
86 "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"
90 var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3;Enlist=N", pithosDbPath);
91 properties.Add("connection.connection_string", connectionString);
93 var source = new InPlaceConfigurationSource();
94 source.Add(typeof (ActiveRecordBase), properties);
95 source.SetDebugFlag(false);
99 public void StartProcessing(CancellationToken token)
101 _persistenceAgent = Agent<Action>.Start(queue =>
106 var job = queue.Receive();
107 job.ContinueWith(t =>
109 var action = job.Result;
114 catch (SQLiteException ex)
116 Log.ErrorFormat("[ERROR] SQL \n{0}", ex);
120 Log.ErrorFormat("[ERROR] STATE \n{0}", ex);
122 // ReSharper disable AccessToModifiedClosure
124 // ReSharper restore AccessToModifiedClosure
136 _persistenceAgent.Stop();
140 public void ProcessExistingFiles(IEnumerable<FileInfo> existingFiles)
142 if(existingFiles ==null)
143 throw new ArgumentNullException("existingFiles");
144 Contract.EndContractBlock();
146 //Find new or matching files with a left join to the stored states
147 var fileStates = FileState.Queryable;
148 var currentFiles=from file in existingFiles
149 join state in fileStates on file.FullName.ToLower() equals state.FilePath.ToLower() into gs
150 from substate in gs.DefaultIfEmpty()
151 select new {File = file, State = substate};
153 //To get the deleted files we must get the states that have no corresponding
155 //We can't use the File.Exists method inside a query, so we get all file paths from the states
156 var statePaths = (from state in fileStates
157 select new {state.Id, state.FilePath}).ToList();
159 var missingStates= (from path in statePaths
160 where !File.Exists(path.FilePath) && !Directory.Exists(path.FilePath)
161 select path.Id).ToList();
162 //Finally, retrieve the states that correspond to the deleted files
163 var deletedFiles = from state in fileStates
164 where missingStates.Contains(state.Id)
165 select new { File = default(FileInfo), State = state };
167 var pairs = currentFiles.Union(deletedFiles);
169 foreach(var pair in pairs)
171 var fileState = pair.State;
172 var file = pair.File;
173 if (fileState == null)
176 var fullPath = pair.File.FullName;
177 var createState = FileState.CreateForAsync(fullPath, BlockSize, BlockHash);
178 createState.ContinueWith(state => _persistenceAgent.Post(state.Result.Create));
180 else if (file == null)
182 //This file was deleted while we were down. We should mark it as deleted
183 //We have to go through UpdateStatus here because the state object we are using
184 //was created by a different ORM session.
185 _persistenceAgent.Post(()=> UpdateStatusDirect(fileState.Id, FileStatus.Deleted));
189 //This file has a matching state. Need to check for possible changes
190 var hashString = file.CalculateHash(BlockSize,BlockHash);
191 //If the hashes don't match the file was changed
192 if (fileState.Checksum != hashString)
194 _persistenceAgent.Post(() => UpdateStatusDirect(fileState.Id, FileStatus.Modified));
201 private int UpdateStatusDirect(Guid id, FileStatus status)
203 using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect"))
209 using (var connection = GetConnection())
211 var command = new SQLiteCommand("update FileState set FileStatus= :fileStatus where Id = :id ",
214 command.Parameters.AddWithValue("fileStatus", status);
216 command.Parameters.AddWithValue("id", id);
218 var affected = command.ExecuteNonQuery();
224 catch (Exception exc)
226 Log.Error(exc.ToString());
232 private int UpdateStatusDirect(string path, FileStatus status)
234 using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect"))
241 using (var connection = GetConnection())
244 new SQLiteCommand("update FileState set FileStatus= :fileStatus where FilePath = :path COLLATE NOCASE",
249 command.Parameters.AddWithValue("fileStatus", status);
251 command.Parameters.AddWithValue("path", path);
253 var affected = command.ExecuteNonQuery();
257 catch (Exception exc)
259 Log.Error(exc.ToString());
265 private int UpdateStatusDirect(string absolutePath, FileStatus fileStatus, FileOverlayStatus overlayStatus)
267 using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect"))
274 using (var connection = GetConnection())
278 "update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus where FilePath = :path COLLATE NOCASE ",
282 command.Parameters.AddWithValue("path", absolutePath);
283 command.Parameters.AddWithValue("fileStatus", fileStatus);
284 command.Parameters.AddWithValue("overlayStatus", overlayStatus);
286 var affected = command.ExecuteNonQuery();
290 catch (Exception exc)
292 Log.Error(exc.ToString());
300 public string BlockHash { get; set; }
302 public int BlockSize { get; set; }
303 public void ChangeRoots(string oldPath, string newPath)
305 if (String.IsNullOrWhiteSpace(oldPath))
306 throw new ArgumentNullException("oldPath");
307 if (!Path.IsPathRooted(oldPath))
308 throw new ArgumentException("oldPath must be an absolute path", "oldPath");
309 if (string.IsNullOrWhiteSpace(newPath))
310 throw new ArgumentNullException("newPath");
311 if (!Path.IsPathRooted(newPath))
312 throw new ArgumentException("newPath must be an absolute path", "newPath");
313 Contract.EndContractBlock();
315 FileState.ChangeRootPath(oldPath,newPath);
319 private PithosStatus _pithosStatus=PithosStatus.InSynch;
321 public void SetPithosStatus(PithosStatus status)
323 _pithosStatus = status;
326 public PithosStatus GetPithosStatus()
328 return _pithosStatus;
332 private readonly string _pithosDataPath;
335 public FileState GetStateByFilePath(string path)
337 if (String.IsNullOrWhiteSpace(path))
338 throw new ArgumentNullException("path");
339 if (!Path.IsPathRooted(path))
340 throw new ArgumentException("The path must be rooted", "path");
341 Contract.EndContractBlock();
346 using (var connection = GetConnection())
347 using (var command = new SQLiteCommand("select Id, FilePath, OverlayStatus,FileStatus ,Checksum ,Version ,VersionTimeStamp,IsShared ,SharedBy ,ShareWrite from FileState where FilePath=:path COLLATE NOCASE", connection))
350 command.Parameters.AddWithValue("path", path);
352 using (var reader = command.ExecuteReader())
356 //var values = new object[reader.FieldCount];
357 //reader.GetValues(values);
358 var state = new FileState
360 Id = reader.GetGuid(0),
361 FilePath = reader.IsDBNull(1)?"":reader.GetString(1),
362 OverlayStatus =reader.IsDBNull(2)?FileOverlayStatus.Unversioned: (FileOverlayStatus) reader.GetInt64(2),
363 FileStatus = reader.IsDBNull(3)?FileStatus.Missing:(FileStatus) reader.GetInt64(3),
364 Checksum = reader.IsDBNull(4)?"":reader.GetString(4),
365 Version = reader.IsDBNull(5)?default(long):reader.GetInt64(5),
366 VersionTimeStamp = reader.IsDBNull(6)?default(DateTime):reader.GetDateTime(6),
367 IsShared = !reader.IsDBNull(7) && reader.GetBoolean(7),
368 SharedBy = reader.IsDBNull(8)?"":reader.GetString(8),
369 ShareWrite = !reader.IsDBNull(9) && reader.GetBoolean(9)
372 var state = new FileState
374 Id = (Guid) values[0],
375 FilePath = (string) values[1],
376 OverlayStatus = (FileOverlayStatus) (long)values[2],
377 FileStatus = (FileStatus) (long)values[3],
378 Checksum = (string) values[4],
379 Version = (long?) values[5],
380 VersionTimeStamp = (DateTime?) values[6],
381 IsShared = (long)values[7] == 1,
382 SharedBy = (string) values[8],
383 ShareWrite = (long)values[9] == 1
396 catch (Exception exc)
398 Log.ErrorFormat(exc.ToString());
403 public FileOverlayStatus GetFileOverlayStatus(string path)
405 if (String.IsNullOrWhiteSpace(path))
406 throw new ArgumentNullException("path");
407 if (!Path.IsPathRooted(path))
408 throw new ArgumentException("The path must be rooted", "path");
409 Contract.EndContractBlock();
414 using (var connection = GetConnection())
415 using (var command = new SQLiteCommand("select OverlayStatus from FileState where FilePath=:path COLLATE NOCASE", connection))
418 command.Parameters.AddWithValue("path", path);
420 var s = command.ExecuteScalar();
421 return (FileOverlayStatus) Convert.ToInt32(s);
424 catch (Exception exc)
426 Log.ErrorFormat(exc.ToString());
427 return FileOverlayStatus.Unversioned;
431 private string GetConnectionString()
433 var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3;Enlist=N;Pooling=True", _pithosDataPath);
434 return connectionString;
437 private SQLiteConnection GetConnection()
439 var connectionString = GetConnectionString();
440 var connection = new SQLiteConnection(connectionString);
442 using(var cmd =connection.CreateCommand())
444 cmd.CommandText = "PRAGMA journal_mode=WAL";
445 cmd.ExecuteNonQuery();
450 public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
452 if (String.IsNullOrWhiteSpace(path))
453 throw new ArgumentNullException("path");
454 if (!Path.IsPathRooted(path))
455 throw new ArgumentException("The path must be rooted","path");
456 Contract.EndContractBlock();
458 _persistenceAgent.Post(() => FileState.StoreOverlayStatus(path,overlayStatus));
461 /* public void RenameFileOverlayStatus(string oldPath, string newPath)
463 if (String.IsNullOrWhiteSpace(oldPath))
464 throw new ArgumentNullException("oldPath");
465 if (!Path.IsPathRooted(oldPath))
466 throw new ArgumentException("The oldPath must be rooted", "oldPath");
467 if (String.IsNullOrWhiteSpace(newPath))
468 throw new ArgumentNullException("newPath");
469 if (!Path.IsPathRooted(newPath))
470 throw new ArgumentException("The newPath must be rooted", "newPath");
471 Contract.EndContractBlock();
473 _persistenceAgent.Post(() =>FileState.RenameState(oldPath, newPath));
476 public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
478 if (String.IsNullOrWhiteSpace(path))
479 throw new ArgumentNullException("path");
480 if (!Path.IsPathRooted(path))
481 throw new ArgumentException("The path must be rooted", "path");
482 Contract.EndContractBlock();
484 Debug.Assert(!path.Contains(FolderConstants.CacheFolder));
485 Debug.Assert(!path.EndsWith(".ignore"));
487 _persistenceAgent.Post(() => UpdateStatusDirect(path, fileStatus, overlayStatus));
491 public void StoreInfo(string path,ObjectInfo objectInfo)
493 if (String.IsNullOrWhiteSpace(path))
494 throw new ArgumentNullException("path");
495 if (!Path.IsPathRooted(path))
496 throw new ArgumentException("The path must be rooted", "path");
497 if (objectInfo == null)
498 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
499 Contract.EndContractBlock();
501 _persistenceAgent.Post(() =>
503 var filePath = path.ToLower();
504 //Load the existing files state and set its properties in one session
505 using (new SessionScope())
507 //Forgetting to use a sessionscope results in two sessions being created, one by
508 //FirstOrDefault and one by Save()
509 var state =FileState.FindByFilePath(filePath);
511 //Create a new empty state object if this is a new file
512 state = state ?? new FileState();
514 state.FilePath = filePath;
515 state.Checksum = objectInfo.Hash;
516 state.Version = objectInfo.Version;
517 state.VersionTimeStamp = objectInfo.VersionTimestamp;
519 state.FileStatus = FileStatus.Unchanged;
520 state.OverlayStatus = FileOverlayStatus.Normal;
531 public void StoreInfo(string path, ObjectInfo objectInfo)
533 if (String.IsNullOrWhiteSpace(path))
534 throw new ArgumentNullException("path");
535 if (!Path.IsPathRooted(path))
536 throw new ArgumentException("The path must be rooted", "path");
537 if (objectInfo == null)
538 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
539 Contract.EndContractBlock();
541 _persistenceAgent.Post(() => StoreInfoDirect(path, objectInfo));
545 private void StoreInfoDirect(string path, ObjectInfo objectInfo)
550 using (var connection = GetConnection())
551 using (var command = new SQLiteCommand(connection))
553 if (StateExists(path, connection))
554 command.CommandText =
555 "update FileState set FileStatus= :fileStatus where FilePath = :path COLLATE NOCASE ";
558 command.CommandText =
559 "INSERT INTO FileState (Id,FilePath,Checksum,Version,VersionTimeStamp,FileStatus,OverlayStatus) VALUES (:id,:path,:checksum,:version,:versionTimeStamp,:fileStatus,:overlayStatus)";
560 command.Parameters.AddWithValue("id", Guid.NewGuid());
563 command.Parameters.AddWithValue("path", path);
564 command.Parameters.AddWithValue("checksum", objectInfo.Hash);
565 command.Parameters.AddWithValue("version", objectInfo.Version);
566 command.Parameters.AddWithValue("versionTimeStamp",
567 objectInfo.VersionTimestamp);
568 command.Parameters.AddWithValue("fileStatus", FileStatus.Unchanged);
569 command.Parameters.AddWithValue("overlayStatus",
570 FileOverlayStatus.Normal);
572 var affected = command.ExecuteNonQuery();
576 catch (Exception exc)
578 Log.Error(exc.ToString());
583 private bool StateExists(string filePath,SQLiteConnection connection)
585 using (var command = new SQLiteCommand("Select count(*) from FileState where FilePath=:path COLLATE NOCASE", connection))
587 command.Parameters.AddWithValue("path", filePath);
588 var result = command.ExecuteScalar();
589 return ((long)result >= 1);
594 public void SetFileStatus(string path, FileStatus status)
596 if (String.IsNullOrWhiteSpace(path))
597 throw new ArgumentNullException("path");
598 if (!Path.IsPathRooted(path))
599 throw new ArgumentException("The path must be rooted", "path");
600 Contract.EndContractBlock();
602 _persistenceAgent.Post(() => UpdateStatusDirect(path, status));
605 public FileStatus GetFileStatus(string path)
607 if (String.IsNullOrWhiteSpace(path))
608 throw new ArgumentNullException("path");
609 if (!Path.IsPathRooted(path))
610 throw new ArgumentException("The path must be rooted", "path");
611 Contract.EndContractBlock();
614 using (var connection = GetConnection())
616 var command = new SQLiteCommand("select FileStatus from FileState where FilePath=:path COLLATE NOCASE", connection);
617 command.Parameters.AddWithValue("path", path);
619 var statusValue = command.ExecuteScalar();
620 if (statusValue==null)
621 return FileStatus.Missing;
622 return (FileStatus)Convert.ToInt32(statusValue);
627 /// Deletes the status of the specified file
629 /// <param name="path"></param>
630 public void ClearFileStatus(string path)
632 if (String.IsNullOrWhiteSpace(path))
633 throw new ArgumentNullException("path");
634 if (!Path.IsPathRooted(path))
635 throw new ArgumentException("The path must be rooted", "path");
636 Contract.EndContractBlock();
638 _persistenceAgent.Post(() => DeleteDirect(path));
642 /// Deletes the status of the specified folder and all its contents
644 /// <param name="path"></param>
645 public void ClearFolderStatus(string path)
647 if (String.IsNullOrWhiteSpace(path))
648 throw new ArgumentNullException("path");
649 if (!Path.IsPathRooted(path))
650 throw new ArgumentException("The path must be rooted", "path");
651 Contract.EndContractBlock();
653 _persistenceAgent.Post(() => DeleteFolderDirect(path));
656 public IEnumerable<FileState> GetChildren(FileState fileState)
658 if (fileState == null)
659 throw new ArgumentNullException("fileState");
660 Contract.EndContractBlock();
662 var children = from state in FileState.Queryable
663 where state.FilePath.StartsWith(fileState.FilePath + "\\")
668 private int DeleteDirect(string filePath)
670 using (log4net.ThreadContext.Stacks["StatusAgent"].Push("DeleteDirect"))
677 using (var connection = GetConnection())
679 var command = new SQLiteCommand("delete from FileState where FilePath = :path COLLATE NOCASE",
682 command.Parameters.AddWithValue("path", filePath);
684 var affected = command.ExecuteNonQuery();
688 catch (Exception exc)
690 Log.Error(exc.ToString());
696 private int DeleteFolderDirect(string filePath)
698 using (log4net.ThreadContext.Stacks["StatusAgent"].Push("DeleteDirect"))
705 using (var connection = GetConnection())
707 var command = new SQLiteCommand("delete from FileState where FilePath = :path or FilePath like :path + '/%' COLLATE NOCASE",
710 command.Parameters.AddWithValue("path", filePath);
712 var affected = command.ExecuteNonQuery();
716 catch (Exception exc)
718 Log.Error(exc.ToString());
724 public void UpdateFileChecksum(string path, string checksum)
726 if (String.IsNullOrWhiteSpace(path))
727 throw new ArgumentNullException("path");
728 if (!Path.IsPathRooted(path))
729 throw new ArgumentException("The path must be rooted", "path");
730 Contract.EndContractBlock();
732 _persistenceAgent.Post(() => FileState.UpdateChecksum(path, checksum));