Replaces AutoResetEvent with manual reset event
[pithos-ms-client] / trunk / Pithos.Core / Agents / NetworkAgent.cs
index f92376f..293b207 100644 (file)
-using System;
+// -----------------------------------------------------------------------
+// <copyright file="NetworkAgent.cs" company="GRNET">
+// Copyright 2011 GRNET S.A. All rights reserved.
+// 
+// Redistribution and use in source and binary forms, with or
+// without modification, are permitted provided that the following
+// conditions are met:
+// 
+//   1. Redistributions of source code must retain the above
+//      copyright notice, this list of conditions and the following
+//      disclaimer.
+// 
+//   2. Redistributions in binary form must reproduce the above
+//      copyright notice, this list of conditions and the following
+//      disclaimer in the documentation and/or other materials
+//      provided with the distribution.
+// 
+// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+// 
+// The views and conclusions contained in the software and
+// documentation are those of the authors and should not be
+// interpreted as representing official policies, either expressed
+// or implied, of GRNET S.A.
+// </copyright>
+// -----------------------------------------------------------------------
+
+using System;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.ComponentModel.Composition;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.IO;
 using System.Linq;
-using System.Text;
+using System.Net;
+using System.Threading;
 using System.Threading.Tasks;
+using System.Threading.Tasks.Dataflow;
+using Castle.ActiveRecord;
 using Pithos.Interfaces;
 using Pithos.Network;
+using log4net;
 
 namespace Pithos.Core.Agents
 {
+    //TODO: Ensure all network operations use exact casing. Pithos is case sensitive
     [Export]
     public class NetworkAgent
     {
         private Agent<CloudAction> _agent;
 
-        [Import]
+        //A separate agent is used to execute delete actions immediatelly;
+        private ActionBlock<CloudDeleteAction> _deleteAgent;
+        readonly ConcurrentDictionary<string,DateTime> _deletedFiles=new ConcurrentDictionary<string, DateTime>();
+
+
+        private readonly ManualResetEventSlim _pauseAgent = new ManualResetEventSlim(true);
+
+        [System.ComponentModel.Composition.Import]
         public IStatusKeeper StatusKeeper { get; set; }
         
         public IStatusNotification StatusNotification { get; set; }
-        [Import]
-        public ICloudClient CloudClient { get; set; }
 
-        [Import]
-        public FileAgent FileAgent {get;set;}
+        private static readonly ILog Log = LogManager.GetLogger("NetworkAgent");
 
-        /*
-        [Import]
-        public IPithosWorkflow Workflow { get; set; }
-*/
+        private readonly ConcurrentBag<AccountInfo> _accounts = new ConcurrentBag<AccountInfo>();
 
+        public void Start()
+        {
 
-        private string _pithosContainer;
-        private string _trashContainer;
+            _agent = Agent<CloudAction>.Start(inbox =>
+            {
+                Action loop = null;
+                loop = () =>
+                {
+                    _pauseAgent.Wait();
+                    var message = inbox.Receive();
+                    var process=message.Then(Process,inbox.CancellationToken);
+                    inbox.LoopAsync(process, loop);
+                };
+                loop();
+            });
 
-        private int _blockSize;
-        private string _blockHash;
+            _deleteAgent = new ActionBlock<CloudDeleteAction>(message =>ProcessDelete(message),new ExecutionDataflowBlockOptions{MaxDegreeOfParallelism=4});
+            /*
+                Action loop = null;
+                loop = () =>
+                            {
+                                var message = inbox.Receive();
+                                var process = message.Then(ProcessDelete,inbox.CancellationToken);
+                                inbox.LoopAsync(process, loop);
+                            };
+                loop();
+*/
 
+        }
 
-        public void Start(string pithosContainer, string trashContainer, int blockSize, string blockHash)
+        private async Task Process(CloudAction action)
         {
-            if (String.IsNullOrWhiteSpace(pithosContainer))
-                throw new ArgumentNullException("pithosContainer");
-            if (String.IsNullOrWhiteSpace(trashContainer))
-                throw new ArgumentNullException("trashContainer");
+            if (action == null)
+                throw new ArgumentNullException("action");
+            if (action.AccountInfo==null)
+                throw new ArgumentException("The action.AccountInfo is empty","action");
             Contract.EndContractBlock();
 
-            _pithosContainer = pithosContainer;
-            _trashContainer = trashContainer;
-            _blockSize = blockSize;
-            _blockHash = blockHash;
+            var accountInfo = action.AccountInfo;
 
+            using (log4net.ThreadContext.Stacks["NETWORK"].Push("PROCESS"))
+            {                
+                Log.InfoFormat("[ACTION] Start Processing {0}", action);
 
-            _agent = Agent<CloudAction>.Start(inbox =>
-            {
-                Action loop = null;
-                loop = () =>
+                var cloudFile = action.CloudFile;
+                var downloadPath = action.GetDownloadPath();
+
+                try
                 {
-                    var message = inbox.Receive();
-                    var process = message.ContinueWith(t =>
+
+                    if (action.Action == CloudActionType.DeleteCloud)
                     {
-                        var action = t.Result;
-                        
-                        Process(action);
-                        inbox.DoAsync(loop);
-                    });
-                    process.ContinueWith(t =>
+                        //Redirect deletes to the delete agent 
+                        _deleteAgent.Post((CloudDeleteAction)action);                        
+                    }
+                    if (IsDeletedFile(action))
                     {
-                        inbox.DoAsync(loop);
-                        if (t.IsFaulted)
+                        //Clear the status of already deleted files to avoid reprocessing
+                        if (action.LocalFile != null)
+                            this.StatusKeeper.ClearFileStatus(action.LocalFile.FullName);
+                    }
+                    else
+                    {
+                        switch (action.Action)
                         {
-                            var ex = t.Exception.InnerException;
-                            if (ex is OperationCanceledException)
-                                inbox.Stop();
+                            case CloudActionType.UploadUnconditional:
+                                //Abort if the file was deleted before we reached this point
+                                await UploadCloudFile(action);
+                                break;
+                            case CloudActionType.DownloadUnconditional:
+                                await DownloadCloudFile(accountInfo, cloudFile, downloadPath);
+                                break;
+                            case CloudActionType.RenameCloud:
+                                var moveAction = (CloudMoveAction) action;
+                                RenameCloudFile(accountInfo, moveAction);
+                                break;
+                            case CloudActionType.MustSynch:
+                                if (!File.Exists(downloadPath) && !Directory.Exists(downloadPath))
+                                {
+                                    await DownloadCloudFile(accountInfo, cloudFile, downloadPath);
+                                }
+                                else
+                                {
+                                    await SyncFiles(accountInfo, action);
+                                }
+                                break;
                         }
-                    });
+                    }
+                    Log.InfoFormat("[ACTION] End Processing {0}:{1}->{2}", action.Action, action.LocalFile,
+                                           action.CloudFile.Name);
+                }
+                catch (WebException exc)
+                {
+                    Log.ErrorFormat("[WEB ERROR] {0} : {1} -> {2} due to exception\r\n{3}", action.Action, action.LocalFile, action.CloudFile, exc);
+                }
+                catch (OperationCanceledException)
+                {
+                    throw;
+                }
+                catch (DirectoryNotFoundException)
+                {
+                    Log.ErrorFormat("{0} : {1} -> {2}  failed because the directory was not found.\n Rescheduling a delete",
+                        action.Action, action.LocalFile, action.CloudFile);
+                    //Post a delete action for the missing file
+                    Post(new CloudDeleteAction(action));                    
+                }
+                catch (FileNotFoundException)
+                {
+                    Log.ErrorFormat("{0} : {1} -> {2}  failed because the file was not found.\n Rescheduling a delete",
+                        action.Action, action.LocalFile, action.CloudFile);
+                    //Post a delete action for the missing file
+                    Post(new CloudDeleteAction(action));
+                }
+                catch (Exception exc)
+                {
+                    Log.ErrorFormat("[REQUEUE] {0} : {1} -> {2} due to exception\r\n{3}",
+                                     action.Action, action.LocalFile, action.CloudFile, exc);
 
-                };
-                loop();
-            });
+                    _agent.Post(action);
+                }                
+            }
+        }
+
+        /// <summary>
+        /// Processes cloud delete actions
+        /// </summary>
+        /// <param name="action">The delete action to execute</param>
+        /// <returns></returns>
+        /// <remarks>
+        /// When a file/folder is deleted locally, we must delete it ASAP from the server and block any download
+        /// operations that may be in progress.
+        /// <para>
+        /// A separate agent is used to process deletes because the main agent may be busy with a long operation.
+        /// </para>
+        /// </remarks>
+        private async Task ProcessDelete(CloudDeleteAction action)
+        {
+            if (action == null)
+                throw new ArgumentNullException("action");
+            if (action.AccountInfo==null)
+                throw new ArgumentException("The action.AccountInfo is empty","action");
+            Contract.EndContractBlock();
+
+            var accountInfo = action.AccountInfo;
+
+            using (log4net.ThreadContext.Stacks["NETWORK"].Push("PROCESS"))
+            {                
+                Log.InfoFormat("[ACTION] Start Processing {0}", action);
+
+                var cloudFile = action.CloudFile;
+
+                try
+                {
+                    //Acquire a lock on the deleted file to prevent uploading/downloading operations from the normal
+                    //agent
+                    using (var gate = NetworkGate.Acquire(action.LocalFile.FullName, NetworkOperation.Deleting))
+                    {
+
+                        //Add the file URL to the deleted files list
+                        var key = GetFileKey(action.CloudFile);
+                        _deletedFiles[key] = DateTime.Now;
+
+                        _pauseAgent.Reset();
+                        // and then delete the file from the server
+                        DeleteCloudFile(accountInfo, cloudFile);
+
+                        Log.InfoFormat("[ACTION] End Delete {0}:{1}->{2}", action.Action, action.LocalFile,
+                                       action.CloudFile.Name);
+                    }
+                }
+                catch (WebException exc)
+                {
+                    Log.ErrorFormat("[WEB ERROR] {0} : {1} -> {2} due to exception\r\n{3}", action.Action, action.LocalFile, action.CloudFile, exc);
+                }
+                catch (OperationCanceledException)
+                {
+                    throw;
+                }
+                catch (DirectoryNotFoundException)
+                {
+                    Log.ErrorFormat("{0} : {1} -> {2}  failed because the directory was not found.\n Rescheduling a delete",
+                        action.Action, action.LocalFile, action.CloudFile);
+                    //Repost a delete action for the missing file
+                    _deleteAgent.Post(action);
+                }
+                catch (FileNotFoundException)
+                {
+                    Log.ErrorFormat("{0} : {1} -> {2}  failed because the file was not found.\n Rescheduling a delete",
+                        action.Action, action.LocalFile, action.CloudFile);
+                    //Post a delete action for the missing file
+                    _deleteAgent.Post(action);
+                }
+                catch (Exception exc)
+                {
+                    Log.ErrorFormat("[REQUEUE] {0} : {1} -> {2} due to exception\r\n{3}",
+                                     action.Action, action.LocalFile, action.CloudFile, exc);
+
+                    _deleteAgent.Post(action);
+                }
+                finally
+                {
+                    //Set the event when all delete actions are processed
+                    if (_deleteAgent.InputCount == 0)
+                        _pauseAgent.Set();
+
+                }
+            }
         }
 
