wasStitchNET – Blame information for rev 17

Subversion Repositories:
Rev:
Rev Author Line No. Line
13 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
17 office 7 using System.Collections.Generic;
13 office 8 using System.IO;
9 using System.Linq;
10 using System.Net;
11 using System.Security.Cryptography;
12 using System.Threading.Tasks;
13 using wasDAVClient;
17 office 14 using wasDAVClient.Model;
13 office 15 using wasSharp;
16 using wasSharpNET.IO.Utilities;
17  
18 namespace wasStitchNET.Repository
19 {
20 public class Hashing
21 {
22 /// <summary>
23 /// Hash local files inside a directory.
24 /// </summary>
25 /// <param name="path">the path to a directory to hash</param>
26 /// <param name="bufferSize">the partial hash buffer (default: 16384)</param>
27 /// <returns>a SHA1 hash of all files in a directory ordered by name</returns>
28 public static async Task<string> HashLocalFiles(string path, int bufferSize = 16384)
29 {
30 return await new Task<string>(() =>
31 {
32 using (var sha1managed = new SHA1Managed())
33 {
34 foreach (var stream in Directory
35 .GetFiles(path, "*.*", SearchOption.AllDirectories)
36 .OrderBy(o => o.Split(Path.DirectorySeparatorChar).Last())
37 .Select(file => IOExtensions.GetWriteStream(file, FileMode.Open,
17 office 38 FileAccess.Read, FileShare.Read, STITCH_CONSTANTS.LOCAL_FILE_ACCESS_TIMEOUT)))
13 office 39 using (var binaryReader = new BinaryReader(stream))
40 {
41 var buff = new byte[bufferSize];
42 int read;
43 while ((read = binaryReader.Read(buff, 0, buff.Length)) != 0)
44 sha1managed.TransformBlock(buff, 0, read, null, 0);
45 }
46  
47 sha1managed.TransformFinalBlock(new byte[] { }, 0, 0);
48 return sha1managed.Hash.ToHexString();
49 }
50 });
51 }
52  
53 /// <summary>
54 /// Hash the files of a Stitch remote repository release.
55 /// </summary>
56 /// <param name="server">the server to hash the release for</param>
57 /// <param name="update">the Stitch release to hash</param>
58 /// <param name="timeout">the timeout in milliseconds to allow the server to respond</param>
59 /// <returns>a SHA1 hash of all files in the release directory ordered by name</returns>
60 public static async Task<string> HashRemoteFiles(string server, string update, int timeout)
61 {
62 using (var client = new Client(new NetworkCredential())
63 {
64 Timeout = timeout,
65 UserAgent = STITCH_CONSTANTS.USER_AGENT.Product.Name,
66 UserAgentVersion = STITCH_CONSTANTS.USER_AGENT.Product.Version,
67 Server = server,
68 BasePath = string.Join(@"/", STITCH_CONSTANTS.UPDATE_PATH,
69 STITCH_CONSTANTS.PROGRESSIVE_PATH)
70 })
71 {
72 return $"{await HashRemoteFiles(client, string.Join(@"/", update, STITCH_CONSTANTS.UPDATE_DATA_PATH))}";
73 }
74 }
75  
76 /// <summary>
77 /// Hash the files of a Stitch remote repository release using an existing was DAV client.
78 /// </summary>
79 /// <param name="client">the was DAV client to use</param>
80 /// <param name="path">the path to the repository release folder</param>
81 /// <param name="bufferSize">the partial hash buffer (default: 16384)</param>
82 /// <returns>a SHA1 hash of all files in the release directory ordered by name</returns>
83 public static async Task<string> HashRemoteFiles(Client client, string path, int bufferSize = 16384)
84 {
85 using (var sha1managed = new SHA1Managed())
86 {
17 office 87 //var list = new List<Item>();
13 office 88 foreach (var stream in (await client.List(path, Constants.DavDepth.ALL))
89 .OrderBy(o => o.DisplayName)
90 .Where(item => !item.IsCollection)
17 office 91 .Select(file => client.Download(file.Href)))
13 office 92 using (var binaryReader = new BinaryReader(await stream))
93 {
94 var buff = new byte[bufferSize];
95 int read;
96 while ((read = binaryReader.Read(buff, 0, buff.Length)) != 0)
97 sha1managed.TransformBlock(buff, 0, read, null, 0);
98 }
99  
100 sha1managed.TransformFinalBlock(new byte[] { }, 0, 0);
101 return sha1managed.Hash.ToHexString();
102 }
103 }
104 }
105 }