root / trunk / Pithos.Core / Agents / NetworkAgent.cs @ 89472316
History | View | Annotate | Download (37.9 kB)
1 | 255f5f86 | Panagiotis Kanavos | #region |
---|---|---|---|
2 | 255f5f86 | Panagiotis Kanavos | /* ----------------------------------------------------------------------- |
3 | 255f5f86 | Panagiotis Kanavos | * <copyright file="NetworkAgent.cs" company="GRNet"> |
4 | 255f5f86 | Panagiotis Kanavos | * |
5 | 255f5f86 | Panagiotis Kanavos | * Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 | 255f5f86 | Panagiotis Kanavos | * |
7 | 255f5f86 | Panagiotis Kanavos | * Redistribution and use in source and binary forms, with or |
8 | 255f5f86 | Panagiotis Kanavos | * without modification, are permitted provided that the following |
9 | 255f5f86 | Panagiotis Kanavos | * conditions are met: |
10 | 255f5f86 | Panagiotis Kanavos | * |
11 | 255f5f86 | Panagiotis Kanavos | * 1. Redistributions of source code must retain the above |
12 | 255f5f86 | Panagiotis Kanavos | * copyright notice, this list of conditions and the following |
13 | 255f5f86 | Panagiotis Kanavos | * disclaimer. |
14 | 255f5f86 | Panagiotis Kanavos | * |
15 | 255f5f86 | Panagiotis Kanavos | * 2. Redistributions in binary form must reproduce the above |
16 | 255f5f86 | Panagiotis Kanavos | * copyright notice, this list of conditions and the following |
17 | 255f5f86 | Panagiotis Kanavos | * disclaimer in the documentation and/or other materials |
18 | 255f5f86 | Panagiotis Kanavos | * provided with the distribution. |
19 | 255f5f86 | Panagiotis Kanavos | * |
20 | 255f5f86 | Panagiotis Kanavos | * |
21 | 255f5f86 | Panagiotis Kanavos | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 | 255f5f86 | Panagiotis Kanavos | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | 255f5f86 | Panagiotis Kanavos | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 | 255f5f86 | Panagiotis Kanavos | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 | 255f5f86 | Panagiotis Kanavos | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | 255f5f86 | Panagiotis Kanavos | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | 255f5f86 | Panagiotis Kanavos | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 | 255f5f86 | Panagiotis Kanavos | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 | 255f5f86 | Panagiotis Kanavos | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | 255f5f86 | Panagiotis Kanavos | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 | 255f5f86 | Panagiotis Kanavos | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 | 255f5f86 | Panagiotis Kanavos | * POSSIBILITY OF SUCH DAMAGE. |
33 | 255f5f86 | Panagiotis Kanavos | * |
34 | 255f5f86 | Panagiotis Kanavos | * The views and conclusions contained in the software and |
35 | 255f5f86 | Panagiotis Kanavos | * documentation are those of the authors and should not be |
36 | 255f5f86 | Panagiotis Kanavos | * interpreted as representing official policies, either expressed |
37 | 255f5f86 | Panagiotis Kanavos | * or implied, of GRNET S.A. |
38 | 255f5f86 | Panagiotis Kanavos | * </copyright> |
39 | 255f5f86 | Panagiotis Kanavos | * ----------------------------------------------------------------------- |
40 | 255f5f86 | Panagiotis Kanavos | */ |
41 | 255f5f86 | Panagiotis Kanavos | #endregion |
42 | f3d080df | Panagiotis Kanavos | |
43 | 34bdb91d | Panagiotis Kanavos | //TODO: Now there is a UUID tag. This can be used for renames/moves |
44 | 34bdb91d | Panagiotis Kanavos | |
45 | 34bdb91d | Panagiotis Kanavos | |
46 | f3d080df | Panagiotis Kanavos | using System; |
47 | 4f6d51d4 | Panagiotis Kanavos | using System.Collections.Concurrent; |
48 | 9c4346c9 | Panagiotis Kanavos | using System.Collections.Generic; |
49 | 9c4346c9 | Panagiotis Kanavos | using System.ComponentModel.Composition; |
50 | 9c4346c9 | Panagiotis Kanavos | using System.Diagnostics; |
51 | 9c4346c9 | Panagiotis Kanavos | using System.Diagnostics.Contracts; |
52 | 9c4346c9 | Panagiotis Kanavos | using System.IO; |
53 | 9c4346c9 | Panagiotis Kanavos | using System.Linq; |
54 | 0af3141d | Panagiotis Kanavos | using System.Net; |
55 | 0b346191 | Panagiotis Kanavos | using System.Threading; |
56 | 9c4346c9 | Panagiotis Kanavos | using System.Threading.Tasks; |
57 | 3c76f045 | Panagiotis Kanavos | using System.Threading.Tasks.Dataflow; |
58 | e81dd1f6 | Panagiotis Kanavos | using Castle.ActiveRecord; |
59 | 9c4346c9 | Panagiotis Kanavos | using Pithos.Interfaces; |
60 | 5ce54458 | Panagiotis Kanavos | using Pithos.Network; |
61 | cfed7823 | Panagiotis Kanavos | using log4net; |
62 | 9c4346c9 | Panagiotis Kanavos | |
63 | 9c4346c9 | Panagiotis Kanavos | namespace Pithos.Core.Agents |
64 | 9c4346c9 | Panagiotis Kanavos | { |
65 | f3d080df | Panagiotis Kanavos | //TODO: Ensure all network operations use exact casing. Pithos is case sensitive |
66 | 9c4346c9 | Panagiotis Kanavos | [Export] |
67 | 9c4346c9 | Panagiotis Kanavos | public class NetworkAgent |
68 | 9c4346c9 | Panagiotis Kanavos | { |
69 | 9c4346c9 | Panagiotis Kanavos | private Agent<CloudAction> _agent; |
70 | 9c4346c9 | Panagiotis Kanavos | |
71 | 3742088d | Panagiotis Kanavos | [System.ComponentModel.Composition.Import] |
72 | 3742088d | Panagiotis Kanavos | private DeleteAgent _deleteAgent=new DeleteAgent(); |
73 | 0b346191 | Panagiotis Kanavos | |
74 | e81dd1f6 | Panagiotis Kanavos | [System.ComponentModel.Composition.Import] |
75 | 9c4346c9 | Panagiotis Kanavos | public IStatusKeeper StatusKeeper { get; set; } |
76 | 9c4346c9 | Panagiotis Kanavos | |
77 | 9c4346c9 | Panagiotis Kanavos | public IStatusNotification StatusNotification { get; set; } |
78 | 9c4346c9 | Panagiotis Kanavos | |
79 | 5120f3cb | Panagiotis Kanavos | private static readonly ILog Log = LogManager.GetLogger("NetworkAgent"); |
80 | cfed7823 | Panagiotis Kanavos | |
81 | 4f6d51d4 | Panagiotis Kanavos | private readonly ConcurrentBag<AccountInfo> _accounts = new ConcurrentBag<AccountInfo>(); |
82 | 5ce54458 | Panagiotis Kanavos | |
83 | 133f83c2 | Panagiotis Kanavos | [System.ComponentModel.Composition.Import] |
84 | 133f83c2 | Panagiotis Kanavos | public IPithosSettings Settings { get; set; } |
85 | 540b8cf8 | Panagiotis Kanavos | |
86 | 38ac43a6 | Panagiotis Kanavos | //The Proceed signals the poll agent that it can proceed with polling. |
87 | 38ac43a6 | Panagiotis Kanavos | //Essentially it stops the poll agent to give priority to the network agent |
88 | aa7ac00e | Panagiotis Kanavos | //Initially the event is signalled because we don't need to pause |
89 | 38ac43a6 | Panagiotis Kanavos | private readonly AsyncManualResetEvent _proceedEvent = new AsyncManualResetEvent(true); |
90 | aa7ac00e | Panagiotis Kanavos | |
91 | 38ac43a6 | Panagiotis Kanavos | public AsyncManualResetEvent ProceedEvent |
92 | aa7ac00e | Panagiotis Kanavos | { |
93 | 38ac43a6 | Panagiotis Kanavos | get { return _proceedEvent; } |
94 | aa7ac00e | Panagiotis Kanavos | } |
95 | 303596f9 | Panagiotis Kanavos | |
96 | 29a6b387 | Panagiotis Kanavos | |
97 | 73cdd135 | Panagiotis Kanavos | public void Start() |
98 | 9c4346c9 | Panagiotis Kanavos | { |
99 | 9c4346c9 | Panagiotis Kanavos | _agent = Agent<CloudAction>.Start(inbox => |
100 | 9c4346c9 | Panagiotis Kanavos | { |
101 | 9c4346c9 | Panagiotis Kanavos | Action loop = null; |
102 | 9c4346c9 | Panagiotis Kanavos | loop = () => |
103 | 9c4346c9 | Panagiotis Kanavos | { |
104 | 38ac43a6 | Panagiotis Kanavos | _deleteAgent.ProceedEvent.Wait(); |
105 | 9c4346c9 | Panagiotis Kanavos | var message = inbox.Receive(); |
106 | a64c87c8 | Panagiotis Kanavos | var process=message.Then(Process,inbox.CancellationToken); |
107 | a27aa447 | Panagiotis Kanavos | inbox.LoopAsync(process, loop); |
108 | a64c87c8 | Panagiotis Kanavos | }; |
109 | a64c87c8 | Panagiotis Kanavos | loop(); |
110 | f3d080df | Panagiotis Kanavos | }); |
111 | f3d080df | Panagiotis Kanavos | |
112 | a64c87c8 | Panagiotis Kanavos | } |
113 | a27aa447 | Panagiotis Kanavos | |
114 | 27361404 | Panagiotis Kanavos | private async Task Process(CloudAction action) |
115 | a64c87c8 | Panagiotis Kanavos | { |
116 | a64c87c8 | Panagiotis Kanavos | if (action == null) |
117 | a64c87c8 | Panagiotis Kanavos | throw new ArgumentNullException("action"); |
118 | c53aa229 | Panagiotis Kanavos | if (action.AccountInfo==null) |
119 | c53aa229 | Panagiotis Kanavos | throw new ArgumentException("The action.AccountInfo is empty","action"); |
120 | a64c87c8 | Panagiotis Kanavos | Contract.EndContractBlock(); |
121 | a27aa447 | Panagiotis Kanavos | |
122 | aa7ac00e | Panagiotis Kanavos | |
123 | aa7ac00e | Panagiotis Kanavos | |
124 | c53aa229 | Panagiotis Kanavos | |
125 | 5120f3cb | Panagiotis Kanavos | using (log4net.ThreadContext.Stacks["NETWORK"].Push("PROCESS")) |
126 | cfed7823 | Panagiotis Kanavos | { |
127 | 27361404 | Panagiotis Kanavos | Log.InfoFormat("[ACTION] Start Processing {0}", action); |
128 | cfed7823 | Panagiotis Kanavos | |
129 | 1bfc38f1 | Panagiotis Kanavos | var cloudFile = action.CloudFile; |
130 | 1bfc38f1 | Panagiotis Kanavos | var downloadPath = action.GetDownloadPath(); |
131 | cfed7823 | Panagiotis Kanavos | |
132 | cfed7823 | Panagiotis Kanavos | try |
133 | aa7ac00e | Panagiotis Kanavos | { |
134 | 38ac43a6 | Panagiotis Kanavos | _proceedEvent.Reset(); |
135 | aa7ac00e | Panagiotis Kanavos | UpdateStatus(PithosStatus.Syncing); |
136 | aa7ac00e | Panagiotis Kanavos | var accountInfo = action.AccountInfo; |
137 | aa7ac00e | Panagiotis Kanavos | |
138 | 14ecd267 | Panagiotis Kanavos | if (action.Action == CloudActionType.DeleteCloud) |
139 | eae84ae8 | Panagiotis Kanavos | { |
140 | 14ecd267 | Panagiotis Kanavos | //Redirect deletes to the delete agent |
141 | e5b65606 | Panagiotis Kanavos | _deleteAgent.Post((CloudDeleteAction)action); |
142 | 14ecd267 | Panagiotis Kanavos | } |
143 | 3742088d | Panagiotis Kanavos | if (_deleteAgent.IsDeletedFile(action)) |
144 | 14ecd267 | Panagiotis Kanavos | { |
145 | 14ecd267 | Panagiotis Kanavos | //Clear the status of already deleted files to avoid reprocessing |
146 | 3c76f045 | Panagiotis Kanavos | if (action.LocalFile != null) |
147 | 3c76f045 | Panagiotis Kanavos | this.StatusKeeper.ClearFileStatus(action.LocalFile.FullName); |
148 | 14ecd267 | Panagiotis Kanavos | } |
149 | 14ecd267 | Panagiotis Kanavos | else |
150 | 14ecd267 | Panagiotis Kanavos | { |
151 | 14ecd267 | Panagiotis Kanavos | switch (action.Action) |
152 | 14ecd267 | Panagiotis Kanavos | { |
153 | 14ecd267 | Panagiotis Kanavos | case CloudActionType.UploadUnconditional: |
154 | 14ecd267 | Panagiotis Kanavos | //Abort if the file was deleted before we reached this point |
155 | 039a89e5 | Panagiotis Kanavos | await UploadCloudFile(action); |
156 | 14ecd267 | Panagiotis Kanavos | break; |
157 | 14ecd267 | Panagiotis Kanavos | case CloudActionType.DownloadUnconditional: |
158 | 039a89e5 | Panagiotis Kanavos | await DownloadCloudFile(accountInfo, cloudFile, downloadPath); |
159 | 14ecd267 | Panagiotis Kanavos | break; |
160 | 14ecd267 | Panagiotis Kanavos | case CloudActionType.RenameCloud: |
161 | e5b65606 | Panagiotis Kanavos | var moveAction = (CloudMoveAction)action; |
162 | 039a89e5 | Panagiotis Kanavos | RenameCloudFile(accountInfo, moveAction); |
163 | 14ecd267 | Panagiotis Kanavos | break; |
164 | b666b39a | Panagiotis Kanavos | case CloudActionType.RenameLocal: |
165 | b666b39a | Panagiotis Kanavos | RenameLocalFile(accountInfo, action); |
166 | b666b39a | Panagiotis Kanavos | break; |
167 | 14ecd267 | Panagiotis Kanavos | case CloudActionType.MustSynch: |
168 | 14ecd267 | Panagiotis Kanavos | if (!File.Exists(downloadPath) && !Directory.Exists(downloadPath)) |
169 | 14ecd267 | Panagiotis Kanavos | { |
170 | 039a89e5 | Panagiotis Kanavos | await DownloadCloudFile(accountInfo, cloudFile, downloadPath); |
171 | 14ecd267 | Panagiotis Kanavos | } |
172 | 14ecd267 | Panagiotis Kanavos | else |
173 | 14ecd267 | Panagiotis Kanavos | { |
174 | 039a89e5 | Panagiotis Kanavos | await SyncFiles(accountInfo, action); |
175 | 14ecd267 | Panagiotis Kanavos | } |
176 | 14ecd267 | Panagiotis Kanavos | break; |
177 | 14ecd267 | Panagiotis Kanavos | } |
178 | cfed7823 | Panagiotis Kanavos | } |
179 | cfed7823 | Panagiotis Kanavos | Log.InfoFormat("[ACTION] End Processing {0}:{1}->{2}", action.Action, action.LocalFile, |
180 | cfed7823 | Panagiotis Kanavos | action.CloudFile.Name); |
181 | cfed7823 | Panagiotis Kanavos | } |
182 | a0dcfcc9 | Panagiotis Kanavos | catch (WebException exc) |
183 | a0dcfcc9 | Panagiotis Kanavos | { |
184 | 73cdd135 | Panagiotis Kanavos | Log.ErrorFormat("[WEB ERROR] {0} : {1} -> {2} due to exception\r\n{3}", action.Action, action.LocalFile, action.CloudFile, exc); |
185 | a0dcfcc9 | Panagiotis Kanavos | } |
186 | cfed7823 | Panagiotis Kanavos | catch (OperationCanceledException) |
187 | cfed7823 | Panagiotis Kanavos | { |
188 | cfed7823 | Panagiotis Kanavos | throw; |
189 | cfed7823 | Panagiotis Kanavos | } |
190 | 73cdd135 | Panagiotis Kanavos | catch (DirectoryNotFoundException) |
191 | 73cdd135 | Panagiotis Kanavos | { |
192 | 73cdd135 | Panagiotis Kanavos | Log.ErrorFormat("{0} : {1} -> {2} failed because the directory was not found.\n Rescheduling a delete", |
193 | 73cdd135 | Panagiotis Kanavos | action.Action, action.LocalFile, action.CloudFile); |
194 | 73cdd135 | Panagiotis Kanavos | //Post a delete action for the missing file |
195 | e5b65606 | Panagiotis Kanavos | Post(new CloudDeleteAction(action)); |
196 | 73cdd135 | Panagiotis Kanavos | } |
197 | 73cdd135 | Panagiotis Kanavos | catch (FileNotFoundException) |
198 | cfed7823 | Panagiotis Kanavos | { |
199 | cfed7823 | Panagiotis Kanavos | Log.ErrorFormat("{0} : {1} -> {2} failed because the file was not found.\n Rescheduling a delete", |
200 | 73cdd135 | Panagiotis Kanavos | action.Action, action.LocalFile, action.CloudFile); |
201 | 1bfc38f1 | Panagiotis Kanavos | //Post a delete action for the missing file |
202 | 1bfc38f1 | Panagiotis Kanavos | Post(new CloudDeleteAction(action)); |
203 | cfed7823 | Panagiotis Kanavos | } |
204 | cfed7823 | Panagiotis Kanavos | catch (Exception exc) |
205 | cfed7823 | Panagiotis Kanavos | { |
206 | cfed7823 | Panagiotis Kanavos | Log.ErrorFormat("[REQUEUE] {0} : {1} -> {2} due to exception\r\n{3}", |
207 | cfed7823 | Panagiotis Kanavos | action.Action, action.LocalFile, action.CloudFile, exc); |
208 | cfed7823 | Panagiotis Kanavos | |
209 | cfed7823 | Panagiotis Kanavos | _agent.Post(action); |
210 | e5b65606 | Panagiotis Kanavos | } |
211 | e5b65606 | Panagiotis Kanavos | finally |
212 | e5b65606 | Panagiotis Kanavos | { |
213 | aa7ac00e | Panagiotis Kanavos | if (_agent.IsEmpty) |
214 | 38ac43a6 | Panagiotis Kanavos | _proceedEvent.Set(); |
215 | aa7ac00e | Panagiotis Kanavos | UpdateStatus(PithosStatus.InSynch); |
216 | e5b65606 | Panagiotis Kanavos | } |
217 | cfed7823 | Panagiotis Kanavos | } |
218 | cfed7823 | Panagiotis Kanavos | } |
219 | cfed7823 | Panagiotis Kanavos | |
220 | b666b39a | Panagiotis Kanavos | |
221 | eae84ae8 | Panagiotis Kanavos | private void UpdateStatus(PithosStatus status) |
222 | eae84ae8 | Panagiotis Kanavos | { |
223 | eae84ae8 | Panagiotis Kanavos | StatusKeeper.SetPithosStatus(status); |
224 | eae84ae8 | Panagiotis Kanavos | StatusNotification.Notify(new Notification()); |
225 | eae84ae8 | Panagiotis Kanavos | } |
226 | eae84ae8 | Panagiotis Kanavos | |
227 | b666b39a | Panagiotis Kanavos | private void RenameLocalFile(AccountInfo accountInfo, CloudAction action) |
228 | b666b39a | Panagiotis Kanavos | { |
229 | b666b39a | Panagiotis Kanavos | if (accountInfo == null) |
230 | b666b39a | Panagiotis Kanavos | throw new ArgumentNullException("accountInfo"); |
231 | b666b39a | Panagiotis Kanavos | if (action == null) |
232 | b666b39a | Panagiotis Kanavos | throw new ArgumentNullException("action"); |
233 | b666b39a | Panagiotis Kanavos | if (action.LocalFile == null) |
234 | b666b39a | Panagiotis Kanavos | throw new ArgumentException("The action's local file is not specified", "action"); |
235 | b666b39a | Panagiotis Kanavos | if (!Path.IsPathRooted(action.LocalFile.FullName)) |
236 | b666b39a | Panagiotis Kanavos | throw new ArgumentException("The action's local file path must be absolute", "action"); |
237 | b666b39a | Panagiotis Kanavos | if (action.CloudFile == null) |
238 | b666b39a | Panagiotis Kanavos | throw new ArgumentException("The action's cloud file is not specified", "action"); |
239 | b666b39a | Panagiotis Kanavos | Contract.EndContractBlock(); |
240 | b666b39a | Panagiotis Kanavos | |
241 | b666b39a | Panagiotis Kanavos | //We assume that the local file already exists, otherwise the poll agent |
242 | b666b39a | Panagiotis Kanavos | //would have issued a download request |
243 | b666b39a | Panagiotis Kanavos | |
244 | b666b39a | Panagiotis Kanavos | var currentInfo = action.CloudFile; |
245 | b666b39a | Panagiotis Kanavos | var previousInfo = action.CloudFile.Previous; |
246 | b666b39a | Panagiotis Kanavos | var fileAgent = FileAgent.GetFileAgent(accountInfo); |
247 | b666b39a | Panagiotis Kanavos | |
248 | b666b39a | Panagiotis Kanavos | var previousRelativepath = previousInfo.RelativeUrlToFilePath(accountInfo.UserName); |
249 | b666b39a | Panagiotis Kanavos | var previousFile = fileAgent.GetFileSystemInfo(previousRelativepath); |
250 | b666b39a | Panagiotis Kanavos | |
251 | b666b39a | Panagiotis Kanavos | //In every case we need to move the local file first |
252 | b666b39a | Panagiotis Kanavos | MoveLocalFile(accountInfo, previousFile, fileAgent, currentInfo); |
253 | b666b39a | Panagiotis Kanavos | |
254 | b666b39a | Panagiotis Kanavos | } |
255 | b666b39a | Panagiotis Kanavos | |
256 | b666b39a | Panagiotis Kanavos | private void MoveLocalFile(AccountInfo accountInfo, FileSystemInfo previousFile, FileAgent fileAgent, |
257 | b666b39a | Panagiotis Kanavos | ObjectInfo currentInfo) |
258 | b666b39a | Panagiotis Kanavos | { |
259 | b666b39a | Panagiotis Kanavos | var currentRelativepath = currentInfo.RelativeUrlToFilePath(accountInfo.UserName); |
260 | b666b39a | Panagiotis Kanavos | var newPath = Path.Combine(fileAgent.RootPath, currentRelativepath); |
261 | b666b39a | Panagiotis Kanavos | |
262 | b666b39a | Panagiotis Kanavos | var isFile= (previousFile is FileInfo); |
263 | b666b39a | Panagiotis Kanavos | var previousFullPath = isFile? |
264 | b666b39a | Panagiotis Kanavos | FileInfoExtensions.GetProperFilePathCapitalization(previousFile.FullName): |
265 | b666b39a | Panagiotis Kanavos | FileInfoExtensions.GetProperDirectoryCapitalization(previousFile.FullName); |
266 | b666b39a | Panagiotis Kanavos | |
267 | b666b39a | Panagiotis Kanavos | using (var gateOld = NetworkGate.Acquire(previousFullPath, NetworkOperation.Renaming)) |
268 | b666b39a | Panagiotis Kanavos | using (var gateNew = NetworkGate.Acquire(newPath,NetworkOperation.Renaming)) |
269 | b666b39a | Panagiotis Kanavos | using (new SessionScope(FlushAction.Auto)) |
270 | b666b39a | Panagiotis Kanavos | { |
271 | b666b39a | Panagiotis Kanavos | if (isFile) |
272 | b666b39a | Panagiotis Kanavos | (previousFile as FileInfo).MoveTo(newPath); |
273 | b666b39a | Panagiotis Kanavos | else |
274 | b666b39a | Panagiotis Kanavos | { |
275 | b666b39a | Panagiotis Kanavos | (previousFile as DirectoryInfo).MoveTo(newPath); |
276 | b666b39a | Panagiotis Kanavos | } |
277 | b666b39a | Panagiotis Kanavos | var state = StatusKeeper.GetStateByFilePath(previousFullPath); |
278 | b666b39a | Panagiotis Kanavos | state.FilePath = newPath; |
279 | b666b39a | Panagiotis Kanavos | state.SaveCopy(); |
280 | b666b39a | Panagiotis Kanavos | StatusKeeper.SetFileState(previousFullPath,FileStatus.Deleted,FileOverlayStatus.Deleted); |
281 | b666b39a | Panagiotis Kanavos | } |
282 | b666b39a | Panagiotis Kanavos | } |
283 | b666b39a | Panagiotis Kanavos | |
284 | 27361404 | Panagiotis Kanavos | private async Task SyncFiles(AccountInfo accountInfo,CloudAction action) |
285 | cfed7823 | Panagiotis Kanavos | { |
286 | c53aa229 | Panagiotis Kanavos | if (accountInfo == null) |
287 | c53aa229 | Panagiotis Kanavos | throw new ArgumentNullException("accountInfo"); |
288 | cfed7823 | Panagiotis Kanavos | if (action==null) |
289 | cfed7823 | Panagiotis Kanavos | throw new ArgumentNullException("action"); |
290 | cfed7823 | Panagiotis Kanavos | if (action.LocalFile==null) |
291 | cfed7823 | Panagiotis Kanavos | throw new ArgumentException("The action's local file is not specified","action"); |
292 | cfed7823 | Panagiotis Kanavos | if (!Path.IsPathRooted(action.LocalFile.FullName)) |
293 | cfed7823 | Panagiotis Kanavos | throw new ArgumentException("The action's local file path must be absolute","action"); |
294 | cfed7823 | Panagiotis Kanavos | if (action.CloudFile== null) |
295 | cfed7823 | Panagiotis Kanavos | throw new ArgumentException("The action's cloud file is not specified", "action"); |
296 | cfed7823 | Panagiotis Kanavos | Contract.EndContractBlock(); |
297 | cfed7823 | Panagiotis Kanavos | |
298 | a64c87c8 | Panagiotis Kanavos | var localFile = action.LocalFile; |
299 | a64c87c8 | Panagiotis Kanavos | var cloudFile = action.CloudFile; |
300 | f3d080df | Panagiotis Kanavos | var downloadPath=action.LocalFile.GetProperCapitalization(); |
301 | a27aa447 | Panagiotis Kanavos | |
302 | cfed7823 | Panagiotis Kanavos | var cloudHash = cloudFile.Hash.ToLower(); |
303 | f8c471f7 | Panagiotis Kanavos | var previousCloudHash = cloudFile.PreviousHash.ToLower(); |
304 | cfed7823 | Panagiotis Kanavos | var localHash = action.LocalHash.Value.ToLower(); |
305 | cfed7823 | Panagiotis Kanavos | var topHash = action.TopHash.Value.ToLower(); |
306 | cfed7823 | Panagiotis Kanavos | |
307 | f6ca240a | Panagiotis Kanavos | //At this point we know that an object has changed on the server and that a local |
308 | f6ca240a | Panagiotis Kanavos | //file already exists. We need to decide whether the file has only changed on |
309 | f6ca240a | Panagiotis Kanavos | //the server or there is a conflicting change on the client. |
310 | f6ca240a | Panagiotis Kanavos | // |
311 | cfed7823 | Panagiotis Kanavos | |
312 | f6ca240a | Panagiotis Kanavos | //Not enough to compare only the local hashes (MD5), also have to compare the tophashes |
313 | cfed7823 | Panagiotis Kanavos | //If any of the hashes match, we are done |
314 | cfed7823 | Panagiotis Kanavos | if ((cloudHash == localHash || cloudHash == topHash)) |
315 | a64c87c8 | Panagiotis Kanavos | { |
316 | cfed7823 | Panagiotis Kanavos | Log.InfoFormat("Skipping {0}, hashes match",downloadPath); |
317 | cfed7823 | Panagiotis Kanavos | return; |
318 | cfed7823 | Panagiotis Kanavos | } |
319 | cfed7823 | Panagiotis Kanavos | |
320 | cfed7823 | Panagiotis Kanavos | //The hashes DON'T match. We need to sync |
321 | f6ca240a | Panagiotis Kanavos | |
322 | f6ca240a | Panagiotis Kanavos | // If the previous tophash matches the local tophash, the file was only changed on the server. |
323 | f6ca240a | Panagiotis Kanavos | if (localHash == previousCloudHash) |
324 | cfed7823 | Panagiotis Kanavos | { |
325 | f6ca240a | Panagiotis Kanavos | await DownloadCloudFile(accountInfo, cloudFile, downloadPath); |
326 | cfed7823 | Panagiotis Kanavos | } |
327 | cfed7823 | Panagiotis Kanavos | else |
328 | cfed7823 | Panagiotis Kanavos | { |
329 | f6ca240a | Panagiotis Kanavos | //If the previous and local hash don't match, there was a local conflict |
330 | f6ca240a | Panagiotis Kanavos | //that was not uploaded to the server. We have a conflict |
331 | f6ca240a | Panagiotis Kanavos | ReportConflict(downloadPath); |
332 | cfed7823 | Panagiotis Kanavos | } |
333 | cfed7823 | Panagiotis Kanavos | } |
334 | cfed7823 | Panagiotis Kanavos | |
335 | cfed7823 | Panagiotis Kanavos | private void ReportConflict(string downloadPath) |
336 | cfed7823 | Panagiotis Kanavos | { |
337 | cfed7823 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(downloadPath)) |
338 | cfed7823 | Panagiotis Kanavos | throw new ArgumentNullException("downloadPath"); |
339 | cfed7823 | Panagiotis Kanavos | Contract.EndContractBlock(); |
340 | cfed7823 | Panagiotis Kanavos | |
341 | cfed7823 | Panagiotis Kanavos | StatusKeeper.SetFileOverlayStatus(downloadPath, FileOverlayStatus.Conflict); |
342 | eae84ae8 | Panagiotis Kanavos | UpdateStatus(PithosStatus.HasConflicts); |
343 | cfed7823 | Panagiotis Kanavos | var message = String.Format("Conflict detected for file {0}", downloadPath); |
344 | cfed7823 | Panagiotis Kanavos | Log.Warn(message); |
345 | cfed7823 | Panagiotis Kanavos | StatusNotification.NotifyChange(message, TraceLevel.Warning); |
346 | cfed7823 | Panagiotis Kanavos | } |
347 | cfed7823 | Panagiotis Kanavos | |
348 | 9c4346c9 | Panagiotis Kanavos | public void Post(CloudAction cloudAction) |
349 | 9c4346c9 | Panagiotis Kanavos | { |
350 | 9c4346c9 | Panagiotis Kanavos | if (cloudAction == null) |
351 | 9c4346c9 | Panagiotis Kanavos | throw new ArgumentNullException("cloudAction"); |
352 | c53aa229 | Panagiotis Kanavos | if (cloudAction.AccountInfo==null) |
353 | c53aa229 | Panagiotis Kanavos | throw new ArgumentException("The CloudAction.AccountInfo is empty","cloudAction"); |
354 | 9c4346c9 | Panagiotis Kanavos | Contract.EndContractBlock(); |
355 | f3d080df | Panagiotis Kanavos | |
356 | 38ac43a6 | Panagiotis Kanavos | _deleteAgent.ProceedEvent.Wait(); |
357 | 9d6d2f6e | Panagiotis Kanavos | |
358 | a27aa447 | Panagiotis Kanavos | //If the action targets a local file, add a treehash calculation |
359 | e81dd1f6 | Panagiotis Kanavos | if (!(cloudAction is CloudDeleteAction) && cloudAction.LocalFile as FileInfo != null) |
360 | a27aa447 | Panagiotis Kanavos | { |
361 | c53aa229 | Panagiotis Kanavos | var accountInfo = cloudAction.AccountInfo; |
362 | 4f6d51d4 | Panagiotis Kanavos | var localFile = (FileInfo) cloudAction.LocalFile; |
363 | 4f6d51d4 | Panagiotis Kanavos | if (localFile.Length > accountInfo.BlockSize) |
364 | 4f6d51d4 | Panagiotis Kanavos | cloudAction.TopHash = |
365 | 4f6d51d4 | Panagiotis Kanavos | new Lazy<string>(() => Signature.CalculateTreeHashAsync(localFile, |
366 | 4f6d51d4 | Panagiotis Kanavos | accountInfo.BlockSize, |
367 | 422c9598 | Panagiotis Kanavos | accountInfo.BlockHash, Settings.HashingParallelism).Result |
368 | 4f6d51d4 | Panagiotis Kanavos | .TopHash.ToHashString()); |
369 | 4f6d51d4 | Panagiotis Kanavos | else |
370 | cfed7823 | Panagiotis Kanavos | { |
371 | 4f6d51d4 | Panagiotis Kanavos | cloudAction.TopHash = new Lazy<string>(() => cloudAction.LocalHash.Value); |
372 | cfed7823 | Panagiotis Kanavos | } |
373 | 4f6d51d4 | Panagiotis Kanavos | } |
374 | 4f6d51d4 | Panagiotis Kanavos | else |
375 | 4f6d51d4 | Panagiotis Kanavos | { |
376 | 4f6d51d4 | Panagiotis Kanavos | //The hash for a directory is the empty string |
377 | 4f6d51d4 | Panagiotis Kanavos | cloudAction.TopHash = new Lazy<string>(() => String.Empty); |
378 | a27aa447 | Panagiotis Kanavos | } |
379 | f3d080df | Panagiotis Kanavos | |
380 | f3d080df | Panagiotis Kanavos | if (cloudAction is CloudDeleteAction) |
381 | f3d080df | Panagiotis Kanavos | _deleteAgent.Post((CloudDeleteAction)cloudAction); |
382 | f3d080df | Panagiotis Kanavos | else |
383 | f3d080df | Panagiotis Kanavos | _agent.Post(cloudAction); |
384 | 9c4346c9 | Panagiotis Kanavos | } |
385 | a9faac18 | Panagiotis Kanavos | |
386 | 9c4346c9 | Panagiotis Kanavos | |
387 | aa7ac00e | Panagiotis Kanavos | public IEnumerable<CloudAction> GetEnumerable() |
388 | 540b8cf8 | Panagiotis Kanavos | { |
389 | aa7ac00e | Panagiotis Kanavos | return _agent.GetEnumerable(); |
390 | 540b8cf8 | Panagiotis Kanavos | } |
391 | 540b8cf8 | Panagiotis Kanavos | |
392 | aa7ac00e | Panagiotis Kanavos | public Task GetDeleteAwaiter() |
393 | 5750d7cc | Panagiotis Kanavos | { |
394 | 38ac43a6 | Panagiotis Kanavos | return _deleteAgent.ProceedEvent.WaitAsync(); |
395 | 5750d7cc | Panagiotis Kanavos | } |
396 | aa7ac00e | Panagiotis Kanavos | public CancellationToken CancellationToken |
397 | 422c9598 | Panagiotis Kanavos | { |
398 | aa7ac00e | Panagiotis Kanavos | get { return _agent.CancellationToken; } |
399 | 422c9598 | Panagiotis Kanavos | } |
400 | 422c9598 | Panagiotis Kanavos | |
401 | c28a075a | Panagiotis Kanavos | private static FileAgent GetFileAgent(AccountInfo accountInfo) |
402 | c28a075a | Panagiotis Kanavos | { |
403 | c28a075a | Panagiotis Kanavos | return AgentLocator<FileAgent>.Get(accountInfo.AccountPath); |
404 | c28a075a | Panagiotis Kanavos | } |
405 | c28a075a | Panagiotis Kanavos | |
406 | 0af3141d | Panagiotis Kanavos | |
407 | 9c4346c9 | Panagiotis Kanavos | |
408 | 27361404 | Panagiotis Kanavos | private void RenameCloudFile(AccountInfo accountInfo,CloudMoveAction action) |
409 | 9c4346c9 | Panagiotis Kanavos | { |
410 | c53aa229 | Panagiotis Kanavos | if (accountInfo==null) |
411 | c53aa229 | Panagiotis Kanavos | throw new ArgumentNullException("accountInfo"); |
412 | 1bfc38f1 | Panagiotis Kanavos | if (action==null) |
413 | 1bfc38f1 | Panagiotis Kanavos | throw new ArgumentNullException("action"); |
414 | 27361404 | Panagiotis Kanavos | if (action.CloudFile==null) |
415 | 27361404 | Panagiotis Kanavos | throw new ArgumentException("CloudFile","action"); |
416 | 27361404 | Panagiotis Kanavos | if (action.LocalFile==null) |
417 | 27361404 | Panagiotis Kanavos | throw new ArgumentException("LocalFile","action"); |
418 | 27361404 | Panagiotis Kanavos | if (action.OldLocalFile==null) |
419 | 27361404 | Panagiotis Kanavos | throw new ArgumentException("OldLocalFile","action"); |
420 | 27361404 | Panagiotis Kanavos | if (action.OldCloudFile==null) |
421 | 27361404 | Panagiotis Kanavos | throw new ArgumentException("OldCloudFile","action"); |
422 | 9c4346c9 | Panagiotis Kanavos | Contract.EndContractBlock(); |
423 | 27361404 | Panagiotis Kanavos | |
424 | f3d080df | Panagiotis Kanavos | |
425 | f3d080df | Panagiotis Kanavos | var newFilePath = action.LocalFile.FullName; |
426 | f3d080df | Panagiotis Kanavos | |
427 | f3d080df | Panagiotis Kanavos | //How do we handle concurrent renames and deletes/uploads/downloads? |
428 | f3d080df | Panagiotis Kanavos | //* A conflicting upload means that a file was renamed before it had a chance to finish uploading |
429 | f3d080df | Panagiotis Kanavos | // This should never happen as the network agent executes only one action at a time |
430 | f3d080df | Panagiotis Kanavos | //* A conflicting download means that the file was modified on the cloud. While we can go on and complete |
431 | f3d080df | Panagiotis Kanavos | // the rename, there may be a problem if the file is downloaded in blocks, as subsequent block requests for the |
432 | f3d080df | Panagiotis Kanavos | // same name will fail. |
433 | f3d080df | Panagiotis Kanavos | // This should never happen as the network agent executes only one action at a time. |
434 | f3d080df | Panagiotis Kanavos | //* A conflicting delete can happen if the rename was followed by a delete action that didn't have the chance |
435 | f3d080df | Panagiotis Kanavos | // to remove the rename from the queue. |
436 | f3d080df | Panagiotis Kanavos | // We can probably ignore this case. It will result in an error which should be ignored |
437 | f3d080df | Panagiotis Kanavos | |
438 | f3d080df | Panagiotis Kanavos | |
439 | 9c4346c9 | Panagiotis Kanavos | //The local file is already renamed |
440 | 73cdd135 | Panagiotis Kanavos | StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Modified); |
441 | 9c4346c9 | Panagiotis Kanavos | |
442 | 1bfc38f1 | Panagiotis Kanavos | |
443 | 1bfc38f1 | Panagiotis Kanavos | var account = action.CloudFile.Account ?? accountInfo.UserName; |
444 | 27361404 | Panagiotis Kanavos | var container = action.CloudFile.Container; |
445 | 1bfc38f1 | Panagiotis Kanavos | |
446 | c53aa229 | Panagiotis Kanavos | var client = new CloudFilesClient(accountInfo); |
447 | f3d080df | Panagiotis Kanavos | //TODO: What code is returned when the source file doesn't exist? |
448 | 27361404 | Panagiotis Kanavos | client.MoveObject(account, container, action.OldCloudFile.Name, container, action.CloudFile.Name); |
449 | 9c4346c9 | Panagiotis Kanavos | |
450 | 73cdd135 | Panagiotis Kanavos | StatusKeeper.SetFileStatus(newFilePath, FileStatus.Unchanged); |
451 | 73cdd135 | Panagiotis Kanavos | StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Normal); |
452 | 27361404 | Panagiotis Kanavos | NativeMethods.RaiseChangeNotification(newFilePath); |
453 | 9c4346c9 | Panagiotis Kanavos | } |
454 | 9c4346c9 | Panagiotis Kanavos | |
455 | 5ce54458 | Panagiotis Kanavos | //Download a file. |
456 | f3d080df | Panagiotis Kanavos | private async Task DownloadCloudFile(AccountInfo accountInfo, ObjectInfo cloudFile , string filePath) |
457 | 9c4346c9 | Panagiotis Kanavos | { |
458 | c53aa229 | Panagiotis Kanavos | if (accountInfo == null) |
459 | c53aa229 | Panagiotis Kanavos | throw new ArgumentNullException("accountInfo"); |
460 | d3a13891 | Panagiotis Kanavos | if (cloudFile == null) |
461 | d3a13891 | Panagiotis Kanavos | throw new ArgumentNullException("cloudFile"); |
462 | 1bfc38f1 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(cloudFile.Account)) |
463 | 1bfc38f1 | Panagiotis Kanavos | throw new ArgumentNullException("cloudFile"); |
464 | 1bfc38f1 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(cloudFile.Container)) |
465 | 1bfc38f1 | Panagiotis Kanavos | throw new ArgumentNullException("cloudFile"); |
466 | f3d080df | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(filePath)) |
467 | f3d080df | Panagiotis Kanavos | throw new ArgumentNullException("filePath"); |
468 | f3d080df | Panagiotis Kanavos | if (!Path.IsPathRooted(filePath)) |
469 | f3d080df | Panagiotis Kanavos | throw new ArgumentException("The filePath must be rooted", "filePath"); |
470 | 0af3141d | Panagiotis Kanavos | Contract.EndContractBlock(); |
471 | eae84ae8 | Panagiotis Kanavos | |
472 | f3d080df | Panagiotis Kanavos | |
473 | f3d080df | Panagiotis Kanavos | var localPath = Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath); |
474 | 73cdd135 | Panagiotis Kanavos | var relativeUrl = new Uri(cloudFile.Name, UriKind.Relative); |
475 | d3a13891 | Panagiotis Kanavos | |
476 | 5ce54458 | Panagiotis Kanavos | var url = relativeUrl.ToString(); |
477 | d3a13891 | Panagiotis Kanavos | if (cloudFile.Name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase)) |
478 | 27361404 | Panagiotis Kanavos | return; |
479 | 9c4346c9 | Panagiotis Kanavos | |
480 | 039a89e5 | Panagiotis Kanavos | |
481 | 5ce54458 | Panagiotis Kanavos | //Are we already downloading or uploading the file? |
482 | 5ce54458 | Panagiotis Kanavos | using (var gate=NetworkGate.Acquire(localPath, NetworkOperation.Downloading)) |
483 | 5ce54458 | Panagiotis Kanavos | { |
484 | 5ce54458 | Panagiotis Kanavos | if (gate.Failed) |
485 | 27361404 | Panagiotis Kanavos | return; |
486 | 5ce54458 | Panagiotis Kanavos | |
487 | c53aa229 | Panagiotis Kanavos | var client = new CloudFilesClient(accountInfo); |
488 | 1bfc38f1 | Panagiotis Kanavos | var account = cloudFile.Account; |
489 | 1bfc38f1 | Panagiotis Kanavos | var container = cloudFile.Container; |
490 | 1bfc38f1 | Panagiotis Kanavos | |
491 | 73cdd135 | Panagiotis Kanavos | if (cloudFile.Content_Type == @"application/directory") |
492 | 73cdd135 | Panagiotis Kanavos | { |
493 | 73cdd135 | Panagiotis Kanavos | if (!Directory.Exists(localPath)) |
494 | 73cdd135 | Panagiotis Kanavos | Directory.CreateDirectory(localPath); |
495 | 73cdd135 | Panagiotis Kanavos | } |
496 | 27361404 | Panagiotis Kanavos | else |
497 | eae84ae8 | Panagiotis Kanavos | { |
498 | 73cdd135 | Panagiotis Kanavos | //Retrieve the hashmap from the server |
499 | 73cdd135 | Panagiotis Kanavos | var serverHash = await client.GetHashMap(account, container, url); |
500 | 73cdd135 | Panagiotis Kanavos | //If it's a small file |
501 | 73cdd135 | Panagiotis Kanavos | if (serverHash.Hashes.Count == 1) |
502 | 73cdd135 | Panagiotis Kanavos | //Download it in one go |
503 | 73cdd135 | Panagiotis Kanavos | await |
504 | 73cdd135 | Panagiotis Kanavos | DownloadEntireFileAsync(accountInfo, client, cloudFile, relativeUrl, localPath, serverHash); |
505 | 73cdd135 | Panagiotis Kanavos | //Otherwise download it block by block |
506 | 73cdd135 | Panagiotis Kanavos | else |
507 | 73cdd135 | Panagiotis Kanavos | await DownloadWithBlocks(accountInfo, client, cloudFile, relativeUrl, localPath, serverHash); |
508 | 73cdd135 | Panagiotis Kanavos | |
509 | 73cdd135 | Panagiotis Kanavos | if (cloudFile.AllowedTo == "read") |
510 | 73cdd135 | Panagiotis Kanavos | { |
511 | 73cdd135 | Panagiotis Kanavos | var attributes = File.GetAttributes(localPath); |
512 | 73cdd135 | Panagiotis Kanavos | File.SetAttributes(localPath, attributes | FileAttributes.ReadOnly); |
513 | 73cdd135 | Panagiotis Kanavos | } |
514 | d3a13891 | Panagiotis Kanavos | } |
515 | 73cdd135 | Panagiotis Kanavos | |
516 | 1bfc38f1 | Panagiotis Kanavos | //Now we can store the object's metadata without worrying about ghost status entries |
517 | 1bfc38f1 | Panagiotis Kanavos | StatusKeeper.StoreInfo(localPath, cloudFile); |
518 | 0af3141d | Panagiotis Kanavos | |
519 | 5ce54458 | Panagiotis Kanavos | } |
520 | 9c4346c9 | Panagiotis Kanavos | } |
521 | 9c4346c9 | Panagiotis Kanavos | |
522 | a27aa447 | Panagiotis Kanavos | //Download a small file with a single GET operation |
523 | f3d080df | Panagiotis Kanavos | private async Task DownloadEntireFileAsync(AccountInfo accountInfo, CloudFilesClient client, ObjectInfo cloudFile, Uri relativeUrl, string filePath,TreeHash serverHash) |
524 | a27aa447 | Panagiotis Kanavos | { |
525 | c53aa229 | Panagiotis Kanavos | if (client == null) |
526 | c53aa229 | Panagiotis Kanavos | throw new ArgumentNullException("client"); |
527 | 1bfc38f1 | Panagiotis Kanavos | if (cloudFile==null) |
528 | 1bfc38f1 | Panagiotis Kanavos | throw new ArgumentNullException("cloudFile"); |
529 | a27aa447 | Panagiotis Kanavos | if (relativeUrl == null) |
530 | a27aa447 | Panagiotis Kanavos | throw new ArgumentNullException("relativeUrl"); |
531 | f3d080df | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(filePath)) |
532 | f3d080df | Panagiotis Kanavos | throw new ArgumentNullException("filePath"); |
533 | f3d080df | Panagiotis Kanavos | if (!Path.IsPathRooted(filePath)) |
534 | f3d080df | Panagiotis Kanavos | throw new ArgumentException("The localPath must be rooted", "filePath"); |
535 | a27aa447 | Panagiotis Kanavos | Contract.EndContractBlock(); |
536 | a27aa447 | Panagiotis Kanavos | |
537 | f3d080df | Panagiotis Kanavos | var localPath = Pithos.Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath); |
538 | d3a13891 | Panagiotis Kanavos | //If the file already exists |
539 | d3a13891 | Panagiotis Kanavos | if (File.Exists(localPath)) |
540 | d3a13891 | Panagiotis Kanavos | { |
541 | d3a13891 | Panagiotis Kanavos | //First check with MD5 as this is a small file |
542 | d3a13891 | Panagiotis Kanavos | var localMD5 = Signature.CalculateMD5(localPath); |
543 | d3a13891 | Panagiotis Kanavos | var cloudHash=serverHash.TopHash.ToHashString(); |
544 | d3a13891 | Panagiotis Kanavos | if (localMD5==cloudHash) |
545 | 27361404 | Panagiotis Kanavos | return; |
546 | d3a13891 | Panagiotis Kanavos | //Then check with a treehash |
547 | d3a13891 | Panagiotis Kanavos | var localTreeHash = Signature.CalculateTreeHash(localPath, serverHash.BlockSize, serverHash.BlockHash); |
548 | d3a13891 | Panagiotis Kanavos | var localHash = localTreeHash.TopHash.ToHashString(); |
549 | d3a13891 | Panagiotis Kanavos | if (localHash==cloudHash) |
550 | 27361404 | Panagiotis Kanavos | return; |
551 | d3a13891 | Panagiotis Kanavos | } |
552 | eae84ae8 | Panagiotis Kanavos | StatusNotification.Notify(new CloudNotification { Data = cloudFile }); |
553 | d3a13891 | Panagiotis Kanavos | |
554 | c28a075a | Panagiotis Kanavos | var fileAgent = GetFileAgent(accountInfo); |
555 | a27aa447 | Panagiotis Kanavos | //Calculate the relative file path for the new file |
556 | a27aa447 | Panagiotis Kanavos | var relativePath = relativeUrl.RelativeUriToFilePath(); |
557 | a27aa447 | Panagiotis Kanavos | //The file will be stored in a temporary location while downloading with an extension .download |
558 | 77e10b4f | Panagiotis Kanavos | var tempPath = Path.Combine(fileAgent.CachePath, relativePath + ".download"); |
559 | a27aa447 | Panagiotis Kanavos | //Make sure the target folder exists. DownloadFileTask will not create the folder |
560 | d3a13891 | Panagiotis Kanavos | var tempFolder = Path.GetDirectoryName(tempPath); |
561 | d3a13891 | Panagiotis Kanavos | if (!Directory.Exists(tempFolder)) |
562 | d3a13891 | Panagiotis Kanavos | Directory.CreateDirectory(tempFolder); |
563 | a27aa447 | Panagiotis Kanavos | |
564 | a27aa447 | Panagiotis Kanavos | //Download the object to the temporary location |
565 | f3d080df | Panagiotis Kanavos | await client.GetObject(cloudFile.Account, cloudFile.Container, relativeUrl.ToString(), tempPath); |
566 | 0bd56b7c | Panagiotis Kanavos | |
567 | f3d080df | Panagiotis Kanavos | //Create the local folder if it doesn't exist (necessary for shared objects) |
568 | f3d080df | Panagiotis Kanavos | var localFolder = Path.GetDirectoryName(localPath); |
569 | f3d080df | Panagiotis Kanavos | if (!Directory.Exists(localFolder)) |
570 | f3d080df | Panagiotis Kanavos | Directory.CreateDirectory(localFolder); |
571 | f3d080df | Panagiotis Kanavos | //And move it to its actual location once downloading is finished |
572 | f3d080df | Panagiotis Kanavos | if (File.Exists(localPath)) |
573 | f3d080df | Panagiotis Kanavos | File.Replace(tempPath,localPath,null,true); |
574 | f3d080df | Panagiotis Kanavos | else |
575 | f3d080df | Panagiotis Kanavos | File.Move(tempPath,localPath); |
576 | f3d080df | Panagiotis Kanavos | //Notify listeners that a local file has changed |
577 | f3d080df | Panagiotis Kanavos | StatusNotification.NotifyChangedFile(localPath); |
578 | f3d080df | Panagiotis Kanavos | |
579 | f3d080df | Panagiotis Kanavos | |
580 | a27aa447 | Panagiotis Kanavos | } |
581 | a27aa447 | Panagiotis Kanavos | |
582 | 0af3141d | Panagiotis Kanavos | //Download a file asynchronously using blocks |
583 | f3d080df | Panagiotis Kanavos | public async Task DownloadWithBlocks(AccountInfo accountInfo, CloudFilesClient client, ObjectInfo cloudFile, Uri relativeUrl, string filePath, TreeHash serverHash) |
584 | 0af3141d | Panagiotis Kanavos | { |
585 | c53aa229 | Panagiotis Kanavos | if (client == null) |
586 | c53aa229 | Panagiotis Kanavos | throw new ArgumentNullException("client"); |
587 | 1bfc38f1 | Panagiotis Kanavos | if (cloudFile == null) |
588 | 1bfc38f1 | Panagiotis Kanavos | throw new ArgumentNullException("cloudFile"); |
589 | 0af3141d | Panagiotis Kanavos | if (relativeUrl == null) |
590 | 0af3141d | Panagiotis Kanavos | throw new ArgumentNullException("relativeUrl"); |
591 | f3d080df | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(filePath)) |
592 | f3d080df | Panagiotis Kanavos | throw new ArgumentNullException("filePath"); |
593 | f3d080df | Panagiotis Kanavos | if (!Path.IsPathRooted(filePath)) |
594 | f3d080df | Panagiotis Kanavos | throw new ArgumentException("The filePath must be rooted", "filePath"); |
595 | 0af3141d | Panagiotis Kanavos | if (serverHash == null) |
596 | 0af3141d | Panagiotis Kanavos | throw new ArgumentNullException("serverHash"); |
597 | 0af3141d | Panagiotis Kanavos | Contract.EndContractBlock(); |
598 | 0af3141d | Panagiotis Kanavos | |
599 | 27361404 | Panagiotis Kanavos | var fileAgent = GetFileAgent(accountInfo); |
600 | f3d080df | Panagiotis Kanavos | var localPath = Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath); |
601 | a64c87c8 | Panagiotis Kanavos | |
602 | a27aa447 | Panagiotis Kanavos | //Calculate the relative file path for the new file |
603 | a27aa447 | Panagiotis Kanavos | var relativePath = relativeUrl.RelativeUriToFilePath(); |
604 | 77e10b4f | Panagiotis Kanavos | var blockUpdater = new BlockUpdater(fileAgent.CachePath, localPath, relativePath, serverHash); |
605 | a27aa447 | Panagiotis Kanavos | |
606 | a64c87c8 | Panagiotis Kanavos | |
607 | 0af3141d | Panagiotis Kanavos | |
608 | 0af3141d | Panagiotis Kanavos | //Calculate the file's treehash |
609 | 422c9598 | Panagiotis Kanavos | var treeHash = await Signature.CalculateTreeHashAsync(localPath, serverHash.BlockSize, serverHash.BlockHash, 2); |
610 | a27aa447 | Panagiotis Kanavos | |
611 | 0af3141d | Panagiotis Kanavos | //And compare it with the server's hash |
612 | 0af3141d | Panagiotis Kanavos | var upHashes = serverHash.GetHashesAsStrings(); |
613 | 0af3141d | Panagiotis Kanavos | var localHashes = treeHash.HashDictionary; |
614 | 0af3141d | Panagiotis Kanavos | for (int i = 0; i < upHashes.Length; i++) |
615 | 0af3141d | Panagiotis Kanavos | { |
616 | 0af3141d | Panagiotis Kanavos | //For every non-matching hash |
617 | 0af3141d | Panagiotis Kanavos | var upHash = upHashes[i]; |
618 | 0af3141d | Panagiotis Kanavos | if (!localHashes.ContainsKey(upHash)) |
619 | a27aa447 | Panagiotis Kanavos | { |
620 | eae84ae8 | Panagiotis Kanavos | StatusNotification.Notify(new CloudNotification { Data = cloudFile }); |
621 | eae84ae8 | Panagiotis Kanavos | |
622 | 0af3141d | Panagiotis Kanavos | if (blockUpdater.UseOrphan(i, upHash)) |
623 | a27aa447 | Panagiotis Kanavos | { |
624 | cfed7823 | Panagiotis Kanavos | Log.InfoFormat("[BLOCK GET] ORPHAN FOUND for {0} of {1} for {2}", i, upHashes.Length, localPath); |
625 | 0af3141d | Panagiotis Kanavos | continue; |
626 | 0af3141d | Panagiotis Kanavos | } |
627 | cfed7823 | Panagiotis Kanavos | Log.InfoFormat("[BLOCK GET] START {0} of {1} for {2}", i, upHashes.Length, localPath); |
628 | c53aa229 | Panagiotis Kanavos | var start = i*serverHash.BlockSize; |
629 | 0af3141d | Panagiotis Kanavos | //To download the last block just pass a null for the end of the range |
630 | 0af3141d | Panagiotis Kanavos | long? end = null; |
631 | 0af3141d | Panagiotis Kanavos | if (i < upHashes.Length - 1 ) |
632 | c53aa229 | Panagiotis Kanavos | end= ((i + 1)*serverHash.BlockSize) ; |
633 | a27aa447 | Panagiotis Kanavos | |
634 | 0af3141d | Panagiotis Kanavos | //Download the missing block |
635 | 27361404 | Panagiotis Kanavos | var block = await client.GetBlock(cloudFile.Account, cloudFile.Container, relativeUrl, start, end); |
636 | a64c87c8 | Panagiotis Kanavos | |
637 | 0af3141d | Panagiotis Kanavos | //and store it |
638 | 27361404 | Panagiotis Kanavos | blockUpdater.StoreBlock(i, block); |
639 | a27aa447 | Panagiotis Kanavos | |
640 | cfed7823 | Panagiotis Kanavos | |
641 | cfed7823 | Panagiotis Kanavos | Log.InfoFormat("[BLOCK GET] FINISH {0} of {1} for {2}", i, upHashes.Length, localPath); |
642 | a27aa447 | Panagiotis Kanavos | } |
643 | 0af3141d | Panagiotis Kanavos | } |
644 | a27aa447 | Panagiotis Kanavos | |
645 | 0bd56b7c | Panagiotis Kanavos | //Want to avoid notifications if no changes were made |
646 | 0bd56b7c | Panagiotis Kanavos | var hasChanges = blockUpdater.HasBlocks; |
647 | 0af3141d | Panagiotis Kanavos | blockUpdater.Commit(); |
648 | 0bd56b7c | Panagiotis Kanavos | |
649 | 0bd56b7c | Panagiotis Kanavos | if (hasChanges) |
650 | 0bd56b7c | Panagiotis Kanavos | //Notify listeners that a local file has changed |
651 | 0bd56b7c | Panagiotis Kanavos | StatusNotification.NotifyChangedFile(localPath); |
652 | 0bd56b7c | Panagiotis Kanavos | |
653 | cfed7823 | Panagiotis Kanavos | Log.InfoFormat("[BLOCK GET] COMPLETE {0}", localPath); |
654 | 0af3141d | Panagiotis Kanavos | } |
655 | a27aa447 | Panagiotis Kanavos | |
656 | a27aa447 | Panagiotis Kanavos | |
657 | 27361404 | Panagiotis Kanavos | private async Task UploadCloudFile(CloudAction action) |
658 | 9c4346c9 | Panagiotis Kanavos | { |
659 | 1bfc38f1 | Panagiotis Kanavos | if (action == null) |
660 | 1bfc38f1 | Panagiotis Kanavos | throw new ArgumentNullException("action"); |
661 | 0af3141d | Panagiotis Kanavos | Contract.EndContractBlock(); |
662 | 0af3141d | Panagiotis Kanavos | |
663 | 1bfc38f1 | Panagiotis Kanavos | try |
664 | eae84ae8 | Panagiotis Kanavos | { |
665 | 692ec33b | Panagiotis Kanavos | var accountInfo = action.AccountInfo; |
666 | 692ec33b | Panagiotis Kanavos | |
667 | 692ec33b | Panagiotis Kanavos | var fileInfo = action.LocalFile; |
668 | 1bfc38f1 | Panagiotis Kanavos | |
669 | 437abfca | Panagiotis Kanavos | if (fileInfo.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase)) |
670 | 437abfca | Panagiotis Kanavos | return; |
671 | 039a89e5 | Panagiotis Kanavos | |
672 | 437abfca | Panagiotis Kanavos | var relativePath = fileInfo.AsRelativeTo(accountInfo.AccountPath); |
673 | 437abfca | Panagiotis Kanavos | if (relativePath.StartsWith(FolderConstants.OthersFolder)) |
674 | 1bfc38f1 | Panagiotis Kanavos | { |
675 | 692ec33b | Panagiotis Kanavos | var parts = relativePath.Split('\\'); |
676 | 437abfca | Panagiotis Kanavos | var accountName = parts[1]; |
677 | 437abfca | Panagiotis Kanavos | var oldName = accountInfo.UserName; |
678 | 437abfca | Panagiotis Kanavos | var absoluteUri = accountInfo.StorageUri.AbsoluteUri; |
679 | 692ec33b | Panagiotis Kanavos | var nameIndex = absoluteUri.IndexOf(oldName); |
680 | 692ec33b | Panagiotis Kanavos | var root = absoluteUri.Substring(0, nameIndex); |
681 | 437abfca | Panagiotis Kanavos | |
682 | 437abfca | Panagiotis Kanavos | accountInfo = new AccountInfo |
683 | 437abfca | Panagiotis Kanavos | { |
684 | 437abfca | Panagiotis Kanavos | UserName = accountName, |
685 | 437abfca | Panagiotis Kanavos | AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]), |
686 | 437abfca | Panagiotis Kanavos | StorageUri = new Uri(root + accountName), |
687 | 692ec33b | Panagiotis Kanavos | BlockHash = accountInfo.BlockHash, |
688 | 692ec33b | Panagiotis Kanavos | BlockSize = accountInfo.BlockSize, |
689 | 692ec33b | Panagiotis Kanavos | Token = accountInfo.Token |
690 | 437abfca | Panagiotis Kanavos | }; |
691 | 437abfca | Panagiotis Kanavos | } |
692 | 1bfc38f1 | Panagiotis Kanavos | |
693 | 1bfc38f1 | Panagiotis Kanavos | |
694 | f3d080df | Panagiotis Kanavos | var fullFileName = fileInfo.GetProperCapitalization(); |
695 | 692ec33b | Panagiotis Kanavos | using (var gate = NetworkGate.Acquire(fullFileName, NetworkOperation.Uploading)) |
696 | 437abfca | Panagiotis Kanavos | { |
697 | 437abfca | Panagiotis Kanavos | //Abort if the file is already being uploaded or downloaded |
698 | 437abfca | Panagiotis Kanavos | if (gate.Failed) |
699 | 437abfca | Panagiotis Kanavos | return; |
700 | 9c4346c9 | Panagiotis Kanavos | |
701 | 437abfca | Panagiotis Kanavos | var cloudFile = action.CloudFile; |
702 | 437abfca | Panagiotis Kanavos | var account = cloudFile.Account ?? accountInfo.UserName; |
703 | 1bfc38f1 | Panagiotis Kanavos | |
704 | 4f6d51d4 | Panagiotis Kanavos | var client = new CloudFilesClient(accountInfo); |
705 | 437abfca | Panagiotis Kanavos | //Even if GetObjectInfo times out, we can proceed with the upload |
706 | 692ec33b | Panagiotis Kanavos | var info = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name); |
707 | 5ce54458 | Panagiotis Kanavos | |
708 | 4f6d51d4 | Panagiotis Kanavos | //If this is a read-only file, do not upload changes |
709 | 4f6d51d4 | Panagiotis Kanavos | if (info.AllowedTo == "read") |
710 | 4f6d51d4 | Panagiotis Kanavos | return; |
711 | f3d080df | Panagiotis Kanavos | |
712 | f3d080df | Panagiotis Kanavos | //TODO: Check how a directory hash is calculated -> All dirs seem to have the same hash |
713 | 4f6d51d4 | Panagiotis Kanavos | if (fileInfo is DirectoryInfo) |
714 | 437abfca | Panagiotis Kanavos | { |
715 | 4f6d51d4 | Panagiotis Kanavos | //If the directory doesn't exist the Hash property will be empty |
716 | 4f6d51d4 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(info.Hash)) |
717 | 4f6d51d4 | Panagiotis Kanavos | //Go on and create the directory |
718 | 3c76f045 | Panagiotis Kanavos | await client.PutObject(account, cloudFile.Container, cloudFile.Name, fullFileName, String.Empty, "application/directory"); |
719 | 437abfca | Panagiotis Kanavos | } |
720 | 4f6d51d4 | Panagiotis Kanavos | else |
721 | 4f6d51d4 | Panagiotis Kanavos | { |
722 | 5ce54458 | Panagiotis Kanavos | |
723 | 4f6d51d4 | Panagiotis Kanavos | var cloudHash = info.Hash.ToLower(); |
724 | d3a13891 | Panagiotis Kanavos | |
725 | 4f6d51d4 | Panagiotis Kanavos | var hash = action.LocalHash.Value; |
726 | 4f6d51d4 | Panagiotis Kanavos | var topHash = action.TopHash.Value; |
727 | 4f6d51d4 | Panagiotis Kanavos | |
728 | 4f6d51d4 | Panagiotis Kanavos | //If the file hashes match, abort the upload |
729 | 4f6d51d4 | Panagiotis Kanavos | if (hash == cloudHash || topHash == cloudHash) |
730 | 4f6d51d4 | Panagiotis Kanavos | { |
731 | 4f6d51d4 | Panagiotis Kanavos | //but store any metadata changes |
732 | 4f6d51d4 | Panagiotis Kanavos | StatusKeeper.StoreInfo(fullFileName, info); |
733 | 4f6d51d4 | Panagiotis Kanavos | Log.InfoFormat("Skip upload of {0}, hashes match", fullFileName); |
734 | 4f6d51d4 | Panagiotis Kanavos | return; |
735 | 4f6d51d4 | Panagiotis Kanavos | } |
736 | a27aa447 | Panagiotis Kanavos | |
737 | 5750d7cc | Panagiotis Kanavos | |
738 | 4f6d51d4 | Panagiotis Kanavos | //Mark the file as modified while we upload it |
739 | 4f6d51d4 | Panagiotis Kanavos | StatusKeeper.SetFileOverlayStatus(fullFileName, FileOverlayStatus.Modified); |
740 | 4f6d51d4 | Panagiotis Kanavos | //And then upload it |
741 | 692ec33b | Panagiotis Kanavos | |
742 | 4f6d51d4 | Panagiotis Kanavos | //Upload even small files using the Hashmap. The server may already contain |
743 | 4f6d51d4 | Panagiotis Kanavos | //the relevant block |
744 | 5750d7cc | Panagiotis Kanavos | |
745 | 4f6d51d4 | Panagiotis Kanavos | //First, calculate the tree hash |
746 | f3d080df | Panagiotis Kanavos | var treeHash = await Signature.CalculateTreeHashAsync(fullFileName, accountInfo.BlockSize, |
747 | 422c9598 | Panagiotis Kanavos | accountInfo.BlockHash, 2); |
748 | 4f6d51d4 | Panagiotis Kanavos | |
749 | 4f6d51d4 | Panagiotis Kanavos | await UploadWithHashMap(accountInfo, cloudFile, fileInfo as FileInfo, cloudFile.Name, treeHash); |
750 | 4f6d51d4 | Panagiotis Kanavos | } |
751 | 437abfca | Panagiotis Kanavos | //If everything succeeds, change the file and overlay status to normal |
752 | 73cdd135 | Panagiotis Kanavos | StatusKeeper.SetFileState(fullFileName, FileStatus.Unchanged, FileOverlayStatus.Normal); |
753 | 437abfca | Panagiotis Kanavos | } |
754 | 437abfca | Panagiotis Kanavos | //Notify the Shell to update the overlays |
755 | 437abfca | Panagiotis Kanavos | NativeMethods.RaiseChangeNotification(fullFileName); |
756 | 437abfca | Panagiotis Kanavos | StatusNotification.NotifyChangedFile(fullFileName); |
757 | 5ce54458 | Panagiotis Kanavos | } |
758 | 437abfca | Panagiotis Kanavos | catch (AggregateException ex) |
759 | 437abfca | Panagiotis Kanavos | { |
760 | 437abfca | Panagiotis Kanavos | var exc = ex.InnerException as WebException; |
761 | 437abfca | Panagiotis Kanavos | if (exc == null) |
762 | 437abfca | Panagiotis Kanavos | throw ex.InnerException; |
763 | 692ec33b | Panagiotis Kanavos | if (HandleUploadWebException(action, exc)) |
764 | 437abfca | Panagiotis Kanavos | return; |
765 | 692ec33b | Panagiotis Kanavos | throw; |
766 | 692ec33b | Panagiotis Kanavos | } |
767 | 692ec33b | Panagiotis Kanavos | catch (WebException ex) |
768 | 692ec33b | Panagiotis Kanavos | { |
769 | 692ec33b | Panagiotis Kanavos | if (HandleUploadWebException(action, ex)) |
770 | 692ec33b | Panagiotis Kanavos | return; |
771 | 692ec33b | Panagiotis Kanavos | throw; |
772 | 692ec33b | Panagiotis Kanavos | } |
773 | 692ec33b | Panagiotis Kanavos | catch (Exception ex) |
774 | 692ec33b | Panagiotis Kanavos | { |
775 | 692ec33b | Panagiotis Kanavos | Log.Error("Unexpected error while uploading file", ex); |
776 | 437abfca | Panagiotis Kanavos | throw; |
777 | 437abfca | Panagiotis Kanavos | } |
778 | 437abfca | Panagiotis Kanavos | |
779 | 9c4346c9 | Panagiotis Kanavos | } |
780 | 9c4346c9 | Panagiotis Kanavos | |
781 | 3742088d | Panagiotis Kanavos | |
782 | 039a89e5 | Panagiotis Kanavos | |
783 | 692ec33b | Panagiotis Kanavos | private bool HandleUploadWebException(CloudAction action, WebException exc) |
784 | 692ec33b | Panagiotis Kanavos | { |
785 | 692ec33b | Panagiotis Kanavos | var response = exc.Response as HttpWebResponse; |
786 | 692ec33b | Panagiotis Kanavos | if (response == null) |
787 | 692ec33b | Panagiotis Kanavos | throw exc; |
788 | 692ec33b | Panagiotis Kanavos | if (response.StatusCode == HttpStatusCode.Unauthorized) |
789 | 692ec33b | Panagiotis Kanavos | { |
790 | 692ec33b | Panagiotis Kanavos | Log.Error("Not allowed to upload file", exc); |
791 | 692ec33b | Panagiotis Kanavos | var message = String.Format("Not allowed to uplad file {0}", action.LocalFile.FullName); |
792 | 692ec33b | Panagiotis Kanavos | StatusKeeper.SetFileState(action.LocalFile.FullName, FileStatus.Unchanged, FileOverlayStatus.Normal); |
793 | 692ec33b | Panagiotis Kanavos | StatusNotification.NotifyChange(message, TraceLevel.Warning); |
794 | 692ec33b | Panagiotis Kanavos | return true; |
795 | 692ec33b | Panagiotis Kanavos | } |
796 | 692ec33b | Panagiotis Kanavos | return false; |
797 | 692ec33b | Panagiotis Kanavos | } |
798 | 692ec33b | Panagiotis Kanavos | |
799 | 27361404 | Panagiotis Kanavos | public async Task UploadWithHashMap(AccountInfo accountInfo,ObjectInfo cloudFile,FileInfo fileInfo,string url,TreeHash treeHash) |
800 | a27aa447 | Panagiotis Kanavos | { |
801 | c53aa229 | Panagiotis Kanavos | if (accountInfo == null) |
802 | c53aa229 | Panagiotis Kanavos | throw new ArgumentNullException("accountInfo"); |
803 | 1bfc38f1 | Panagiotis Kanavos | if (cloudFile==null) |
804 | 1bfc38f1 | Panagiotis Kanavos | throw new ArgumentNullException("cloudFile"); |
805 | cfed7823 | Panagiotis Kanavos | if (fileInfo == null) |
806 | 0af3141d | Panagiotis Kanavos | throw new ArgumentNullException("fileInfo"); |
807 | 0af3141d | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(url)) |
808 | 0af3141d | Panagiotis Kanavos | throw new ArgumentNullException(url); |
809 | 0af3141d | Panagiotis Kanavos | if (treeHash==null) |
810 | 0af3141d | Panagiotis Kanavos | throw new ArgumentNullException("treeHash"); |
811 | ec6f3895 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(cloudFile.Container) ) |
812 | ec6f3895 | Panagiotis Kanavos | throw new ArgumentException("Invalid container","cloudFile"); |
813 | 0af3141d | Panagiotis Kanavos | Contract.EndContractBlock(); |
814 | 0af3141d | Panagiotis Kanavos | |
815 | f3d080df | Panagiotis Kanavos | var fullFileName = fileInfo.GetProperCapitalization(); |
816 | a27aa447 | Panagiotis Kanavos | |
817 | 1bfc38f1 | Panagiotis Kanavos | var account = cloudFile.Account ?? accountInfo.UserName; |
818 | 27361404 | Panagiotis Kanavos | var container = cloudFile.Container ; |
819 | 1bfc38f1 | Panagiotis Kanavos | |
820 | c53aa229 | Panagiotis Kanavos | var client = new CloudFilesClient(accountInfo); |
821 | a27aa447 | Panagiotis Kanavos | //Send the hashmap to the server |
822 | 437abfca | Panagiotis Kanavos | var missingHashes = await client.PutHashMap(account, container, url, treeHash); |
823 | 0af3141d | Panagiotis Kanavos | //If the server returns no missing hashes, we are done |
824 | 0af3141d | Panagiotis Kanavos | while (missingHashes.Count > 0) |
825 | a27aa447 | Panagiotis Kanavos | { |
826 | 0af3141d | Panagiotis Kanavos | |
827 | c53aa229 | Panagiotis Kanavos | var buffer = new byte[accountInfo.BlockSize]; |
828 | 0af3141d | Panagiotis Kanavos | foreach (var missingHash in missingHashes) |
829 | a27aa447 | Panagiotis Kanavos | { |
830 | a27aa447 | Panagiotis Kanavos | //Find the proper block |
831 | 437abfca | Panagiotis Kanavos | var blockIndex = treeHash.HashDictionary[missingHash]; |
832 | c53aa229 | Panagiotis Kanavos | var offset = blockIndex*accountInfo.BlockSize; |
833 | a27aa447 | Panagiotis Kanavos | |
834 | c53aa229 | Panagiotis Kanavos | var read = fileInfo.Read(buffer, offset, accountInfo.BlockSize); |
835 | 0af3141d | Panagiotis Kanavos | |
836 | 437abfca | Panagiotis Kanavos | try |
837 | 437abfca | Panagiotis Kanavos | { |
838 | 437abfca | Panagiotis Kanavos | //And upload the block |
839 | 437abfca | Panagiotis Kanavos | await client.PostBlock(account, container, buffer, 0, read); |
840 | 437abfca | Panagiotis Kanavos | Log.InfoFormat("[BLOCK] Block {0} of {1} uploaded", blockIndex, fullFileName); |
841 | 437abfca | Panagiotis Kanavos | } |
842 | 437abfca | Panagiotis Kanavos | catch (Exception exc) |
843 | 437abfca | Panagiotis Kanavos | { |
844 | 437abfca | Panagiotis Kanavos | Log.ErrorFormat("[ERROR] uploading block {0} of {1}\n{2}", blockIndex, fullFileName, exc); |
845 | 437abfca | Panagiotis Kanavos | } |
846 | 0af3141d | Panagiotis Kanavos | |
847 | a27aa447 | Panagiotis Kanavos | } |
848 | a27aa447 | Panagiotis Kanavos | |
849 | 437abfca | Panagiotis Kanavos | //Repeat until there are no more missing hashes |
850 | 437abfca | Panagiotis Kanavos | missingHashes = await client.PutHashMap(account, container, url, treeHash); |
851 | 0af3141d | Panagiotis Kanavos | } |
852 | a27aa447 | Panagiotis Kanavos | } |
853 | a27aa447 | Panagiotis Kanavos | |
854 | 9c4346c9 | Panagiotis Kanavos | |
855 | c53aa229 | Panagiotis Kanavos | public void AddAccount(AccountInfo accountInfo) |
856 | c53aa229 | Panagiotis Kanavos | { |
857 | c53aa229 | Panagiotis Kanavos | if (!_accounts.Contains(accountInfo)) |
858 | c53aa229 | Panagiotis Kanavos | _accounts.Add(accountInfo); |
859 | c53aa229 | Panagiotis Kanavos | } |
860 | 9c4346c9 | Panagiotis Kanavos | } |
861 | 9c4346c9 | Panagiotis Kanavos | |
862 | 5ce54458 | Panagiotis Kanavos | |
863 | 5ce54458 | Panagiotis Kanavos | |
864 | 9c4346c9 | Panagiotis Kanavos | |
865 | 9c4346c9 | Panagiotis Kanavos | } |