| 1 | using System.Runtime.InteropServices; |
| 2 | using Zipelo.Core; |
| 3 | |
| 4 | namespace Zipelo.Runtime; |
| 5 | |
| 6 | public static class NewFolderPublisher |
| 7 | { |
| 8 | // 구현 진입 가능 여부임. 일반 UI 활성화나 OS 검증 완료를 뜻하지 않음. |
| 9 | public static bool IsSupportedPlatform => RuntimeInformation.ProcessArchitecture == Architecture.X64 && |
| 10 | (OperatingSystem.IsWindows() || OperatingSystem.IsLinux()); |
| 11 | |
| 12 | public static async Task<ZipPublicationResult> PublishAsync(ZipWorkerResult result, |
| 13 | string destinationPath, CancellationToken token) |
| 14 | { |
| 15 | PendingFolderPublication? pending = null; |
| 16 | var published = false; |
| 17 | var error = ArchiveError.None; |
| 18 | var cleanupFailed = false; |
| 19 | int? posixError = null; |
| 20 | string? temporaryName = null; |
| 21 | PublicationNativeFailure? nativeFailure = null; |
| 22 | PublicationNativeFailure? cleanupNativeFailure = null; |
| 23 | try |
| 24 | { |
| 25 | token.ThrowIfCancellationRequested(); |
| 26 | if (!IsSupportedPlatform) { throw new ArchiveException(ArchiveError.OutputUnavailable); } |
| 27 | var entries = ZipExtraction.ValidateResult(result, token, snapshot: true); |
| 28 | if (!Path.IsPathFullyQualified(destinationPath)) { throw new ArchiveException(ArchiveError.UnsafePath); } |
| 29 | var fullPath = Path.GetFullPath(destinationPath); |
| 30 | if (OperatingSystem.IsLinux()) { _ = OutputPathPolicy.Validate(fullPath[1..], FileNamePolicy.Linux); } |
| 31 | var parent = Path.GetDirectoryName(fullPath); |
| 32 | var leaf = Path.GetFileName(fullPath); |
| 33 | if (parent is null || OutputPathPolicy.Validate(leaf, OutputPathPolicy.CurrentPolicy).Count != 1) |
| 34 | { |
| 35 | throw new ArchiveException(ArchiveError.UnsafePath); |
| 36 | } |
| 37 | pending = OperatingSystem.IsWindows() ? new WindowsFolderPublication(parent) : new LinuxFolderPublication(parent); |
| 38 | pending.Initialize(); |
| 39 | foreach (var entry in entries) { await pending.WriteAsync(entry, token).ConfigureAwait(false); } |
| 40 | pending.Verify(token); |
| 41 | token.ThrowIfCancellationRequested(); |
| 42 | pending.Publish(leaf, token); |
| 43 | published = true; |
| 44 | } |
| 45 | catch (OperationCanceledException) when (token.IsCancellationRequested) { error = ArchiveError.Cancelled; } |
| 46 | catch (PublicationNativeException failure) { error = failure.Code; nativeFailure = failure.Failure; } |
| 47 | catch (PosixPublicationException failure) { error = failure.Code; posixError = failure.NativeError; } |
| 48 | catch (ArchiveException failure) { error = failure.Code; } |
| 49 | catch (ArgumentException) { error = ArchiveError.UnsafePath; } |
| 50 | catch { error = ArchiveError.OutputUnavailable; } |
| 51 | finally |
| 52 | { |
| 53 | if (pending is not null) |
| 54 | { |
| 55 | if (!published) |
| 56 | { |
| 57 | try { cleanupFailed = !pending.Abort(); } |
| 58 | catch { cleanupFailed = true; } |
| 59 | if (cleanupFailed) |
| 60 | { |
| 61 | temporaryName = pending.TemporaryName; |
| 62 | cleanupNativeFailure = pending.CleanupNativeFailure; |
| 63 | } |
| 64 | } |
| 65 | pending.Dispose(); |
| 66 | } |
| 67 | } |
| 68 | return new(published, error, cleanupFailed, temporaryName) |
| 69 | { |
| 70 | NativeFailure = nativeFailure, |
| 71 | CleanupNativeFailure = cleanupNativeFailure, |
| 72 | PosixError = posixError, |
| 73 | CleanupPosixError = (pending as LinuxFolderPublication)?.CleanupError |
| 74 | }; |
| 75 | } |
| 76 | } |