mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Add mkdirSafe/mkdirSyncSafe helpers that swallow EACCES/EPERM when the target directory already exists with execute permission, and route all recursive mkdir call sites through them so pre-existing directories owned by another user do not break backend startup, file ops, or repo mirroring.
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { promises as fs } from 'node:fs'
|
|
import { randomBytes } from 'node:crypto'
|
|
import { logger } from './logger'
|
|
import { mkdirSafe } from './fs-safe'
|
|
|
|
const fileLockPromises = new Map<string, Promise<unknown>>()
|
|
|
|
export async function readJsonSafe<T>(filePath: string, fallback: T): Promise<T> {
|
|
try {
|
|
const content = await fs.readFile(filePath, 'utf8')
|
|
return JSON.parse(content) as T
|
|
} catch (error: unknown) {
|
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
return fallback
|
|
}
|
|
logger.warn(`Failed to read or parse JSON from ${filePath}: ${error instanceof Error ? error.message : String(error)}`)
|
|
return fallback
|
|
}
|
|
}
|
|
|
|
export async function writeJsonAtomic(filePath: string, data: unknown): Promise<void> {
|
|
const tmpPath = `${filePath}.tmp.${process.pid}.${Date.now()}.${randomBytes(4).toString('hex')}`
|
|
try {
|
|
await mkdirSafe(filePath.substring(0, filePath.lastIndexOf('/')))
|
|
await fs.writeFile(tmpPath, JSON.stringify(data, null, 2), 'utf8')
|
|
await fs.rename(tmpPath, filePath)
|
|
} catch (error) {
|
|
await fs.unlink(tmpPath).catch(() => undefined)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function withFileLock<T>(filePath: string, fn: () => Promise<T>): Promise<T> {
|
|
const absolutePath = filePath
|
|
const previousLock = fileLockPromises.get(absolutePath)
|
|
|
|
const executeWithLock = async (): Promise<T> => {
|
|
try {
|
|
return await fn()
|
|
} finally {
|
|
if (fileLockPromises.get(absolutePath) === newLock) {
|
|
fileLockPromises.delete(absolutePath)
|
|
}
|
|
}
|
|
}
|
|
|
|
const newLock = previousLock ? previousLock.then(executeWithLock) : executeWithLock()
|
|
fileLockPromises.set(absolutePath, newLock)
|
|
|
|
return newLock as Promise<T>
|
|
}
|