2 using System.Collections.Generic;
\r
3 using System.ComponentModel.Composition;
\r
4 using System.Diagnostics;
\r
5 using System.Diagnostics.Contracts;
\r
9 using System.Reflection;
\r
10 using System.Security.Cryptography;
\r
11 using System.Threading;
\r
12 using System.Threading.Tasks;
\r
13 using Pithos.Interfaces;
\r
14 using Pithos.Network;
\r
17 namespace Pithos.Core.Agents
\r
19 [Export(typeof(Uploader))]
\r
20 public class Uploader
\r
22 private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
\r
25 private IStatusKeeper StatusKeeper { get; set; }
\r
28 public IStatusNotification StatusNotification { get; set; }
\r
31 //CancellationTokenSource _cts = new CancellationTokenSource();
\r
32 /*public void SignalStop()
\r
37 public async Task UploadCloudFile(CloudAction action,CancellationToken cancellationToken)
\r
40 throw new ArgumentNullException("action");
\r
41 Contract.EndContractBlock();
\r
43 using (ThreadContext.Stacks["Operation"].Push("UploadCloudFile"))
\r
47 await UnpauseEvent.WaitAsync();
\r
49 var fileInfo = action.LocalFile;
\r
51 if (fileInfo.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase))
\r
54 if (!Selectives.IsSelected(action.AccountInfo, fileInfo))
\r
57 //Try to load the action's local state, if it is empty
\r
58 if (action.FileState == null)
\r
59 action.FileState = StatusKeeper.GetStateByFilePath(fileInfo.FullName);
\r
60 if (action.FileState == null)
\r
62 Log.WarnFormat("File [{0}] has no local state. It was probably created by a download action", fileInfo.FullName);
\r
66 var latestState = action.FileState;
\r
68 //Do not upload files in conflict
\r
69 if (latestState.FileStatus == FileStatus.Conflict)
\r
71 Log.InfoFormat("Skipping file in conflict [{0}]", fileInfo.FullName);
\r
74 //Do not upload files when we have no permission
\r
75 if (latestState.FileStatus == FileStatus.Forbidden)
\r
77 Log.InfoFormat("Skipping forbidden file [{0}]", fileInfo.FullName);
\r
81 //Are we targeting our own account or a sharer account?
\r
82 var relativePath = fileInfo.AsRelativeTo(action.AccountInfo.AccountPath);
\r
83 var accountInfo = relativePath.StartsWith(FolderConstants.OthersFolder)
\r
84 ? GetSharerAccount(relativePath, action.AccountInfo)
\r
85 : action.AccountInfo;
\r
89 var fullFileName = fileInfo.GetProperCapitalization();
\r
90 using (var gate = NetworkGate.Acquire(fullFileName, NetworkOperation.Uploading))
\r
92 //Abort if the file is already being uploaded or downloaded
\r
96 var cloudFile = action.CloudFile;
\r
97 var account = cloudFile.Account ?? accountInfo.UserName;
\r
101 var client = new CloudFilesClient(accountInfo);
\r
103 //Even if GetObjectInfo times out, we can proceed with the upload
\r
104 var cloudInfo = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name);
\r
106 //If this a shared file
\r
107 if (!cloudFile.Account.Equals(action.AccountInfo.UserName,StringComparison.InvariantCultureIgnoreCase))
\r
109 //If this is a read-only file, do not upload changes
\r
110 if (!cloudInfo.IsWritable(action.AccountInfo.UserName))
\r
112 MakeFileReadOnly(fullFileName);
\r
116 //If the file is new, can we upload it?
\r
117 if ( !cloudInfo.Exists && !client.CanUpload(account, cloudFile))
\r
119 MakeFileReadOnly(fullFileName);
\r
125 await UnpauseEvent.WaitAsync();
\r
127 if (fileInfo is DirectoryInfo)
\r
129 //If the directory doesn't exist the Hash property will be empty
\r
130 if (String.IsNullOrWhiteSpace(cloudInfo.Hash))
\r
131 //Go on and create the directory
\r
132 await client.PutObject(account, cloudFile.Container, cloudFile.Name, fullFileName,
\r
133 String.Empty, "application/directory");
\r
138 var cloudHash = cloudInfo.Hash.ToLower();
\r
142 using(StatusNotification.GetNotifier("Hashing {0} for Upload", "Finished hashing {0}",fileInfo.Name))
\r
144 treeHash = action.TreeHash.Value;
\r
145 topHash = treeHash.TopHash.ToHashString();
\r
150 //If the file hashes match, abort the upload
\r
151 if (cloudInfo != ObjectInfo.Empty && (topHash == cloudHash ))
\r
153 //but store any metadata changes
\r
154 StatusKeeper.StoreInfo(fullFileName, cloudInfo);
\r
155 Log.InfoFormat("Skip upload of {0}, hashes match", fullFileName);
\r
160 //Mark the file as modified while we upload it
\r
161 StatusKeeper.SetFileOverlayStatus(fullFileName, FileOverlayStatus.Modified);
\r
162 //And then upload it
\r
164 //Upload even small files using the Hashmap. The server may already contain
\r
165 //the relevant block
\r
169 await UploadWithHashMap(accountInfo, cloudFile, fileInfo as FileInfo, cloudFile.Name, treeHash,cancellationToken);
\r
171 //If everything succeeds, change the file and overlay status to normal
\r
172 StatusKeeper.SetFileState(fullFileName, FileStatus.Unchanged, FileOverlayStatus.Normal, "");
\r
174 catch (WebException exc)
\r
176 var response = (exc.Response as HttpWebResponse);
\r
177 if (response == null)
\r
179 if (response.StatusCode == HttpStatusCode.Forbidden)
\r
181 StatusKeeper.SetFileState(fileInfo.FullName, FileStatus.Forbidden, FileOverlayStatus.Conflict, "Forbidden");
\r
182 MakeFileReadOnly(fullFileName);
\r
185 //In any other case, propagate the error
\r
189 //Notify the Shell to update the overlays
\r
190 NativeMethods.RaiseChangeNotification(fullFileName);
\r
191 StatusNotification.NotifyChangedFile(fullFileName);
\r
193 catch (AggregateException ex)
\r
195 var exc = ex.InnerException as WebException;
\r
197 throw ex.InnerException;
\r
198 if (HandleUploadWebException(action, exc))
\r
202 catch (WebException ex)
\r
204 if (HandleUploadWebException(action, ex))
\r
208 catch (Exception ex)
\r
210 Log.Error("Unexpected error while uploading file", ex);
\r
216 private static void MakeFileReadOnly(string fullFileName)
\r
218 var attributes = File.GetAttributes(fullFileName);
\r
219 //Do not make any modifications if not necessary
\r
220 if (attributes.HasFlag(FileAttributes.ReadOnly))
\r
222 File.SetAttributes(fullFileName, attributes | FileAttributes.ReadOnly);
\r
225 private static AccountInfo GetSharerAccount(string relativePath, AccountInfo accountInfo)
\r
227 var parts = relativePath.Split('\\');
\r
228 var accountName = parts[1];
\r
229 var oldName = accountInfo.UserName;
\r
230 var absoluteUri = accountInfo.StorageUri.AbsoluteUri;
\r
231 var nameIndex = absoluteUri.IndexOf(oldName, StringComparison.Ordinal);
\r
232 var root = absoluteUri.Substring(0, nameIndex);
\r
234 accountInfo = new AccountInfo
\r
236 UserName = accountName,
\r
237 AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
\r
238 StorageUri = new Uri(root + accountName),
\r
239 BlockHash = accountInfo.BlockHash,
\r
240 BlockSize = accountInfo.BlockSize,
\r
241 Token = accountInfo.Token
\r
243 return accountInfo;
\r
247 public async Task UploadWithHashMap(AccountInfo accountInfo, ObjectInfo cloudFile, FileInfo fileInfo, string url, TreeHash treeHash, CancellationToken token)
\r
249 if (accountInfo == null)
\r
250 throw new ArgumentNullException("accountInfo");
\r
251 if (cloudFile == null)
\r
252 throw new ArgumentNullException("cloudFile");
\r
253 if (fileInfo == null)
\r
254 throw new ArgumentNullException("fileInfo");
\r
255 if (String.IsNullOrWhiteSpace(url))
\r
256 throw new ArgumentNullException(url);
\r
257 if (treeHash == null)
\r
258 throw new ArgumentNullException("treeHash");
\r
259 if (String.IsNullOrWhiteSpace(cloudFile.Container))
\r
260 throw new ArgumentException("Invalid container", "cloudFile");
\r
261 Contract.EndContractBlock();
\r
264 using (StatusNotification.GetNotifier("Uploading {0}", "Finished Uploading {0}", fileInfo.Name))
\r
266 if (await WaitOrAbort(accountInfo,cloudFile, token))
\r
269 var fullFileName = fileInfo.GetProperCapitalization();
\r
271 var account = cloudFile.Account ?? accountInfo.UserName;
\r
272 var container = cloudFile.Container;
\r
275 var client = new CloudFilesClient(accountInfo);
\r
276 //Send the hashmap to the server
\r
277 var missingHashes = await client.PutHashMap(account, container, url, treeHash);
\r
279 ReportUploadProgress(fileInfo.Name, block++, missingHashes.Count, fileInfo.Length);
\r
280 //If the server returns no missing hashes, we are done
\r
281 while (missingHashes.Count > 0)
\r
284 if (await WaitOrAbort(accountInfo,cloudFile, token))
\r
288 var buffer = new byte[accountInfo.BlockSize];
\r
289 foreach (var missingHash in missingHashes)
\r
291 if (await WaitOrAbort(accountInfo,cloudFile, token))
\r
295 //Find the proper block
\r
296 var blockIndex = treeHash.HashDictionary[missingHash];
\r
297 long offset = blockIndex*accountInfo.BlockSize;
\r
299 var read = fileInfo.Read(buffer, offset, accountInfo.BlockSize);
\r
303 //And upload the block
\r
304 await client.PostBlock(account, container, buffer, 0, read);
\r
305 Log.InfoFormat("[BLOCK] Block {0} of {1} uploaded", blockIndex, fullFileName);
\r
307 catch (Exception exc)
\r
309 Log.Error(String.Format("Uploading block {0} of {1}", blockIndex, fullFileName), exc);
\r
311 ReportUploadProgress(fileInfo.Name, block++, missingHashes.Count, fileInfo.Length);
\r
314 token.ThrowIfCancellationRequested();
\r
315 //Repeat until there are no more missing hashes
\r
316 missingHashes = await client.PutHashMap(account, container, url, treeHash);
\r
319 ReportUploadProgress(fileInfo.Name, missingHashes.Count, missingHashes.Count, fileInfo.Length);
\r
323 private async Task<bool> WaitOrAbort(AccountInfo account,ObjectInfo cloudFile, CancellationToken token)
\r
325 token.ThrowIfCancellationRequested();
\r
326 await UnpauseEvent.WaitAsync();
\r
327 var shouldAbort = !Selectives.IsSelected(account,cloudFile);
\r
329 Log.InfoFormat("Aborting [{0}]",cloudFile.Uri);
\r
330 return shouldAbort;
\r
333 private void ReportUploadProgress(string fileName, int block, int totalBlocks, long fileSize)
\r
335 StatusNotification.Notify(totalBlocks == 0
\r
336 ? new ProgressNotification(fileName, "Uploading", 1, 1, fileSize)
\r
337 : new ProgressNotification(fileName, "Uploading", block, totalBlocks, fileSize));
\r
341 private bool HandleUploadWebException(CloudAction action, WebException exc)
\r
343 var response = exc.Response as HttpWebResponse;
\r
344 if (response == null)
\r
346 if (response.StatusCode == HttpStatusCode.Unauthorized)
\r
348 Log.Error("Not allowed to upload file", exc);
\r
349 var message = String.Format("Not allowed to uplad file {0}", action.LocalFile.FullName);
\r
350 StatusKeeper.SetFileState(action.LocalFile.FullName, FileStatus.Unchanged, FileOverlayStatus.Normal, "");
\r
351 StatusNotification.NotifyChange(message, TraceLevel.Warning);
\r
358 public Selectives Selectives { get; set; }
\r
360 public AsyncManualResetEvent UnpauseEvent { get; set; }
\r