+        private static string GetFileKey(ObjectInfo info)
+        {
+            var key = String.Format("{0}/{1}/{2}", info.Account, info.Container,info.Name);
+            return key;
+        }
+
+        private async Task SyncFiles(AccountInfo accountInfo,CloudAction action)
+        {
+            if (accountInfo == null)
+                throw new ArgumentNullException("accountInfo");
+            if (action==null)
+                throw new ArgumentNullException("action");
+            if (action.LocalFile==null)
+                throw new ArgumentException("The action's local file is not specified","action");
+            if (!Path.IsPathRooted(action.LocalFile.FullName))
+                throw new ArgumentException("The action's local file path must be absolute","action");
+            if (action.CloudFile== null)
+                throw new ArgumentException("The action's cloud file is not specified", "action");
+            Contract.EndContractBlock();
+
+            var localFile = action.LocalFile;
+            var cloudFile = action.CloudFile;
+            var downloadPath=action.LocalFile.GetProperCapitalization();
+
+            var cloudHash = cloudFile.Hash.ToLower();
+            var localHash = action.LocalHash.Value.ToLower();
+            var topHash = action.TopHash.Value.ToLower();
+
+            //Not enough to compare only the local hashes, also have to compare the tophashes
+            
+            //If any of the hashes match, we are done
+            if ((cloudHash == localHash || cloudHash == topHash))
+            {
+                Log.InfoFormat("Skipping {0}, hashes match",downloadPath);
+                return;
+            }
+
+            //The hashes DON'T match. We need to sync
+            var lastLocalTime = localFile.LastWriteTime;
+            var lastUpTime = cloudFile.Last_Modified;
+            
+            //If the local file is newer upload it
+            if (lastUpTime <= lastLocalTime)
+            {
+                //It probably means it was changed while the app was down                        
+                UploadCloudFile(action);
+            }
+            else
+            {
+                //It the cloud file has a later date, it was modified by another user or computer.
+                //We need to check the local file's status                
+                var status = StatusKeeper.GetFileStatus(downloadPath);
+                switch (status)
+                {
+                    case FileStatus.Unchanged:                        
+                        //If the local file's status is Unchanged, we can go on and download the newer cloud file
+                        await DownloadCloudFile(accountInfo,cloudFile,downloadPath);
+                        break;
+                    case FileStatus.Modified:
+                        //If the local file is Modified, we may have a conflict. In this case we should mark the file as Conflict
+                        //We can't ensure that a file modified online since the last time will appear as Modified, unless we 
+                        //index all files before we start listening.                       
+                    case FileStatus.Created:
+                        //If the local file is Created, it means that the local and cloud files aren't related,
+                        // yet they have the same name.
+
+                        //In both cases we must mark the file as in conflict
+                        ReportConflict(downloadPath);
+                        break;
+                    default:
+                        //Other cases should never occur. Mark them as Conflict as well but log a warning
+                        ReportConflict(downloadPath);
+                        Log.WarnFormat("Unexcepted status {0} for file {1}->{2}", status,
+                                       downloadPath, action.CloudFile.Name);
+                        break;
+                }
+            }
+        }
+
+        private void ReportConflict(string downloadPath)
+        {
+            if (String.IsNullOrWhiteSpace(downloadPath))
+                throw new ArgumentNullException("downloadPath");
+            Contract.EndContractBlock();
+
+            StatusKeeper.SetFileOverlayStatus(downloadPath, FileOverlayStatus.Conflict);
+            var message = String.Format("Conflict detected for file {0}", downloadPath);
+            Log.Warn(message);
+            StatusNotification.NotifyChange(message, TraceLevel.Warning);
+        }
 
         public void Post(CloudAction cloudAction)
         {
             if (cloudAction == null)
                 throw new ArgumentNullException("cloudAction");
+            if (cloudAction.AccountInfo==null)
+                throw new ArgumentException("The CloudAction.AccountInfo is empty","cloudAction");
             Contract.EndContractBlock();
 
-            _agent.Post(cloudAction);
+            //If the action targets a local file, add a treehash calculation
+            if (!(cloudAction is CloudDeleteAction) && cloudAction.LocalFile as FileInfo != null)
+            {
+                var accountInfo = cloudAction.AccountInfo;
+                var localFile = (FileInfo) cloudAction.LocalFile;
+                if (localFile.Length > accountInfo.BlockSize)
+                    cloudAction.TopHash =
+                        new Lazy<string>(() => Signature.CalculateTreeHashAsync(localFile,
+                                                                                accountInfo.BlockSize,
+                                                                                accountInfo.BlockHash).Result
+                                                    .TopHash.ToHashString());
+                else
+                {
+                    cloudAction.TopHash = new Lazy<string>(() => cloudAction.LocalHash.Value);
+                }
+            }
+            else
+            {
+                //The hash for a directory is the empty string
+                cloudAction.TopHash = new Lazy<string>(() => String.Empty);
+            }
+            
+            if (cloudAction is CloudDeleteAction)
+                _deleteAgent.Post((CloudDeleteAction)cloudAction);
+            else
+                _agent.Post(cloudAction);
         }
 
