Merge branch 'master' of https://code.grnet.gr/git/pithos-ms-client
[pithos-ms-client] / trunk / Pithos.Core / Agents / CloudTransferAction.cs
index 7bd5005..8a81b1b 100644 (file)
@@ -1,3 +1,44 @@
+#region
+/* -----------------------------------------------------------------------
+ * <copyright file="CloudTransferAction.cs" company="GRNet">
+ * 
+ * Copyright 2011-2012 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>
+ * -----------------------------------------------------------------------
+ */
+#endregion
 using System;
 using System.Diagnostics.Contracts;
 using System.IO;
 using System;
 using System.Diagnostics.Contracts;
 using System.IO;
@@ -14,7 +55,8 @@ namespace Pithos.Core.Agents
         DownloadUnconditional,
         DeleteLocal,
         DeleteCloud,
         DownloadUnconditional,
         DeleteLocal,
         DeleteCloud,
-        RenameCloud
+        RenameCloud,
+        RenameLocal
     }
 
     public class CloudAction
     }
 
     public class CloudAction
@@ -26,14 +68,11 @@ namespace Pithos.Core.Agents
         public FileState FileState { get; set; }
         public string Container { get; set; }
 
         public FileState FileState { get; set; }
         public string Container { get; set; }
 
+        public readonly DateTime Created = DateTime.Now;
 
 
-        public Lazy<string> LocalHash { get; protected set; }
-        private Lazy<string> _topHash;
-        public Lazy<string> TopHash
-        {
-            get { return _topHash; }
-            set { _topHash = value; }
-        }
+
+        public Lazy<TreeHash> TreeHash { get; protected set; }
+        //public Lazy<string> TopHash { get; set; }
 
 
         [ContractInvariantMethod]
 
 
         [ContractInvariantMethod]
@@ -42,6 +81,11 @@ namespace Pithos.Core.Agents
             Contract.Invariant(AccountInfo!=null);
         }
 
             Contract.Invariant(AccountInfo!=null);
         }
 
+        public bool IsShared
+        {
+            get { return  CloudFile!=null && AccountInfo.UserName != CloudFile.Account; }
+        }
+
         protected CloudAction(AccountInfo accountInfo,CloudActionType action)
         {
             if (accountInfo==null)
         protected CloudAction(AccountInfo accountInfo,CloudActionType action)
         {
             if (accountInfo==null)
@@ -55,15 +99,18 @@ namespace Pithos.Core.Agents
         public CloudAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm)
             : this(accountInfo,action)
         {
         public CloudAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm)
             : this(accountInfo,action)
         {
+            if(blockSize<=0)
+                throw new ArgumentOutOfRangeException("blockSize");
+            Contract.EndContractBlock();
             LocalFile = localFile.WithProperCapitalization();
             CloudFile = cloudFile;
             FileState = state;
             LocalFile = localFile.WithProperCapitalization();
             CloudFile = cloudFile;
             FileState = state;
-            if (LocalFile != null)
-            {
+            
+            if (LocalFile == null) 
+                return;
 
 
-                LocalHash = new Lazy<string>(() => LocalFile.CalculateHash(blockSize,algorithm),
-                                             LazyThreadSafetyMode.ExecutionAndPublication);
-            }
+            TreeHash = new Lazy<TreeHash>(() => Signature.CalculateTreeHash(LocalFile, blockSize,algorithm),
+                                            LazyThreadSafetyMode.ExecutionAndPublication);
         }
 
         //Calculate the download path for the cloud file
         }
 
         //Calculate the download path for the cloud file
@@ -77,7 +124,45 @@ namespace Pithos.Core.Agents
 
         public override string ToString()
         {
 
         public override string ToString()
         {
-            return String.Format("{0}:{1}->{2}", this.Action, this.LocalFile.FullName, this.CloudFile.Name);
+            return String.Format("{0}:{1}->{2}", Action, LocalFile.FullName, CloudFile.Name);
+        }
+
+        protected static ObjectInfo CreateObjectInfoFor(AccountInfo accountInfo, FileSystemInfo fileInfo)
+        {
+            if(accountInfo==null)
+                throw new ArgumentNullException("accountInfo");
+            if(fileInfo==null)
+                throw new ArgumentNullException("fileInfo");
+            Contract.Ensures(Contract.Result<ObjectInfo>()!=null);
+            Contract.EndContractBlock();
+
+            var capitalizedFileInfo = fileInfo.WithProperCapitalization();
+            var fullLocalName = capitalizedFileInfo.FullName;
+            var othersPath = Path.Combine(accountInfo.AccountPath, FolderConstants.OthersFolder);
+
+            ObjectInfo objectInfo;
+
+            var isShared = fullLocalName.StartsWith(othersPath, StringComparison.InvariantCultureIgnoreCase);
+            if (isShared)
+            {                
+                var pathRelativeToOther = fullLocalName.Substring(othersPath.Length + 1);
+                var otherParts = pathRelativeToOther.Split('\\');
+                var otherName = otherParts[0];
+                var otherContainer = otherParts[1];
+                objectInfo=new ObjectInfo
+                           {
+                               Account = otherName, 
+                               Container = otherContainer, 
+                               Name = String.Join("/", otherParts.Splice(2))
+                           };
+            }
+            else
+                objectInfo=new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, fileInfo);
+            
+            objectInfo.Content_Type= (fileInfo is DirectoryInfo)
+                                    ?   "appication/directory"
+                                    :   "application/octet-stream";
+            return objectInfo;
         }
     }    
 
         }
     }    
 
