root / trunk / Pithos.Core / Agents / FileInfoExtensions.cs @ ab2f6f79
History | View | Annotate | Download (2.5 kB)
1 | 1caef52e | Panagiotis Kanavos | using System; |
---|---|---|---|
2 | 1caef52e | Panagiotis Kanavos | using System.Collections.Generic; |
3 | cfed7823 | Panagiotis Kanavos | using System.Diagnostics.Contracts; |
4 | 1caef52e | Panagiotis Kanavos | using System.Linq; |
5 | 1caef52e | Panagiotis Kanavos | using System.Text; |
6 | 1caef52e | Panagiotis Kanavos | using System.IO; |
7 | 1caef52e | Panagiotis Kanavos | using System.Text.RegularExpressions; |
8 | a27aa447 | Panagiotis Kanavos | using System.Threading.Tasks; |
9 | 0af3141d | Panagiotis Kanavos | using Pithos.Network; |
10 | 1caef52e | Panagiotis Kanavos | |
11 | 1caef52e | Panagiotis Kanavos | namespace Pithos.Core.Agents |
12 | 1caef52e | Panagiotis Kanavos | { |
13 | 1caef52e | Panagiotis Kanavos | static class FileInfoExtensions |
14 | 1caef52e | Panagiotis Kanavos | { |
15 | 27361404 | Panagiotis Kanavos | |
16 | cfed7823 | Panagiotis Kanavos | |
17 | a27aa447 | Panagiotis Kanavos | public static int Read(this FileInfo fileInfo,byte[] buffer,int offset,int count) |
18 | a27aa447 | Panagiotis Kanavos | { |
19 | a27aa447 | Panagiotis Kanavos | //Open the stream only long enough to read a block |
20 | a27aa447 | Panagiotis Kanavos | using (var stream = fileInfo.OpenRead()) |
21 | a27aa447 | Panagiotis Kanavos | { |
22 | a27aa447 | Panagiotis Kanavos | stream.Seek(offset, SeekOrigin.Begin); |
23 | a27aa447 | Panagiotis Kanavos | return stream.Read(buffer, 0, count); |
24 | a27aa447 | Panagiotis Kanavos | } |
25 | a27aa447 | Panagiotis Kanavos | } |
26 | a27aa447 | Panagiotis Kanavos | |
27 | a27aa447 | Panagiotis Kanavos | public static Task<int> ReadAsync(this FileInfo fileInfo, byte[] buffer, int offset, int count) |
28 | a27aa447 | Panagiotis Kanavos | { |
29 | a27aa447 | Panagiotis Kanavos | //The using statement is not used because we will work asyncronously |
30 | a27aa447 | Panagiotis Kanavos | var stream = FileAsync.OpenRead(fileInfo.FullName); |
31 | a27aa447 | Panagiotis Kanavos | try |
32 | a27aa447 | Panagiotis Kanavos | { |
33 | a27aa447 | Panagiotis Kanavos | stream.Seek(offset, SeekOrigin.Begin); |
34 | a27aa447 | Panagiotis Kanavos | var read = stream.ReadAsync(buffer, offset, count); |
35 | a27aa447 | Panagiotis Kanavos | return read.ContinueWith(t => |
36 | a27aa447 | Panagiotis Kanavos | { |
37 | a27aa447 | Panagiotis Kanavos | //Make sure the stream closes |
38 | a27aa447 | Panagiotis Kanavos | stream.Close(); |
39 | 24477d85 | Panagiotis Kanavos | stream = null; |
40 | 24477d85 | Panagiotis Kanavos | |
41 | 24477d85 | Panagiotis Kanavos | t.PropagateExceptions(); |
42 | 24477d85 | Panagiotis Kanavos | |
43 | a27aa447 | Panagiotis Kanavos | return t.Result; |
44 | a27aa447 | Panagiotis Kanavos | }); |
45 | a27aa447 | Panagiotis Kanavos | } |
46 | a27aa447 | Panagiotis Kanavos | catch (Exception) |
47 | a27aa447 | Panagiotis Kanavos | { |
48 | 24477d85 | Panagiotis Kanavos | //In case of error make sure we close the stream |
49 | 24477d85 | Panagiotis Kanavos | //The stream may have been |
50 | 24477d85 | Panagiotis Kanavos | if (stream!=null) |
51 | 24477d85 | Panagiotis Kanavos | stream.Close(); |
52 | a27aa447 | Panagiotis Kanavos | throw; |
53 | a27aa447 | Panagiotis Kanavos | } |
54 | a27aa447 | Panagiotis Kanavos | |
55 | a27aa447 | Panagiotis Kanavos | } |
56 | 1caef52e | Panagiotis Kanavos | |
57 | 0af3141d | Panagiotis Kanavos | public static string CalculateHash(this FileInfo info,int blockSize,string algorithm) |
58 | 0af3141d | Panagiotis Kanavos | { |
59 | cfed7823 | Panagiotis Kanavos | if (info==null) |
60 | cfed7823 | Panagiotis Kanavos | throw new ArgumentNullException("info"); |
61 | cfed7823 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(info.FullName)) |
62 | cfed7823 | Panagiotis Kanavos | throw new ArgumentException("info"); |
63 | cfed7823 | Panagiotis Kanavos | if (blockSize<=0) |
64 | cfed7823 | Panagiotis Kanavos | throw new ArgumentOutOfRangeException("blockSize",blockSize,"blockSize must be greater than 0"); |
65 | cfed7823 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(algorithm)) |
66 | cfed7823 | Panagiotis Kanavos | throw new ArgumentNullException("algorithm"); |
67 | cfed7823 | Panagiotis Kanavos | Contract.EndContractBlock(); |
68 | cfed7823 | Panagiotis Kanavos | |
69 | 0af3141d | Panagiotis Kanavos | if (info.Length <= blockSize) |
70 | 0af3141d | Panagiotis Kanavos | return Signature.CalculateMD5(info.FullName); |
71 | 0af3141d | Panagiotis Kanavos | else |
72 | 0af3141d | Panagiotis Kanavos | return Signature.CalculateTreeHash(info.FullName, blockSize, algorithm).TopHash.ToHashString(); |
73 | 0af3141d | Panagiotis Kanavos | |
74 | 0af3141d | Panagiotis Kanavos | } |
75 | 0af3141d | Panagiotis Kanavos | |
76 | 1caef52e | Panagiotis Kanavos | } |
77 | 1caef52e | Panagiotis Kanavos | } |