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
+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;