+ 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 shortHash, 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, shortHash,checksum));
+ }
+
+
+ public void CleanupOrphanStates()
+ {
+ //Orphan states are those that do not correspond to an account, ie. their paths
+ //do not start with the root path of any registered account
+
+ var roots=(from account in Settings.Accounts
+ select account.RootPath).ToList();
+
+ var allStates = from state in FileState.Queryable
+ select state.FilePath;
+
+ foreach (var statePath in allStates)
+ {
+ if (!roots.Any(root=>statePath.StartsWith(root,StringComparison.InvariantCultureIgnoreCase)))
+ this.DeleteDirect(statePath);
+ }
+ }
+
+ public void CleanupStaleStates(AccountInfo accountInfo, List<ObjectInfo> objectInfos)
+ {
+ if (accountInfo == null)
+ throw new ArgumentNullException("accountInfo");
+ if (objectInfos == null)
+ throw new ArgumentNullException("objectInfos");
+ Contract.EndContractBlock();
+
+
+
+ //Stale states are those that have no corresponding local or server file
+
+
+ var agent=FileAgent.GetFileAgent(accountInfo);
+
+ var localFiles=agent.EnumerateFiles();
+ var localSet = new HashSet<string>(localFiles);
+
+ //RelativeUrlToFilePath will fail for
+ //infos of accounts, containers which have no Name
+
+ var serverFiles = from info in objectInfos
+ where info.Name != null
+ select Path.Combine(accountInfo.AccountPath,info.RelativeUrlToFilePath(accountInfo.UserName));
+ var serverSet = new HashSet<string>(serverFiles);
+
+ var allStates = from state in FileState.Queryable
+ where state.FilePath.StartsWith(agent.RootPath)
+ select state.FilePath;
+ var stateSet = new HashSet<string>(allStates);
+ stateSet.ExceptWith(serverSet);
+ stateSet.ExceptWith(localSet);
+
+ foreach (var remainder in stateSet)
+ {
+ DeleteDirect(remainder);
+ }
+
+
+ }