| 1 | using Microsoft.Win32.SafeHandles; |
| 2 | using Zipelo.Core; |
| 3 | |
| 4 | namespace Zipelo.Runtime; |
| 5 | |
| 6 | internal abstract class PendingFolderPublication : IDisposable |
| 7 | { |
| 8 | protected sealed class Node(string name, Node? parent, SafeFileHandle handle, bool directory, string identity) |
| 9 | { |
| 10 | internal string Name { get; } = name; |
| 11 | internal Node? Parent { get; } = parent; |
| 12 | internal SafeFileHandle Handle { get; set; } = handle; |
| 13 | internal bool Directory { get; } = directory; |
| 14 | internal string Identity { get; } = identity; |
| 15 | } |
| 16 | |
| 17 | protected readonly List<Node> Owned = []; |
| 18 | private readonly Dictionary<string, Node> folders = new(OutputPathPolicy.Comparer(OutputPathPolicy.CurrentPolicy)); |
| 19 | internal string TemporaryName { get; } = $".zipelo-{Guid.NewGuid():N}.tmp"; |
| 20 | internal PublicationNativeFailure? CleanupNativeFailure { get; set; } |
| 21 | protected Node Root => Owned[0]; |
| 22 | protected abstract SafeFileHandle Create(SafeFileHandle parent, string name, bool directory); |
| 23 | protected abstract string Inspect(SafeFileHandle handle, bool directory); |
| 24 | protected abstract void VerifyChildren(Node node, IReadOnlyCollection<string> names); |
| 25 | internal abstract void Initialize(); |
| 26 | internal abstract void Publish(string leaf, CancellationToken token); |
| 27 | internal abstract bool Abort(); |
| 28 | public abstract void Dispose(); |
| 29 | |
| 30 | protected void AddRoot(SafeFileHandle handle) |
| 31 | { |
| 32 | // 검사 실패도 호출자의 Abort가 생성 핸들을 정리하도록 먼저 기록함. |
| 33 | var root = new Node(TemporaryName, null, handle, true, ""); |
| 34 | Owned.Add(root); |
| 35 | var verified = new Node(TemporaryName, null, handle, true, Inspect(handle, true)); |
| 36 | Owned[0] = verified; |
| 37 | folders.Add("", verified); |
| 38 | } |
| 39 | |
| 40 | internal async Task WriteAsync(ZipExtractedEntry item, CancellationToken token) |
| 41 | { |
| 42 | var segments = OutputPathPolicy.Validate(item.Entry.Name, OutputPathPolicy.CurrentPolicy); |
| 43 | var parent = Root; |
| 44 | for (var index = 0; index < segments.Count; index++) |
| 45 | { |
| 46 | token.ThrowIfCancellationRequested(); |
| 47 | var path = string.Join('/', segments.Take(index + 1)); |
| 48 | var directory = index < segments.Count - 1 || item.Entry.Kind == EntryKind.Directory; |
| 49 | if (directory && folders.TryGetValue(path, out var existing)) { parent = existing; continue; } |
| 50 | var handle = Create(parent.Handle, segments[index], directory); |
| 51 | var node = new Node(segments[index], parent, handle, directory, ""); |
| 52 | Owned.Add(node); |
| 53 | node = new Node(node.Name, parent, handle, directory, Inspect(handle, directory)); |
| 54 | Owned[^1] = node; |
| 55 | if (directory) |
| 56 | { |
| 57 | folders.Add(path, node); |
| 58 | parent = node; |
| 59 | continue; |
| 60 | } |
| 61 | var buffer = new byte[65536]; |
| 62 | for (var offset = 0; offset < item.Content.Length; offset += buffer.Length) |
| 63 | { |
| 64 | token.ThrowIfCancellationRequested(); |
| 65 | var count = Math.Min(buffer.Length, item.Content.Length - offset); |
| 66 | await RandomAccess.WriteAsync(handle, item.Content.AsMemory(offset, count), offset, token).ConfigureAwait(false); |
| 67 | } |
| 68 | RandomAccess.FlushToDisk(handle); |
| 69 | if (RandomAccess.GetLength(handle) != item.Content.Length) { throw new ArchiveException(ArchiveError.IntegrityFailure); } |
| 70 | for (var offset = 0; offset < item.Content.Length;) |
| 71 | { |
| 72 | var count = await RandomAccess.ReadAsync(handle, buffer.AsMemory(0, |
| 73 | Math.Min(buffer.Length, item.Content.Length - offset)), offset, token).ConfigureAwait(false); |
| 74 | if (count == 0 || !buffer.AsSpan(0, count).SequenceEqual(item.Content.AsSpan(offset, count))) |
| 75 | { |
| 76 | throw new ArchiveException(ArchiveError.IntegrityFailure); |
| 77 | } |
| 78 | offset += count; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | internal void Verify(CancellationToken token) |
| 84 | { |
| 85 | foreach (var node in Owned) |
| 86 | { |
| 87 | token.ThrowIfCancellationRequested(); |
| 88 | if (Inspect(node.Handle, node.Directory) != node.Identity) { throw new ArchiveException(ArchiveError.UnsafePath); } |
| 89 | if (node.Directory) |
| 90 | { |
| 91 | VerifyChildren(node, Owned.Where(child => child.Parent == node).Select(child => child.Name).ToArray()); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | protected void DisposeOwned() |
| 97 | { |
| 98 | for (var index = Owned.Count - 1; index >= 0; index--) { Owned[index].Handle.Dispose(); } |
| 99 | } |
| 100 | } |