fix: improve cross-platform compatibility for path and file operations

Use basename() instead of split('/').pop() and 'wx' flag instead of
fs constants for better Windows compatibility.
This commit is contained in:
Evan7198
2025-12-28 12:16:51 +08:00
committed by weishu
parent 2ef7e0ec5f
commit 62f0cf4c41
2 changed files with 7 additions and 11 deletions
@@ -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);
+4 -8
View File
@@ -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<FileHandle | null> {
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;