2 /* -----------------------------------------------------------------------
3 * <copyright file="CloudTransferAction.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 * -----------------------------------------------------------------------
43 using System.Diagnostics.Contracts;
45 using System.Threading;
46 using Pithos.Interfaces;
49 namespace Pithos.Core.Agents
51 public enum CloudActionType
55 DownloadUnconditional,
62 public class CloudAction
64 public AccountInfo AccountInfo { get; set; }
65 public CloudActionType Action { get; set; }
66 public FileSystemInfo LocalFile { get; set; }
67 public ObjectInfo CloudFile { get; set; }
68 public FileState FileState { get; set; }
69 public string Container { get; set; }
71 public readonly DateTime Created = DateTime.Now;
74 public Lazy<TreeHash> TreeHash { get; protected set; }
75 //public Lazy<string> TopHash { get; set; }
78 [ContractInvariantMethod]
79 private void Invariants()
81 Contract.Invariant(AccountInfo!=null);
86 get { return CloudFile!=null && AccountInfo.UserName != CloudFile.Account; }
89 protected CloudAction(AccountInfo accountInfo,CloudActionType action)
91 if (accountInfo==null)
92 throw new ArgumentNullException("accountInfo");
93 Contract.EndContractBlock();
96 AccountInfo = accountInfo;
99 public CloudAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm)
100 : this(accountInfo,action)
102 LocalFile = localFile.WithProperCapitalization();
103 CloudFile = cloudFile;
106 if (LocalFile == null)
109 TreeHash = new Lazy<TreeHash>(() => Signature.CalculateTreeHash(LocalFile, blockSize,algorithm),
110 LazyThreadSafetyMode.ExecutionAndPublication);
113 //Calculate the download path for the cloud file
114 public string GetDownloadPath()
116 if (CloudFile == null)
118 var filePath = CloudFile.RelativeUrlToFilePath(AccountInfo.UserName);
119 return Path.Combine(AccountInfo.AccountPath, filePath);
122 public override string ToString()
124 return String.Format("{0}:{1}->{2}", Action, LocalFile.FullName, CloudFile.Name);
127 protected static ObjectInfo CreateObjectInfoFor(AccountInfo accountInfo, FileSystemInfo fileInfo)
129 Contract.Requires(accountInfo!=null);
130 Contract.Requires(fileInfo!=null);
131 Contract.Ensures(Contract.Result<ObjectInfo>()!=null);
133 var capitalizedFileInfo = fileInfo.WithProperCapitalization();
134 var fullLocalName = capitalizedFileInfo.FullName;
135 var othersPath = Path.Combine(accountInfo.AccountPath, FolderConstants.OthersFolder);
137 var isShared = fullLocalName.StartsWith(othersPath, StringComparison.InvariantCultureIgnoreCase);
140 var pathRelativeToOther = fullLocalName.Substring(othersPath.Length + 1);
141 var otherParts = pathRelativeToOther.Split('\\');
142 var otherName = otherParts[0];
143 var otherContainer = otherParts[1];
144 return new ObjectInfo
147 Container = otherContainer,
148 Name = String.Join("/", otherParts.Splice(2))
151 return new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, fileInfo);
155 public class CloudDownloadAction:CloudAction
157 public CloudDownloadAction(AccountInfo accountInfo, ObjectInfo cloudFile)
158 :base(accountInfo,CloudActionType.DownloadUnconditional)
160 if (String.IsNullOrWhiteSpace(cloudFile.Container))
161 throw new ArgumentException("CloudFile.Container","cloudFile");
162 Contract.EndContractBlock();
164 CloudFile = cloudFile;
167 [ContractInvariantMethod]
168 private void Invariants()
170 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
173 public override string ToString()
175 return String.Format("{0}: _ <- {1}", this.Action, this.CloudFile.Name);
179 public class CloudDeleteAction:CloudAction
181 public CloudDeleteAction(AccountInfo accountInfo,FileSystemInfo fileInfo, FileState fileState)
182 : this(accountInfo,fileInfo,CreateObjectInfoFor(accountInfo, fileInfo),fileState)
186 public CloudDeleteAction(AccountInfo accountInfo, FileSystemInfo fileInfo,ObjectInfo cloudFile, FileState fileState)
187 : base(accountInfo,CloudActionType.DeleteCloud)
189 CloudFile = cloudFile;
190 LocalFile = fileInfo;
191 FileState = fileState;
194 public CloudDeleteAction(CloudAction action)
195 : this(action.AccountInfo,action.LocalFile,action.CloudFile,action.FileState)
198 [ContractInvariantMethod]
199 private void Invariants()
201 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
204 public override string ToString()
206 return String.Format("{0}: _ ->{1}", Action, CloudFile.Name);
211 public class CloudUploadAction:CloudAction
213 public CloudUploadAction(AccountInfo accountInfo, FileSystemInfo fileInfo, FileState state, int blockSize, string algorithm)
214 : base(accountInfo, CloudActionType.UploadUnconditional,fileInfo,CreateObjectInfoFor(accountInfo,fileInfo),state,blockSize,algorithm)
218 [ContractInvariantMethod]
219 private void Invariants()
221 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
226 public class CloudMoveAction:CloudAction
228 public ObjectInfo OldCloudFile { get; set; }
230 public FileSystemInfo OldLocalFile { get; set; }
232 public CloudMoveAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo oldFile, FileSystemInfo newFile)
233 :base(accountInfo,action)
236 CloudFile = CreateObjectInfoFor(accountInfo, newFile);
238 OldLocalFile = oldFile;
239 OldCloudFile = CreateObjectInfoFor(accountInfo, oldFile);
241 //This is a rename operation, a hash will not be used
242 TreeHash = new Lazy<TreeHash>(() => Network.TreeHash.Empty, LazyThreadSafetyMode.ExecutionAndPublication);
245 public override string ToString()
247 return String.Format("{0}:{1}->{2}", Action, OldCloudFile.Name, CloudFile.Name);