fix: handle file copy errors gracefully in init

This commit is contained in:
xmk23333 2026-01-15 18:11:15 +08:00
parent 9d5d2bb73d
commit 3d9507b10c

View File

@ -178,28 +178,23 @@ async function initFiles(): Promise<void> {
const sourcePath = path.join(resourcesFilesDir(), file)
if (!existsSync(sourcePath)) return
const targets = [
path.join(mihomoWorkDir(), file),
path.join(mihomoTestDir(), file)
]
const targets = [path.join(mihomoWorkDir(), file), path.join(mihomoTestDir(), file)]
await Promise.all(
targets.map(async (targetPath) => {
const shouldCopy = !existsSync(targetPath) || (await isSourceNewer(sourcePath, targetPath))
if (shouldCopy) {
// 先删除目标force: true 会忽略 ENOENT避免 cp 内部 unlink 的问题
if (!shouldCopy) return
try {
await rm(targetPath, { recursive: true, force: true })
await cp(sourcePath, targetPath, { recursive: true, force: true })
} catch (error: unknown) {
// EPERM: 文件被占用,跳过本次复制
if ((error as NodeJS.ErrnoException).code === 'EPERM') {
const code = (error as NodeJS.ErrnoException).code
if (code === 'EPERM' || code === 'EBUSY') {
await initLogger.warn(`Skipping ${file}: file is in use`)
return
}
throw error
}
await cp(sourcePath, targetPath, { recursive: true })
}
})
)
}