forked from staticvoidmain/sym-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloader.cs
358 lines (295 loc) · 8.37 KB
/
Downloader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace sym_fetch
{
public enum OutputStyle
{
Debugger,
SideBySide
}
public enum DownloadResult
{
NotFound,
Success,
Exists,
Error,
NoSymbols
}
public sealed class Downloader
{
public Downloader(
string directory = null,
OutputStyle style = OutputStyle.SideBySide,
string server = "https://msdl.microsoft.com/download/symbols",
int bufferSize = 4096)
{
this.style = style;
this.server = server;
this.directory = directory;
this.bufferSize = bufferSize;
}
public struct Assembly
{
public string Path;
public string Name;
public string PdbGuid;
public bool IsCompressed;
public Assembly(String path)
{
this.Path = path;
var segments = path.Split("/"[0]);
this.Name = segments[segments.Length - 1];
this.PdbGuid = segments[segments.Length - 2];
this.IsCompressed = false;
}
public void SetPath(string path)
{
this.Path = path;
}
public void SetName()
{
this.Name = this.Path.Split("/"[0])[this.Path.Split("/"[0]).Length - 1];
}
public void SetNameUsingPath(string path)
{
this.Name = path;
}
}
private const Int32 default_decimals = 2;
private const string userAgent = @"Microsoft-Symbol-Server/10.0.10522.521";
private readonly OutputStyle style;
private readonly string server;
private readonly string directory;
private readonly Int32 bufferSize = 4096;
public readonly Dictionary<string, string> FailedFiles = new Dictionary<string, string>();
private HttpWebResponse Retry(Assembly asm, bool headVerb)
{
var path = ProbeWithUnderscore(asm.Path);
var req = (HttpWebRequest)System.Net.WebRequest.Create(path);
req.UserAgent = userAgent;
if (headVerb)
{
req.Method = "HEAD";
}
return GetResponseNoException(req);
}
private HttpWebResponse RetryFilePointer(Assembly asm)
{
var path = ProbeWithFilePointer(asm.Path);
var req = (HttpWebRequest)System.Net.WebRequest.Create(path);
req.UserAgent = userAgent;
return GetResponseNoException(req);
}
// todo: clean this up
private long ProcessFileSize(HttpWebResponse webResp, out string filePath)
{
long length = 0;
filePath = null;
Stream receiveStream = webResp.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(receiveStream, encode);
Char[] read = new Char[webResp.ContentLength];
readStream.Read(read, 0, (int)webResp.ContentLength);
string file = new string(read, 0, (int)webResp.ContentLength);
if (file.Contains("PATH"))
{
file = file.Substring(5, file.Length - 5); //Removing PATH: from the output
try
{
FileInfo fInfo = new FileInfo(file);
if (fInfo.Exists)
{
length = fInfo.Length;
filePath = file;
}
}
catch (Exception ex)
{
WriteToLog(file, ex);
}
}
else
{
int position = webResp.ResponseUri.PathAndQuery.IndexOf(".pdb");
string fileName = webResp.ResponseUri.PathAndQuery.Substring(1, position + 3);
if (!FailedFiles.ContainsKey(fileName))
FailedFiles.Add(fileName, " - No matching PDBs found - " + file);
}
return length;
}
private static string ProbeWithUnderscore(string path)
{
path = path.Remove(path.Length - 1);
return path.Insert(path.Length, "_");
}
private static string ProbeWithFilePointer(string path)
{
int position = path.LastIndexOf('/');
path = path.Remove(position, (path.Length - position));
return path.Insert(path.Length, "/file.ptr");
}
public static HttpWebResponse GetResponseNoException(HttpWebRequest req)
{
try
{
return (HttpWebResponse)req.GetResponse();
}
catch (WebException we)
{
var resp = we.Response as HttpWebResponse;
if (resp == null)
throw;
return resp;
}
}
// UserAgent: Microsoft-Symbol-Server/10.0.10036.206
// Host: msdl.microsoft.com
// URI: /download/symbols/iiscore.pdb/6E3058DA562C4EB187071DC08CF7B59E1/iiscore.pdb
public string BuildUrl(string filename)
{
var meta = MetadataReader.Read(filename);
if (meta == null || string.IsNullOrEmpty(meta.PdbName))
{
return string.Empty;
}
else
{
var segments = meta.PdbName.Split(new char[] { '\\' });
var pdbName = segments[segments.Length - 1];
return this.server + "/" + pdbName + "/" + meta.DebugGUID.ToString("N").ToUpper() + meta.PdbAge + "/" + pdbName;
}
}
private string GetOutputDirectory(Assembly asm)
{
if (this.style == OutputStyle.Debugger)
{
return this.directory + "\\" + asm.Name + "\\" + asm.PdbGuid;
}
return this.directory;
}
public DownloadResult DownloadFile(string path)
{
bool headVerb = false;
bool fileptr = false;
var url = BuildUrl(path);
if(String.IsNullOrEmpty(url))
return DownloadResult.NoSymbols;
Assembly file = new Assembly(url);
string dirPath = GetOutputDirectory(file);
if (File.Exists(Path.Combine(dirPath, file.Name)))
{
return DownloadResult.Exists;
}
try
{
var req = (HttpWebRequest)System.Net.WebRequest.Create(file.Path);
req.UserAgent = userAgent;
var res = GetResponseNoException(req);
if (res.StatusCode == HttpStatusCode.NotFound)
{
res = Retry(file, headVerb);
if (res.StatusCode == HttpStatusCode.OK)
{
file.IsCompressed = true;
}
if (res.StatusCode == HttpStatusCode.NotFound)
{
res = RetryFilePointer(file);
fileptr = true;
}
if (res.StatusCode != HttpStatusCode.OK)
{
FailedFiles[file.Name] = " - " + res.StatusCode + " " + res.StatusDescription;
}
}
if (res.StatusCode == HttpStatusCode.OK)
{
HandleSuccess(res, file, fileptr, dirPath);
return DownloadResult.Success;
}
}
catch (Exception ex)
{
WriteToLog(file.Name, ex);
return DownloadResult.Error;
}
return DownloadResult.NotFound;
}
private void HandleSuccess(HttpWebResponse res, Assembly file, bool fileptr, string dirPath)
{
Byte[] readBytes = new Byte[this.bufferSize];
Directory.CreateDirectory(dirPath);
if (fileptr)
{
string filePath = dirPath + "\\" + file.Name;
string srcFile = null;
ProcessFileSize(res, out srcFile);
if (srcFile != null)
{
File.Copy(srcFile, filePath);
}
}
else
{
if (file.IsCompressed)
{
file.Name = ProbeWithUnderscore(file.Name);
}
var filePath = dirPath + "\\" + file.Name;
var writer = new FileStream(filePath, FileMode.Create);
var stream = res.GetResponseStream();
stream.CopyTo(writer);
if (file.IsCompressed)
{
HandleCompression(filePath);
}
}
}
private static void WriteToLog(string fileName, Exception exc)
{
WriteToLog(fileName, exc.ToString());
}
private static void WriteToLog(string fileName, string text)
{
// todo: format in the current directory
using (FileStream fs = new FileStream("Log.txt", FileMode.Append))
using (StreamWriter sr = new StreamWriter(fs))
{
sr.WriteLine(DateTime.Now.ToString() + " " + fileName + " - " + text);
}
}
private void HandleCompression(string filePath)
{
// out is the same as in, unless it ends in
var uncompressedFilePath = filePath;
if (filePath.EndsWith("_"))
{
uncompressedFilePath = filePath.Remove(filePath.Length - 1) + "b";
}
string args = string.Format("\"{0}\" \"{1}\"", filePath, uncompressedFilePath);
ProcessStartInfo startInfo = new ProcessStartInfo("expand", args);
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.Verb = "runas";
startInfo.CreateNoWindow = true;
try
{
var started = Process.Start(startInfo);
started.WaitForExit(600000);
File.Delete(filePath);
}
catch (Exception ex)
{
WriteToLog(filePath, ex);
}
}
}
}