Skip to content

Commit 4b07159

Browse files
committed
Handle the same file in a directory tree when creating the temp files and add better logging.
1 parent c8ea46b commit 4b07159

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

src/Brutal.Dev.StrongNameSigner/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private static ReaderParameters GetReadParameters(string assemblyPath, string[]
236236
readParams = new ReaderParameters
237237
{
238238
InMemory = true,
239-
ReadingMode = ReadingMode.Immediate,
239+
ReadingMode = ReadingMode.Deferred,
240240
AssemblyResolver = resolver,
241241
ReadSymbols = File.Exists(Path.ChangeExtension(assemblyPath, ".pdb"))
242242
};
@@ -246,7 +246,7 @@ private static ReaderParameters GetReadParameters(string assemblyPath, string[]
246246
readParams = new ReaderParameters
247247
{
248248
InMemory = true,
249-
ReadingMode = ReadingMode.Immediate,
249+
ReadingMode = ReadingMode.Deferred,
250250
AssemblyResolver = resolver
251251
};
252252
}

src/Brutal.Dev.StrongNameSigner/InputOutputFilePair.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public InputOutputFilePair(string inputFilePath, string outputFilePath)
1616
{
1717
InputFilePath = inputFilePath;
1818
OutputFilePath = outputFilePath;
19+
IsSameFile = InputFilePath == OutputFilePath;
20+
InputPdbPath = Path.ChangeExtension(InputFilePath, ".pdb");
21+
BackupAssemblyPath = InputFilePath + ".unsigned";
22+
BackupPdbPath = InputPdbPath + ".unsigned";
23+
HasSymbols = File.Exists(InputPdbPath);
1924
}
2025

2126
/// <summary>
@@ -40,29 +45,29 @@ public InputOutputFilePair(string inputFilePath, string outputFilePath)
4045
/// <remarks>
4146
/// Does not check for existence of the file, use <see cref="HasSymbols"/> for existence check.
4247
/// </remarks>
43-
public string InputPdbPath => Path.ChangeExtension(InputFilePath, ".pdb");
48+
public string InputPdbPath { get; }
4449

4550
/// <summary>
4651
/// Gets if both input and output file paths are the same file.
4752
/// </summary>
4853
/// <value>
4954
/// <c>true</c> if the paths are the same, <c>false</c> if they are not.
5055
/// </value>
51-
public bool IsSameFile => InputFilePath == OutputFilePath;
56+
public bool IsSameFile { get; }
5257

5358
/// <summary>
5459
/// Gets the path to an unsigned backup of the input assembly.
5560
/// </summary>
56-
public string BackupAssemblyPath => InputFilePath + ".unsigned";
61+
public string BackupAssemblyPath { get; }
5762

5863
/// <summary>
5964
/// Gets the path to where a backup of the source .PDB file.
6065
/// </summary>
61-
public string BackupPdbPath => InputPdbPath + ".unsigned";
66+
public string BackupPdbPath { get; }
6267

6368
/// <summary>
6469
/// Gets a value indicating whether the input assembly has a matching PDB file.
6570
/// </summary>
66-
public bool HasSymbols => File.Exists(InputPdbPath);
71+
public bool HasSymbols { get; }
6772
}
6873
}

src/Brutal.Dev.StrongNameSigner/SigningHelper.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
196196
}
197197
}
198198

199+
Log("1. Loading assemblies...");
200+
199201
// Convert all paths into AssemblyInfo objects.
200202
var allAssemblies = new HashSet<AssemblyInfo>();
201203

@@ -208,24 +210,30 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
208210
{
209211
try
210212
{
211-
var tempFilePath = Path.Combine(tempPath, Path.GetFileName(inputOutpuFilePair.InputFilePath));
213+
var tempFilePath = Path.Combine(tempPath, $"{Path.GetFileNameWithoutExtension(inputOutpuFilePair.InputFilePath)}.{Guid.NewGuid()}{Path.GetExtension(inputOutpuFilePair.InputFilePath)}");
212214
File.Copy(inputOutpuFilePair.InputFilePath, tempFilePath, true);
213215

214-
if (File.Exists(Path.ChangeExtension(inputOutpuFilePair.InputFilePath, ".pdb")))
216+
if (inputOutpuFilePair.HasSymbols)
215217
{
216-
File.Copy(Path.ChangeExtension(inputOutpuFilePair.InputFilePath, ".pdb"), Path.ChangeExtension(tempFilePath, ".pdb"), true);
218+
File.Copy(inputOutpuFilePair.InputPdbPath, Path.ChangeExtension(tempFilePath, ".pdb"), true);
217219
}
218220

219221
tempFilePathToInputOutputFilePairMap.Add(tempFilePath, inputOutpuFilePair);
220222

221223
allAssemblies.Add(new AssemblyInfo(tempFilePath, probingPaths));
222224
}
225+
catch (BadImageFormatException ex)
226+
{
227+
Log($" Unsupported assembly '{inputOutpuFilePair.InputFilePath}': {ex.Message}");
228+
}
223229
catch (Exception ex)
224230
{
225-
Log(ex.ToString());
231+
Log($" Failed to load assembly '{inputOutpuFilePair.InputFilePath}': {ex.Message}");
226232
}
227233
}
228234