-        class ObjectInfoByNameComparer:IEqualityComparer<ObjectInfo>
+       /* class ObjectInfoByNameComparer:IEqualityComparer<ObjectInfo>
         {
             public bool Equals(ObjectInfo x, ObjectInfo y)
             {
@@ -104,419 +423,706 @@ namespace Pithos.Core.Agents
             {
                 return obj.Name.ToLower().GetHashCode();
             }
-        }
+        }*/
 
-        public Task ProcessRemoteFiles(string accountPath,DateTime? since=null)
-        {            
-
-            Trace.CorrelationManager.StartLogicalOperation();
-            Trace.TraceInformation("[LISTENER] Scheduled");
-            var listObjects = Task.Factory.StartNewDelayed(10000).ContinueWith(t =>
-                CloudClient.ListObjects(_pithosContainer,since));
+        
 
-            DateTime nextSince = DateTime.Now.AddSeconds(-1);
+        //Remote files are polled periodically. Any changes are processed
+        public async Task ProcessRemoteFiles(DateTime? since = null)
+        {            
+            await TaskEx.Delay(TimeSpan.FromSeconds(10),_agent.CancellationToken);
 
-            var enqueueFiles = listObjects.ContinueWith(task =>
+            using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push("All accounts"))
             {
-                if (task.IsFaulted)
+
+                try
                 {
-                    //ListObjects failed at this point, need to reschedule
-                    Trace.TraceError("[FAIL] ListObjects in ProcessRemoteFiles with {0}", task.Exception);
-                    ProcessRemoteFiles(accountPath, since);
-                    return;
-                }
-                Trace.CorrelationManager.StartLogicalOperation("Listener");
-                Trace.TraceInformation("[LISTENER] Start Processing");
+                    //Next time we will check for all changes since the current check minus 1 second
+                    //This is done to ensure there are no discrepancies due to clock differences
+                    DateTime nextSince = DateTime.Now.AddSeconds(-1);
 
-                var remoteObjects = task.Result;
+                    var tasks = from accountInfo in _accounts
+                                select ProcessAccountFiles(accountInfo, since);
 
-                var remote=from info in remoteObjects
-                                  let name=info.Name
-                                  where !name.EndsWith(".ignore",StringComparison.InvariantCultureIgnoreCase) &&
-                                  !name.StartsWith("fragments/",StringComparison.InvariantCultureIgnoreCase)
-                                select info;
+                    await TaskEx.WhenAll(tasks.ToList());
 
-                var commonObjects = new List<Tuple<ObjectInfo, FileInfo,FileState>>();
-                var remoteOnly = new List<ObjectInfo>();
-                
-                //In order to avoid multiple iterations over the files, we iterate only once
-                //over the remote files
-                foreach (var objectInfo in remote)
+                    ProcessRemoteFiles(nextSince);
+                }
+                catch (Exception ex)
                 {
-                    var relativePath= objectInfo.Name.RelativeUrlToFilePath();// fileInfo.AsRelativeUrlTo(FileAgent.RootPath);
-                    //and remove any matching objects from the list, adding them to the commonObjects list
-                    if (FileAgent.Exists(relativePath))
-                    {
-                        var localFile = FileAgent.GetFileInfo(relativePath);
-                        var state=FileState.FindByFilePath(localFile.FullName);                        
-                        commonObjects.Add(Tuple.Create(objectInfo, localFile,state));
-                    }
-                    else
-                        //If there is no match we add them to the localFiles list
-                        remoteOnly.Add(objectInfo);
+                    Log.ErrorFormat("Error while processing accounts\r\n{0}",ex);
+                    //In case of failure retry with the same parameter
+                    ProcessRemoteFiles(since);
                 }
+                
 
-                //At the end of the iteration, the *remote* list will contain the files that exist 
-                //only on the server
+            }
+        }
 
-                //Remote files should be downloaded
-                var actionsForRemote = from upFile in remoteOnly
-                                       select new CloudAction(CloudActionType.DownloadUnconditional,upFile);
+        public async Task ProcessAccountFiles(AccountInfo accountInfo,DateTime? since=null)
+        {   
+            if (accountInfo==null)
+                throw new ArgumentNullException("accountInfo");
+            if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))
+                throw new ArgumentException("The AccountInfo.AccountPath is empty","accountInfo");
+            Contract.EndContractBlock();
 
-                //Common files should be checked on a per-case basis to detect differences, which is newer
-                var actionsForCommon = from pair in commonObjects
-                                       let objectInfo = pair.Item1
-                                       let localFile = pair.Item2
-                                       let state=pair.Item3
-                                       select new CloudAction(CloudActionType.MustSynch, 
-                                           localFile, objectInfo,state);
-                
+            using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName))
+            {
+                _pauseAgent.Wait();
 
-                //Collect all the actions
-                var allActions = actionsForRemote.Union(actionsForCommon);
+                Log.Info("Scheduled");
+                var client=new CloudFilesClient(accountInfo);
 
-                //And remove those that are already being processed by the agent
-                var distinctActions =allActions
-                        .Except(_agent.GetEnumerable(), new PithosMonitor.LocalFileComparer())
-                        .ToList();
+                var containers = client.ListContainers(accountInfo.UserName);
 
-                //Queue all the actions
-                _agent.AddFromEnumerable(distinctActions);
 
-                if(remoteOnly.Count>0)
-                    StatusNotification.NotifyChange(String.Format("Processing {0} new files", remoteOnly.Count));
+                CreateContainerFolders(accountInfo, containers);
 
-                Trace.TraceInformation("[LISTENER] End Processing");
-                Trace.CorrelationManager.StopLogicalOperation();
+                try
+                {
+                    
+                    //Get the list of server objects changed since the last check
+                    //The name of the container is passed as state in order to create a dictionary of tasks in a subsequent step
+                    var listObjects = from container in containers
+                                      select  Task<IList<ObjectInfo>>.Factory.StartNew(_ =>
+                                            client.ListObjects(accountInfo.UserName,container.Name, since),container.Name);
 
-            });
 
