- if (client == null)
- throw new ArgumentNullException("client");
- if (cloudFile==null)
- throw new ArgumentNullException("cloudFile");
- if (relativeUrl == null)
- throw new ArgumentNullException("relativeUrl");
- if (String.IsNullOrWhiteSpace(localPath))
- throw new ArgumentNullException("localPath");
- if (!Path.IsPathRooted(localPath))
- throw new ArgumentException("The localPath must be rooted", "localPath");
- if(serverHash==null)
- throw new ArgumentNullException("serverHash");
- Contract.EndContractBlock();
-
- var fileAgent = GetFileAgent(accountInfo);
-
- //Calculate the relative file path for the new file
- var relativePath = relativeUrl.RelativeUriToFilePath();
- var blockUpdater = new BlockUpdater(fileAgent.FragmentsPath, localPath, relativePath, serverHash);
-
-
-
- //Calculate the file's treehash
- var calcHash = Signature.CalculateTreeHashAsync(localPath, serverHash.BlockSize,serverHash.BlockHash);
- yield return calcHash;
- var treeHash = calcHash.Result;
-
- //And compare it with the server's hash
- var upHashes = serverHash.GetHashesAsStrings();
- var localHashes = treeHash.HashDictionary;
- for (int i = 0; i < upHashes.Length; i++)
- {
- //For every non-matching hash
- var upHash = upHashes[i];
- if (!localHashes.ContainsKey(upHash))
- {
- if (blockUpdater.UseOrphan(i, upHash))
- {
- Log.InfoFormat("[BLOCK GET] ORPHAN FOUND for {0} of {1} for {2}", i, upHashes.Length, localPath);
- continue;
- }
- Log.InfoFormat("[BLOCK GET] START {0} of {1} for {2}", i, upHashes.Length, localPath);
- var start = i*serverHash.BlockSize;
- //To download the last block just pass a null for the end of the range
- long? end = null;
- if (i < upHashes.Length - 1 )
- end= ((i + 1)*serverHash.BlockSize) ;
-
- //Download the missing block
- var getBlock = client.GetBlock(cloudFile.Account, cloudFile.Container, relativeUrl, start, end);
- yield return getBlock;
- var block = getBlock.Result;
-
- //and store it
- yield return blockUpdater.StoreBlock(i, block);
-
-
- Log.InfoFormat("[BLOCK GET] FINISH {0} of {1} for {2}", i, upHashes.Length, localPath);
- }
- }
-
- blockUpdater.Commit();
- Log.InfoFormat("[BLOCK GET] COMPLETE {0}", localPath);