From 0592ce8f84943c3dc8028dba8ea70c26a31686b6 Mon Sep 17 00:00:00 2001 From: Bernd Schoolmann Date: Fri, 30 May 2025 16:28:31 +0200 Subject: [PATCH] Add linkOrCopy --- .../desktop/src/main/native-messaging.main.ts | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/apps/desktop/src/main/native-messaging.main.ts b/apps/desktop/src/main/native-messaging.main.ts index 240be749459..1372f6b199f 100644 --- a/apps/desktop/src/main/native-messaging.main.ts +++ b/apps/desktop/src/main/native-messaging.main.ts @@ -212,10 +212,7 @@ export class NativeMessagingMain { const browserBinaryPath = path.join(nhmsPath, ".bitwarden_desktop_proxy"); await fs.mkdir(nhmsPath, { recursive: true }); - if (existsSync(browserBinaryPath)) { - await fs.unlink(browserBinaryPath); - } - await fs.link(binaryPath, browserBinaryPath); + await this.linkOrCopy(binaryPath, browserBinaryPath); this.logService.info( `[Native messaging] Hard-linked ${binaryPath} to ${browserBinaryPath}`, ); @@ -239,10 +236,7 @@ export class NativeMessagingMain { for (const [key, value] of Object.entries(this.getFlatpakNMHS())) { if (existsSync(value)) { const sandboxedProxyBinaryPath = path.join(value, ".bitwarden_desktop_proxy"); - if (existsSync(sandboxedProxyBinaryPath)) { - await fs.unlink(sandboxedProxyBinaryPath); - } - await fs.link(binaryPath, path.join(value, ".bitwarden_desktop_proxy")); + await this.linkOrCopy(binaryPath, sandboxedProxyBinaryPath); this.logService.info( `[Native messaging] Hard-linked ${binaryPath} to ${sandboxedProxyBinaryPath}`, ); @@ -530,4 +524,20 @@ export class NativeMessagingMain { await fs.unlink(path); } } + + private async linkOrCopy(source: string, destination: string) { + try { + if (existsSync(destination)) { + await fs.unlink(destination); + } + await fs.link(source, destination); + this.logService.info(`[Native messaging] Hard-linked ${source} to ${destination}`); + } catch (e) { + this.logService.warning( + `[Native messaging] Failed to hard-link ${source} to ${destination}, copying instead: ${e}`, + ); + await fs.copyFile(source, destination); + this.logService.info(`[Native messaging] Copied ${source} to ${destination}`); + } + } }