Skip to content

Commit 987d1a7

Browse files
authored
[release/10.0] Enhance logging for file extraction and repackaging with matching permissions (#16271)
1 parent c982c6f commit 987d1a7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/Microsoft.DotNet.SignTool/src/Configuration.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,11 +830,12 @@ private bool TryBuildZipData(FileSignInfo zipFileSignInfo, out ZipData zipData,
830830

831831
// if we already encountered file that has the same content we can reuse its signed version when repackaging the container.
832832
var fileName = Path.GetFileName(entry.RelativePath);
833+
string entryPerms = entry.UnixFileMode.HasValue ? Convert.ToString((uint)entry.UnixFileMode.Value, 8) : "default perms";
833834
if (!_filesByContentKey.TryGetValue(fileUniqueKey, out var fileSignInfo))
834835
{
835836
string extractPathRoot = _useHashInExtractionPath ? fileUniqueKey.StringHash : _filesByContentKey.Count().ToString();
836837
string tempPath = Path.Combine(_pathToContainerUnpackingDirectory, extractPathRoot, entry.RelativePath);
837-
_log.LogMessage($"Extracting file '{fileName}' from '{archivePath}' to '{tempPath}'.");
838+
_log.LogMessage($"Extracting file '{fileName}' from '{archivePath}' to '{tempPath}'. (Caching with perms: {entryPerms})");
838839

839840
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
840841

@@ -845,6 +846,10 @@ private bool TryBuildZipData(FileSignInfo zipFileSignInfo, out ZipData zipData,
845846
PathWithHash nestedFile = new PathWithHash(tempPath, entry.ContentHash);
846847
fileSignInfo = TrackFile(nestedFile, zipFileSignInfo.File, collisionPriorityId);
847848
}
849+
else
850+
{
851+
_log.LogMessage($"Reusing previously extracted file '{fileName}' for '{archivePath}'. (Archival perms: {entryPerms})");
852+
}
848853

849854
if (fileSignInfo.ShouldTrack)
850855
{

src/Microsoft.DotNet.SignTool/src/ZipData.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,15 @@ private static IEnumerable<ZipDataEntry> ReadPkgOrAppBundleEntries(
376376

377377
private void RepackPkgOrAppBundles(TaskLoggingHelper log, string tempDir, string dotNetPathTooling, string pkgToolPath)
378378
{
379+
#if NET472
380+
throw new NotImplementedException("PKG manipulation is not supported on .NET Framework");
381+
#else
382+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
383+
{
384+
log.LogError("Pkg/AppBundle repackaging is not supported on Windows.");
385+
return;
386+
}
387+
379388
string extractDir = Path.Combine(tempDir, Guid.NewGuid().ToString());
380389
try
381390
{
@@ -395,8 +404,12 @@ private void RepackPkgOrAppBundles(TaskLoggingHelper log, string tempDir, string
395404
continue;
396405
}
397406

398-
log.LogMessage(MessageImportance.Low, $"Copying signed stream from {signedPart.Value.FileSignInfo.FullPath} to {FileSignInfo.FullPath} -> {relativePath}.");
407+
// Preserve the original file mode from the PKG/App. The sign cache might bring if from an entry in an archive with different perms.
408+
UnixFileMode extractedFileMode = File.GetUnixFileMode(path);
409+
410+
log.LogMessage(MessageImportance.Low, $"Copying signed stream from {signedPart.Value.FileSignInfo.FullPath} to {FileSignInfo.FullPath} -> {relativePath} (perms: {Convert.ToString((uint)extractedFileMode, 8)}).");
399411
File.Copy(signedPart.Value.FileSignInfo.FullPath, path, overwrite: true);
412+
File.SetUnixFileMode(path, extractedFileMode);
400413
}
401414

402415
if (!RunPkgProcess(srcPath: extractDir, dstPath: FileSignInfo.FullPath, "pack", dotNetPathTooling, pkgToolPath))
@@ -411,6 +424,7 @@ private void RepackPkgOrAppBundles(TaskLoggingHelper log, string tempDir, string
411424
Directory.Delete(extractDir, recursive: true);
412425
}
413426
}
427+
#endif
414428
}
415429

416430
#if NETFRAMEWORK
@@ -515,8 +529,7 @@ private void RepackTarGZip(TaskLoggingHelper log, string tempDir, string dotNetP
515529
entry.DataStream = signedStream;
516530
entry.DataStream.Position = 0;
517531
writer.WriteEntry(entry);
518-
519-
log.LogMessage(MessageImportance.Low, $"Copying signed stream from {signedPart.Value.FileSignInfo.FullPath} to {FileSignInfo.FullPath} -> {relativeName}.");
532+
log.LogMessage(MessageImportance.Low, $"Copying signed stream from {signedPart.Value.FileSignInfo.FullPath} to {FileSignInfo.FullPath} -> {relativeName} (perms: {Convert.ToString((uint)entry.Mode, 8)}).");
520533
continue;
521534
}
522535

0 commit comments

Comments
 (0)