Modifications to delete folder contents
[pithos-ms-client] / trunk / Pithos.Core / Agents / FileAgent.cs
index 93da842..8218cc6 100644 (file)
@@ -14,17 +14,17 @@ using log4net.Core;
 
 namespace Pithos.Core.Agents
 {
-    [Export,PartCreationPolicy(CreationPolicy.NonShared)]
+//    [Export]
     public class FileAgent
     {
         Agent<WorkflowState> _agent;
         private FileSystemWatcher _watcher;
 
-        [Import]
+        //[Import]
         public IStatusKeeper StatusKeeper { get; set; }
-        [Import]
+        //[Import]
         public IPithosWorkflow Workflow { get; set; }
-        [Import]
+        //[Import]
         public WorkflowAgent WorkflowAgent { get; set; }
 
         private AccountInfo AccountInfo { get; set; }
@@ -75,7 +75,8 @@ namespace Pithos.Core.Agents
                 throw new ArgumentNullException("state");
             Contract.EndContractBlock();
 
-            Debug.Assert(!Ignore(state.Path));
+            if (Ignore(state.Path))
+                return CompletedTask<object>.Default;
 
             var networkState = NetworkGate.GetNetworkState(state.Path);
             //Skip if the file is already being downloaded or uploaded and 
@@ -108,20 +109,12 @@ namespace Pithos.Core.Agents
             }
             catch (Exception exc)
             {
-                Log.WarnFormat("Error occured while indexing{0. The file will be skipped}\n{1}", state.Path, exc);
+                Log.WarnFormat("Error occured while indexing{0}. The file will be skipped\n{1}",
+                               state.Path, exc);
             }
             return CompletedTask<object>.Default;
         }
 
-
-/*
-        private Task Process(Task<WorkflowState> action)
-        {
-            return action.ContinueWith(t => Process(t.Result));
-        }
-*/
-
-
         public bool Pause
         {
             get { return _watcher == null || !_watcher.EnableRaisingEvents; }
@@ -132,7 +125,15 @@ namespace Pithos.Core.Agents
             }
         }
 
-        public string FragmentsPath { get; set; }
+        public string CachePath { get; set; }
+
+        private List<string> _selectivePaths = new List<string>();
+        public List<string> SelectivePaths
+        {
+            get { return _selectivePaths; }
+            set { _selectivePaths = value; }
+        }
+
 
         public void Post(WorkflowState workflowState)
         {
@@ -192,7 +193,10 @@ namespace Pithos.Core.Agents
 
         private bool Ignore(string filePath)
         {
-            if (filePath.StartsWith(FragmentsPath))
+            var pithosPath = Path.Combine(RootPath, "pithos");
+            if (pithosPath.Equals(filePath, StringComparison.InvariantCultureIgnoreCase))
+                return true;
+            if (filePath.StartsWith(CachePath))
                 return true;
             if (_ignoreFiles.ContainsKey(filePath.ToLower()))
                 return true;
@@ -202,11 +206,12 @@ namespace Pithos.Core.Agents
         //Post a Change message for all events except rename
         void OnFileEvent(object sender, FileSystemEventArgs e)
         {
-            //Ignore events that affect the Fragments folder
+            //Ignore events that affect the cache folder
             var filePath = e.FullPath;
             if (Ignore(filePath)) 
                 return;
-            _agent.Post(new WorkflowState(AccountInfo) { Path = filePath, FileName = e.Name, TriggeringChange = e.ChangeType });
+
+            _agent.Post(new WorkflowState{AccountInfo=AccountInfo, Path = filePath, FileName = e.Name, TriggeringChange = e.ChangeType });
         }
 
 
@@ -218,8 +223,9 @@ namespace Pithos.Core.Agents
             if (Ignore(oldFullPath) || Ignore(fullPath))
                 return;
 
-            _agent.Post(new WorkflowState(AccountInfo)
+            _agent.Post(new WorkflowState
             {
+                AccountInfo=AccountInfo,
                 OldPath = oldFullPath,
                 OldFileName = e.OldName,
                 Path = fullPath,
@@ -341,7 +347,7 @@ namespace Pithos.Core.Agents
             return false;
         }
 
-        public FileInfo GetFileInfo(string relativePath)
+        public FileSystemInfo GetFileSystemInfo(string relativePath)
         {
             if (String.IsNullOrWhiteSpace(relativePath))
                 throw new ArgumentNullException("relativePath");
@@ -351,20 +357,37 @@ namespace Pithos.Core.Agents
             Contract.EndContractBlock();            
 
             var absolutePath = Path.Combine(RootPath, relativePath);
-//            Debug.Assert(File.Exists(absolutePath),String.Format("Path {0} doesn't exist",absolutePath));
 
-            return new FileInfo(absolutePath);
+            if (Directory.Exists(absolutePath))
+                return new DirectoryInfo(absolutePath).WithProperCapitalization();
+            else
+                return new FileInfo(absolutePath).WithProperCapitalization();
             
         }
 
         public void Delete(string relativePath)
         {
-            var absolutePath = Path.Combine(RootPath, relativePath);
+            var absolutePath = Path.Combine(RootPath, relativePath).ToLower();
             if (File.Exists(absolutePath))
-            {                   
-                File.Delete(absolutePath);
-                _ignoreFiles[absolutePath.ToLower()] = absolutePath.ToLower();                
+            {    
+                try
+                {
+                    File.Delete(absolutePath);
+                }
+                //The file may have been deleted by another thread. Just ignore the relevant exception
+                catch (FileNotFoundException) { }
             }
+            else if (Directory.Exists(absolutePath))
+            {
+                try
+                {
+                    Directory.Delete(absolutePath, true);
+                }
+                //The directory may have been deleted by another thread. Just ignore the relevant exception
+                catch (DirectoryNotFoundException){}                
+            }
+        
+            //_ignoreFiles[absolutePath] = absolutePath;                
             StatusKeeper.ClearFileStatus(absolutePath);
         }
     }