diff --git a/cli/src/modules/common/registerCommonHandlers.ts b/cli/src/modules/common/registerCommonHandlers.ts index c3796dca..c13be033 100644 --- a/cli/src/modules/common/registerCommonHandlers.ts +++ b/cli/src/modules/common/registerCommonHandlers.ts @@ -3,7 +3,7 @@ import { exec, ExecOptions } from 'child_process'; import { promisify } from 'util'; import { readFile, writeFile, readdir, stat } from 'fs/promises'; import { createHash } from 'crypto'; -import { join, resolve } from 'path'; +import { basename, join, resolve } from 'path'; import { run as runRipgrep } from '@/modules/ripgrep/index'; import { run as runDifftastic } from '@/modules/difftastic/index'; import { RpcHandlerManager } from '../../api/rpc/RpcHandlerManager'; @@ -409,8 +409,8 @@ export function registerCommonHandlers(rpcHandlerManager: RpcHandlerManager, wor return { success: false, error: 'maxDepth must be non-negative' }; } - // Get the base name for the root node - const baseName = data.path === '/' ? '/' : data.path.split('/').pop() || data.path; + // Get the base name for the root node (cross-platform) + const baseName = data.path === '/' ? '/' : basename(data.path) || data.path; // Build the tree starting from the requested path const tree = await buildTree(data.path, baseName, 0); diff --git a/cli/src/persistence.ts b/cli/src/persistence.ts index 24d0e800..29d77709 100644 --- a/cli/src/persistence.ts +++ b/cli/src/persistence.ts @@ -7,7 +7,6 @@ import { FileHandle } from 'node:fs/promises' import { readFile, writeFile, mkdir, open, unlink, rename, stat } from 'node:fs/promises' import { existsSync, writeFileSync, readFileSync, unlinkSync } from 'node:fs' -import { constants } from 'node:fs' import { configuration } from '@/configuration' import * as z from 'zod'; import { encodeBase64 } from '@/api/encryption'; @@ -87,8 +86,8 @@ export async function updateSettings( // Acquire exclusive lock with retries while (attempts < MAX_LOCK_ATTEMPTS) { try { - // O_CREAT | O_EXCL | O_WRONLY = create exclusively, fail if exists - fileHandle = await open(lockFile, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY); + // 'wx' = create exclusively, fail if exists (cross-platform compatible) + fileHandle = await open(lockFile, 'wx'); break; } catch (err: any) { if (err.code === 'EEXIST') { @@ -270,11 +269,8 @@ export async function acquireDaemonLock( ): Promise { for (let attempt = 1; attempt <= maxAttempts; attempt++) { try { - // O_EXCL ensures we only create if it doesn't exist (atomic lock acquisition) - const fileHandle = await open( - configuration.daemonLockFile, - constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY - ); + // 'wx' ensures we only create if it doesn't exist (atomic lock acquisition) + const fileHandle = await open(configuration.daemonLockFile, 'wx'); // Write PID to lock file for debugging await fileHandle.writeFile(String(process.pid)); return fileHandle;