Revision 437abfca trunk/Pithos.Network/Signature.cs
b/trunk/Pithos.Network/Signature.cs | ||
---|---|---|
108 | 108 |
return hash.Result; |
109 | 109 |
} |
110 | 110 |
|
111 |
public static Task<TreeHash> CalculateTreeHashAsync(FileInfo fileInfo, int blockSize, string algorithm) |
|
111 |
public static async Task<TreeHash> CalculateTreeHashAsync(FileInfo fileInfo, int blockSize, string algorithm)
|
|
112 | 112 |
{ |
113 | 113 |
if (fileInfo == null) |
114 | 114 |
throw new ArgumentNullException("fileInfo"); |
... | ... | |
120 | 120 |
throw new ArgumentNullException("algorithm"); |
121 | 121 |
Contract.EndContractBlock(); |
122 | 122 |
|
123 |
return CalculateTreeHashAsync(fileInfo.FullName, blockSize, algorithm); |
|
123 |
return await CalculateTreeHashAsync(fileInfo.FullName, blockSize, algorithm);
|
|
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
|
127 |
public static Task<TreeHash> CalculateTreeHashAsync(string filePath, int blockSize,string algorithm) |
|
127 |
public static async Task<TreeHash> CalculateTreeHashAsync(string filePath, int blockSize,string algorithm)
|
|
128 | 128 |
{ |
129 | 129 |
if (String.IsNullOrWhiteSpace(filePath)) |
130 | 130 |
throw new ArgumentNullException("filePath"); |
... | ... | |
136 | 136 |
|
137 | 137 |
//DON'T calculate hashes for folders |
138 | 138 |
if (Directory.Exists(filePath)) |
139 |
return Task.Factory.StartNew(()=>new TreeHash(algorithm));
|
|
139 |
return new TreeHash(algorithm);
|
|
140 | 140 |
//The hash of a non-existent file is the empty hash |
141 | 141 |
if (!File.Exists(filePath)) |
142 |
return Task.Factory.StartNew(()=>new TreeHash(algorithm));
|
|
142 |
return new TreeHash(algorithm);
|
|
143 | 143 |
|
144 | 144 |
//Calculate the hash of all blocks using a blockhash iterator |
145 |
var treeHash =Iterate<TreeHash>(BlockHashIterator(filePath, blockSize, algorithm)); |
|
146 |
|
|
147 |
return treeHash; |
|
148 |
} |
|
149 |
|
|
150 |
|
|
151 |
private static IEnumerable<Task> BlockHashIterator(string filePath, int blockSize, string algorithm) |
|
152 |
{ |
|
153 | 145 |
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, blockSize, true)) |
154 | 146 |
{ |
155 | 147 |
//Calculate the blocks asyncrhonously |
156 |
var hashes= CalculateBlockHashesAsync(stream, blockSize, algorithm); |
|
157 |
yield return hashes; |
|
158 |
|
|
148 |
var hashes = await CalculateBlockHashesAsync(stream, blockSize, algorithm); |
|
149 |
|
|
159 | 150 |
//And then proceed with creating and returning a TreeHash |
160 |
var length = stream.Length;
|
|
161 |
var list = hashes.Result.OrderBy(pair => pair.Key).Select(pair => pair.Value).ToList();
|
|
151 |
var length = stream.Length; |
|
152 |
var list = hashes.OrderBy(pair => pair.Key).Select(pair => pair.Value).ToList(); |
|
162 | 153 |
|
163 | 154 |
var treeHash = new TreeHash(algorithm) |
164 | 155 |
{ |
165 |
Bytes = length,
|
|
166 |
BlockSize = blockSize,
|
|
156 |
Bytes = length, |
|
157 |
BlockSize = blockSize, |
|
167 | 158 |
Hashes = list |
168 | 159 |
}; |
169 | 160 |
|
170 |
yield return Task.Factory.FromResult(treeHash);
|
|
161 |
return treeHash;
|
|
171 | 162 |
} |
172 |
|
|
173 | 163 |
} |
174 | 164 |
|
165 |
|
|
175 | 166 |
//The Task Iterator style allows the execution of a sequence of patterns using |
176 | 167 |
//iterator syntax. |
177 | 168 |
//This particular variation returns the result of the last task, if there is one |
Also available in: Unified diff