-            var loop = enqueueFiles.ContinueWith(t =>
-            {
-                if (t.IsFaulted)
-                {
-                    Trace.TraceError("[LISTENER] Exception: {0}", t.Exception);
+                    var listTasks = await Task.Factory.WhenAll(listObjects.ToArray());
+
+                    using (log4net.ThreadContext.Stacks["SCHEDULE"].Push("Process Results"))
+                    {
+                        var dict = listTasks.ToDictionary(t => t.AsyncState);
+
+                        //Get all non-trash objects. Remember, the container name is stored in AsyncState
+                        var remoteObjects = from objectList in listTasks
+                                            where (string) objectList.AsyncState != "trash"
+                                            from obj in objectList.Result
+                                            select obj;
+
+                        var trashObjects = dict["trash"].Result;
+                        //var sharedObjects = ((Task<IList<ObjectInfo>>) task.Result[2]).Result;
+
+                        //Items with the same name, hash may be both in the container and the trash
+                        //Don't delete items that exist in the container
+                        var realTrash = from trash in trashObjects
+                                        where
+                                            !remoteObjects.Any(
+                                                info => info.Name == trash.Name && info.Hash == trash.Hash)
+                                        select trash;
+                        ProcessDeletedFiles(accountInfo, realTrash);
+
+
+                        var remote = from info in remoteObjects
+                                     //.Union(sharedObjects)
+                                     let name = info.Name
+                                     where !name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase) &&
+                                           !name.StartsWith(FolderConstants.CacheFolder + "/",
+                                                            StringComparison.InvariantCultureIgnoreCase)
+                                     select info;
+
+                        //Create a list of actions from the remote files
+                        var allActions = ObjectsToActions(accountInfo, remote);
+
+                        //And remove those that are already being processed by the agent
+                        var distinctActions = allActions
+                            .Except(_agent.GetEnumerable(), new PithosMonitor.LocalFileComparer())
+                            .ToList();
+
+                        //Queue all the actions
+                        foreach (var message in distinctActions)
+                        {
+                            Post(message);
+                        }
+
+                        Log.Info("[LISTENER] End Processing");
+                    }
                 }
-                else
+                catch (Exception ex)
                 {
-                    Trace.TraceInformation("[LISTENER] Finished");
+                    Log.ErrorFormat("[FAIL] ListObjects for{0} in ProcessRemoteFiles with {1}", accountInfo.UserName, ex);
+                    return;
                 }
-                ProcessRemoteFiles(accountPath, nextSince);
-                
-            });
-            return loop;
+
+                Log.Info("[LISTENER] Finished");
+
+            }
         }
 
-        
+        private static void CreateContainerFolders(AccountInfo accountInfo, IEnumerable<ContainerInfo> containers)
+        {
+            var containerPaths = from container in containers
+                                 let containerPath = Path.Combine(accountInfo.AccountPath, container.Name)
+                                 where container.Name != FolderConstants.TrashContainer && !Directory.Exists(containerPath)
+                                 select containerPath;
 
+            foreach (var path in containerPaths)
+            {
+                Directory.CreateDirectory(path);
+            }
+        }
 
-        private void Process(CloudAction action)
+        //Creates an appropriate action for each server file
+        private IEnumerable<CloudAction> ObjectsToActions(AccountInfo accountInfo,IEnumerable<ObjectInfo> remote)
         {
-            if (action==null)
-                throw new ArgumentNullException("action");
+            if (remote==null)
+                throw new ArgumentNullException();
             Contract.EndContractBlock();
+            var fileAgent = GetFileAgent(accountInfo);
 
-            Trace.TraceInformation("[ACTION] Start Processing {0}:{1}->{2}", action.Action, action.LocalFile, action.CloudFile.Name);
-            var localFile = action.LocalFile;
-            var cloudFile = action.CloudFile;
-            var downloadPath = (cloudFile == null) ? String.Empty
-                                    : Path.Combine(FileAgent.RootPath, cloudFile.Name.RelativeUrlToFilePath());
-            
-            try
+            //In order to avoid multiple iterations over the files, we iterate only once
+            //over the remote files
+            foreach (var objectInfo in remote)
             {
-                switch (action.Action)
+                var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
+                //and remove any matching objects from the list, adding them to the commonObjects list
+                
+                if (fileAgent.Exists(relativePath))
                 {
-                    case CloudActionType.UploadUnconditional:
-                        UploadCloudFile(localFile, action.LocalHash.Value,action.TopHash.Value);
-                        break;
-                    case CloudActionType.DownloadUnconditional:
-                        DownloadCloudFile(_pithosContainer, new Uri(cloudFile.Name,UriKind.Relative), downloadPath);
-                        break;
-                    case CloudActionType.DeleteCloud:
-                        DeleteCloudFile(cloudFile.Name);
-                        break;
-                    case CloudActionType.RenameCloud:
-                        RenameCloudFile(action.OldFileName, action.NewPath, action.NewFileName);
-                        break;
-                    case CloudActionType.MustSynch:
-                        if (File.Exists(downloadPath))
-                        {                            
-                            var cloudHash = cloudFile.Hash;
-                            var localHash = action.LocalHash.Value;
-                            var topHash = action.TopHash.Value;
-                            //Not enough to compare only the local hashes, also have to compare the tophashes
-                            if (!cloudHash.Equals(localHash, StringComparison.InvariantCultureIgnoreCase) &&
-                                !cloudHash.Equals(topHash, StringComparison.InvariantCultureIgnoreCase))
-                            {
-                                var lastLocalTime = localFile.LastWriteTime;
-                                var lastUpTime = cloudFile.Last_Modified;
-                                if (lastUpTime <= lastLocalTime)
-                                {
-                                    //Local change while the app was down or Files in conflict
-                                    //Maybe need to store version as well, to check who has the latest version
+                    //If a directory object already exists, we don't need to perform any other action                    
+                    var localFile = fileAgent.GetFileSystemInfo(relativePath);
+                    if (objectInfo.Content_Type == @"application/directory" && localFile is DirectoryInfo)
+                        continue;
+                    using (new SessionScope(FlushAction.Never))
+                    {
+                        var state =  StatusKeeper.GetStateByFilePath(localFile.FullName);
+                        //FileState.FindByFilePath(localFile.FullName);
+                        //Common files should be checked on a per-case basis to detect differences, which is newer
 
-                                    //StatusKeeper.SetFileOverlayStatus(downloadPath, FileOverlayStatus.Conflict);
-                                    UploadCloudFile(localFile, action.LocalHash.Value,action.TopHash.Value);
-                                }
-                                else
-                                {
-                                    var status = StatusKeeper.GetFileStatus(downloadPath);
-                                    switch (status)
-                                    {
-                                        case FileStatus.Unchanged:
-                                            //It he cloud file has a later date, it was modified by another user or computer.
-                                            //If the local file's status is Unchanged, we should go on and download the cloud file
-                                            DownloadCloudFile(_pithosContainer, new Uri(action.CloudFile.Name,UriKind.Relative), downloadPath);
-                                            break;
-                                        case FileStatus.Modified:
-                                            //If the local file is Modified, we may have a conflict. In this case we should mark the file as Conflict
-                                            //We can't ensure that a file modified online since the last time will appear as Modified, unless we 
-                                            //index all files before we start listening.
-                                            StatusKeeper.SetFileOverlayStatus(downloadPath, FileOverlayStatus.Conflict);
-                                            break;
-                                        case FileStatus.Created:
-                                            //If the local file is Created, it means that the local and cloud files aren't related yet have the same name
-                                            //In this case we must mark the file as in conflict
-                                            //Other cases should never occur. Mark them as Conflict as well but log a warning
-                                            StatusKeeper.SetFileOverlayStatus(downloadPath,FileOverlayStatus.Conflict);
-                                            break;
-                                        default:
-                                            //If the local file is Created, it means that the local and cloud files aren't related yet have the same name
-                                            //In this case we must mark the file as in conflict
-                                            //Other cases should never occur. Mark them as Conflict as well but log a warning
-                                            StatusKeeper.SetFileOverlayStatus(downloadPath,FileOverlayStatus.Conflict);
-                                            Trace.TraceWarning("Unexcepted status {0} for file {1}->{2}",status,downloadPath,action.CloudFile.Name);
-                                            break;
-                                    }
-                                }
-                            }
-                        }
-                        else
-                            DownloadCloudFile(_pithosContainer, new Uri(action.CloudFile.Name,UriKind.Relative), downloadPath);
-                        break;
+                        yield return new CloudAction(accountInfo, CloudActionType.MustSynch,
+                                                     localFile, objectInfo, state, accountInfo.BlockSize,
+                                                     accountInfo.BlockHash);
+                    }
                 }
-                Trace.TraceInformation("[ACTION] End Processing {0}:{1}->{2}", action.Action, action.LocalFile, action.CloudFile.Name);
-            }
-            catch (OperationCanceledException)
-            {
-                throw;
-            }
-            catch (Exception exc)
-            {
-                Trace.TraceError("[REQUEUE] {0} : {1} -> {2} due to exception\r\n{3}",
-                                action.Action, action.LocalFile, action.CloudFile, exc);
+                else
+                {
+                    //If there is no match we add them to the localFiles list
+                    //but only if the file is not marked for deletion
+                    var targetFile = Path.Combine(accountInfo.AccountPath, relativePath);
+                    var fileStatus = StatusKeeper.GetFileStatus(targetFile);
+                    if (fileStatus != FileStatus.Deleted)
+                    {
+                        //Remote files should be downloaded
+                        yield return new CloudDownloadAction(accountInfo,objectInfo);
+                    }
+                }
+            }            
+        }
 