235+
Log("2. Checking assembly references...");
236+
229237
try
230238
{
231239
// Start with assemblies that are not signed.
@@ -240,7 +248,7 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
240248

241249
foreach (var assembly in allAssemblies)
242250
{
243-
Log($"Checking assembly references in '{assembly.FilePath}'.");
251+
Log($" Checking assembly references in '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
244252

245253
foreach (var reference in assembly.Definition.MainModule.AssemblyReferences
246254
.Where(reference => set.Contains(reference.Name)))
@@ -250,10 +258,12 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
250258
}
251259
}
252260

261+
Log("3. Strong-name unsigned assemblies...");
262+
253263
// Strong-name sign all the unsigned assemblies.
254264
foreach (var assembly in assembliesToProcess)
255265
{
256-
Log($"Signing assembly '{assembly.FilePath}'.");
266+
Log($" Signing assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
257267

258268
var name = assembly.Definition.Name;
259269
name.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
@@ -262,6 +272,8 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
262272
name.Attributes |= AssemblyAttributes.PublicKey;
263273
}
264274

275+
Log("4. Fix InternalVisibleToAttribute references...");
276+
265277
// Fix InternalVisibleToAttribute.
266278
foreach (var assembly in allAssemblies)
267279
{
@@ -278,7 +290,7 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
278290

279291
if (signedAssembly != null)
280292
{
281-
Log($"Fixing {signedAssembly.Definition.Name.Name} friend reference in assembly '{assembly.FilePath}'.");
293+
Log($" Fixing {signedAssembly.Definition.Name.Name} friend reference in assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
282294

283295
var assemblyName = signedAssembly.Definition.Name.Name + ", PublicKey=" + BitConverter.ToString(signedAssembly.Definition.Name.PublicKey).Replace("-", string.Empty);
284296
var updatedArgument = new CustomAttributeArgument(argument.Type, assemblyName);
@@ -290,6 +302,8 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
290302
}
291303
}
292304

305+
Log("5. Fix BAML references...");
306+
293307
// Fix BAML references.
294308
foreach (var assembly in allAssemblies)
295309
{
@@ -358,7 +372,7 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
358372

359373
if (modifyResource)
360374
{
361-
Log($"Replacing BAML entry in assembly '{assembly.FilePath}'.");
375+
Log($" Replacing BAML entry in assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
362376

363377
resources.RemoveAt(resIndex);
364378
writer.Generate();
@@ -373,6 +387,8 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
373387
}
374388
}
375389

390+
Log("6. Save assembly changes...");
391+
376392
// Write all updated assemblies.
377393
foreach (var assembly in assembliesToProcess.Where(a => !a.Definition.Name.IsRetargetable))
378394
{
@@ -388,15 +404,15 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
388404
}
389405
}
390406

391-
Log($"Saving changes to assembly '{inputOutpuFilePair.OutputFilePath}'.");
407+
Log($" Saving changes to assembly '{inputOutpuFilePair.OutputFilePath}'.");
392408

393409
try
394410
{
395411
assembly.Save(inputOutpuFilePair.OutputFilePath, keyPair);
396412
}
397413
catch (NotSupportedException ex)
398414
{
399-
Log($"Failed to save assembly '{inputOutpuFilePair.OutputFilePath}': {ex.Message}");
415+
Log($" Failed to save assembly '{inputOutpuFilePair.OutputFilePath}': {ex.Message}");
400416

401417
if (inputOutpuFilePair.IsSameFile)
402418
{
@@ -419,6 +435,8 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
419435
}
420436
finally
421437
{
438+
Log("7. Cleanup...");
439+
422440
tempFilePathToInputOutputFilePairMap.Clear();
423441

424442
foreach (var assembly in allAssemblies)
@@ -432,7 +450,7 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
432450
}
433451
catch (Exception ex)
434452
{
435-
Log($"Failed to delete temp working directory '{tempPath}': {ex.Message}");
453+
Log($" Failed to delete temp working directory '{tempPath}': {ex.Message}");
436454
}
437455
}
438456
}

0 commit comments

Comments
 (0)