From 8782681111a68a31d2fc3729daf59db0d83874cb Mon Sep 17 00:00:00 2001 From: Santiago Squarzon Date: Fri, 10 Jan 2025 19:26:23 -0300 Subject: [PATCH] big fuckup --- docs/en-US/Get-ZipEntry.md | 2 +- .../Commands/ConvertFromGzipStringCommand.cs | 4 + .../Commands/ConvertToGzipStringCommand.cs | 4 + .../Commands/ExpandGzipArchiveCommand.cs | 4 + .../Commands/ExpandZipEntryCommand.cs | 8 +- .../Commands/GetZipEntryCommand.cs | 8 ++ .../Commands/GetZipEntryContentCommand.cs | 4 + .../Commands/NewZipEntryCommand.cs | 12 ++- .../Commands/RemoveZipEntryCommand.cs | 5 ++ .../Extensions/PathExtensions.cs | 77 +------------------ 10 files changed, 50 insertions(+), 78 deletions(-) diff --git a/docs/en-US/Get-ZipEntry.md b/docs/en-US/Get-ZipEntry.md index c373663..3990579 100644 --- a/docs/en-US/Get-ZipEntry.md +++ b/docs/en-US/Get-ZipEntry.md @@ -127,7 +127,7 @@ Archive 2/22/2024 1:19 PM 1.55 KB 5.35 KB Set-ZipEnt ```powershell PS ..\pwsh> $package = Invoke-WebRequest https://www.powershellgallery.com/api/v2/package/PSCompression -PS ..\pwsh> $package | Get-ZipEntry +PS ..\pwsh> $package | Get-ZipEntry | Select-Object -First 5 Directory: / diff --git a/src/PSCompression/Commands/ConvertFromGzipStringCommand.cs b/src/PSCompression/Commands/ConvertFromGzipStringCommand.cs index 4a9557f..2016913 100644 --- a/src/PSCompression/Commands/ConvertFromGzipStringCommand.cs +++ b/src/PSCompression/Commands/ConvertFromGzipStringCommand.cs @@ -45,6 +45,10 @@ protected override void ProcessRecord() WriteObject(reader.ReadLine()); } } + catch (Exception _) when (_ is PipelineStoppedException or FlowControlException) + { + throw; + } catch (Exception exception) { WriteError(exception.ToEnumerationError(line)); diff --git a/src/PSCompression/Commands/ConvertToGzipStringCommand.cs b/src/PSCompression/Commands/ConvertToGzipStringCommand.cs index 2a744a5..1d8d722 100644 --- a/src/PSCompression/Commands/ConvertToGzipStringCommand.cs +++ b/src/PSCompression/Commands/ConvertToGzipStringCommand.cs @@ -80,6 +80,10 @@ protected override void EndProcessing() WriteObject(Convert.ToBase64String(_outstream.ToArray())); } + catch (Exception _) when (_ is PipelineStoppedException or FlowControlException) + { + throw; + } catch (Exception exception) { WriteError(exception.ToWriteError(_outstream)); diff --git a/src/PSCompression/Commands/ExpandGzipArchiveCommand.cs b/src/PSCompression/Commands/ExpandGzipArchiveCommand.cs index 01dade8..e454822 100644 --- a/src/PSCompression/Commands/ExpandGzipArchiveCommand.cs +++ b/src/PSCompression/Commands/ExpandGzipArchiveCommand.cs @@ -150,6 +150,10 @@ protected override void ProcessRecord() encoding: Encoding, cmdlet: this); } + catch (Exception _) when (_ is PipelineStoppedException or FlowControlException) + { + throw; + } catch (Exception exception) { WriteError(exception.ToOpenError(path)); diff --git a/src/PSCompression/Commands/ExpandZipEntryCommand.cs b/src/PSCompression/Commands/ExpandZipEntryCommand.cs index fb9245e..e13e849 100644 --- a/src/PSCompression/Commands/ExpandZipEntryCommand.cs +++ b/src/PSCompression/Commands/ExpandZipEntryCommand.cs @@ -34,7 +34,9 @@ protected override void BeginProcessing() if (Destination.IsArchive()) { ThrowTerminatingError( - ExceptionHelper.NotDirectoryPath(Destination, nameof(Destination))); + ExceptionHelper.NotDirectoryPath( + Destination, + nameof(Destination))); } if (!Directory.Exists(Destination)) @@ -67,6 +69,10 @@ protected override void ProcessRecord() WriteObject(new DirectoryInfo(path)); } } + catch (Exception _) when (_ is PipelineStoppedException or FlowControlException) + { + throw; + } catch (Exception exception) { WriteError(exception.ToExtractEntryError(entry)); diff --git a/src/PSCompression/Commands/GetZipEntryCommand.cs b/src/PSCompression/Commands/GetZipEntryCommand.cs index 43502a4..aa9a536 100644 --- a/src/PSCompression/Commands/GetZipEntryCommand.cs +++ b/src/PSCompression/Commands/GetZipEntryCommand.cs @@ -86,6 +86,10 @@ ZipEntryBase CreateFromStream(ZipArchiveEntry entry, bool isDirectory) => WriteObject(entries, enumerateCollection: true); return; } + catch (Exception _) when (_ is PipelineStoppedException or FlowControlException) + { + throw; + } catch (InvalidDataException exception) { ThrowTerminatingError(exception.ToInvalidZipArchive()); @@ -122,6 +126,10 @@ ZipEntryBase CreateFromFile(ZipArchiveEntry entry, bool isDirectory) => WriteObject(entries, enumerateCollection: true); } + catch (Exception _) when (_ is PipelineStoppedException or FlowControlException) + { + throw; + } catch (InvalidDataException exception) { ThrowTerminatingError(exception.ToInvalidZipArchive()); diff --git a/src/PSCompression/Commands/GetZipEntryContentCommand.cs b/src/PSCompression/Commands/GetZipEntryContentCommand.cs index 7e66171..a6030ff 100644 --- a/src/PSCompression/Commands/GetZipEntryContentCommand.cs +++ b/src/PSCompression/Commands/GetZipEntryContentCommand.cs @@ -42,6 +42,10 @@ protected override void ProcessRecord() ZipContentReader reader = new(GetOrAdd(entry)); ReadEntry(entry, reader); } + catch (Exception _) when (_ is PipelineStoppedException or FlowControlException) + { + throw; + } catch (Exception exception) { WriteError(exception.ToOpenError(entry.Source)); diff --git a/src/PSCompression/Commands/NewZipEntryCommand.cs b/src/PSCompression/Commands/NewZipEntryCommand.cs index f88b1e1..0bfdd95 100644 --- a/src/PSCompression/Commands/NewZipEntryCommand.cs +++ b/src/PSCompression/Commands/NewZipEntryCommand.cs @@ -103,7 +103,9 @@ protected override void BeginProcessing() if (!SourcePath.IsArchive()) { ThrowTerminatingError( - ExceptionHelper.NotArchivePath(SourcePath, nameof(SourcePath))); + ExceptionHelper.NotArchivePath( + SourcePath, + nameof(SourcePath))); } using FileStream fileStream = File.Open( @@ -136,6 +138,10 @@ protected override void BeginProcessing() compressionLevel: CompressionLevel)); } } + catch (Exception _) when (_ is PipelineStoppedException or FlowControlException) + { + throw; + } catch (Exception exception) { ThrowTerminatingError(exception.ToOpenError(Destination)); @@ -185,6 +191,10 @@ protected override void EndProcessing() _zip?.Dispose(); WriteObject(GetResult(), enumerateCollection: true); } + catch (Exception _) when (_ is PipelineStoppedException or FlowControlException) + { + throw; + } catch (Exception exception) { ThrowTerminatingError(exception.ToOpenError(Destination)); diff --git a/src/PSCompression/Commands/RemoveZipEntryCommand.cs b/src/PSCompression/Commands/RemoveZipEntryCommand.cs index 5690685..f952b1d 100644 --- a/src/PSCompression/Commands/RemoveZipEntryCommand.cs +++ b/src/PSCompression/Commands/RemoveZipEntryCommand.cs @@ -6,6 +6,7 @@ namespace PSCompression.Commands; [Cmdlet(VerbsCommon.Remove, "ZipEntry", SupportsShouldProcess = true)] +[OutputType(typeof(void))] public sealed class RemoveZipEntryCommand : PSCmdlet, IDisposable { private readonly ZipArchiveCache _cache = new(ZipArchiveMode.Update); @@ -24,6 +25,10 @@ protected override void ProcessRecord() entry.Remove(_cache.GetOrAdd(entry)); } } + catch (Exception _) when (_ is PipelineStoppedException or FlowControlException) + { + throw; + } catch (NotSupportedException exception) { ThrowTerminatingError(exception.ToStreamOpenError(entry)); diff --git a/src/PSCompression/Extensions/PathExtensions.cs b/src/PSCompression/Extensions/PathExtensions.cs index 1525fd9..1809627 100644 --- a/src/PSCompression/Extensions/PathExtensions.cs +++ b/src/PSCompression/Extensions/PathExtensions.cs @@ -19,79 +19,6 @@ public static class PathExtensions private const string _directorySeparator = "/"; - // [ThreadStatic] - // private static List? s_normalizedPaths; - - // internal static string[] NormalizePath( - // this string[] paths, - // bool isLiteral, - // PSCmdlet cmdlet, - // bool throwOnInvalidProvider = false) - // { - // s_normalizedPaths ??= []; - // Collection resolvedPaths; - // ProviderInfo provider; - // s_normalizedPaths.Clear(); - - // foreach (string path in paths) - // { - // if (isLiteral) - // { - // string resolvedPath = cmdlet - // .SessionState.Path - // .GetUnresolvedProviderPathFromPSPath(path, out provider, out _); - - // if (!provider.IsFileSystem()) - // { - // if (throwOnInvalidProvider) - // { - // cmdlet.ThrowTerminatingError(provider.ToInvalidProviderError(path)); - // } - - // cmdlet.WriteError(provider.ToInvalidProviderError(path)); - // continue; - // } - - // s_normalizedPaths.Add(resolvedPath); - // continue; - // } - - // try - // { - // resolvedPaths = cmdlet.GetResolvedProviderPathFromPSPath(path, out provider); - - // foreach (string resolvedPath in resolvedPaths) - // { - // if (!provider.IsFileSystem()) - // { - // cmdlet.WriteError(provider.ToInvalidProviderError(resolvedPath)); - // continue; - // } - - // s_normalizedPaths.Add(resolvedPath); - // } - // } - // catch (Exception exception) - // { - - // cmdlet.WriteError(exception.ToResolvePathError(path)); - // } - // } - - // return [.. s_normalizedPaths]; - // } - - // internal static string NormalizePath( - // this string path, - // bool isLiteral, - // PSCmdlet cmdlet, - // bool throwOnInvalidProvider = false) => - // NormalizePath([path], isLiteral, cmdlet, throwOnInvalidProvider) - // .FirstOrDefault(); - - // internal static bool IsFileSystem(this ProviderInfo provider) => - // provider.ImplementingType == typeof(FileSystemProvider); - internal static string ResolvePath(this string path, PSCmdlet cmdlet) { string resolved = cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath( @@ -150,6 +77,6 @@ internal static bool IsDirectoryPath(this string path) => public static string NormalizePath(this string path) => s_reEntryDir.IsMatch(path) - ? NormalizeEntryPath(path) - : NormalizeFileEntryPath(path); + ? NormalizeEntryPath(path) + : NormalizeFileEntryPath(path); }