-                _agent.Post(action);
-            }
+        private static FileAgent GetFileAgent(AccountInfo accountInfo)
+        {
+            return AgentLocator<FileAgent>.Get(accountInfo.AccountPath);
+        }
 
+        private void ProcessDeletedFiles(AccountInfo accountInfo,IEnumerable<ObjectInfo> trashObjects)
+        {
+            var fileAgent = GetFileAgent(accountInfo);
+            foreach (var trashObject in trashObjects)
+            {
+                var barePath = trashObject.RelativeUrlToFilePath(accountInfo.UserName);
+                //HACK: Assume only the "pithos" container is used. Must find out what happens when
+                //deleting a file from a different container
+                var relativePath = Path.Combine("pithos", barePath);
+                fileAgent.Delete(relativePath);                                
+            }
         }
 
-        
 
-        private void RenameCloudFile(string oldFileName, string newPath, string newFileName)
+        private void RenameCloudFile(AccountInfo accountInfo,CloudMoveAction action)
         {
-            if (String.IsNullOrWhiteSpace(oldFileName))
-                throw new ArgumentNullException("oldFileName");
-            if (String.IsNullOrWhiteSpace(oldFileName))
-                throw new ArgumentNullException("newPath");
-            if (String.IsNullOrWhiteSpace(oldFileName))
-                throw new ArgumentNullException("newFileName");
+            if (accountInfo==null)
+                throw new ArgumentNullException("accountInfo");
+            if (action==null)
+                throw new ArgumentNullException("action");
+            if (action.CloudFile==null)
+                throw new ArgumentException("CloudFile","action");
+            if (action.LocalFile==null)
+                throw new ArgumentException("LocalFile","action");
+            if (action.OldLocalFile==null)
+                throw new ArgumentException("OldLocalFile","action");
+            if (action.OldCloudFile==null)
+                throw new ArgumentException("OldCloudFile","action");
             Contract.EndContractBlock();
+            
+            
+            var newFilePath = action.LocalFile.FullName;
+            
+            //How do we handle concurrent renames and deletes/uploads/downloads?
+            //* A conflicting upload means that a file was renamed before it had a chance to finish uploading
+            //  This should never happen as the network agent executes only one action at a time
+            //* A conflicting download means that the file was modified on the cloud. While we can go on and complete
+            //  the rename, there may be a problem if the file is downloaded in blocks, as subsequent block requests for the 
+            //  same name will fail.
+            //  This should never happen as the network agent executes only one action at a time.
+            //* A conflicting delete can happen if the rename was followed by a delete action that didn't have the chance
+            //  to remove the rename from the queue.
+            //  We can probably ignore this case. It will result in an error which should be ignored            
+
+            
             //The local file is already renamed
-            this.StatusKeeper.SetFileOverlayStatus(newPath, FileOverlayStatus.Modified);
+            StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Modified);
+
 
-            CloudClient.MoveObject(_pithosContainer, oldFileName, _pithosContainer, newFileName);
+            var account = action.CloudFile.Account ?? accountInfo.UserName;
+            var container = action.CloudFile.Container;
+            
+            var client = new CloudFilesClient(accountInfo);
+            //TODO: What code is returned when the source file doesn't exist?
+            client.MoveObject(account, container, action.OldCloudFile.Name, container, action.CloudFile.Name);
 
-            this.StatusKeeper.SetFileStatus(newPath, FileStatus.Unchanged);
-            this.StatusKeeper.SetFileOverlayStatus(newPath, FileOverlayStatus.Normal);
-            NativeMethods.RaiseChangeNotification(newPath);
+            StatusKeeper.SetFileStatus(newFilePath, FileStatus.Unchanged);
+            StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Normal);
+            NativeMethods.RaiseChangeNotification(newFilePath);
         }
 