@@ -101,26 +186,27 @@ namespace Pithos.Core.Agents
 
         public override string ToString()
         {
 
         public override string ToString()
         {
-            return String.Format("{0}: _ <- {1}", this.Action, this.CloudFile.Name);
+            return String.Format("{0}: _ <- {1}", Action, CloudFile.Name);
         }
         
     }
     public class CloudDeleteAction:CloudAction
     {
         public CloudDeleteAction(AccountInfo accountInfo,FileSystemInfo fileInfo, FileState fileState)
         }
         
     }
     public class CloudDeleteAction:CloudAction
     {
         public CloudDeleteAction(AccountInfo accountInfo,FileSystemInfo fileInfo, FileState fileState)
-            : this(accountInfo,new ObjectInfo(accountInfo.AccountPath,accountInfo.UserName,fileInfo),fileState)
-        {
+            : this(accountInfo,fileInfo,CreateObjectInfoFor(accountInfo, fileInfo),fileState)
+        {            
         }
 
         }
 
-        public CloudDeleteAction(AccountInfo accountInfo, ObjectInfo cloudFile, FileState fileState) 
+        public CloudDeleteAction(AccountInfo accountInfo, FileSystemInfo fileInfo,ObjectInfo cloudFile, FileState fileState) 
             : base(accountInfo,CloudActionType.DeleteCloud)
         {
             CloudFile = cloudFile;
             : base(accountInfo,CloudActionType.DeleteCloud)
         {
             CloudFile = cloudFile;
+            LocalFile = fileInfo;
             FileState = fileState;
         }
 
         public CloudDeleteAction(CloudAction action)
             FileState = fileState;
         }
 
         public CloudDeleteAction(CloudAction action)
-            : this(action.AccountInfo,action.CloudFile,action.FileState)
+            : this(action.AccountInfo,action.LocalFile,action.CloudFile,action.FileState)
         {}
 
         [ContractInvariantMethod]
         {}
 
         [ContractInvariantMethod]
@@ -131,7 +217,7 @@ namespace Pithos.Core.Agents
 
         public override string ToString()
         {
 
         public override string ToString()
         {
-            return String.Format("{0}: _ ->{1}", this.Action, this.CloudFile.Name);
+            return String.Format("{0}: _ ->{1}", Action, CloudFile.Name);
         }
 
     }
         }
 
     }
@@ -139,8 +225,7 @@ namespace Pithos.Core.Agents
     public class CloudUploadAction:CloudAction
     {
         public CloudUploadAction(AccountInfo accountInfo, FileSystemInfo fileInfo, FileState state, int blockSize, string algorithm)
     public class CloudUploadAction:CloudAction
     {
         public CloudUploadAction(AccountInfo accountInfo, FileSystemInfo fileInfo, FileState state, int blockSize, string algorithm)
-            : base(accountInfo, CloudActionType.UploadUnconditional, fileInfo, 
-            new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName,fileInfo), state, blockSize, algorithm)
+            : base(accountInfo, CloudActionType.UploadUnconditional,fileInfo,CreateObjectInfoFor(accountInfo,fileInfo),state,blockSize,algorithm)             
         {
         }
 
         {
         }
 
@@ -162,18 +247,18 @@ namespace Pithos.Core.Agents
             :base(accountInfo,action)
         {
             LocalFile = newFile;
             :base(accountInfo,action)
         {
             LocalFile = newFile;
-            CloudFile = new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, newFile);
+            CloudFile = CreateObjectInfoFor(accountInfo, newFile);
             
             OldLocalFile = oldFile;
             
             OldLocalFile = oldFile;
-            OldCloudFile = new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, oldFile);
+            OldCloudFile = CreateObjectInfoFor(accountInfo, oldFile);
 
             //This is a rename operation, a hash will not be used
 
             //This is a rename operation, a hash will not be used
-            LocalHash = new Lazy<string>(() => String.Empty, LazyThreadSafetyMode.ExecutionAndPublication);
+            TreeHash = new Lazy<TreeHash>(() => Network.TreeHash.Empty, LazyThreadSafetyMode.ExecutionAndPublication);
         }
 
         public override string ToString()
         {
         }
 
         public override string ToString()
         {
-            return String.Format("{0}:{1}->{2}", this.Action, OldCloudFile.Name, CloudFile.Name);
+            return String.Format("{0}:{1}->{2}", Action, OldCloudFile.Name, CloudFile.Name);
         }
 
     }
         }
 
     }