| 1 | using Microsoft.Win32.SafeHandles; |
| 2 | using Zipelo.Core; |
| 3 | |
| 4 | namespace Zipelo.Runtime; |
| 5 | |
| 6 | internal sealed class WindowsFolderPublication(string parent) : PendingFolderPublication |
| 7 | { |
| 8 | private readonly List<SafeFileHandle> ancestors = WindowsPublication.OpenParent(parent, readSecurity: true); |
| 9 | |
| 10 | internal override void Initialize() |
| 11 | { |
| 12 | WindowsFolderGuard.RequireNtfs(ancestors[^1]); |
| 13 | WindowsFolderGuard.RequirePrivateWriters(ancestors[^1]); |
| 14 | AddRoot(Create(ancestors[^1], TemporaryName, true)); |
| 15 | } |
| 16 | |
| 17 | protected override SafeFileHandle Create(SafeFileHandle directory, string name, bool folder) |
| 18 | => WindowsPublication.OpenRelative(directory, name, create: true, directory: folder, read: !folder); |
| 19 | |
| 20 | protected override string Inspect(SafeFileHandle handle, bool directory) |
| 21 | => WindowsFolderGuard.Inspect(handle, directory); |
| 22 | |
| 23 | protected override void VerifyChildren(Node node, IReadOnlyCollection<string> names) |
| 24 | => WindowsFolderGuard.VerifyChildren(node.Handle, names); |
| 25 | |
| 26 | internal override void Publish(string leaf, CancellationToken token) |
| 27 | { |
| 28 | // NTFS는 열린 자식 파일을 가진 디렉터리 rename을 허용하지 않음. |
| 29 | // ACL 검증으로 다른 계정의 쓰기를 배제한 트리에서만 자식 핸들을 닫음. |
| 30 | for (var index = Owned.Count - 1; index > 0; index--) |
| 31 | { |
| 32 | token.ThrowIfCancellationRequested(); |
| 33 | Owned[index].Handle.Dispose(); |
| 34 | } |
| 35 | token.ThrowIfCancellationRequested(); |
| 36 | WindowsPublication.Rename(Root.Handle, ancestors[^1], leaf); |
| 37 | } |
| 38 | |
| 39 | private SafeFileHandle Reopen(Node node) |
| 40 | { |
| 41 | if (!node.Handle.IsClosed) { return node.Handle; } |
| 42 | if (node.Parent is null) { throw new ArchiveException(ArchiveError.UnsafePath); } |
| 43 | var handle = WindowsPublication.OpenRelative(Reopen(node.Parent), node.Name, |
| 44 | create: false, directory: node.Directory, reopenOwned: true); |
| 45 | try |
| 46 | { |
| 47 | if (Inspect(handle, node.Directory) != node.Identity) { throw new ArchiveException(ArchiveError.UnsafePath); } |
| 48 | node.Handle = handle; |
| 49 | return handle; |
| 50 | } |
| 51 | catch { handle.Dispose(); throw; } |
| 52 | } |
| 53 | |
| 54 | internal override bool Abort() |
| 55 | { |
| 56 | var success = true; |
| 57 | // 삭제 대상은 소유 핸들 또는 같은 파일 ID로 재확인한 핸들뿐임. |
| 58 | for (var index = Owned.Count - 1; index >= 0; index--) |
| 59 | { |
| 60 | try |
| 61 | { |
| 62 | if (!WindowsPublication.DeleteOwned(Reopen(Owned[index]), out var failure)) |
| 63 | { |
| 64 | success = false; |
| 65 | CleanupNativeFailure ??= failure; |
| 66 | } |
| 67 | } |
| 68 | catch (PublicationNativeException failure) { success = false; CleanupNativeFailure ??= failure.Failure; } |
| 69 | catch { success = false; } |
| 70 | finally { Owned[index].Handle.Dispose(); } |
| 71 | } |
| 72 | return success; |
| 73 | } |
| 74 | |
| 75 | public override void Dispose() |
| 76 | { |
| 77 | DisposeOwned(); |
| 78 | for (var index = ancestors.Count - 1; index >= 0; index--) { ancestors[index].Dispose(); } |
| 79 | } |
| 80 | } |