-        private void DeleteCloudFile(string fileName)
-        {            
-            if (String.IsNullOrWhiteSpace(fileName))
-                throw new ArgumentNullException("fileName");
-            if (Path.IsPathRooted(fileName))
-                throw new ArgumentException("The fileName should not be rooted","fileName");
+        private void DeleteCloudFile(AccountInfo accountInfo, ObjectInfo cloudFile)
+        {
+            if (accountInfo == null)
+                throw new ArgumentNullException("accountInfo");
+            if (cloudFile==null)
+                throw new ArgumentNullException("cloudFile");
+
+            if (String.IsNullOrWhiteSpace(cloudFile.Container))
+                throw new ArgumentException("Invalid container", "cloudFile");
             Contract.EndContractBlock();
+            
+            var fileAgent = GetFileAgent(accountInfo);
+
+            using ( log4net.ThreadContext.Stacks["DeleteCloudFile"].Push("Delete"))
+            {
+                var fileName= cloudFile.RelativeUrlToFilePath(accountInfo.UserName);
+                var info = fileAgent.GetFileSystemInfo(fileName);                
+                var fullPath = info.FullName.ToLower();
+
+                StatusKeeper.SetFileOverlayStatus(fullPath, FileOverlayStatus.Modified);
 
-            this.StatusKeeper.SetFileOverlayStatus(fileName, FileOverlayStatus.Modified);
+                var account = cloudFile.Account ?? accountInfo.UserName;
+                var container = cloudFile.Container ;//?? FolderConstants.PithosContainer;
 
-            CloudClient.MoveObject(_pithosContainer, fileName, _trashContainer, fileName);
-            this.StatusKeeper.ClearFileStatus(fileName);
-            this.StatusKeeper.RemoveFileOverlayStatus(fileName);
+                var client = new CloudFilesClient(accountInfo);
+                client.DeleteObject(account, container, cloudFile.Name);
+
+                StatusKeeper.ClearFileStatus(fullPath);
+            }
         }
 
         //Download a file.
-        private void DownloadCloudFile(string container, Uri relativeUrl, string localPath)
+        private async Task DownloadCloudFile(AccountInfo accountInfo, ObjectInfo cloudFile , string filePath)
         {
-            if (String.IsNullOrWhiteSpace(container))
-                throw new ArgumentNullException("container");
-            if (relativeUrl==null)
-                throw new ArgumentNullException("relativeUrl");
-
-            if (String.IsNullOrWhiteSpace(localPath))
-                throw new ArgumentNullException("localPath");
-            if (!Path.IsPathRooted(localPath))
-                throw new ArgumentException("The localPath must be rooted", "localPath");
+            if (accountInfo == null)
+                throw new ArgumentNullException("accountInfo");
+            if (cloudFile == null)
+                throw new ArgumentNullException("cloudFile");
+            if (String.IsNullOrWhiteSpace(cloudFile.Account))
+                throw new ArgumentNullException("cloudFile");
+            if (String.IsNullOrWhiteSpace(cloudFile.Container))
+                throw new ArgumentNullException("cloudFile");
+            if (String.IsNullOrWhiteSpace(filePath))
+                throw new ArgumentNullException("filePath");
+            if (!Path.IsPathRooted(filePath))
+                throw new ArgumentException("The filePath must be rooted", "filePath");
             Contract.EndContractBlock();
 
+            var localPath = Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
+            var relativeUrl = new Uri(cloudFile.Name, UriKind.Relative);
+
             var url = relativeUrl.ToString();
-            if (url.EndsWith(".ignore",StringComparison.InvariantCultureIgnoreCase))
+            if (cloudFile.Name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase))
                 return;
 
+
             //Are we already downloading or uploading the file? 
             using (var gate=NetworkGate.Acquire(localPath, NetworkOperation.Downloading))
             {
                 if (gate.Failed)
                     return;
-                //Calculate the relative file path for the new file
-                var relativePath = relativeUrl.RelativeUriToFilePath();
-                //The file will be stored in a temporary location while downloading with an extension .download
-                var tempPath = Path.Combine(FileAgent.FragmentsPath, relativePath + ".download");
                 //The file's hashmap will be stored in the same location with the extension .hashmap
-                var hashPath = Path.Combine(FileAgent.FragmentsPath, relativePath + ".hashmap");
+                //var hashPath = Path.Combine(FileAgent.CachePath, relativePath + ".hashmap");
                 
-                //Retrieve the hashmap from the server
-                var getHashMap = CloudClient.GetHashMap(container,url);
-                //And save it
-                var saveHashMap = getHashMap.ContinueWith(t =>
-                {
-                    var treeHash = t.Result;
-                    treeHash.Save(hashPath);
-                });
-
-                //Make sure the target folder exists. DownloadFileTask will not create the folder
-                var directoryPath=Path.GetDirectoryName(tempPath);
-                if (!Directory.Exists(directoryPath))
-                    Directory.CreateDirectory(directoryPath);
+                var client = new CloudFilesClient(accountInfo);
+                var account = cloudFile.Account;
+                var container = cloudFile.Container;
 
-                //Download the object to the temporary location
-                var getObject = CloudClient.GetObject(container, url, tempPath).ContinueWith(t =>
+                if (cloudFile.Content_Type == @"application/directory")
+                {
+                    if (!Directory.Exists(localPath))
+                        Directory.CreateDirectory(localPath);
+                }
+                else
                 {
-                    //And move it to its actual location once downloading is finished
-                    if (File.Exists(localPath))
-                        File.Replace(tempPath,localPath,null,true);
+                    //Retrieve the hashmap from the server
+                    var serverHash = await client.GetHashMap(account, container, url);
+                    //If it's a small file
+                    if (serverHash.Hashes.Count == 1)
+                        //Download it in one go
+                        await
+                            DownloadEntireFileAsync(accountInfo, client, cloudFile, relativeUrl, localPath, serverHash);
+                        //Otherwise download it block by block
                     else
-                        File.Move(tempPath,localPath);
-                });
-                
-                //Retrieve the object's metadata
-                var getInfo = saveHashMap.ContinueWith(t => getObject).ContinueWith(t =>
-                            CloudClient.GetObjectInfo(container, url));
-                //And store it
-                var storeInfo = getInfo.ContinueWith(t =>
-                            StatusKeeper.StoreInfo(localPath, t.Result));
-
-                storeInfo.Wait();
-                StatusNotification.NotifyChangedFile(localPath);
+                        await DownloadWithBlocks(accountInfo, client, cloudFile, relativeUrl, localPath, serverHash);
 
+                    if (cloudFile.AllowedTo == "read")
+                    {
+                        var attributes = File.GetAttributes(localPath);
+                        File.SetAttributes(localPath, attributes | FileAttributes.ReadOnly);                        
+                    }
+                }
+
+                //Now we can store the object's metadata without worrying about ghost status entries
+                StatusKeeper.StoreInfo(localPath, cloudFile);
+                
             }
         }
 
-        private void UploadCloudFile(FileInfo fileInfo, string hash,string topHash)
+        //Download a small file with a single GET operation
+        private async Task DownloadEntireFileAsync(AccountInfo accountInfo, CloudFilesClient client, ObjectInfo cloudFile, Uri relativeUrl, string filePath,TreeHash serverHash)
         {
-            if (fileInfo==null)
-                throw new ArgumentNullException("fileInfo");
-            if (String.IsNullOrWhiteSpace(hash))
-                throw new ArgumentNullException("hash");
+            if (client == null)
+                throw new ArgumentNullException("client");
+            if (cloudFile==null)
+                throw new ArgumentNullException("cloudFile");
+            if (relativeUrl == null)
+                throw new ArgumentNullException("relativeUrl");
+            if (String.IsNullOrWhiteSpace(filePath))
+                throw new ArgumentNullException("filePath");
+            if (!Path.IsPathRooted(filePath))
+                throw new ArgumentException("The localPath must be rooted", "filePath");
             Contract.EndContractBlock();
 
-            if (fileInfo.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase))
-                return;
-            
-            var url = fileInfo.AsRelativeUrlTo(FileAgent.RootPath);
-
-            var fullFileName = fileInfo.FullName;
-            using(var gate=NetworkGate.Acquire(fullFileName,NetworkOperation.Uploading))
+            var localPath = Pithos.Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
+            //If the file already exists
+            if (File.Exists(localPath))
             {
-                //Abort if the file is already being uploaded or downloaded
-                if (gate.Failed)
+                //First check with MD5 as this is a small file
+                var localMD5 = Signature.CalculateMD5(localPath);
+                var cloudHash=serverHash.TopHash.ToHashString();
+                if (localMD5==cloudHash)
                     return;
+                //Then check with a treehash
+                var localTreeHash = Signature.CalculateTreeHash(localPath, serverHash.BlockSize, serverHash.BlockHash);
+                var localHash = localTreeHash.TopHash.ToHashString();
+                if (localHash==cloudHash)
+                    return;
+            }
 
+            var fileAgent = GetFileAgent(accountInfo);
+            //Calculate the relative file path for the new file
+            var relativePath = relativeUrl.RelativeUriToFilePath();
+            //The file will be stored in a temporary location while downloading with an extension .download
+            var tempPath = Path.Combine(fileAgent.CachePath, relativePath + ".download");
+            //Make sure the target folder exists. DownloadFileTask will not create the folder
+            var tempFolder = Path.GetDirectoryName(tempPath);
+            if (!Directory.Exists(tempFolder))
+                Directory.CreateDirectory(tempFolder);
+
+            //Download the object to the temporary location
+            await client.GetObject(cloudFile.Account, cloudFile.Container, relativeUrl.ToString(), tempPath);
+
+            //Create the local folder if it doesn't exist (necessary for shared objects)
+            var localFolder = Path.GetDirectoryName(localPath);
+            if (!Directory.Exists(localFolder))
+                Directory.CreateDirectory(localFolder);            
+            //And move it to its actual location once downloading is finished
+            if (File.Exists(localPath))
+                File.Replace(tempPath,localPath,null,true);
+            else
+                File.Move(tempPath,localPath);
+            //Notify listeners that a local file has changed
+            StatusNotification.NotifyChangedFile(localPath);
+
+                       
+        }
 
