From f4bba267aefc2eb8dbd68a905ad072af9f849bfd Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Thu, 5 Oct 2023 22:54:37 -0300 Subject: [PATCH] Enable `SignBinary` with `PathTooLong` path. --- CHANGELOG.md | 7 ++ ricaun.Nuke/Extensions/PathTooLongUtils.cs | 80 ++++++++++++++++++++++ ricaun.Nuke/Extensions/SignExtension.cs | 61 +++++++++-------- ricaun.Nuke/ricaun.Nuke.csproj | 2 +- 4 files changed, 120 insertions(+), 30 deletions(-) create mode 100644 ricaun.Nuke/Extensions/PathTooLongUtils.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a3655e..db7b0ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [1.7.2] / 2023-10-05 +### Features +- Enable `SignBinary` with `PathTooLong` path. +### Updated +- Create `PathTooLongUtils` to prevent `PathTooLongException` + ## [1.7.1] / 2023-10-05 ### Features - Prerelease Nuget force to unlist. @@ -319,6 +325,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - First Release [vNext]: ../../compare/1.0.0...HEAD +[1.7.2]: ../../compare/1.7.1...1.7.2 [1.7.1]: ../../compare/1.7.0...1.7.1 [1.7.0]: ../../compare/1.6.1...1.7.0 [1.6.1]: ../../compare/1.6.0...1.6.1 diff --git a/ricaun.Nuke/Extensions/PathTooLongUtils.cs b/ricaun.Nuke/Extensions/PathTooLongUtils.cs new file mode 100644 index 0000000..7942c76 --- /dev/null +++ b/ricaun.Nuke/Extensions/PathTooLongUtils.cs @@ -0,0 +1,80 @@ +using System; +using System.IO; + +namespace ricaun.Nuke.Extensions +{ + /// + /// PathTooLongUtils + /// + public class PathTooLongUtils + { + /// + /// MAX_PATH + /// + public const int MAX_PATH = 260; + /// + /// FileMoveToTempUtils + /// + public class FileMoveToTemp : IDisposable + { + private readonly string filePath; + private string tempFilePath; + /// + /// FileMoveToTempUtils + /// + /// + public FileMoveToTemp(string filePath) + { + this.filePath = filePath; + Initialize(); + } + + private void Initialize() + { + if (IsPathTooLong()) + { + tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(this.filePath)); + File.Move(this.filePath, tempFilePath, true); + } + } + + /// + /// GetFilePathLong + /// + /// + public int GetFilePathLong() + { + return GetFilePath().Length; + } + + /// + /// Check if path is too long + /// + /// + public bool IsPathTooLong() + { + return GetFilePathLong() > MAX_PATH; + } + + /// + /// GetFilePath + /// + /// + public string GetFilePath() + { + return tempFilePath ?? filePath; + } + + /// + /// Dispose + /// + public void Dispose() + { + if (tempFilePath is null) + return; + + File.Move(tempFilePath, filePath, true); + } + } + } +} diff --git a/ricaun.Nuke/Extensions/SignExtension.cs b/ricaun.Nuke/Extensions/SignExtension.cs index 73ec1cc..b6e2221 100644 --- a/ricaun.Nuke/Extensions/SignExtension.cs +++ b/ricaun.Nuke/Extensions/SignExtension.cs @@ -71,7 +71,6 @@ public static bool CreateCerFile(string fileNamePfx, string passwordPfx, string if (File.Exists(cert)) return true; return CreateCertificatesCer(fileNamePfx, passwordPfx, cert); } - /// /// https://github.com/DataDog/dd-trace-dotnet/blob/master/tracer/build/_build/Build.Gitlab.cs /// @@ -80,44 +79,48 @@ public static bool CreateCerFile(string fileNamePfx, string passwordPfx, string /// public static void SignBinary(string certPath, string certPassword, string binaryPath) { - if (binaryPath.Length > 260) + using (var utils = new PathTooLongUtils.FileMoveToTemp(binaryPath)) { - var messageError = $"FilePath.Length to big: {binaryPath}"; - Serilog.Log.Error(messageError); - throw new PathTooLongException(messageError); - } + var filePath = utils.GetFilePath(); + if (utils.IsPathTooLong()) + { + var messageError = $"FilePath.Length too long: {filePath}"; + Serilog.Log.Error(messageError); + throw new PathTooLongException(messageError); + } - if (HasSignature(binaryPath)) return; + if (HasSignature(filePath)) return; - Serilog.Log.Information($"Signing: {binaryPath}"); + Serilog.Log.Information($"Signing [{utils.GetFilePathLong()}]: {filePath}"); - if (!SignToolTasks.SignToolPath.SkipEmpty()) - { - Serilog.Log.Error($"SignToolPath is not found, set SIGNTOOL_EXE enviroment variable path... {SignToolTasks.SignToolPath}"); - return; - } - - foreach (var timestampServer in timestampServers) - { - try + if (!SignToolTasks.SignToolPath.SkipEmpty()) { - SignToolTasks.SignTool(x => x - .SetFiles(binaryPath) - .SetFile(certPath) - .SetPassword(certPassword) - .SetTimestampServerUrl(timestampServer) - .SetFileDigestAlgorithm(SignToolDigestAlgorithm.SHA256) - .EnableQuiet() - ); - Serilog.Log.Information($"Signing done with {timestampServer}"); + Serilog.Log.Error($"SignToolPath is not found, set SIGNTOOL_EXE enviroment variable path... {SignToolTasks.SignToolPath}"); return; } - catch (Exception) + + foreach (var timestampServer in timestampServers) { - Serilog.Log.Warning($"Failed to sign file with {timestampServer}"); + try + { + SignToolTasks.SignTool(x => x + .SetFiles(filePath) + .SetFile(certPath) + .SetPassword(certPassword) + .SetTimestampServerUrl(timestampServer) + .SetFileDigestAlgorithm(SignToolDigestAlgorithm.SHA256) + .EnableQuiet() + ); + Serilog.Log.Information($"Signing done with {timestampServer}"); + return; + } + catch (Exception) + { + Serilog.Log.Warning($"Failed to sign file with {timestampServer}"); + } } + Serilog.Log.Error($"Failed to sign file {filePath}"); } - Serilog.Log.Error($"Failed to sign file {binaryPath}"); } /// diff --git a/ricaun.Nuke/ricaun.Nuke.csproj b/ricaun.Nuke/ricaun.Nuke.csproj index 3188264..c14f1ed 100644 --- a/ricaun.Nuke/ricaun.Nuke.csproj +++ b/ricaun.Nuke/ricaun.Nuke.csproj @@ -7,7 +7,7 @@ ricaun.Nuke - 1.7.1 + 1.7.2-alpha