1 // -----------------------------------------------------------------------
2 // <copyright file="NetworkAgent.cs" company="GRNET">
3 // Copyright 2011 GRNET S.A. All rights reserved.
5 // Redistribution and use in source and binary forms, with or
6 // without modification, are permitted provided that the following
9 // 1. Redistributions of source code must retain the above
10 // copyright notice, this list of conditions and the following
13 // 2. Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials
16 // provided with the distribution.
18 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
31 // The views and conclusions contained in the software and
32 // documentation are those of the authors and should not be
33 // interpreted as representing official policies, either expressed
34 // or implied, of GRNET S.A.
36 // -----------------------------------------------------------------------
39 using System.Collections.Concurrent;
40 using System.Collections.Generic;
41 using System.ComponentModel.Composition;
42 using System.Diagnostics;
43 using System.Diagnostics.Contracts;
47 using System.Threading.Tasks;
48 using Castle.ActiveRecord;
49 using Pithos.Interfaces;
53 namespace Pithos.Core.Agents
55 //TODO: Ensure all network operations use exact casing. Pithos is case sensitive
57 public class NetworkAgent
59 private Agent<CloudAction> _agent;
61 //A separate agent is used to execute delete actions immediatelly;
62 private Agent<CloudDeleteAction> _deleteAgent;
63 readonly ConcurrentDictionary<string,DateTime> _deletedFiles=new ConcurrentDictionary<string, DateTime>();
65 [System.ComponentModel.Composition.Import]
66 public IStatusKeeper StatusKeeper { get; set; }
68 public IStatusNotification StatusNotification { get; set; }
70 private static readonly ILog Log = LogManager.GetLogger("NetworkAgent");
72 private readonly ConcurrentBag<AccountInfo> _accounts = new ConcurrentBag<AccountInfo>();
77 _agent = Agent<CloudAction>.Start(inbox =>
82 var message = inbox.Receive();
83 var process=message.Then(Process,inbox.CancellationToken);
84 inbox.LoopAsync(process, loop);
89 _deleteAgent = Agent<CloudDeleteAction>.Start(inbox =>
94 var message = inbox.Receive();
95 var process = message.Then(ProcessDelete,inbox.CancellationToken);
96 inbox.LoopAsync(process, loop);
103 private async Task Process(CloudAction action)
106 throw new ArgumentNullException("action");
107 if (action.AccountInfo==null)
108 throw new ArgumentException("The action.AccountInfo is empty","action");
109 Contract.EndContractBlock();
111 var accountInfo = action.AccountInfo;
113 using (log4net.ThreadContext.Stacks["NETWORK"].Push("PROCESS"))
115 Log.InfoFormat("[ACTION] Start Processing {0}", action);
117 var cloudFile = action.CloudFile;
118 var downloadPath = action.GetDownloadPath();
123 if (action.Action == CloudActionType.DeleteCloud)
125 //Redirect deletes to the delete agent
126 _deleteAgent.Post((CloudDeleteAction)action);
128 if (IsDeletedFile(action))
130 //Clear the status of already deleted files to avoid reprocessing
131 this.StatusKeeper.ClearFileStatus(action.LocalFile.FullName);
135 switch (action.Action)
137 case CloudActionType.UploadUnconditional:
138 //Abort if the file was deleted before we reached this point
139 await UploadCloudFile(action);
141 case CloudActionType.DownloadUnconditional:
142 await DownloadCloudFile(accountInfo, cloudFile, downloadPath);
144 case CloudActionType.RenameCloud:
145 var moveAction = (CloudMoveAction) action;
146 RenameCloudFile(accountInfo, moveAction);
148 case CloudActionType.MustSynch:
149 if (!File.Exists(downloadPath) && !Directory.Exists(downloadPath))
151 await DownloadCloudFile(accountInfo, cloudFile, downloadPath);
155 await SyncFiles(accountInfo, action);
160 Log.InfoFormat("[ACTION] End Processing {0}:{1}->{2}", action.Action, action.LocalFile,
161 action.CloudFile.Name);
163 catch (WebException exc)
165 Log.ErrorFormat("[WEB ERROR] {0} : {1} -> {2} due to exception\r\n{3}", action.Action, action.LocalFile, action.CloudFile, exc);
167 catch (OperationCanceledException)
171 catch (DirectoryNotFoundException)
173 Log.ErrorFormat("{0} : {1} -> {2} failed because the directory was not found.\n Rescheduling a delete",
174 action.Action, action.LocalFile, action.CloudFile);
175 //Post a delete action for the missing file
176 Post(new CloudDeleteAction(action));
178 catch (FileNotFoundException)
180 Log.ErrorFormat("{0} : {1} -> {2} failed because the file was not found.\n Rescheduling a delete",
181 action.Action, action.LocalFile, action.CloudFile);
182 //Post a delete action for the missing file
183 Post(new CloudDeleteAction(action));
185 catch (Exception exc)
187 Log.ErrorFormat("[REQUEUE] {0} : {1} -> {2} due to exception\r\n{3}",
188 action.Action, action.LocalFile, action.CloudFile, exc);
196 /// Processes cloud delete actions
198 /// <param name="action">The delete action to execute</param>
199 /// <returns></returns>
201 /// When a file/folder is deleted locally, we must delete it ASAP from the server and block any download
202 /// operations that may be in progress.
204 /// A separate agent is used to process deletes because the main agent may be busy with a long operation.
207 private async Task ProcessDelete(CloudDeleteAction action)
210 throw new ArgumentNullException("action");
211 if (action.AccountInfo==null)
212 throw new ArgumentException("The action.AccountInfo is empty","action");
213 Contract.EndContractBlock();
215 var accountInfo = action.AccountInfo;
217 using (log4net.ThreadContext.Stacks["NETWORK"].Push("PROCESS"))
219 Log.InfoFormat("[ACTION] Start Processing {0}", action);
221 var cloudFile = action.CloudFile;
225 //Acquire a lock on the deleted file to prevent uploading/downloading operations from the normal
227 using (var gate = NetworkGate.Acquire(action.LocalFile.FullName, NetworkOperation.Deleting))
230 //Add the file URL to the deleted files list
231 var key = GetFileKey(action.CloudFile);
232 _deletedFiles[key]=DateTime.Now;
234 // and then delete the file from the server
235 DeleteCloudFile(accountInfo, cloudFile);
236 Log.InfoFormat("[ACTION] End Delete {0}:{1}->{2}", action.Action, action.LocalFile,
237 action.CloudFile.Name);
240 catch (WebException exc)
242 Log.ErrorFormat("[WEB ERROR] {0} : {1} -> {2} due to exception\r\n{3}", action.Action, action.LocalFile, action.CloudFile, exc);
244 catch (OperationCanceledException)
248 catch (DirectoryNotFoundException)
250 Log.ErrorFormat("{0} : {1} -> {2} failed because the directory was not found.\n Rescheduling a delete",
251 action.Action, action.LocalFile, action.CloudFile);
252 //Repost a delete action for the missing file
253 _deleteAgent.Post(action);
255 catch (FileNotFoundException)
257 Log.ErrorFormat("{0} : {1} -> {2} failed because the file was not found.\n Rescheduling a delete",
258 action.Action, action.LocalFile, action.CloudFile);
259 //Post a delete action for the missing file
260 _deleteAgent.Post(action);
262 catch (Exception exc)
264 Log.ErrorFormat("[REQUEUE] {0} : {1} -> {2} due to exception\r\n{3}",
265 action.Action, action.LocalFile, action.CloudFile, exc);
267 _deleteAgent.Post(action);
272 private static string GetFileKey(ObjectInfo info)
274 var key = String.Format("{0}/{1}/{2}", info.Account, info.Container,info.Name);
278 private async Task SyncFiles(AccountInfo accountInfo,CloudAction action)
280 if (accountInfo == null)
281 throw new ArgumentNullException("accountInfo");
283 throw new ArgumentNullException("action");
284 if (action.LocalFile==null)
285 throw new ArgumentException("The action's local file is not specified","action");
286 if (!Path.IsPathRooted(action.LocalFile.FullName))
287 throw new ArgumentException("The action's local file path must be absolute","action");
288 if (action.CloudFile== null)
289 throw new ArgumentException("The action's cloud file is not specified", "action");
290 Contract.EndContractBlock();
292 var localFile = action.LocalFile;
293 var cloudFile = action.CloudFile;
294 var downloadPath=action.LocalFile.GetProperCapitalization();
296 var cloudHash = cloudFile.Hash.ToLower();
297 var localHash = action.LocalHash.Value.ToLower();
298 var topHash = action.TopHash.Value.ToLower();
300 //Not enough to compare only the local hashes, also have to compare the tophashes
302 //If any of the hashes match, we are done
303 if ((cloudHash == localHash || cloudHash == topHash))
305 Log.InfoFormat("Skipping {0}, hashes match",downloadPath);
309 //The hashes DON'T match. We need to sync
310 var lastLocalTime = localFile.LastWriteTime;
311 var lastUpTime = cloudFile.Last_Modified;
313 //If the local file is newer upload it
314 if (lastUpTime <= lastLocalTime)
316 //It probably means it was changed while the app was down
317 UploadCloudFile(action);
321 //It the cloud file has a later date, it was modified by another user or computer.
322 //We need to check the local file's status
323 var status = StatusKeeper.GetFileStatus(downloadPath);
326 case FileStatus.Unchanged:
327 //If the local file's status is Unchanged, we can go on and download the newer cloud file
328 await DownloadCloudFile(accountInfo,cloudFile,downloadPath);
330 case FileStatus.Modified:
331 //If the local file is Modified, we may have a conflict. In this case we should mark the file as Conflict
332 //We can't ensure that a file modified online since the last time will appear as Modified, unless we
333 //index all files before we start listening.
334 case FileStatus.Created:
335 //If the local file is Created, it means that the local and cloud files aren't related,
336 // yet they have the same name.
338 //In both cases we must mark the file as in conflict
339 ReportConflict(downloadPath);
342 //Other cases should never occur. Mark them as Conflict as well but log a warning
343 ReportConflict(downloadPath);
344 Log.WarnFormat("Unexcepted status {0} for file {1}->{2}", status,
345 downloadPath, action.CloudFile.Name);
351 private void ReportConflict(string downloadPath)
353 if (String.IsNullOrWhiteSpace(downloadPath))
354 throw new ArgumentNullException("downloadPath");
355 Contract.EndContractBlock();
357 StatusKeeper.SetFileOverlayStatus(downloadPath, FileOverlayStatus.Conflict);
358 var message = String.Format("Conflict detected for file {0}", downloadPath);
360 StatusNotification.NotifyChange(message, TraceLevel.Warning);
363 public void Post(CloudAction cloudAction)
365 if (cloudAction == null)
366 throw new ArgumentNullException("cloudAction");
367 if (cloudAction.AccountInfo==null)
368 throw new ArgumentException("The CloudAction.AccountInfo is empty","cloudAction");
369 Contract.EndContractBlock();
371 //If the action targets a local file, add a treehash calculation
372 if (!(cloudAction is CloudDeleteAction) && cloudAction.LocalFile as FileInfo != null)
374 var accountInfo = cloudAction.AccountInfo;
375 var localFile = (FileInfo) cloudAction.LocalFile;
376 if (localFile.Length > accountInfo.BlockSize)
377 cloudAction.TopHash =
378 new Lazy<string>(() => Signature.CalculateTreeHashAsync(localFile,
379 accountInfo.BlockSize,
380 accountInfo.BlockHash).Result
381 .TopHash.ToHashString());
384 cloudAction.TopHash = new Lazy<string>(() => cloudAction.LocalHash.Value);
389 //The hash for a directory is the empty string
390 cloudAction.TopHash = new Lazy<string>(() => String.Empty);
393 if (cloudAction is CloudDeleteAction)
394 _deleteAgent.Post((CloudDeleteAction)cloudAction);
396 _agent.Post(cloudAction);
399 /* class ObjectInfoByNameComparer:IEqualityComparer<ObjectInfo>
401 public bool Equals(ObjectInfo x, ObjectInfo y)
403 return x.Name.Equals(y.Name,StringComparison.InvariantCultureIgnoreCase);
406 public int GetHashCode(ObjectInfo obj)
408 return obj.Name.ToLower().GetHashCode();
414 //Remote files are polled periodically. Any changes are processed
415 public async Task ProcessRemoteFiles(DateTime? since = null)
417 await TaskEx.Delay(TimeSpan.FromSeconds(10),_agent.CancellationToken);
419 using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push("All accounts"))
424 //Next time we will check for all changes since the current check minus 1 second
425 //This is done to ensure there are no discrepancies due to clock differences
426 DateTime nextSince = DateTime.Now.AddSeconds(-1);
428 var tasks = from accountInfo in _accounts
429 select ProcessAccountFiles(accountInfo, since);
431 await TaskEx.WhenAll(tasks.ToList());
433 ProcessRemoteFiles(nextSince);
437 Log.ErrorFormat("Error while processing accounts\r\n{0}",ex);
438 //In case of failure retry with the same parameter
439 ProcessRemoteFiles(since);
446 public async Task ProcessAccountFiles(AccountInfo accountInfo,DateTime? since=null)
448 if (accountInfo==null)
449 throw new ArgumentNullException("accountInfo");
450 if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))
451 throw new ArgumentException("The AccountInfo.AccountPath is empty","accountInfo");
452 Contract.EndContractBlock();
454 using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName))
456 Log.Info("Scheduled");
457 var client=new CloudFilesClient(accountInfo);
459 var containers = client.ListContainers(accountInfo.UserName);
461 CreateContainerFolders(accountInfo, containers);
466 //Get the list of server objects changed since the last check
467 //The name of the container is passed as state in order to create a dictionary of tasks in a subsequent step
468 var listObjects = from container in containers
469 select Task<IList<ObjectInfo>>.Factory.StartNew(_ =>
470 client.ListObjects(accountInfo.UserName,container.Name, since),container.Name);
473 var listTasks = await Task.Factory.WhenAll(listObjects.ToArray());
475 using (log4net.ThreadContext.Stacks["SCHEDULE"].Push("Process Results"))
477 var dict = listTasks.ToDictionary(t => t.AsyncState);
479 //Get all non-trash objects. Remember, the container name is stored in AsyncState
480 var remoteObjects = from objectList in listTasks
481 where (string) objectList.AsyncState != "trash"
482 from obj in objectList.Result
485 var trashObjects = dict["trash"].Result;
486 //var sharedObjects = ((Task<IList<ObjectInfo>>) task.Result[2]).Result;
488 //Items with the same name, hash may be both in the container and the trash
489 //Don't delete items that exist in the container
490 var realTrash = from trash in trashObjects
493 info => info.Name == trash.Name && info.Hash == trash.Hash)
495 ProcessDeletedFiles(accountInfo, realTrash);
498 var remote = from info in remoteObjects
499 //.Union(sharedObjects)
501 where !name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase) &&
502 !name.StartsWith(FolderConstants.CacheFolder + "/",
503 StringComparison.InvariantCultureIgnoreCase)
506 //Create a list of actions from the remote files
507 var allActions = ObjectsToActions(accountInfo, remote);
509 //And remove those that are already being processed by the agent
510 var distinctActions = allActions
511 .Except(_agent.GetEnumerable(), new PithosMonitor.LocalFileComparer())
514 //Queue all the actions
515 foreach (var message in distinctActions)
520 Log.Info("[LISTENER] End Processing");
525 Log.ErrorFormat("[FAIL] ListObjects for{0} in ProcessRemoteFiles with {1}", accountInfo.UserName, ex);
529 Log.Info("[LISTENER] Finished");
534 private static void CreateContainerFolders(AccountInfo accountInfo, IEnumerable<ContainerInfo> containers)
536 var containerPaths = from container in containers
537 let containerPath = Path.Combine(accountInfo.AccountPath, container.Name)
538 where container.Name != FolderConstants.TrashContainer && !Directory.Exists(containerPath)
539 select containerPath;
541 foreach (var path in containerPaths)
543 Directory.CreateDirectory(path);
547 //Creates an appropriate action for each server file
548 private IEnumerable<CloudAction> ObjectsToActions(AccountInfo accountInfo,IEnumerable<ObjectInfo> remote)
551 throw new ArgumentNullException();
552 Contract.EndContractBlock();
553 var fileAgent = GetFileAgent(accountInfo);
555 //In order to avoid multiple iterations over the files, we iterate only once
556 //over the remote files
557 foreach (var objectInfo in remote)
559 var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
560 //and remove any matching objects from the list, adding them to the commonObjects list
562 if (fileAgent.Exists(relativePath))
564 //If a directory object already exists, we don't need to perform any other action
565 var localFile = fileAgent.GetFileSystemInfo(relativePath);
566 if (objectInfo.Content_Type == @"application/directory" && localFile is DirectoryInfo)
568 using (new SessionScope(FlushAction.Never))
570 var state = StatusKeeper.GetStateByFilePath(localFile.FullName);
571 //FileState.FindByFilePath(localFile.FullName);
572 //Common files should be checked on a per-case basis to detect differences, which is newer
574 yield return new CloudAction(accountInfo, CloudActionType.MustSynch,
575 localFile, objectInfo, state, accountInfo.BlockSize,
576 accountInfo.BlockHash);
581 //If there is no match we add them to the localFiles list
582 //but only if the file is not marked for deletion
583 var targetFile = Path.Combine(accountInfo.AccountPath, relativePath);
584 var fileStatus = StatusKeeper.GetFileStatus(targetFile);
585 if (fileStatus != FileStatus.Deleted)
587 //Remote files should be downloaded
588 yield return new CloudDownloadAction(accountInfo,objectInfo);
594 private static FileAgent GetFileAgent(AccountInfo accountInfo)
596 return AgentLocator<FileAgent>.Get(accountInfo.AccountPath);
599 private void ProcessDeletedFiles(AccountInfo accountInfo,IEnumerable<ObjectInfo> trashObjects)
601 var fileAgent = GetFileAgent(accountInfo);
602 foreach (var trashObject in trashObjects)
604 var barePath = trashObject.RelativeUrlToFilePath(accountInfo.UserName);
605 //HACK: Assume only the "pithos" container is used. Must find out what happens when
606 //deleting a file from a different container
607 var relativePath = Path.Combine("pithos", barePath);
608 fileAgent.Delete(relativePath);
613 private void RenameCloudFile(AccountInfo accountInfo,CloudMoveAction action)
615 if (accountInfo==null)
616 throw new ArgumentNullException("accountInfo");
618 throw new ArgumentNullException("action");
619 if (action.CloudFile==null)
620 throw new ArgumentException("CloudFile","action");
621 if (action.LocalFile==null)
622 throw new ArgumentException("LocalFile","action");
623 if (action.OldLocalFile==null)
624 throw new ArgumentException("OldLocalFile","action");
625 if (action.OldCloudFile==null)
626 throw new ArgumentException("OldCloudFile","action");
627 Contract.EndContractBlock();
630 var newFilePath = action.LocalFile.FullName;
632 //How do we handle concurrent renames and deletes/uploads/downloads?
633 //* A conflicting upload means that a file was renamed before it had a chance to finish uploading
634 // This should never happen as the network agent executes only one action at a time
635 //* A conflicting download means that the file was modified on the cloud. While we can go on and complete
636 // the rename, there may be a problem if the file is downloaded in blocks, as subsequent block requests for the
637 // same name will fail.
638 // This should never happen as the network agent executes only one action at a time.
639 //* A conflicting delete can happen if the rename was followed by a delete action that didn't have the chance
640 // to remove the rename from the queue.
641 // We can probably ignore this case. It will result in an error which should be ignored
644 //The local file is already renamed
645 StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Modified);
648 var account = action.CloudFile.Account ?? accountInfo.UserName;
649 var container = action.CloudFile.Container;
651 var client = new CloudFilesClient(accountInfo);
652 //TODO: What code is returned when the source file doesn't exist?
653 client.MoveObject(account, container, action.OldCloudFile.Name, container, action.CloudFile.Name);
655 StatusKeeper.SetFileStatus(newFilePath, FileStatus.Unchanged);
656 StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Normal);
657 NativeMethods.RaiseChangeNotification(newFilePath);
660 private void DeleteCloudFile(AccountInfo accountInfo, ObjectInfo cloudFile)
662 if (accountInfo == null)
663 throw new ArgumentNullException("accountInfo");
665 throw new ArgumentNullException("cloudFile");
667 if (String.IsNullOrWhiteSpace(cloudFile.Container))
668 throw new ArgumentException("Invalid container", "cloudFile");
669 Contract.EndContractBlock();
671 var fileAgent = GetFileAgent(accountInfo);
673 using ( log4net.ThreadContext.Stacks["DeleteCloudFile"].Push("Delete"))
675 var fileName= cloudFile.RelativeUrlToFilePath(accountInfo.UserName);
676 var info = fileAgent.GetFileSystemInfo(fileName);
677 var fullPath = info.FullName.ToLower();
679 StatusKeeper.SetFileOverlayStatus(fullPath, FileOverlayStatus.Modified);
681 var account = cloudFile.Account ?? accountInfo.UserName;
682 var container = cloudFile.Container ;//?? FolderConstants.PithosContainer;
684 var client = new CloudFilesClient(accountInfo);
685 client.DeleteObject(account, container, cloudFile.Name);
687 StatusKeeper.ClearFileStatus(fullPath);
692 private async Task DownloadCloudFile(AccountInfo accountInfo, ObjectInfo cloudFile , string filePath)
694 if (accountInfo == null)
695 throw new ArgumentNullException("accountInfo");
696 if (cloudFile == null)
697 throw new ArgumentNullException("cloudFile");
698 if (String.IsNullOrWhiteSpace(cloudFile.Account))
699 throw new ArgumentNullException("cloudFile");
700 if (String.IsNullOrWhiteSpace(cloudFile.Container))
701 throw new ArgumentNullException("cloudFile");
702 if (String.IsNullOrWhiteSpace(filePath))
703 throw new ArgumentNullException("filePath");
704 if (!Path.IsPathRooted(filePath))
705 throw new ArgumentException("The filePath must be rooted", "filePath");
706 Contract.EndContractBlock();
708 var localPath = Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
709 var relativeUrl = new Uri(cloudFile.Name, UriKind.Relative);
711 var url = relativeUrl.ToString();
712 if (cloudFile.Name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase))
716 //Are we already downloading or uploading the file?
717 using (var gate=NetworkGate.Acquire(localPath, NetworkOperation.Downloading))
721 //The file's hashmap will be stored in the same location with the extension .hashmap
722 //var hashPath = Path.Combine(FileAgent.CachePath, relativePath + ".hashmap");
724 var client = new CloudFilesClient(accountInfo);
725 var account = cloudFile.Account;
726 var container = cloudFile.Container;
728 if (cloudFile.Content_Type == @"application/directory")
730 if (!Directory.Exists(localPath))
731 Directory.CreateDirectory(localPath);
735 //Retrieve the hashmap from the server
736 var serverHash = await client.GetHashMap(account, container, url);
737 //If it's a small file
738 if (serverHash.Hashes.Count == 1)
739 //Download it in one go
741 DownloadEntireFileAsync(accountInfo, client, cloudFile, relativeUrl, localPath, serverHash);
742 //Otherwise download it block by block
744 await DownloadWithBlocks(accountInfo, client, cloudFile, relativeUrl, localPath, serverHash);
746 if (cloudFile.AllowedTo == "read")
748 var attributes = File.GetAttributes(localPath);
749 File.SetAttributes(localPath, attributes | FileAttributes.ReadOnly);
753 //Now we can store the object's metadata without worrying about ghost status entries
754 StatusKeeper.StoreInfo(localPath, cloudFile);
759 //Download a small file with a single GET operation
760 private async Task DownloadEntireFileAsync(AccountInfo accountInfo, CloudFilesClient client, ObjectInfo cloudFile, Uri relativeUrl, string filePath,TreeHash serverHash)
763 throw new ArgumentNullException("client");
765 throw new ArgumentNullException("cloudFile");
766 if (relativeUrl == null)
767 throw new ArgumentNullException("relativeUrl");
768 if (String.IsNullOrWhiteSpace(filePath))
769 throw new ArgumentNullException("filePath");
770 if (!Path.IsPathRooted(filePath))
771 throw new ArgumentException("The localPath must be rooted", "filePath");
772 Contract.EndContractBlock();
774 var localPath = Pithos.Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
775 //If the file already exists
776 if (File.Exists(localPath))
778 //First check with MD5 as this is a small file
779 var localMD5 = Signature.CalculateMD5(localPath);
780 var cloudHash=serverHash.TopHash.ToHashString();
781 if (localMD5==cloudHash)
783 //Then check with a treehash
784 var localTreeHash = Signature.CalculateTreeHash(localPath, serverHash.BlockSize, serverHash.BlockHash);
785 var localHash = localTreeHash.TopHash.ToHashString();
786 if (localHash==cloudHash)
790 var fileAgent = GetFileAgent(accountInfo);
791 //Calculate the relative file path for the new file
792 var relativePath = relativeUrl.RelativeUriToFilePath();
793 //The file will be stored in a temporary location while downloading with an extension .download
794 var tempPath = Path.Combine(fileAgent.CachePath, relativePath + ".download");
795 //Make sure the target folder exists. DownloadFileTask will not create the folder
796 var tempFolder = Path.GetDirectoryName(tempPath);
797 if (!Directory.Exists(tempFolder))
798 Directory.CreateDirectory(tempFolder);
800 //Download the object to the temporary location
801 await client.GetObject(cloudFile.Account, cloudFile.Container, relativeUrl.ToString(), tempPath);
803 //Create the local folder if it doesn't exist (necessary for shared objects)
804 var localFolder = Path.GetDirectoryName(localPath);
805 if (!Directory.Exists(localFolder))
806 Directory.CreateDirectory(localFolder);
807 //And move it to its actual location once downloading is finished
808 if (File.Exists(localPath))
809 File.Replace(tempPath,localPath,null,true);
811 File.Move(tempPath,localPath);
812 //Notify listeners that a local file has changed
813 StatusNotification.NotifyChangedFile(localPath);
818 //Download a file asynchronously using blocks
819 public async Task DownloadWithBlocks(AccountInfo accountInfo, CloudFilesClient client, ObjectInfo cloudFile, Uri relativeUrl, string filePath, TreeHash serverHash)
822 throw new ArgumentNullException("client");
823 if (cloudFile == null)
824 throw new ArgumentNullException("cloudFile");
825 if (relativeUrl == null)
826 throw new ArgumentNullException("relativeUrl");
827 if (String.IsNullOrWhiteSpace(filePath))
828 throw new ArgumentNullException("filePath");
829 if (!Path.IsPathRooted(filePath))
830 throw new ArgumentException("The filePath must be rooted", "filePath");
831 if (serverHash == null)
832 throw new ArgumentNullException("serverHash");
833 Contract.EndContractBlock();
835 var fileAgent = GetFileAgent(accountInfo);
836 var localPath = Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
838 //Calculate the relative file path for the new file
839 var relativePath = relativeUrl.RelativeUriToFilePath();
840 var blockUpdater = new BlockUpdater(fileAgent.CachePath, localPath, relativePath, serverHash);
844 //Calculate the file's treehash
845 var treeHash = await Signature.CalculateTreeHashAsync(localPath, serverHash.BlockSize, serverHash.BlockHash);
847 //And compare it with the server's hash
848 var upHashes = serverHash.GetHashesAsStrings();
849 var localHashes = treeHash.HashDictionary;
850 for (int i = 0; i < upHashes.Length; i++)
852 //For every non-matching hash
853 var upHash = upHashes[i];
854 if (!localHashes.ContainsKey(upHash))
856 if (blockUpdater.UseOrphan(i, upHash))
858 Log.InfoFormat("[BLOCK GET] ORPHAN FOUND for {0} of {1} for {2}", i, upHashes.Length, localPath);
861 Log.InfoFormat("[BLOCK GET] START {0} of {1} for {2}", i, upHashes.Length, localPath);
862 var start = i*serverHash.BlockSize;
863 //To download the last block just pass a null for the end of the range
865 if (i < upHashes.Length - 1 )
866 end= ((i + 1)*serverHash.BlockSize) ;
868 //Download the missing block
869 var block = await client.GetBlock(cloudFile.Account, cloudFile.Container, relativeUrl, start, end);
872 blockUpdater.StoreBlock(i, block);
875 Log.InfoFormat("[BLOCK GET] FINISH {0} of {1} for {2}", i, upHashes.Length, localPath);
879 //Want to avoid notifications if no changes were made
880 var hasChanges = blockUpdater.HasBlocks;
881 blockUpdater.Commit();
884 //Notify listeners that a local file has changed
885 StatusNotification.NotifyChangedFile(localPath);
887 Log.InfoFormat("[BLOCK GET] COMPLETE {0}", localPath);
891 private async Task UploadCloudFile(CloudAction action)
894 throw new ArgumentNullException("action");
895 Contract.EndContractBlock();
899 var accountInfo = action.AccountInfo;
901 var fileInfo = action.LocalFile;
903 if (fileInfo.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase))
906 var relativePath = fileInfo.AsRelativeTo(accountInfo.AccountPath);
907 if (relativePath.StartsWith(FolderConstants.OthersFolder))
909 var parts = relativePath.Split('\\');
910 var accountName = parts[1];
911 var oldName = accountInfo.UserName;
912 var absoluteUri = accountInfo.StorageUri.AbsoluteUri;
913 var nameIndex = absoluteUri.IndexOf(oldName);
914 var root = absoluteUri.Substring(0, nameIndex);
916 accountInfo = new AccountInfo
918 UserName = accountName,
919 AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
920 StorageUri = new Uri(root + accountName),
921 BlockHash = accountInfo.BlockHash,
922 BlockSize = accountInfo.BlockSize,
923 Token = accountInfo.Token
928 var fullFileName = fileInfo.GetProperCapitalization();
929 using (var gate = NetworkGate.Acquire(fullFileName, NetworkOperation.Uploading))
931 //Abort if the file is already being uploaded or downloaded
935 var cloudFile = action.CloudFile;
936 var account = cloudFile.Account ?? accountInfo.UserName;
938 var client = new CloudFilesClient(accountInfo);
939 //Even if GetObjectInfo times out, we can proceed with the upload
940 var info = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name);
942 //If this is a read-only file, do not upload changes
943 if (info.AllowedTo == "read")
946 //TODO: Check how a directory hash is calculated -> All dirs seem to have the same hash
947 if (fileInfo is DirectoryInfo)
949 //If the directory doesn't exist the Hash property will be empty
950 if (String.IsNullOrWhiteSpace(info.Hash))
951 //Go on and create the directory
952 client.PutObject(account, cloudFile.Container, cloudFile.Name, fullFileName, String.Empty, "application/directory");
957 var cloudHash = info.Hash.ToLower();
959 var hash = action.LocalHash.Value;
960 var topHash = action.TopHash.Value;
962 //If the file hashes match, abort the upload
963 if (hash == cloudHash || topHash == cloudHash)
965 //but store any metadata changes
966 StatusKeeper.StoreInfo(fullFileName, info);
967 Log.InfoFormat("Skip upload of {0}, hashes match", fullFileName);
972 //Mark the file as modified while we upload it
973 StatusKeeper.SetFileOverlayStatus(fullFileName, FileOverlayStatus.Modified);
976 //Upload even small files using the Hashmap. The server may already contain
979 //First, calculate the tree hash
980 var treeHash = await Signature.CalculateTreeHashAsync(fullFileName, accountInfo.BlockSize,
981 accountInfo.BlockHash);
983 await UploadWithHashMap(accountInfo, cloudFile, fileInfo as FileInfo, cloudFile.Name, treeHash);
985 //If everything succeeds, change the file and overlay status to normal
986 StatusKeeper.SetFileState(fullFileName, FileStatus.Unchanged, FileOverlayStatus.Normal);
988 //Notify the Shell to update the overlays
989 NativeMethods.RaiseChangeNotification(fullFileName);
990 StatusNotification.NotifyChangedFile(fullFileName);
992 catch (AggregateException ex)
994 var exc = ex.InnerException as WebException;
996 throw ex.InnerException;
997 if (HandleUploadWebException(action, exc))
1001 catch (WebException ex)
1003 if (HandleUploadWebException(action, ex))
1007 catch (Exception ex)
1009 Log.Error("Unexpected error while uploading file", ex);
1015 private bool IsDeletedFile(CloudAction action)
1017 var key = GetFileKey(action.CloudFile);
1019 if (_deletedFiles.TryGetValue(key, out entryDate))
1021 //If the delete entry was created after this action, abort the action
1022 if (entryDate > action.Created)
1024 //Otherwise, remove the stale entry
1025 _deletedFiles.TryRemove(key, out entryDate);
1030 private bool HandleUploadWebException(CloudAction action, WebException exc)
1032 var response = exc.Response as HttpWebResponse;
1033 if (response == null)
1035 if (response.StatusCode == HttpStatusCode.Unauthorized)
1037 Log.Error("Not allowed to upload file", exc);
1038 var message = String.Format("Not allowed to uplad file {0}", action.LocalFile.FullName);
1039 StatusKeeper.SetFileState(action.LocalFile.FullName, FileStatus.Unchanged, FileOverlayStatus.Normal);
1040 StatusNotification.NotifyChange(message, TraceLevel.Warning);
1046 public async Task UploadWithHashMap(AccountInfo accountInfo,ObjectInfo cloudFile,FileInfo fileInfo,string url,TreeHash treeHash)
1048 if (accountInfo == null)
1049 throw new ArgumentNullException("accountInfo");
1050 if (cloudFile==null)
1051 throw new ArgumentNullException("cloudFile");
1052 if (fileInfo == null)
1053 throw new ArgumentNullException("fileInfo");
1054 if (String.IsNullOrWhiteSpace(url))
1055 throw new ArgumentNullException(url);
1057 throw new ArgumentNullException("treeHash");
1058 if (String.IsNullOrWhiteSpace(cloudFile.Container) )
1059 throw new ArgumentException("Invalid container","cloudFile");
1060 Contract.EndContractBlock();
1062 var fullFileName = fileInfo.GetProperCapitalization();
1064 var account = cloudFile.Account ?? accountInfo.UserName;
1065 var container = cloudFile.Container ;
1067 var client = new CloudFilesClient(accountInfo);
1068 //Send the hashmap to the server
1069 var missingHashes = await client.PutHashMap(account, container, url, treeHash);
1070 //If the server returns no missing hashes, we are done
1071 while (missingHashes.Count > 0)
1074 var buffer = new byte[accountInfo.BlockSize];
1075 foreach (var missingHash in missingHashes)
1077 //Find the proper block
1078 var blockIndex = treeHash.HashDictionary[missingHash];
1079 var offset = blockIndex*accountInfo.BlockSize;
1081 var read = fileInfo.Read(buffer, offset, accountInfo.BlockSize);
1085 //And upload the block
1086 await client.PostBlock(account, container, buffer, 0, read);
1087 Log.InfoFormat("[BLOCK] Block {0} of {1} uploaded", blockIndex, fullFileName);
1089 catch (Exception exc)
1091 Log.ErrorFormat("[ERROR] uploading block {0} of {1}\n{2}", blockIndex, fullFileName, exc);
1096 //Repeat until there are no more missing hashes
1097 missingHashes = await client.PutHashMap(account, container, url, treeHash);
1102 public void AddAccount(AccountInfo accountInfo)
1104 if (!_accounts.Contains(accountInfo))
1105 _accounts.Add(accountInfo);