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