| 1 | using System.Runtime.InteropServices; |
| 2 | using Microsoft.Win32.SafeHandles; |
| 3 | using Zipelo.Core; |
| 4 | |
| 5 | namespace Zipelo.Runtime; |
| 6 | |
| 7 | internal sealed class LinuxPublication : PendingPublication |
| 8 | { |
| 9 | private readonly SafeFileHandle directory; |
| 10 | private readonly SafeFileHandle file; |
| 11 | |
| 12 | private LinuxPublication(SafeFileHandle directory, SafeFileHandle file) |
| 13 | { |
| 14 | this.directory = directory; |
| 15 | this.file = file; |
| 16 | } |
| 17 | |
| 18 | internal override SafeFileHandle Handle => file; |
| 19 | |
| 20 | internal static LinuxPublication Create(string parent) |
| 21 | { |
| 22 | // x64 syscall 번호를 사용함. 호출 전 공통 API에서 아키텍처를 제한함. |
| 23 | var how = new OpenHow { Flags = 0x80000 | 0x10000, Mode = 0, Resolve = 0x04 }; |
| 24 | var descriptor = OpenAt2(437, -100, parent, ref how, (nuint)Marshal.SizeOf<OpenHow>()); |
| 25 | var directory = OwnDescriptor(checked((int)descriptor)); |
| 26 | try |
| 27 | { |
| 28 | // O_TMPFILE | O_RDWR | O_CLOEXEC, 생성 권한은 0600 이하임. |
| 29 | var file = OwnDescriptor(OpenAt(directory, ".", 0x410000 | 2 | 0x80000, 0x180)); |
| 30 | return new(directory, file); |
| 31 | } |
| 32 | catch { directory.Dispose(); throw; } |
| 33 | } |
| 34 | |
| 35 | internal override void Publish(string leaf) |
| 36 | { |
| 37 | // 커널 procfs의 보유 fd를 연결하며 새 목적지 링크는 덮어쓰지 않음. |
| 38 | var descriptorPath = $"/proc/self/fd/{file.DangerousGetHandle().ToInt64()}"; |
| 39 | if (LinkAt(-100, descriptorPath, directory, leaf, 0x400) != 0) { ThrowLastError(); } |
| 40 | } |
| 41 | |
| 42 | // 이름 없는 임시 inode는 마지막 핸들이 닫힐 때 제거됨. |
| 43 | internal override bool Abort() => true; |
| 44 | |
| 45 | public override void Dispose() |
| 46 | { |
| 47 | file.Dispose(); |
| 48 | directory.Dispose(); |
| 49 | } |
| 50 | |
| 51 | private static SafeFileHandle OwnDescriptor(int descriptor) |
| 52 | { |
| 53 | if (descriptor < 0) { ThrowLastError(); } |
| 54 | if (descriptor < 3) |
| 55 | { |
| 56 | var duplicate = DuplicateDescriptor(descriptor, 1030, 3); |
| 57 | var error = Marshal.GetLastPInvokeError(); |
| 58 | Close(descriptor); |
| 59 | if (duplicate < 0) { throw Error(error); } |
| 60 | descriptor = duplicate; |
| 61 | } |
| 62 | return new SafeFileHandle((nint)descriptor, ownsHandle: true); |
| 63 | } |
| 64 | |
| 65 | private static void ThrowLastError() => throw Error(Marshal.GetLastPInvokeError()); |
| 66 | private static ArchiveException Error(int error) => new(error switch |
| 67 | { |
| 68 | 17 => ArchiveError.OutputConflict, |
| 69 | 40 => ArchiveError.UnsafePath, |
| 70 | _ => ArchiveError.OutputUnavailable |
| 71 | }); |
| 72 | |
| 73 | [StructLayout(LayoutKind.Sequential)] |
| 74 | private struct OpenHow |
| 75 | { |
| 76 | internal ulong Flags; |
| 77 | internal ulong Mode; |
| 78 | internal ulong Resolve; |
| 79 | } |
| 80 | |
| 81 | [DllImport("libc", EntryPoint = "syscall", SetLastError = true)] |
| 82 | private static extern long OpenAt2(long number, int directory, |
| 83 | [MarshalAs(UnmanagedType.LPUTF8Str)] string path, ref OpenHow how, nuint size); |
| 84 | |
| 85 | [DllImport("libc", EntryPoint = "openat", SetLastError = true)] |
| 86 | private static extern int OpenAt(SafeFileHandle directory, |
| 87 | [MarshalAs(UnmanagedType.LPUTF8Str)] string path, int flags, uint mode); |
| 88 | |
| 89 | [DllImport("libc", EntryPoint = "linkat", SetLastError = true)] |
| 90 | private static extern int LinkAt(int oldDirectory, [MarshalAs(UnmanagedType.LPUTF8Str)] string oldPath, |
| 91 | SafeFileHandle newDirectory, [MarshalAs(UnmanagedType.LPUTF8Str)] string newPath, int flags); |
| 92 | |
| 93 | [DllImport("libc", EntryPoint = "fcntl", SetLastError = true)] |
| 94 | private static extern int DuplicateDescriptor(int descriptor, int command, int minimum); |
| 95 | |
| 96 | [DllImport("libc", EntryPoint = "close")] |
| 97 | private static extern int Close(int descriptor); |
| 98 | } |