root / trunk / Pithos.Core / Agents / BlockExtensions.cs @ 3906933e
History | View | Annotate | Download (9.9 kB)
1 | 12c87c0e | pkanavos | #region |
---|---|---|---|
2 | 12c87c0e | pkanavos | /* ----------------------------------------------------------------------- |
3 | 12c87c0e | pkanavos | * <copyright file="BlockExtensions.cs" company="GRNet"> |
4 | 12c87c0e | pkanavos | * |
5 | 12c87c0e | pkanavos | * Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 | 12c87c0e | pkanavos | * |
7 | 12c87c0e | pkanavos | * Redistribution and use in source and binary forms, with or |
8 | 12c87c0e | pkanavos | * without modification, are permitted provided that the following |
9 | 12c87c0e | pkanavos | * conditions are met: |
10 | 12c87c0e | pkanavos | * |
11 | 12c87c0e | pkanavos | * 1. Redistributions of source code must retain the above |
12 | 12c87c0e | pkanavos | * copyright notice, this list of conditions and the following |
13 | 12c87c0e | pkanavos | * disclaimer. |
14 | 12c87c0e | pkanavos | * |
15 | 12c87c0e | pkanavos | * 2. Redistributions in binary form must reproduce the above |
16 | 12c87c0e | pkanavos | * copyright notice, this list of conditions and the following |
17 | 12c87c0e | pkanavos | * disclaimer in the documentation and/or other materials |
18 | 12c87c0e | pkanavos | * provided with the distribution. |
19 | 12c87c0e | pkanavos | * |
20 | 12c87c0e | pkanavos | * |
21 | 12c87c0e | pkanavos | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 | 12c87c0e | pkanavos | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | 12c87c0e | pkanavos | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 | 12c87c0e | pkanavos | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 | 12c87c0e | pkanavos | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | 12c87c0e | pkanavos | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | 12c87c0e | pkanavos | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 | 12c87c0e | pkanavos | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 | 12c87c0e | pkanavos | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | 12c87c0e | pkanavos | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 | 12c87c0e | pkanavos | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 | 12c87c0e | pkanavos | * POSSIBILITY OF SUCH DAMAGE. |
33 | 12c87c0e | pkanavos | * |
34 | 12c87c0e | pkanavos | * The views and conclusions contained in the software and |
35 | 12c87c0e | pkanavos | * documentation are those of the authors and should not be |
36 | 12c87c0e | pkanavos | * interpreted as representing official policies, either expressed |
37 | 12c87c0e | pkanavos | * or implied, of GRNET S.A. |
38 | 12c87c0e | pkanavos | * </copyright> |
39 | 12c87c0e | pkanavos | * ----------------------------------------------------------------------- |
40 | 12c87c0e | pkanavos | */ |
41 | 12c87c0e | pkanavos | #endregion |
42 | 12c87c0e | pkanavos | using System; |
43 | 12c87c0e | pkanavos | using System.Collections.Generic; |
44 | 12c87c0e | pkanavos | using System.Diagnostics; |
45 | 12c87c0e | pkanavos | using System.Diagnostics.Contracts; |
46 | 12c87c0e | pkanavos | using System.Linq; |
47 | 12c87c0e | pkanavos | using System.Reflection; |
48 | 796a7c29 | pkanavos | |
49 | 12c87c0e | pkanavos | using System.Text; |
50 | 12c87c0e | pkanavos | using System.IO; |
51 | 12c87c0e | pkanavos | using System.Text.RegularExpressions; |
52 | 12c87c0e | pkanavos | using System.Threading; |
53 | 12c87c0e | pkanavos | using System.Threading.Tasks; |
54 | 12c87c0e | pkanavos | using Pithos.Network; |
55 | 12c87c0e | pkanavos | using log4net; |
56 | 796a7c29 | pkanavos | using OpenSSL.Core; |
57 | 796a7c29 | pkanavos | using OpenSSL.Crypto; |
58 | 796a7c29 | pkanavos | |
59 | 12c87c0e | pkanavos | |
60 | 12c87c0e | pkanavos | namespace Pithos.Core.Agents |
61 | 12c87c0e | pkanavos | { |
62 | 12c87c0e | pkanavos | static class BlockExtensions |
63 | 12c87c0e | pkanavos | { |
64 | 12c87c0e | pkanavos | |
65 | 12c87c0e | pkanavos | private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
66 | 12c87c0e | pkanavos | |
67 | 12c87c0e | pkanavos | public static int Read(this FileInfo fileInfo,byte[] buffer,long offset,int count) |
68 | 12c87c0e | pkanavos | { |
69 | 12c87c0e | pkanavos | if (offset < 0) |
70 | 12c87c0e | pkanavos | throw new ArgumentOutOfRangeException("offset", offset, "The file offset can't be negative"); |
71 | 12c87c0e | pkanavos | Contract.EndContractBlock(); |
72 | 12c87c0e | pkanavos | //Open the stream only long enough to read a block |
73 | 12c87c0e | pkanavos | using (var stream = fileInfo.OpenRead()) |
74 | 12c87c0e | pkanavos | { |
75 | 12c87c0e | pkanavos | stream.Seek(offset, SeekOrigin.Begin); |
76 | 12c87c0e | pkanavos | return stream.Read(buffer, 0, count); |
77 | 12c87c0e | pkanavos | } |
78 | 12c87c0e | pkanavos | } |
79 | 12c87c0e | pkanavos | |
80 | b1303755 | pkanavos | |
81 | 12c87c0e | pkanavos | public static string CalculateHash(this FileSystemInfo info,int blockSize,string algorithm,CancellationToken token,IProgress<double> progress ) |
82 | 12c87c0e | pkanavos | { |
83 | 12c87c0e | pkanavos | if (info==null) |
84 | 12c87c0e | pkanavos | throw new ArgumentNullException("info"); |
85 | 12c87c0e | pkanavos | if (String.IsNullOrWhiteSpace(info.FullName)) |
86 | 12c87c0e | pkanavos | throw new ArgumentException("info"); |
87 | 12c87c0e | pkanavos | if (blockSize<=0) |
88 | 12c87c0e | pkanavos | throw new ArgumentOutOfRangeException("blockSize",blockSize,"blockSize must be greater than 0"); |
89 | 12c87c0e | pkanavos | if (String.IsNullOrWhiteSpace(algorithm)) |
90 | 12c87c0e | pkanavos | throw new ArgumentNullException("algorithm"); |
91 | 12c87c0e | pkanavos | Contract.EndContractBlock(); |
92 | 12c87c0e | pkanavos | info.Refresh(); |
93 | 12c87c0e | pkanavos | if (info.FullName.Split('/').Contains(".pithos.cache")) |
94 | 12c87c0e | pkanavos | throw new ArgumentException(String.Format("Trying to hash file from the cache folder: [{0}]", info.FullName)); |
95 | 12c87c0e | pkanavos | |
96 | 12c87c0e | pkanavos | //The hash for directories is an empty string |
97 | 12c87c0e | pkanavos | if (info is DirectoryInfo) |
98 | 12c87c0e | pkanavos | return String.Empty; |
99 | 12c87c0e | pkanavos | //The hash for non-existent files is an empty string |
100 | 12c87c0e | pkanavos | if (!info.Exists) |
101 | 12c87c0e | pkanavos | return String.Empty; |
102 | 12c87c0e | pkanavos | |
103 | 12c87c0e | pkanavos | return Signature.CalculateTreeHash(info.FullName, blockSize, algorithm,token,progress).TopHash.ToHashString(); |
104 | 12c87c0e | pkanavos | |
105 | 12c87c0e | pkanavos | } |
106 | 12c87c0e | pkanavos | |
107 | 12c87c0e | pkanavos | /// <summary> |
108 | 12c87c0e | pkanavos | ///Calculates a simple hash for an entire file |
109 | 12c87c0e | pkanavos | /// </summary> |
110 | 12c87c0e | pkanavos | /// <param name="info">The file to hash</param> |
111 | 12c87c0e | pkanavos | /// <param name="hasher">The hash algorithm to use</param> |
112 | 12c87c0e | pkanavos | /// <returns>A hash value for the entire file. An empty string if the file does not exist.</returns> |
113 | 3906933e | George Pantazis | //public static string ComputeShortHash(this FileInfo info, MessageDigestContext hasher,IStatusNotification notification) |
114 | 3906933e | George Pantazis | //{ |
115 | 3906933e | George Pantazis | // if(info == null) |
116 | 3906933e | George Pantazis | // throw new ArgumentNullException("info"); |
117 | 3906933e | George Pantazis | // if(hasher== null) |
118 | 3906933e | George Pantazis | // throw new ArgumentNullException("hasher"); |
119 | 3906933e | George Pantazis | // Contract.EndContractBlock(); |
120 | 3906933e | George Pantazis | // info.Refresh(); |
121 | 3906933e | George Pantazis | |
122 | 3906933e | George Pantazis | // if (!info.Exists) |
123 | 3906933e | George Pantazis | // return String.Empty; |
124 | 3906933e | George Pantazis | // if (info.FullName.Split('/').Contains(".pithos.cache")) |
125 | 3906933e | George Pantazis | // throw new ArgumentException(String.Format("Trying to hash file from the cache folder: [{0}]", info.FullName)); |
126 | 3906933e | George Pantazis | |
127 | 3906933e | George Pantazis | // if (Log.IsDebugEnabled) |
128 | 3906933e | George Pantazis | // Log.DebugFormat("Short Hashing [{0}] ",info.FullName); |
129 | 3906933e | George Pantazis | |
130 | 3906933e | George Pantazis | // if (info.Length==0) |
131 | 3906933e | George Pantazis | // return Signature.MD5_EMPTY; |
132 | 3906933e | George Pantazis | |
133 | 3906933e | George Pantazis | // var progress = new StatusNotification(""); |
134 | 3906933e | George Pantazis | |
135 | 3906933e | George Pantazis | // using (var stream = new FileStream(info.FullName,FileMode.Open, FileAccess.Read, FileShare.Read,Signature.BufferSize)) |
136 | 3906933e | George Pantazis | // { |
137 | 3906933e | George Pantazis | // var buffer = new byte[65536]; |
138 | 3906933e | George Pantazis | // int counter=0; |
139 | 3906933e | George Pantazis | // int bytesRead; |
140 | 3906933e | George Pantazis | // do |
141 | 3906933e | George Pantazis | // { |
142 | 3906933e | George Pantazis | // bytesRead = stream.Read(buffer, 0, 32768); |
143 | 3906933e | George Pantazis | // if (bytesRead > 0) |
144 | 3906933e | George Pantazis | // { |
145 | 3906933e | George Pantazis | // if (bytesRead == buffer.Length) |
146 | 3906933e | George Pantazis | // { |
147 | 3906933e | George Pantazis | // hasher.Update(buffer); |
148 | 3906933e | George Pantazis | // } |
149 | 3906933e | George Pantazis | // else |
150 | 3906933e | George Pantazis | // { |
151 | 3906933e | George Pantazis | // var block = new byte[bytesRead]; |
152 | 3906933e | George Pantazis | // Buffer.BlockCopy(buffer, 0, block, 0, bytesRead); |
153 | 3906933e | George Pantazis | // hasher.Update(block); |
154 | 3906933e | George Pantazis | // } |
155 | 3906933e | George Pantazis | // } |
156 | 3906933e | George Pantazis | // counter++; |
157 | 3906933e | George Pantazis | // if (counter % 100 == 0) |
158 | 3906933e | George Pantazis | // { |
159 | 3906933e | George Pantazis | // progress.Title = String.Format("Hashing {0:p} of {1}", stream.Position*1.0/stream.Length, |
160 | 3906933e | George Pantazis | // info.Name); |
161 | 3906933e | George Pantazis | // notification.Notify(progress); |
162 | 3906933e | George Pantazis | // } |
163 | 3906933e | George Pantazis | // } while (bytesRead > 0); |
164 | 3906933e | George Pantazis | // var hash = hasher.DigestFinal(); |
165 | 3906933e | George Pantazis | |
166 | 3906933e | George Pantazis | // progress.Title = String.Format("Hashed {0} ", info.Name); |
167 | 3906933e | George Pantazis | // notification.Notify(progress); |
168 | 3906933e | George Pantazis | |
169 | 3906933e | George Pantazis | // var hashString = hash.ToHashString(); |
170 | 3906933e | George Pantazis | |
171 | 3906933e | George Pantazis | // return hashString; |
172 | 3906933e | George Pantazis | // } |
173 | 3906933e | George Pantazis | //} |
174 | 12c87c0e | pkanavos | |
175 | 796a7c29 | pkanavos | /* |
176 | 12c87c0e | pkanavos | public static async Task<string> ComputeShortHash(this FileInfo info, MD5BlockCalculator calculator,IStatusNotification notification) |
177 | 12c87c0e | pkanavos | { |
178 | 12c87c0e | pkanavos | if(info == null) |
179 | 12c87c0e | pkanavos | throw new ArgumentNullException("info"); |
180 | 12c87c0e | pkanavos | if(calculator== null) |
181 | 12c87c0e | pkanavos | throw new ArgumentNullException("calculator"); |
182 | 12c87c0e | pkanavos | Contract.EndContractBlock(); |
183 | 12c87c0e | pkanavos | info.Refresh(); |
184 | 12c87c0e | pkanavos | if (!info.Exists) |
185 | 12c87c0e | pkanavos | return String.Empty; |
186 | 12c87c0e | pkanavos | |
187 | 12c87c0e | pkanavos | if (info.FullName.Split('/').Contains(".pithos.cache")) |
188 | 12c87c0e | pkanavos | throw new ArgumentException(String.Format("Trying to hash file from the cache folder: [{0}]", info.FullName)); |
189 | 12c87c0e | pkanavos | |
190 | 12c87c0e | pkanavos | if (Log.IsDebugEnabled) |
191 | 12c87c0e | pkanavos | Log.DebugFormat("Short Hashing [{0}] ",info.FullName); |
192 | 12c87c0e | pkanavos | |
193 | 12c87c0e | pkanavos | if (info.Length == 0) |
194 | 12c87c0e | pkanavos | return Signature.MD5_EMPTY; |
195 | 12c87c0e | pkanavos | |
196 | 12c87c0e | pkanavos | var progress = new StatusNotification(""); |
197 | 12c87c0e | pkanavos | |
198 | 12c87c0e | pkanavos | |
199 | 12c87c0e | pkanavos | |
200 | 12c87c0e | pkanavos | using (var stream = new FileStream(info.FullName,FileMode.Open, FileAccess.Read, FileShare.Read,Signature.BufferSize)) |
201 | 12c87c0e | pkanavos | { |
202 | 12c87c0e | pkanavos | int counter=0; |
203 | 12c87c0e | pkanavos | int bytesRead; |
204 | 12c87c0e | pkanavos | do |
205 | 12c87c0e | pkanavos | { |
206 | 12c87c0e | pkanavos | var buffer = new byte[65536]; |
207 | 12c87c0e | pkanavos | bytesRead = stream.Read(buffer, 0, 32768); |
208 | 12c87c0e | pkanavos | if (bytesRead > 0) |
209 | 12c87c0e | pkanavos | { |
210 | 12c87c0e | pkanavos | calculator.PostBlock(counter,buffer,bytesRead); |
211 | 12c87c0e | pkanavos | } |
212 | 12c87c0e | pkanavos | counter++; |
213 | 12c87c0e | pkanavos | if (counter % 100 == 0) |
214 | 12c87c0e | pkanavos | { |
215 | 12c87c0e | pkanavos | progress.Title = String.Format("Hashing {0:p} of {1}", stream.Position*1.0/stream.Length, |
216 | 12c87c0e | pkanavos | info.Name); |
217 | 12c87c0e | pkanavos | notification.Notify(progress); |
218 | 12c87c0e | pkanavos | } |
219 | 12c87c0e | pkanavos | } while (bytesRead > 0); |
220 | 12c87c0e | pkanavos | |
221 | 12c87c0e | pkanavos | var hashString = await calculator.GetHash(); |
222 | 12c87c0e | pkanavos | |
223 | 12c87c0e | pkanavos | |
224 | 12c87c0e | pkanavos | progress.Title = String.Format("Hashed {0} ", info.Name); |
225 | 12c87c0e | pkanavos | notification.Notify(progress); |
226 | 12c87c0e | pkanavos | |
227 | 12c87c0e | pkanavos | |
228 | 12c87c0e | pkanavos | return hashString; |
229 | 12c87c0e | pkanavos | } |
230 | 12c87c0e | pkanavos | } |
231 | 796a7c29 | pkanavos | */ |
232 | 12c87c0e | pkanavos | |
233 | 3906933e | George Pantazis | // public static string ComputeShortHash(this FileInfo info,IStatusNotification notification) |
234 | 3906933e | George Pantazis | //{ |
235 | 3906933e | George Pantazis | // if(info == null) |
236 | 3906933e | George Pantazis | // throw new ArgumentNullException("info"); |
237 | 3906933e | George Pantazis | // Contract.EndContractBlock(); |
238 | 3906933e | George Pantazis | // if (info.FullName.Split('/').Contains(".pithos.cache")) |
239 | 3906933e | George Pantazis | // throw new ArgumentException(String.Format("Trying to hash file from the cache folder: [{0}]", info.FullName)); |
240 | 3906933e | George Pantazis | |
241 | 3906933e | George Pantazis | // using (var hasher=new MessageDigestContext(MessageDigest.CreateByName("md5"))) |
242 | 3906933e | George Pantazis | // { |
243 | 3906933e | George Pantazis | // //hasher.Init(); |
244 | 3906933e | George Pantazis | // //return ComputeShortHash(info,hasher,notification); |
245 | 3906933e | George Pantazis | // return null; |
246 | 3906933e | George Pantazis | // } |
247 | 3906933e | George Pantazis | //} |
248 | 12c87c0e | pkanavos | |
249 | 12c87c0e | pkanavos | } |
250 | 12c87c0e | pkanavos | } |