2 /* -----------------------------------------------------------------------
3 * <copyright file="NetworkAgent.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * The views and conclusions contained in the software and
35 * documentation are those of the authors and should not be
36 * interpreted as representing official policies, either expressed
37 * or implied, of GRNET S.A.
39 * -----------------------------------------------------------------------
44 using System.Collections.Generic;
45 using System.ComponentModel.Composition;
46 using System.Diagnostics;
47 using System.Diagnostics.Contracts;
50 using System.Reflection;
51 using System.Threading;
52 using System.Threading.Tasks;
53 using Castle.ActiveRecord;
54 using Pithos.Interfaces;
58 namespace Pithos.Core.Agents
61 public class NetworkAgent
63 private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
65 private Agent<CloudAction> _agent;
67 [System.ComponentModel.Composition.Import]
68 private DeleteAgent _deleteAgent { get; set; }
70 [System.ComponentModel.Composition.Import]
71 public IStatusKeeper StatusKeeper { get; set; }
73 private IStatusNotification _statusNotification;
74 public IStatusNotification StatusNotification
76 get { return _statusNotification; }
79 _statusNotification = value;
80 _deleteAgent.StatusNotification = value;
85 [System.ComponentModel.Composition.Import]
86 public IPithosSettings Settings { get; set; }
88 //The Proceed signals the poll agent that it can proceed with polling.
89 //Essentially it stops the poll agent to give priority to the network agent
90 //Initially the event is signalled because we don't need to pause
91 private readonly AsyncManualResetEvent _proceedEvent = new AsyncManualResetEvent(true);
93 public AsyncManualResetEvent ProceedEvent
95 get { return _proceedEvent; }
104 if (Log.IsDebugEnabled)
105 Log.Debug("Starting Network Agent");
107 _agent = Agent<CloudAction>.Start(inbox =>
112 _deleteAgent.ProceedEvent.Wait();
113 var message = inbox.Receive();
114 var process=message.Then(Process,inbox.CancellationToken);
115 inbox.LoopAsync(process, loop);
122 private async Task Process(CloudAction action)
125 throw new ArgumentNullException("action");
126 if (action.AccountInfo==null)
127 throw new ArgumentException("The action.AccountInfo is empty","action");
128 Contract.EndContractBlock();
133 using (log4net.ThreadContext.Stacks["Operation"].Push(action.ToString()))
136 var cloudFile = action.CloudFile;
137 var downloadPath = action.GetDownloadPath();
141 StatusNotification.SetPithosStatus(PithosStatus.LocalSyncing,"Processing");
142 _proceedEvent.Reset();
143 //UpdateStatus(PithosStatus.Syncing);
144 var accountInfo = action.AccountInfo;
146 if (action.Action == CloudActionType.DeleteCloud)
148 //Redirect deletes to the delete agent
149 _deleteAgent.Post((CloudDeleteAction)action);
151 if (_deleteAgent.IsDeletedFile(action))
153 //Clear the status of already deleted files to avoid reprocessing
154 if (action.LocalFile != null)
155 StatusKeeper.ClearFileStatus(action.LocalFile.FullName);
159 switch (action.Action)
161 case CloudActionType.UploadUnconditional:
162 //Abort if the file was deleted before we reached this point
163 await UploadCloudFile(action);
165 case CloudActionType.DownloadUnconditional:
166 await DownloadCloudFile(accountInfo, cloudFile, downloadPath);
168 case CloudActionType.RenameCloud:
169 var moveAction = (CloudMoveAction)action;
170 RenameCloudFile(accountInfo, moveAction);
172 case CloudActionType.RenameLocal:
173 RenameLocalFile(accountInfo, action);
175 case CloudActionType.MustSynch:
176 if (!File.Exists(downloadPath) && !Directory.Exists(downloadPath))
178 await DownloadCloudFile(accountInfo, cloudFile, downloadPath);
182 await SyncFiles(accountInfo, action);
187 Log.InfoFormat("End Processing {0}:{1}->{2}", action.Action, action.LocalFile,
188 action.CloudFile.Name);
190 catch (WebException exc)
192 Log.ErrorFormat("[WEB ERROR] {0} : {1} -> {2} due to exception\r\n{3}", action.Action, action.LocalFile, action.CloudFile, exc);
194 catch (OperationCanceledException)
198 catch (DirectoryNotFoundException)
200 Log.ErrorFormat("{0} : {1} -> {2} failed because the directory was not found.\n Rescheduling a delete",
201 action.Action, action.LocalFile, action.CloudFile);
202 //Post a delete action for the missing file
203 Post(new CloudDeleteAction(action));
205 catch (FileNotFoundException)
207 Log.ErrorFormat("{0} : {1} -> {2} failed because the file was not found.\n Rescheduling a delete",
208 action.Action, action.LocalFile, action.CloudFile);
209 //Post a delete action for the missing file
210 Post(new CloudDeleteAction(action));
212 catch (Exception exc)
214 Log.ErrorFormat("[REQUEUE] {0} : {1} -> {2} due to exception\r\n{3}",
215 action.Action, action.LocalFile, action.CloudFile, exc);
223 UpdateStatus(PithosStatus.LocalComplete);
229 private void UpdateStatus(PithosStatus status)
231 StatusNotification.SetPithosStatus(status);
232 //StatusNotification.Notify(new Notification());
235 private void RenameLocalFile(AccountInfo accountInfo, CloudAction action)
237 if (accountInfo == null)
238 throw new ArgumentNullException("accountInfo");
240 throw new ArgumentNullException("action");
241 if (action.LocalFile == null)
242 throw new ArgumentException("The action's local file is not specified", "action");
243 if (!Path.IsPathRooted(action.LocalFile.FullName))
244 throw new ArgumentException("The action's local file path must be absolute", "action");
245 if (action.CloudFile == null)
246 throw new ArgumentException("The action's cloud file is not specified", "action");
247 Contract.EndContractBlock();
248 using (ThreadContext.Stacks["Operation"].Push("RenameLocalFile"))
251 //We assume that the local file already exists, otherwise the poll agent
252 //would have issued a download request
254 var currentInfo = action.CloudFile;
255 var previousInfo = action.CloudFile.Previous;
256 var fileAgent = FileAgent.GetFileAgent(accountInfo);
258 var previousRelativepath = previousInfo.RelativeUrlToFilePath(accountInfo.UserName);
259 var previousFile = fileAgent.GetFileSystemInfo(previousRelativepath);
261 //In every case we need to move the local file first
262 MoveLocalFile(accountInfo, previousFile, fileAgent, currentInfo);
266 private void MoveLocalFile(AccountInfo accountInfo, FileSystemInfo previousFile, FileAgent fileAgent,
267 ObjectInfo currentInfo)
269 var currentRelativepath = currentInfo.RelativeUrlToFilePath(accountInfo.UserName);
270 var newPath = Path.Combine(fileAgent.RootPath, currentRelativepath);
272 var isFile= (previousFile is FileInfo);
273 var previousFullPath = isFile?
274 FileInfoExtensions.GetProperFilePathCapitalization(previousFile.FullName):
275 FileInfoExtensions.GetProperDirectoryCapitalization(previousFile.FullName);
277 using (var gateOld = NetworkGate.Acquire(previousFullPath, NetworkOperation.Renaming))
278 using (var gateNew = NetworkGate.Acquire(newPath,NetworkOperation.Renaming))
279 using (new SessionScope(FlushAction.Auto))
282 (previousFile as FileInfo).MoveTo(newPath);
285 (previousFile as DirectoryInfo).MoveTo(newPath);
287 var state = StatusKeeper.GetStateByFilePath(previousFullPath);
288 state.FilePath = newPath;
290 StatusKeeper.SetFileState(previousFullPath,FileStatus.Deleted,FileOverlayStatus.Deleted);
294 private async Task SyncFiles(AccountInfo accountInfo,CloudAction action)
296 if (accountInfo == null)
297 throw new ArgumentNullException("accountInfo");
299 throw new ArgumentNullException("action");
300 if (action.LocalFile==null)
301 throw new ArgumentException("The action's local file is not specified","action");
302 if (!Path.IsPathRooted(action.LocalFile.FullName))
303 throw new ArgumentException("The action's local file path must be absolute","action");
304 if (action.CloudFile== null)
305 throw new ArgumentException("The action's cloud file is not specified", "action");
306 Contract.EndContractBlock();
307 using (ThreadContext.Stacks["Operation"].Push("SyncFiles"))
310 var localFile = action.LocalFile;
311 var cloudFile = action.CloudFile;
312 var downloadPath = action.LocalFile.GetProperCapitalization();
314 var cloudHash = cloudFile.Hash.ToLower();
315 var previousCloudHash = cloudFile.PreviousHash.ToLower();
316 var localHash = action.TreeHash.Value.TopHash.ToHashString();// LocalHash.Value.ToLower();
317 //var topHash = action.TopHash.Value.ToLower();
319 //At this point we know that an object has changed on the server and that a local
320 //file already exists. We need to decide whether the file has only changed on
321 //the server or there is a conflicting change on the client.
324 //If the hashes match, we are done
325 if (cloudHash == localHash)
327 Log.InfoFormat("Skipping {0}, hashes match", downloadPath);
331 //The hashes DON'T match. We need to sync
333 // If the previous tophash matches the local tophash, the file was only changed on the server.
334 if (localHash == previousCloudHash)
336 await DownloadCloudFile(accountInfo, cloudFile, downloadPath);
340 //If the previous and local hash don't match, there was a local conflict
341 //that was not uploaded to the server. We have a conflict
342 ReportConflict(downloadPath);
347 private void ReportConflict(string downloadPath)
349 if (String.IsNullOrWhiteSpace(downloadPath))
350 throw new ArgumentNullException("downloadPath");
351 Contract.EndContractBlock();
353 StatusKeeper.SetFileOverlayStatus(downloadPath, FileOverlayStatus.Conflict);
354 UpdateStatus(PithosStatus.HasConflicts);
355 var message = String.Format("Conflict detected for file {0}", downloadPath);
357 StatusNotification.NotifyChange(message, TraceLevel.Warning);
360 public void Post(CloudAction cloudAction)
362 if (cloudAction == null)
363 throw new ArgumentNullException("cloudAction");
364 if (cloudAction.AccountInfo==null)
365 throw new ArgumentException("The CloudAction.AccountInfo is empty","cloudAction");
366 Contract.EndContractBlock();
368 _deleteAgent.ProceedEvent.Wait();
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;
377 if (localFile.Length > accountInfo.BlockSize)
378 cloudAction.TopHash =
379 new Lazy<string>(() => Signature.CalculateTreeHashAsync(localFile,
380 accountInfo.BlockSize,
381 accountInfo.BlockHash, Settings.HashingParallelism).Result
382 .TopHash.ToHashString());
385 cloudAction.TopHash = new Lazy<string>(() => cloudAction.LocalHash.Value);
391 //The hash for a directory is the empty string
392 cloudAction.TopHash = new Lazy<string>(() => String.Empty);
396 if (cloudAction is CloudDeleteAction)
397 _deleteAgent.Post((CloudDeleteAction)cloudAction);
399 _agent.Post(cloudAction);
403 public IEnumerable<CloudAction> GetEnumerable()
405 return _agent.GetEnumerable();
408 public Task GetDeleteAwaiter()
410 return _deleteAgent.ProceedEvent.WaitAsync();
412 public CancellationToken CancellationToken
414 get { return _agent.CancellationToken; }
417 private static FileAgent GetFileAgent(AccountInfo accountInfo)
419 return AgentLocator<FileAgent>.Get(accountInfo.AccountPath);
424 private void RenameCloudFile(AccountInfo accountInfo,CloudMoveAction action)
426 if (accountInfo==null)
427 throw new ArgumentNullException("accountInfo");
429 throw new ArgumentNullException("action");
430 if (action.CloudFile==null)
431 throw new ArgumentException("CloudFile","action");
432 if (action.LocalFile==null)
433 throw new ArgumentException("LocalFile","action");
434 if (action.OldLocalFile==null)
435 throw new ArgumentException("OldLocalFile","action");
436 if (action.OldCloudFile==null)
437 throw new ArgumentException("OldCloudFile","action");
438 Contract.EndContractBlock();
440 using (ThreadContext.Stacks["Operation"].Push("RenameCloudFile"))
443 var newFilePath = action.LocalFile.FullName;
445 //How do we handle concurrent renames and deletes/uploads/downloads?
446 //* A conflicting upload means that a file was renamed before it had a chance to finish uploading
447 // This should never happen as the network agent executes only one action at a time
448 //* A conflicting download means that the file was modified on the cloud. While we can go on and complete
449 // the rename, there may be a problem if the file is downloaded in blocks, as subsequent block requests for the
450 // same name will fail.
451 // This should never happen as the network agent executes only one action at a time.
452 //* A conflicting delete can happen if the rename was followed by a delete action that didn't have the chance
453 // to remove the rename from the queue.
454 // We can probably ignore this case. It will result in an error which should be ignored
457 //The local file is already renamed
458 StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Modified);
461 var account = action.CloudFile.Account ?? accountInfo.UserName;
462 var container = action.CloudFile.Container;
464 var client = new CloudFilesClient(accountInfo);
465 //TODO: What code is returned when the source file doesn't exist?
466 client.MoveObject(account, container, action.OldCloudFile.Name, container, action.CloudFile.Name);
468 StatusKeeper.SetFileStatus(newFilePath, FileStatus.Unchanged);
469 StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Normal);
470 NativeMethods.RaiseChangeNotification(newFilePath);
475 private async Task DownloadCloudFile(AccountInfo accountInfo, ObjectInfo cloudFile , string filePath)
477 if (accountInfo == null)
478 throw new ArgumentNullException("accountInfo");
479 if (cloudFile == null)
480 throw new ArgumentNullException("cloudFile");
481 if (String.IsNullOrWhiteSpace(cloudFile.Account))
482 throw new ArgumentNullException("cloudFile");
483 if (String.IsNullOrWhiteSpace(cloudFile.Container))
484 throw new ArgumentNullException("cloudFile");
485 if (String.IsNullOrWhiteSpace(filePath))
486 throw new ArgumentNullException("filePath");
487 if (!Path.IsPathRooted(filePath))
488 throw new ArgumentException("The filePath must be rooted", "filePath");
489 Contract.EndContractBlock();
491 using (ThreadContext.Stacks["Operation"].Push("DownloadCloudFile"))
494 var localPath = Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
495 var relativeUrl = new Uri(cloudFile.Name, UriKind.Relative);
497 var url = relativeUrl.ToString();
498 if (cloudFile.Name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase))
502 //Are we already downloading or uploading the file?
503 using (var gate = NetworkGate.Acquire(localPath, NetworkOperation.Downloading))
508 var client = new CloudFilesClient(accountInfo);
509 var account = cloudFile.Account;
510 var container = cloudFile.Container;
512 if (cloudFile.IsDirectory)
514 if (!Directory.Exists(localPath))
517 Directory.CreateDirectory(localPath);
518 if (Log.IsDebugEnabled)
519 Log.DebugFormat("Created Directory [{0}]",localPath);
523 var localInfo = new FileInfo(localPath);
524 if (localInfo.Exists && localInfo.Length == 0)
526 Log.WarnFormat("Malformed directory object detected for [{0}]",localPath);
528 Directory.CreateDirectory(localPath);
529 if (Log.IsDebugEnabled)
530 Log.DebugFormat("Created Directory [{0}]", localPath);
536 var isChanged = IsObjectChanged(cloudFile, localPath);
539 //Retrieve the hashmap from the server
540 var serverHash = await client.GetHashMap(account, container, url);
541 //If it's a small file
542 if (serverHash.Hashes.Count == 1)
543 //Download it in one go
545 DownloadEntireFileAsync(accountInfo, client, cloudFile, relativeUrl, localPath,
547 //Otherwise download it block by block
550 DownloadWithBlocks(accountInfo, client, cloudFile, relativeUrl, localPath,
553 if (cloudFile.AllowedTo == "read")
555 var attributes = File.GetAttributes(localPath);
556 File.SetAttributes(localPath, attributes | FileAttributes.ReadOnly);
561 //Now we can store the object's metadata without worrying about ghost status entries
562 StatusKeeper.StoreInfo(localPath, cloudFile);
568 private bool IsObjectChanged(ObjectInfo cloudFile, string localPath)
570 //If the target is a directory, there are no changes to download
571 if (Directory.Exists(localPath))
573 //If the file doesn't exist, we have a chagne
574 if (!File.Exists(localPath))
576 //If there is no stored state, we have a change
577 var localState = StatusKeeper.GetStateByFilePath(localPath);
578 if (localState == null)
581 var info = new FileInfo(localPath);
582 var shortHash = info.ComputeShortHash();
583 //If the file is different from the stored state, we have a change
584 if (localState.ShortHash != shortHash)
586 //If the top hashes differ, we have a change
587 return (localState.Checksum != cloudFile.Hash);
590 //Download a small file with a single GET operation
591 private async Task DownloadEntireFileAsync(AccountInfo accountInfo, CloudFilesClient client, ObjectInfo cloudFile, Uri relativeUrl, string filePath,TreeHash serverHash)
594 throw new ArgumentNullException("client");
596 throw new ArgumentNullException("cloudFile");
597 if (relativeUrl == null)
598 throw new ArgumentNullException("relativeUrl");
599 if (String.IsNullOrWhiteSpace(filePath))
600 throw new ArgumentNullException("filePath");
601 if (!Path.IsPathRooted(filePath))
602 throw new ArgumentException("The localPath must be rooted", "filePath");
603 if (cloudFile.IsDirectory)
604 throw new ArgumentException("cloudFile is a directory, not a file","cloudFile");
605 Contract.EndContractBlock();
607 var localPath = Pithos.Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
608 StatusNotification.SetPithosStatus(PithosStatus.LocalSyncing,String.Format("Downloading {0}",Path.GetFileName(localPath)));
609 StatusNotification.Notify(new CloudNotification { Data = cloudFile });
611 var fileAgent = GetFileAgent(accountInfo);
612 //Calculate the relative file path for the new file
613 var relativePath = relativeUrl.RelativeUriToFilePath();
614 //The file will be stored in a temporary location while downloading with an extension .download
615 var tempPath = Path.Combine(fileAgent.CachePath, relativePath + ".download");
616 //Make sure the target folder exists. DownloadFileTask will not create the folder
617 var tempFolder = Path.GetDirectoryName(tempPath);
618 if (!Directory.Exists(tempFolder))
619 Directory.CreateDirectory(tempFolder);
621 //Download the object to the temporary location
622 await client.GetObject(cloudFile.Account, cloudFile.Container, relativeUrl.ToString(), tempPath);
624 //Create the local folder if it doesn't exist (necessary for shared objects)
625 var localFolder = Path.GetDirectoryName(localPath);
626 if (!Directory.Exists(localFolder))
629 Directory.CreateDirectory(localFolder);
633 //A file may already exist that has the same name as the new folder.
634 //This may be an artifact of the way Pithos handles directories
635 var fileInfo = new FileInfo(localFolder);
636 if (fileInfo.Exists && fileInfo.Length == 0)
638 Log.WarnFormat("Malformed directory object detected for [{0}]", localFolder);
640 Directory.CreateDirectory(localFolder);
645 //And move it to its actual location once downloading is finished
646 if (File.Exists(localPath))
647 File.Replace(tempPath,localPath,null,true);
649 File.Move(tempPath,localPath);
650 //Notify listeners that a local file has changed
651 StatusNotification.NotifyChangedFile(localPath);
656 //Download a file asynchronously using blocks
657 public async Task DownloadWithBlocks(AccountInfo accountInfo, CloudFilesClient client, ObjectInfo cloudFile, Uri relativeUrl, string filePath, TreeHash serverHash)
660 throw new ArgumentNullException("client");
661 if (cloudFile == null)
662 throw new ArgumentNullException("cloudFile");
663 if (relativeUrl == null)
664 throw new ArgumentNullException("relativeUrl");
665 if (String.IsNullOrWhiteSpace(filePath))
666 throw new ArgumentNullException("filePath");
667 if (!Path.IsPathRooted(filePath))
668 throw new ArgumentException("The filePath must be rooted", "filePath");
669 if (serverHash == null)
670 throw new ArgumentNullException("serverHash");
671 if (cloudFile.IsDirectory)
672 throw new ArgumentException("cloudFile is a directory, not a file", "cloudFile");
673 Contract.EndContractBlock();
675 var fileAgent = GetFileAgent(accountInfo);
676 var localPath = Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
678 //Calculate the relative file path for the new file
679 var relativePath = relativeUrl.RelativeUriToFilePath();
680 var blockUpdater = new BlockUpdater(fileAgent.CachePath, localPath, relativePath, serverHash);
683 StatusNotification.SetPithosStatus(PithosStatus.LocalSyncing,String.Format("Calculating hashmap for {0} before download",Path.GetFileName(localPath)));
684 //Calculate the file's treehash
685 var treeHash = await Signature.CalculateTreeHashAsync(localPath, serverHash.BlockSize, serverHash.BlockHash, 2);
687 //And compare it with the server's hash
688 var upHashes = serverHash.GetHashesAsStrings();
689 var localHashes = treeHash.HashDictionary;
690 ReportDownloadProgress(Path.GetFileName(localPath),0,upHashes.Length,cloudFile.Bytes);
691 for (int i = 0; i < upHashes.Length; i++)
693 //For every non-matching hash
694 var upHash = upHashes[i];
695 if (!localHashes.ContainsKey(upHash))
697 StatusNotification.Notify(new CloudNotification { Data = cloudFile });
699 if (blockUpdater.UseOrphan(i, upHash))
701 Log.InfoFormat("[BLOCK GET] ORPHAN FOUND for {0} of {1} for {2}", i, upHashes.Length, localPath);
704 Log.InfoFormat("[BLOCK GET] START {0} of {1} for {2}", i, upHashes.Length, localPath);
705 var start = i*serverHash.BlockSize;
706 //To download the last block just pass a null for the end of the range
708 if (i < upHashes.Length - 1 )
709 end= ((i + 1)*serverHash.BlockSize) ;
711 //Download the missing block
712 var block = await client.GetBlock(cloudFile.Account, cloudFile.Container, relativeUrl, start, end);
715 blockUpdater.StoreBlock(i, block);
718 Log.InfoFormat("[BLOCK GET] FINISH {0} of {1} for {2}", i, upHashes.Length, localPath);
720 ReportDownloadProgress(Path.GetFileName(localPath), i, upHashes.Length, cloudFile.Bytes);
723 //Want to avoid notifications if no changes were made
724 var hasChanges = blockUpdater.HasBlocks;
725 blockUpdater.Commit();
728 //Notify listeners that a local file has changed
729 StatusNotification.NotifyChangedFile(localPath);
731 Log.InfoFormat("[BLOCK GET] COMPLETE {0}", localPath);
735 private async Task UploadCloudFile(CloudAction action)
738 throw new ArgumentNullException("action");
739 Contract.EndContractBlock();
740 using(ThreadContext.Stacks["Operation"].Push("UploadCloudFile"))
745 var accountInfo = action.AccountInfo;
747 var fileInfo = action.LocalFile;
749 if (fileInfo.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase))
753 if (action.FileState == null)
754 action.FileState = StatusKeeper.GetStateByFilePath(fileInfo.FullName);
755 if (action.FileState == null)
757 Log.WarnFormat("File [{0}] has no local state. It was probably created by a download action",fileInfo.FullName);
760 //Do not upload files in conflict
761 if (action.FileState.FileStatus == FileStatus.Conflict )
763 Log.InfoFormat("Skipping file in conflict [{0}]",fileInfo.FullName);
766 if (action.FileState.FileStatus == FileStatus.Forbidden)
768 Log.InfoFormat("Skipping forbidden file [{0}]",fileInfo.FullName);
772 var relativePath = fileInfo.AsRelativeTo(accountInfo.AccountPath);
773 if (relativePath.StartsWith(FolderConstants.OthersFolder))
775 var parts = relativePath.Split('\\');
776 var accountName = parts[1];
777 var oldName = accountInfo.UserName;
778 var absoluteUri = accountInfo.StorageUri.AbsoluteUri;
779 var nameIndex = absoluteUri.IndexOf(oldName, StringComparison.Ordinal);
780 var root = absoluteUri.Substring(0, nameIndex);
782 accountInfo = new AccountInfo
784 UserName = accountName,
785 AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
786 StorageUri = new Uri(root + accountName),
787 BlockHash = accountInfo.BlockHash,
788 BlockSize = accountInfo.BlockSize,
789 Token = accountInfo.Token
794 var fullFileName = fileInfo.GetProperCapitalization();
795 using (var gate = NetworkGate.Acquire(fullFileName, NetworkOperation.Uploading))
797 //Abort if the file is already being uploaded or downloaded
801 var cloudFile = action.CloudFile;
802 var account = cloudFile.Account ?? accountInfo.UserName;
806 var client = new CloudFilesClient(accountInfo);
807 //Even if GetObjectInfo times out, we can proceed with the upload
808 var cloudInfo = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name);
810 //If this is a read-only file, do not upload changes
811 if (cloudInfo.AllowedTo == "read")
814 //TODO: Check how a directory hash is calculated -> All dirs seem to have the same hash
815 if (fileInfo is DirectoryInfo)
817 //If the directory doesn't exist the Hash property will be empty
818 if (String.IsNullOrWhiteSpace(cloudInfo.Hash))
819 //Go on and create the directory
821 client.PutObject(account, cloudFile.Container, cloudFile.Name, fullFileName,
822 String.Empty, "application/directory");
827 var cloudHash = cloudInfo.Hash.ToLower();
829 StatusNotification.Notify(new StatusNotification(String.Format("Hashing {0} for Upload",fileInfo.Name)));
831 //TODO: This is the same as the calculation for the Local Hash!
832 //First, calculate the tree hash
834 var treeHash = await Signature.CalculateTreeHashAsync(fullFileName, accountInfo.BlockSize,
835 accountInfo.BlockHash, 2);
839 var treeHash = action.TreeHash.Value;
840 var topHash = treeHash.TopHash.ToHashString();
842 //var topHash = action.TopHash.Value;
844 //If the file hashes match, abort the upload
845 if (topHash == cloudHash /*|| topHash == cloudHash*/)
847 //but store any metadata changes
848 StatusKeeper.StoreInfo(fullFileName, cloudInfo);
849 Log.InfoFormat("Skip upload of {0}, hashes match", fullFileName);
854 //Mark the file as modified while we upload it
855 StatusKeeper.SetFileOverlayStatus(fullFileName, FileOverlayStatus.Modified);
858 //Upload even small files using the Hashmap. The server may already contain
861 //TODO: If the upload fails with a 403, abort it and mark conflict
864 UploadWithHashMap(accountInfo, cloudFile, fileInfo as FileInfo, cloudFile.Name, treeHash);
866 //If everything succeeds, change the file and overlay status to normal
867 StatusKeeper.SetFileState(fullFileName, FileStatus.Unchanged, FileOverlayStatus.Normal);
869 catch (WebException exc)
871 var response=(exc.Response as HttpWebResponse);
872 if (response == null)
874 if (response.StatusCode == HttpStatusCode.Forbidden)
876 StatusKeeper.SetFileState(fileInfo.FullName,FileStatus.Forbidden, FileOverlayStatus.Conflict);
880 //Notify the Shell to update the overlays
881 NativeMethods.RaiseChangeNotification(fullFileName);
882 StatusNotification.NotifyChangedFile(fullFileName);
884 catch (AggregateException ex)
886 var exc = ex.InnerException as WebException;
888 throw ex.InnerException;
889 if (HandleUploadWebException(action, exc))
893 catch (WebException ex)
895 if (HandleUploadWebException(action, ex))
901 Log.Error("Unexpected error while uploading file", ex);
909 private bool HandleUploadWebException(CloudAction action, WebException exc)
911 var response = exc.Response as HttpWebResponse;
912 if (response == null)
914 if (response.StatusCode == HttpStatusCode.Unauthorized)
916 Log.Error("Not allowed to upload file", exc);
917 var message = String.Format("Not allowed to uplad file {0}", action.LocalFile.FullName);
918 StatusKeeper.SetFileState(action.LocalFile.FullName, FileStatus.Unchanged, FileOverlayStatus.Normal);
919 StatusNotification.NotifyChange(message, TraceLevel.Warning);
925 public async Task UploadWithHashMap(AccountInfo accountInfo,ObjectInfo cloudFile,FileInfo fileInfo,string url,TreeHash treeHash)
927 if (accountInfo == null)
928 throw new ArgumentNullException("accountInfo");
930 throw new ArgumentNullException("cloudFile");
931 if (fileInfo == null)
932 throw new ArgumentNullException("fileInfo");
933 if (String.IsNullOrWhiteSpace(url))
934 throw new ArgumentNullException(url);
936 throw new ArgumentNullException("treeHash");
937 if (String.IsNullOrWhiteSpace(cloudFile.Container) )
938 throw new ArgumentException("Invalid container","cloudFile");
939 Contract.EndContractBlock();
941 StatusNotification.Notify(new StatusNotification(String.Format("Uploading {0}", fileInfo.Name)));
943 var fullFileName = fileInfo.GetProperCapitalization();
945 var account = cloudFile.Account ?? accountInfo.UserName;
946 var container = cloudFile.Container ;
948 var client = new CloudFilesClient(accountInfo);
949 //Send the hashmap to the server
950 var missingHashes = await client.PutHashMap(account, container, url, treeHash);
952 ReportUploadProgress(fileInfo.Name,block++, missingHashes.Count, fileInfo.Length);
953 //If the server returns no missing hashes, we are done
954 while (missingHashes.Count > 0)
957 var buffer = new byte[accountInfo.BlockSize];
958 foreach (var missingHash in missingHashes)
960 //Find the proper block
961 var blockIndex = treeHash.HashDictionary[missingHash];
962 long offset = blockIndex*accountInfo.BlockSize;
964 var read = fileInfo.Read(buffer, offset, accountInfo.BlockSize);
968 //And upload the block
969 await client.PostBlock(account, container, buffer, 0, read);
970 Log.InfoFormat("[BLOCK] Block {0} of {1} uploaded", blockIndex, fullFileName);
972 catch (Exception exc)
974 Log.Error(String.Format("Uploading block {0} of {1}", blockIndex, fullFileName), exc);
976 ReportUploadProgress(fileInfo.Name,block++, missingHashes.Count, fileInfo.Length);
979 //Repeat until there are no more missing hashes
980 missingHashes = await client.PutHashMap(account, container, url, treeHash);
983 ReportUploadProgress(fileInfo.Name, missingHashes.Count, missingHashes.Count, fileInfo.Length);
986 private void ReportUploadProgress(string fileName,int block, int totalBlocks, long fileSize)
988 StatusNotification.Notify(totalBlocks == 0
989 ? new ProgressNotification(fileName, "Uploading", 1, 1, fileSize)
990 : new ProgressNotification(fileName, "Uploading", block, totalBlocks, fileSize));
993 private void ReportDownloadProgress(string fileName,int block, int totalBlocks, long fileSize)
995 StatusNotification.Notify(totalBlocks == 0
996 ? new ProgressNotification(fileName, "Downloading", 1, 1, fileSize)
997 : new ProgressNotification(fileName, "Downloading", block, totalBlocks, fileSize));