Fixes to hashing
[pithos-ms-client] / trunk / Pithos.Network / Signature.cs
index 296e810..389dd79 100644 (file)
@@ -79,7 +79,7 @@ namespace Pithos.Network
 
             string hash;
             using (var hasher = MD5.Create())
-            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
+            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 65536, true))
             {
                 var hashBytes = hasher.ComputeHash(stream);
                 hash = hashBytes.ToHashString();
@@ -115,18 +115,21 @@ namespace Pithos.Network
             return shb.ToString().ToLower();
         }
 
-        public static TreeHash CalculateTreeHash(FileInfo fileInfo, int blockSize, string algorithm)
+        public static TreeHash CalculateTreeHash(FileSystemInfo fileInfo, int blockSize, string algorithm)
         {
-            if (fileInfo==null)
+            if (fileInfo == null)
                 throw new ArgumentNullException("fileInfo");
             if (String.IsNullOrWhiteSpace(fileInfo.FullName))
-                throw new ArgumentException("fileInfo.FullName is empty","fileInfo");
+                throw new ArgumentException("fileInfo.FullName is empty", "fileInfo");
             if (blockSize <= 0)
                 throw new ArgumentOutOfRangeException("blockSize", "blockSize must be a value greater than zero ");
             if (String.IsNullOrWhiteSpace(algorithm))
                 throw new ArgumentNullException("algorithm");
             Contract.EndContractBlock();
 
+            if (fileInfo is DirectoryInfo || !fileInfo.Exists)
+                return TreeHash.Empty;
+
             return CalculateTreeHash(fileInfo.FullName, blockSize, algorithm);
         }
 
@@ -177,6 +180,9 @@ namespace Pithos.Network
                 throw new ArgumentNullException("algorithm");
             Contract.EndContractBlock();
 
+            if (Log.IsDebugEnabled)
+                Log.DebugFormat("Calc Signature [{0}]",filePath);
+
             //DON'T calculate hashes for folders
             if (Directory.Exists(filePath))
                 return new TreeHash(algorithm);
@@ -188,7 +194,7 @@ namespace Pithos.Network
             using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, blockSize, true))
             {
                 //Calculate the blocks asyncrhonously
-                var hashes = await BlockHashAlgorithms.CalculateBlockHashesAgentAsync(stream, blockSize, algorithm, parallelism);                
+                var hashes = await BlockHashAlgorithms.CalculateBlockHashesInPlacePFor(stream, blockSize, algorithm, parallelism);                
 
                 //And then proceed with creating and returning a TreeHash
                 var length = stream.Length;
@@ -198,9 +204,14 @@ namespace Pithos.Network
                 {
                     Bytes = length,
                     BlockSize = blockSize,
-                    Hashes = list
+                    Hashes = list,
                 };
 
+                string fileHash;
+                var hasher = HashAlgorithm.Create("MD5");
+                stream.Position = 0;
+                treeHash.MD5= hasher.ComputeHash(stream).ToHashString();
+
                 return treeHash;
             }
         }