| 1 | using System.Security; |
| 2 | |
| 3 | namespace Zipelo.Core; |
| 4 | |
| 5 | public enum ArchiveAction { List, Extract, Create, Verify, Preview, Recover } |
| 6 | public enum JobState { Created, Running, WaitingForPassword, Cancelling, Completed, Partial, Cancelled, Failed } |
| 7 | public enum EntryKind { File, Directory, SymbolicLink, HardLink, Other } |
| 8 | public enum EntryOutcome { Listed, Staged, Extracted, Skipped, Failed } |
| 9 | public enum VerificationState { NotChecked, Passed, Failed, Unavailable } |
| 10 | public enum ArchiveError |
| 11 | { |
| 12 | None, |
| 13 | EngineUnavailable, |
| 14 | UnsupportedOperation, |
| 15 | UnsafePath, |
| 16 | UnsupportedEntry, |
| 17 | OutputUnavailable, |
| 18 | OutputConflict, |
| 19 | BudgetExceeded, |
| 20 | PasswordAttemptsExceeded, |
| 21 | InvalidPasswordResponse, |
| 22 | IntegrityFailure, |
| 23 | Cancelled, |
| 24 | EngineFailure |
| 25 | } |
| 26 | |
| 27 | public sealed class ArchiveException(ArchiveError code) : Exception(code.ToString()) |
| 28 | { |
| 29 | public ArchiveError Code { get; } = code; |
| 30 | } |
| 31 | |
| 32 | public sealed record EngineCapability(string Format, ArchiveAction Action); |
| 33 | public sealed record ArchiveEntry(string Id, string Name, EntryKind Kind, long? DeclaredSize); |
| 34 | public sealed record EntryResult(ArchiveEntry Entry, EntryOutcome Outcome, VerificationState Verification, ArchiveError Error) |
| 35 | { |
| 36 | public string EntryId => Entry.Id; |
| 37 | } |
| 38 | public sealed record JobEvent(Guid JobId, long Sequence, JobState State, long ProcessedBytes); |
| 39 | public sealed record PasswordChallenge(Guid JobId, Guid RequestId, int Attempt); |
| 40 | public sealed record JobResult(Guid JobId, JobState State, IReadOnlyList<EntryResult> Entries, ArchiveError Error); |
| 41 | |
| 42 | // 호출자는 반환된 암호의 소유권을 코어에 넘김. 코어가 소비 종료 시 폐기함. |
| 43 | // 제공자는 작업별로 한 번에 하나만 호출됨. 같은 작업의 암호 제공자를 재진입하지 않음. |
| 44 | public delegate ValueTask<SecureString?> PasswordProvider(PasswordChallenge challenge, CancellationToken cancellationToken); |
| 45 | |
| 46 | public interface IArchiveEngine |
| 47 | { |
| 48 | IReadOnlyCollection<EngineCapability> Capabilities { get; } |
| 49 | |
| 50 | // 원본 경로나 출력 경로를 받지 않고 제한된 스트림과 작업 문맥만 사용함. |
| 51 | // 시작한 문맥 작업을 모두 await하고 실패를 숨기지 않아야 함. 입력 스트림은 폐기하지 않음. |
| 52 | Task ExecuteAsync(ArchiveAction action, IReadOnlyList<Stream> inputs, EngineContext context, CancellationToken cancellationToken); |
| 53 | } |
| 54 | |
| 55 | public sealed class UnavailableEngine : IArchiveEngine |
| 56 | { |
| 57 | public IReadOnlyCollection<EngineCapability> Capabilities => Array.Empty<EngineCapability>(); |
| 58 | |
| 59 | public Task ExecuteAsync(ArchiveAction action, IReadOnlyList<Stream> inputs, EngineContext context, CancellationToken cancellationToken) |
| 60 | => Task.FromException(new ArchiveException(ArchiveError.EngineUnavailable)); |
| 61 | } |
| 62 | |
| 63 | // 저장 계층은 링크와 junction, 경로 교체 경쟁, 기존 파일 충돌을 실제 핸들 기준으로 차단해야 함. |
| 64 | // 원본과 기존 파일은 수정하지 않으며 작업 소유 임시 출력만 폐기해야 함. |
| 65 | // CommitAsync는 전체 게시 성공에만 정상 반환함. 실패나 취소 시 이미 게시한 작업 소유 출력도 원복해야 함. |
| 66 | // 게시 성공 뒤에는 늦은 취소로 예외를 던지지 않음. 부분 게시만 가능한 저장 계층은 이 계약에 연결하지 않음. |
| 67 | // DisposeAsync는 성공한 게시를 유지하고 미확정 출력을 정리함. 원복이나 정리 실패를 숨기지 않음. |
| 68 | public interface IOutputTransaction : IAsyncDisposable |
| 69 | { |
| 70 | ValueTask<Stream> CreateFileAsync(IReadOnlyList<string> relativeSegments, CancellationToken cancellationToken); |
| 71 | ValueTask CommitAsync(CancellationToken cancellationToken); |
| 72 | } |
| 73 | |
| 74 | public sealed class UnavailableOutput : IOutputTransaction |
| 75 | { |
| 76 | public ValueTask<Stream> CreateFileAsync(IReadOnlyList<string> relativeSegments, CancellationToken cancellationToken) |
| 77 | => ValueTask.FromException<Stream>(new ArchiveException(ArchiveError.OutputUnavailable)); |
| 78 | |
| 79 | public ValueTask CommitAsync(CancellationToken cancellationToken) |
| 80 | => ValueTask.FromException(new ArchiveException(ArchiveError.OutputUnavailable)); |
| 81 | |
| 82 | public ValueTask DisposeAsync() => ValueTask.CompletedTask; |
| 83 | } |