-                //Even if GetObjectInfo times out, we can proceed with the upload            
-                var info = CloudClient.GetObjectInfo(_pithosContainer, url);
+        //Download a file asynchronously using blocks
+        public async Task DownloadWithBlocks(AccountInfo accountInfo, CloudFilesClient client, ObjectInfo cloudFile, Uri relativeUrl, string filePath, TreeHash serverHash)
+        {
+            if (client == null)
+                throw new ArgumentNullException("client");
+            if (cloudFile == null)
+                throw new ArgumentNullException("cloudFile");
+            if (relativeUrl == null)
+                throw new ArgumentNullException("relativeUrl");
+            if (String.IsNullOrWhiteSpace(filePath))
+                throw new ArgumentNullException("filePath");
+            if (!Path.IsPathRooted(filePath))
+                throw new ArgumentException("The filePath must be rooted", "filePath");
+            if (serverHash == null)
+                throw new ArgumentNullException("serverHash");
+            Contract.EndContractBlock();
+            
+           var fileAgent = GetFileAgent(accountInfo);
+            var localPath = Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
+            
+            //Calculate the relative file path for the new file
+            var relativePath = relativeUrl.RelativeUriToFilePath();
+            var blockUpdater = new BlockUpdater(fileAgent.CachePath, localPath, relativePath, serverHash);
 
-                //If the file hashes match, abort the upload
-                if (hash.Equals(info.Hash, StringComparison.InvariantCultureIgnoreCase) ||
-                    topHash.Equals(info.Hash, StringComparison.InvariantCultureIgnoreCase))
+            
+                        
+            //Calculate the file's treehash
+            var treeHash = await Signature.CalculateTreeHashAsync(localPath, serverHash.BlockSize, serverHash.BlockHash);
+                
+            //And compare it with the server's hash
+            var upHashes = serverHash.GetHashesAsStrings();
+            var localHashes = treeHash.HashDictionary;
+            for (int i = 0; i < upHashes.Length; i++)
+            {
+                //For every non-matching hash
+                var upHash = upHashes[i];
+                if (!localHashes.ContainsKey(upHash))
                 {
-                    //but store any metadata changes 
-                    this.StatusKeeper.StoreInfo(fullFileName, info);
-                    Trace.TraceInformation("Skip upload of {0}, hashes match", fullFileName);
+                    if (blockUpdater.UseOrphan(i, upHash))
+                    {
+                        Log.InfoFormat("[BLOCK GET] ORPHAN FOUND for {0} of {1} for {2}", i, upHashes.Length, localPath);
+                        continue;
+                    }
+                    Log.InfoFormat("[BLOCK GET] START {0} of {1} for {2}", i, upHashes.Length, localPath);
+                    var start = i*serverHash.BlockSize;
+                    //To download the last block just pass a null for the end of the range
+                    long? end = null;
+                    if (i < upHashes.Length - 1 )
+                        end= ((i + 1)*serverHash.BlockSize) ;
+                            
+                    //Download the missing block
+                    var block = await client.GetBlock(cloudFile.Account, cloudFile.Container, relativeUrl, start, end);
+
+                    //and store it
+                    blockUpdater.StoreBlock(i, block);
+
+
+                    Log.InfoFormat("[BLOCK GET] FINISH {0} of {1} for {2}", i, upHashes.Length, localPath);
+                }
+            }
+
+            //Want to avoid notifications if no changes were made
+            var hasChanges = blockUpdater.HasBlocks;
+            blockUpdater.Commit();
+            
+            if (hasChanges)
+                //Notify listeners that a local file has changed
+                StatusNotification.NotifyChangedFile(localPath);
+
+            Log.InfoFormat("[BLOCK GET] COMPLETE {0}", localPath);            
+        }
+
+
+        private async Task UploadCloudFile(CloudAction action)
+        {
+            if (action == null)
+                throw new ArgumentNullException("action");           
+            Contract.EndContractBlock();
+
+            try
+            {
+                var accountInfo = action.AccountInfo;
+
+                var fileInfo = action.LocalFile;
+
+                if (fileInfo.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase))
                     return;
+                
+                var relativePath = fileInfo.AsRelativeTo(accountInfo.AccountPath);
+                if (relativePath.StartsWith(FolderConstants.OthersFolder))
+                {
+                    var parts = relativePath.Split('\\');
+                    var accountName = parts[1];
+                    var oldName = accountInfo.UserName;
+                    var absoluteUri = accountInfo.StorageUri.AbsoluteUri;
+                    var nameIndex = absoluteUri.IndexOf(oldName);
+                    var root = absoluteUri.Substring(0, nameIndex);
+
+                    accountInfo = new AccountInfo
+                    {
+                        UserName = accountName,
+                        AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
+                        StorageUri = new Uri(root + accountName),
+                        BlockHash = accountInfo.BlockHash,
+                        BlockSize = accountInfo.BlockSize,
+                        Token = accountInfo.Token
+                    };
                 }
 
-                //If the file was last modified by a hashmap operation, its hash will be a treehash
-                //We must load the local file's treehash and check it against the object's hash 
-                //The hash will be stored under the fragments path, in the same relative path as to
-                //the pithos root path
-                var relativePath = fileInfo.AsRelativeTo(FileAgent.RootPath);
-/*
-                var hashPath = Path.Combine(FileAgent.FragmentsPath, relativePath) + ".hashmap";                
-                //Load the hash or calculate a new one
-                var hashFileExists = File.Exists(hashPath);
-                var treeHash = hashFileExists ?
-                    TreeHash.LoadTreeHash(hashPath, Guid.NewGuid()).Result
-                    : Signature.CalculateTreeHash(fileInfo.FullName, _blockSize, _blockHash);
-                //If this is a new treehash, store it
-                if (!hashFileExists)
-                    treeHash.Save(hashPath);
-
-                var topHash = treeHash.TopHash.ToHashString();
-                if (topHash.Equals(info.Hash, StringComparison.InvariantCultureIgnoreCase))
+
+                var fullFileName = fileInfo.GetProperCapitalization();
+                using (var gate = NetworkGate.Acquire(fullFileName, NetworkOperation.Uploading))
                 {
-                    this.StatusKeeper.StoreInfo(fileInfo.FullName, info);
-                    Trace.TraceInformation("Skip upload of {0}, treehashes match", fileInfo.FullName);
-                    return;                    
+                    //Abort if the file is already being uploaded or downloaded
+                    if (gate.Failed)
+                        return;
+
+                    var cloudFile = action.CloudFile;
+                    var account = cloudFile.Account ?? accountInfo.UserName;
+
+                    var client = new CloudFilesClient(accountInfo);                    
+                    //Even if GetObjectInfo times out, we can proceed with the upload            
+                    var info = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name);
+
+                    //If this is a read-only file, do not upload changes
+                    if (info.AllowedTo == "read")
+                        return;
+                    
+                    //TODO: Check how a directory hash is calculated -> All dirs seem to have the same hash
+                    if (fileInfo is DirectoryInfo)
+                    {
+                        //If the directory doesn't exist the Hash property will be empty
+                        if (String.IsNullOrWhiteSpace(info.Hash))
+                            //Go on and create the directory
+                            await client.PutObject(account, cloudFile.Container, cloudFile.Name, fullFileName, String.Empty, "application/directory");
+                    }
+                    else
+                    {
+
+                        var cloudHash = info.Hash.ToLower();
+
+                        var hash = action.LocalHash.Value;
+                        var topHash = action.TopHash.Value;
+
+                        //If the file hashes match, abort the upload
+                        if (hash == cloudHash || topHash == cloudHash)
+                        {
+                            //but store any metadata changes 
+                            StatusKeeper.StoreInfo(fullFileName, info);
+                            Log.InfoFormat("Skip upload of {0}, hashes match", fullFileName);
+                            return;
+                        }
+
+
+                        //Mark the file as modified while we upload it
+                        StatusKeeper.SetFileOverlayStatus(fullFileName, FileOverlayStatus.Modified);
+                        //And then upload it
+
+                        //Upload even small files using the Hashmap. The server may already contain
+                        //the relevant block
+
+                        //First, calculate the tree hash
+                        var treeHash = await Signature.CalculateTreeHashAsync(fullFileName, accountInfo.BlockSize,
+                                                                              accountInfo.BlockHash);
+
+                        await UploadWithHashMap(accountInfo, cloudFile, fileInfo as FileInfo, cloudFile.Name, treeHash);
+                    }
+                    //If everything succeeds, change the file and overlay status to normal
+                    StatusKeeper.SetFileState(fullFileName, FileStatus.Unchanged, FileOverlayStatus.Normal);
                 }
-*/
-            
+                //Notify the Shell to update the overlays
+                NativeMethods.RaiseChangeNotification(fullFileName);
+                StatusNotification.NotifyChangedFile(fullFileName);
+            }
+            catch (AggregateException ex)
+            {
+                var exc = ex.InnerException as WebException;
+                if (exc == null)
+                    throw ex.InnerException;
+                if (HandleUploadWebException(action, exc)) 
+                    return;
+                throw;
+            }
+            catch (WebException ex)
+            {
+                if (HandleUploadWebException(action, ex))
+                    return;
+                throw;
+            }
+            catch (Exception ex)
+            {
+                Log.Error("Unexpected error while uploading file", ex);
+                throw;
+            }
 
