()
| 278 | * 清理待删除的旧数据目录(应用启动时调用) |
| 279 | */ |
| 280 | export function cleanupPendingDeleteDir(): void { |
| 281 | try { |
| 282 | const config = readStorageConfig() |
| 283 | const pendingDir = config.pendingDeleteDir |
| 284 | |
| 285 | if (!pendingDir) return |
| 286 | |
| 287 | const currentDir = getUserDataDir() |
| 288 | |
| 289 | if (pendingDir === currentDir) { |
| 290 | console.log('[Paths] Skipping cleanup: pending dir is same as current dir') |
| 291 | writeStorageConfig({ ...config, pendingDeleteDir: undefined }) |
| 292 | return |
| 293 | } |
| 294 | |
| 295 | if (!isPathSafe(pendingDir)) { |
| 296 | console.log('[Paths] Skipping cleanup: pending dir is a system directory:', pendingDir) |
| 297 | writeStorageConfig({ ...config, pendingDeleteDir: undefined }) |
| 298 | return |
| 299 | } |
| 300 | |
| 301 | if (fs.existsSync(pendingDir) && !isExistingUserDataDir(pendingDir)) { |
| 302 | console.log('[Paths] Skipping cleanup: pending dir is not a ChatLab data dir:', pendingDir) |
| 303 | writeStorageConfig({ ...config, pendingDeleteDir: undefined }) |
| 304 | return |
| 305 | } |
| 306 | |
| 307 | if (!fs.existsSync(pendingDir)) { |
| 308 | console.log('[Paths] Pending dir does not exist, skipping cleanup:', pendingDir) |
| 309 | writeStorageConfig({ ...config, pendingDeleteDir: undefined }) |
| 310 | return |
| 311 | } |
| 312 | |
| 313 | console.log('[Paths] Cleaning up old data directory:', pendingDir) |
| 314 | fs.rmSync(pendingDir, { recursive: true, force: true }) |
| 315 | console.log('[Paths] Old data directory deleted:', pendingDir) |
| 316 | |
| 317 | writeStorageConfig({ ...config, pendingDeleteDir: undefined }) |
| 318 | } catch (error) { |
| 319 | console.error('[Paths] Failed to clean up old directory:', error) |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * 获取旧版数据目录(Documents/ChatLab) |
no test coverage detected