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