mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-06 05:09:09 +08:00
Update BackupManager.ts
Add fsync to prevent backup file corruption (#10476)
This commit is contained in:
parent
38ac42af8c
commit
fe9fdec3ca
@ -340,6 +340,21 @@ class BackupManager {
|
|||||||
await fs.remove(this.tempDir)
|
await fs.remove(this.tempDir)
|
||||||
onProgress({ stage: 'completed', progress: 100, total: 100 })
|
onProgress({ stage: 'completed', progress: 100, total: 100 })
|
||||||
|
|
||||||
|
// 强制将文件系统缓存同步到磁盘,防止因竞争条件导致文件损坏。
|
||||||
|
try {
|
||||||
|
const fileDescriptor = await fs.open(backupedFilePath, 'r')
|
||||||
|
await fs.fsync(fileDescriptor)
|
||||||
|
await fs.close(fileDescriptor)
|
||||||
|
logger.debug(`[BackupManager] Backup file synced to disk successfully: ${backupedFilePath}`)
|
||||||
|
} catch (syncError) {
|
||||||
|
logger.error(
|
||||||
|
'[BackupManager] Failed to explicitly sync backup file to disk. Falling back to a brief delay.',
|
||||||
|
syncError as Error
|
||||||
|
)
|
||||||
|
await new Promise((res) => setTimeout(res, 500))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
logger.debug('Backup completed successfully')
|
logger.debug('Backup completed successfully')
|
||||||
return backupedFilePath
|
return backupedFilePath
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user