wasStitchNET – Blame information for rev 13

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