+        }
 
+        private bool IsDeletedFile(CloudAction action)
+        {            
+            var key = GetFileKey(action.CloudFile);
+            DateTime entryDate;
+            if (_deletedFiles.TryGetValue(key, out entryDate))
+            {
+                //If the delete entry was created after this action, abort the action
+                if (entryDate > action.Created)
+                    return true;
+                //Otherwise, remove the stale entry 
+                _deletedFiles.TryRemove(key, out entryDate);
+            }
+            return false;
+        }
 
-                //Does the object exist or not?
+        private bool HandleUploadWebException(CloudAction action, WebException exc)
+        {
+            var response = exc.Response as HttpWebResponse;
+            if (response == null)
+                throw exc;
+            if (response.StatusCode == HttpStatusCode.Unauthorized)
+            {
+                Log.Error("Not allowed to upload file", exc);
+                var message = String.Format("Not allowed to uplad file {0}", action.LocalFile.FullName);
+                StatusKeeper.SetFileState(action.LocalFile.FullName, FileStatus.Unchanged, FileOverlayStatus.Normal);
+                StatusNotification.NotifyChange(message, TraceLevel.Warning);
+                return true;
+            }
+            return false;
+        }
 
-                //Calculate or load the file's hashmap                
+        public async Task UploadWithHashMap(AccountInfo accountInfo,ObjectInfo cloudFile,FileInfo fileInfo,string url,TreeHash treeHash)
+        {
+            if (accountInfo == null)
+                throw new ArgumentNullException("accountInfo");
+            if (cloudFile==null)
+                throw new ArgumentNullException("cloudFile");
+            if (fileInfo == null)
+                throw new ArgumentNullException("fileInfo");
+            if (String.IsNullOrWhiteSpace(url))
+                throw new ArgumentNullException(url);
+            if (treeHash==null)
+                throw new ArgumentNullException("treeHash");
+            if (String.IsNullOrWhiteSpace(cloudFile.Container) )
+                throw new ArgumentException("Invalid container","cloudFile");
+            Contract.EndContractBlock();
 
-                /*
-                 if (fileInfo.Bytes > _blockSize)
-                 {
-                  var hashPath = Path.Combine(FileAgent.FragmentsPath, relativePath) + ".hashmap";
-                
-                //Load the existing hashPath, if we have one, or calculate a new one
-                var treeHash = File.Exists(hashPath) ?
-                    TreeHash.LoadTreeHash(hashPath, Guid.NewGuid()).Result 
-                    : Signature.CalculateTreeHash(fileInfo.FullName, _blockSize , _blockHash);
-                                
-                //Do the Hashmap Put
-                Task<string> putHashMap=CloudClient.PutHashMap(_pithosContainer,url, treeHash);
-                putHashMap.Wait();
-                 }*/                    
+            var fullFileName = fileInfo.GetProperCapitalization();
 
-            //
+            var account = cloudFile.Account ?? accountInfo.UserName;
+            var container = cloudFile.Container ;
 
+            var client = new CloudFilesClient(accountInfo);
+            //Send the hashmap to the server            
+            var missingHashes =  await client.PutHashMap(account, container, url, treeHash);
+            //If the server returns no missing hashes, we are done
+            while (missingHashes.Count > 0)
+            {
 
-                var putOrUpdate = Task.Factory.StartNew(() =>
+                var buffer = new byte[accountInfo.BlockSize];
+                foreach (var missingHash in missingHashes)
                 {
-                    //Mark the file as modified while we upload it
-                    var setStatus = Task.Factory.StartNew(() =>
-                        StatusKeeper.SetFileOverlayStatus(fullFileName,FileOverlayStatus.Modified));
-                    //And then upload it
-                    var put = setStatus.ContinueWith(t =>
-                        CloudClient.PutObject(_pithosContainer,url,fullFileName, hash));
-                });
-                putOrUpdate.Wait();
-                //If everything succeeds, change the file and overlay status to normal
-                this.StatusKeeper.SetFileState(fullFileName, FileStatus.Unchanged, FileOverlayStatus.Normal);
+                    //Find the proper block
+                    var blockIndex = treeHash.HashDictionary[missingHash];
+                    var offset = blockIndex*accountInfo.BlockSize;
+
+                    var read = fileInfo.Read(buffer, offset, accountInfo.BlockSize);
+
+                    try
+                    {
+                        //And upload the block                
+                        await client.PostBlock(account, container, buffer, 0, read);
+                        Log.InfoFormat("[BLOCK] Block {0} of {1} uploaded", blockIndex, fullFileName);
+                    }
+                    catch (Exception exc)
+                    {
+                        Log.ErrorFormat("[ERROR] uploading block {0} of {1}\n{2}", blockIndex, fullFileName, exc);
+                    }
+
+                }
+
+                //Repeat until there are no more missing hashes                
+                missingHashes = await client.PutHashMap(account, container, url, treeHash);
             }
-            //Notify the Shell to update the overlays
-            NativeMethods.RaiseChangeNotification(fullFileName);
-            StatusNotification.NotifyChangedFile(fullFileName);
         }
 
 
+        public void AddAccount(AccountInfo accountInfo)
+        {            
+            if (!_accounts.Contains(accountInfo))
+                _accounts.Add(accountInfo);
+        }
     }