3b5eea37c5b7b1e18a548e12b5d2e81b15750387
[pithos-ms-client] / trunk%2FPithos.Core%2FAgents%2FBlockExtensions.cs
1 #region
2 /* -----------------------------------------------------------------------
3  * <copyright file="BlockExtensions.cs" company="GRNet">
4  * 
5  * Copyright 2011-2012 GRNET S.A. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or
8  * without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  *   1. Redistributions of source code must retain the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer.
14  *
15  *   2. Redistributions in binary form must reproduce the above
16  *      copyright notice, this list of conditions and the following
17  *      disclaimer in the documentation and/or other materials
18  *      provided with the distribution.
19  *
20  *
21  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * The views and conclusions contained in the software and
35  * documentation are those of the authors and should not be
36  * interpreted as representing official policies, either expressed
37  * or implied, of GRNET S.A.
38  * </copyright>
39  * -----------------------------------------------------------------------
40  */
41 #endregion
42 using System;
43 using System.Collections.Generic;
44 using System.Diagnostics.Contracts;
45 using System.Linq;
46 using System.Reflection;
47 using System.Security.Cryptography;
48 using System.Text;
49 using System.IO;
50 using System.Text.RegularExpressions;
51 using System.Threading.Tasks;
52 using Pithos.Network;
53 using log4net;
54
55 namespace Pithos.Core.Agents
56 {
57     static class BlockExtensions
58     {
59
60         private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
61
62         public static int Read(this FileInfo fileInfo,byte[] buffer,long offset,int count)
63         {
64             if (offset < 0)
65                 throw new ArgumentOutOfRangeException("offset", offset, "The file offset can't be negative");
66             Contract.EndContractBlock();
67             //Open the stream only long enough to read a block
68             using (var stream = fileInfo.OpenRead())
69             {
70                 stream.Seek(offset, SeekOrigin.Begin);
71                 return  stream.Read(buffer, 0, count);                
72             }
73         }
74
75        public static string CalculateHash(this FileSystemInfo info,int blockSize,string algorithm)
76         {
77             if (info==null)
78                 throw new ArgumentNullException("info");
79             if (String.IsNullOrWhiteSpace(info.FullName))
80                 throw new ArgumentException("info");
81             if (blockSize<=0)
82                 throw new ArgumentOutOfRangeException("blockSize",blockSize,"blockSize must be greater than 0");
83             if (String.IsNullOrWhiteSpace(algorithm))
84                 throw new ArgumentNullException("algorithm");
85             Contract.EndContractBlock();
86
87            //The hash for directories is an empty string
88            if (info is DirectoryInfo)
89                 return String.Empty;
90            //The hash for non-existent files is an empty string
91            if (!info.Exists)
92                return String.Empty;
93
94            return Signature.CalculateTreeHash(info.FullName, blockSize, algorithm).TopHash.ToHashString();
95
96         }
97
98        /// <summary>
99        ///Calculates a simple hash for an entire file
100        /// </summary>
101        /// <param name="info">The file to hash</param>
102        /// <param name="hasher">The hash algorithm to use</param>
103        /// <returns>A hash value for the entire file. An empty string if the file does not exist.</returns>
104        public static string ComputeShortHash(this FileInfo info, HashAlgorithm hasher)
105        {
106            Contract.Requires(info != null);
107            Contract.Requires(hasher!= null);           
108
109            if (!info.Exists)
110                return String.Empty;
111
112            if (Log.IsDebugEnabled)
113                 Log.DebugFormat("Short Hashing [{0}] ",info.FullName);
114
115            using (var stream = info.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
116            {
117                var hash = hasher.ComputeHash(stream);
118                 var hashString = hash.ToHashString();
119                return hashString;
120            }
121        }
122
123         public static string ComputeShortHash(this FileInfo info)
124        {
125            Contract.Requires(info != null);           
126
127            using (var hasher=HashAlgorithm.Create("sha1"))
128            {               
129                return ComputeShortHash(info,hasher);
130            }
131        }
132
133     }
134 }