|
1 | | -import { dlopen, ptr } from "bun:ffi"; |
| 1 | +import { dlopen, ptr, read } from "bun:ffi"; |
2 | 2 |
|
3 | 3 | const AT_FDCWD = -100; |
4 | 4 | const RENAME_EXCHANGE = 2; |
5 | 5 | const RENAME_SWAP = 0x00000002; |
6 | 6 |
|
| 7 | +type ReplaceOperation = "renameat2" | "renamex_np" | "ReplaceFileW" | "unsupported"; |
| 8 | +type NativeExchangeResult = |
| 9 | + | { ok: true } |
| 10 | + | { ok: false; nativeCode?: number; cause?: unknown }; |
| 11 | + |
| 12 | +type FailedReplacementDetails = { |
| 13 | + operation: ReplaceOperation; |
| 14 | + sourcePath: string; |
| 15 | + targetPath: string; |
| 16 | + backupPath: string; |
| 17 | + result: Extract<NativeExchangeResult, { ok: false }>; |
| 18 | +}; |
| 19 | + |
7 | 20 | export class PreservingReplaceError extends Error { |
8 | | - constructor() { |
9 | | - super("Native credential publication could not complete."); |
| 21 | + readonly operation: ReplaceOperation; |
| 22 | + readonly sourcePath: string; |
| 23 | + readonly targetPath: string; |
| 24 | + readonly backupPath: string; |
| 25 | + readonly platform: NodeJS.Platform; |
| 26 | + readonly nativeCode?: number; |
| 27 | + |
| 28 | + constructor(details: { |
| 29 | + operation: ReplaceOperation; |
| 30 | + sourcePath: string; |
| 31 | + targetPath: string; |
| 32 | + backupPath: string; |
| 33 | + platform: NodeJS.Platform; |
| 34 | + nativeCode?: number; |
| 35 | + cause?: unknown; |
| 36 | + }) { |
| 37 | + super("Native credential publication could not complete.", details.cause === undefined ? undefined : { cause: details.cause }); |
10 | 38 | this.name = "PreservingReplaceError"; |
| 39 | + this.operation = details.operation; |
| 40 | + this.sourcePath = details.sourcePath; |
| 41 | + this.targetPath = details.targetPath; |
| 42 | + this.backupPath = details.backupPath; |
| 43 | + this.platform = details.platform; |
| 44 | + this.nativeCode = details.nativeCode; |
11 | 45 | } |
12 | 46 | } |
13 | 47 |
|
14 | | -type Exchange = (source: string, target: string) => boolean; |
15 | | - |
16 | 48 | function cString(value: string): Buffer { |
17 | 49 | return Buffer.from(`${value}\0`); |
18 | 50 | } |
19 | 51 |
|
20 | | -function unixExchange(symbol: "renameat2" | "renamex_np"): Exchange | null { |
| 52 | +function unixExchange(symbol: "renameat2" | "renamex_np"): (source: string, target: string) => NativeExchangeResult { |
21 | 53 | try { |
| 54 | + const errnoSymbol = process.platform === "darwin" ? "__error" : "__errno_location"; |
22 | 55 | const library = dlopen(process.platform === "darwin" ? "/usr/lib/libSystem.B.dylib" : "libc.so.6", { |
23 | 56 | [symbol]: { |
24 | 57 | args: symbol === "renameat2" ? ["i32", "cstring", "i32", "cstring", "u32"] : ["cstring", "cstring", "u32"], |
25 | 58 | returns: "i32", |
26 | 59 | }, |
| 60 | + [errnoSymbol]: { args: [], returns: "ptr" }, |
27 | 61 | }); |
28 | 62 | const call = library.symbols[symbol] as (...args: unknown[]) => number; |
29 | | - return (source, target) => symbol === "renameat2" |
30 | | - ? call(AT_FDCWD, cString(source), AT_FDCWD, cString(target), RENAME_EXCHANGE) === 0 |
31 | | - : call(cString(source), cString(target), RENAME_SWAP) === 0; |
32 | | - } catch { |
33 | | - return null; |
| 63 | + const errnoLocation = library.symbols[errnoSymbol] as () => number; |
| 64 | + return (source, target) => { |
| 65 | + const status = symbol === "renameat2" |
| 66 | + ? call(AT_FDCWD, cString(source), AT_FDCWD, cString(target), RENAME_EXCHANGE) |
| 67 | + : call(cString(source), cString(target), RENAME_SWAP); |
| 68 | + if (status === 0) return { ok: true }; |
| 69 | + return { ok: false, nativeCode: read.i32(errnoLocation()) }; |
| 70 | + }; |
| 71 | + } catch (cause) { |
| 72 | + return () => ({ ok: false, cause }); |
34 | 73 | } |
35 | 74 | } |
36 | 75 |
|
37 | 76 | function wide(value: string): Buffer { |
38 | 77 | return Buffer.from(`${value}\0`, "utf16le"); |
39 | 78 | } |
40 | 79 |
|
41 | | -function windowsExchange(source: string, target: string, backup: string): boolean { |
| 80 | +function windowsExchange(source: string, target: string, backup: string): NativeExchangeResult { |
42 | 81 | try { |
43 | 82 | const library = dlopen("kernel32.dll", { |
44 | 83 | ReplaceFileW: { args: ["ptr", "ptr", "ptr", "u32", "ptr", "ptr"], returns: "i32" }, |
| 84 | + GetLastError: { args: [], returns: "u32" }, |
45 | 85 | }); |
46 | 86 | const replacement = wide(source); |
47 | 87 | const replaced = wide(target); |
48 | 88 | const privateBackup = wide(backup); |
49 | | - return (library.symbols.ReplaceFileW as (...args: unknown[]) => number)( |
| 89 | + const status = (library.symbols.ReplaceFileW as (...args: unknown[]) => number)( |
50 | 90 | ptr(replaced), ptr(replacement), ptr(privateBackup), 0, null, null, |
51 | | - ) !== 0; |
52 | | - } catch { |
53 | | - return false; |
| 91 | + ); |
| 92 | + if (status !== 0) return { ok: true }; |
| 93 | + return { ok: false, nativeCode: (library.symbols.GetLastError as () => number)() }; |
| 94 | + } catch (cause) { |
| 95 | + return { ok: false, cause }; |
54 | 96 | } |
55 | 97 | } |
56 | 98 |
|
| 99 | +function failedReplacement(details: FailedReplacementDetails): PreservingReplaceError { |
| 100 | + return new PreservingReplaceError({ |
| 101 | + operation: details.operation, |
| 102 | + sourcePath: details.sourcePath, |
| 103 | + targetPath: details.targetPath, |
| 104 | + backupPath: details.backupPath, |
| 105 | + platform: process.platform, |
| 106 | + ...(details.result.nativeCode === undefined ? {} : { nativeCode: details.result.nativeCode }), |
| 107 | + ...(details.result.cause === undefined ? {} : { cause: details.result.cause }), |
| 108 | + }); |
| 109 | +} |
| 110 | + |
57 | 111 | /** |
58 | 112 | * Exchange a staged file with an existing canonical file without a missing-target window. |
59 | 113 | * On Unix the displaced entry remains at `source`; Windows places it at `backup`. |
60 | 114 | */ |
61 | 115 | export function replaceFilePreservingTarget(source: string, target: string, backup: string): void { |
62 | 116 | if (process.platform === "linux") { |
63 | | - const exchange = unixExchange("renameat2"); |
64 | | - if (exchange?.(source, target)) return; |
65 | | - throw new PreservingReplaceError(); |
| 117 | + const result = unixExchange("renameat2")(source, target); |
| 118 | + if (result.ok) return; |
| 119 | + throw failedReplacement({ operation: "renameat2", sourcePath: source, targetPath: target, backupPath: backup, result }); |
66 | 120 | } |
67 | 121 | if (process.platform === "darwin") { |
68 | | - const exchange = unixExchange("renamex_np"); |
69 | | - if (exchange?.(source, target)) return; |
70 | | - throw new PreservingReplaceError(); |
| 122 | + const result = unixExchange("renamex_np")(source, target); |
| 123 | + if (result.ok) return; |
| 124 | + throw failedReplacement({ operation: "renamex_np", sourcePath: source, targetPath: target, backupPath: backup, result }); |
| 125 | + } |
| 126 | + if (process.platform === "win32") { |
| 127 | + const result = windowsExchange(source, target, backup); |
| 128 | + if (result.ok) return; |
| 129 | + throw failedReplacement({ operation: "ReplaceFileW", sourcePath: source, targetPath: target, backupPath: backup, result }); |
71 | 130 | } |
72 | | - if (process.platform === "win32" && windowsExchange(source, target, backup)) return; |
73 | 131 | // No rename fallback is safe: it can make auth.json absent between operations. |
74 | | - throw new PreservingReplaceError(); |
| 132 | + throw failedReplacement({ operation: "unsupported", sourcePath: source, targetPath: target, backupPath: backup, result: { ok: false } }); |
75 | 133 | } |
76 | 134 |
|
77 | 135 | /** Restore a verified displaced entry while preserving the canonical target. */ |
|
0 commit comments