Updated registration for restart manager to push null for app params (c++ dll import...
[pithos-ms-client] / trunk / Pithos.Core / Agents / BlockExtensions.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;
45 using System.Diagnostics.Contracts;
46 using System.Linq;
47 using System.Reflection;
48 using System.Security.Cryptography;
49 using System.Text;
50 using System.IO;
51 using System.Text.RegularExpressions;
52 using System.Threading.Tasks;
53 using Pithos.Network;
54 using log4net;
55
56 namespace Pithos.Core.Agents
57 {
58     static class BlockExtensions
59     {
60
61         private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
62
63         public static int Read(this FileInfo fileInfo,byte[] buffer,long offset,int count)
64         {
65             if (offset < 0)
66                 throw new ArgumentOutOfRangeException("offset", offset, "The file offset can't be negative");
67             Contract.EndContractBlock();
68             //Open the stream only long enough to read a block
69             using (var stream = fileInfo.OpenRead())
70             {
71                 stream.Seek(offset, SeekOrigin.Begin);
72                 return  stream.Read(buffer, 0, count);                
73             }
74         }
75
76        public static string CalculateHash(this FileSystemInfo info,int blockSize,string algorithm)
77         {
78             if (info==null)
79                 throw new ArgumentNullException("info");
80             if (String.IsNullOrWhiteSpace(info.FullName))
81                 throw new ArgumentException("info");
82             if (blockSize<=0)
83                 throw new ArgumentOutOfRangeException("blockSize",blockSize,"blockSize must be greater than 0");
84             if (String.IsNullOrWhiteSpace(algorithm))
85                 throw new ArgumentNullException("algorithm");
86             Contract.EndContractBlock();
87            info.Refresh();
88            //The hash for directories is an empty string
89            if (info is DirectoryInfo)
90                 return String.Empty;
91            //The hash for non-existent files is an empty string
92            if (!info.Exists)
93                return String.Empty;
94
95            return Signature.CalculateTreeHash(info.FullName, blockSize, algorithm).TopHash.ToHashString();
96
97         }
98
99        /// <summary>
100        ///Calculates a simple hash for an entire file
101        /// </summary>
102        /// <param name="info">The file to hash</param>
103        /// <param name="hasher">The hash algorithm to use</param>
104        /// <returns>A hash value for the entire file. An empty string if the file does not exist.</returns>
105        public static string ComputeShortHash(this FileInfo info, HashAlgorithm hasher,IStatusNotification notification)
106        {
107            if(info == null)
108                throw new ArgumentNullException("info");
109            if(hasher== null)
110                throw new ArgumentNullException("hasher");
111            Contract.EndContractBlock();
112            info.Refresh();
113            if (!info.Exists)
114                return String.Empty;
115
116            if (Log.IsDebugEnabled)
117                 Log.DebugFormat("Short Hashing [{0}] ",info.FullName);
118
119            var progress = new StatusNotification("");
120
121            using (var stream = new FileStream(info.FullName,FileMode.Open, FileAccess.Read, FileShare.Read,65536))
122            {
123                var buffer = new byte[65536];
124                int counter=0;
125                int bytesRead;
126                do
127                {
128                    bytesRead = stream.Read(buffer, 0, 32768);
129                    if (bytesRead > 0)
130                    {
131                        hasher.TransformBlock(buffer, 0, bytesRead, null, 0);
132                    }
133                    counter++;
134                    if (counter % 100 == 0)
135                    {
136                        progress.Title = String.Format("Hashing {0:p} of {1}", stream.Position*1.0/stream.Length,
137                                                       info.Name);
138                        notification.Notify(progress);
139                    }
140                } while (bytesRead > 0);
141                hasher.TransformFinalBlock(buffer, 0, 0);
142                var hash = hasher.Hash;
143
144                progress.Title = String.Format("Hashed {0} ", info.Name);
145                notification.Notify(progress);
146
147                var hashString = hash.ToHashString();
148
149                return hashString;
150            }
151        }
152
153         public static string ComputeShortHash(this FileInfo info,IStatusNotification notification)
154        {
155            if(info == null)
156                throw new ArgumentNullException("info");
157             Contract.EndContractBlock();
158
159            using (var hasher=HashAlgorithm.Create("md5"))
160            {               
161                return ComputeShortHash(info,hasher,notification);
162            }
163        }
164
165     }
166 }