Skip to content

Commit a12fa39

Browse files
committed
fix(*)
1 parent 83bea0a commit a12fa39

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

lib/common/global/app_path_manager.dart

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ class AppPathManager {
3131
final Directory appDir = await getApplicationDocumentsDirectory();
3232
final Directory supportDir = await getApplicationSupportDirectory();
3333

34+
bool isRunningOnCDrive = false;
35+
if (!kIsWeb && Platform.isWindows) {
36+
final String exeDir = p.dirname(Platform.resolvedExecutable);
37+
if (exeDir.startsWith(RegExp(r'^[cC]:'))) {
38+
isRunningOnCDrive = true;
39+
log('检测到当前程序运行在 C 盘,将强制跳过历史数据迁移。');
40+
}
41+
}
42+
3443
final List<String> extraOldBasePaths = [
3544
p.join(appDir.path, softNameDir),
3645
p.join(supportDir.path, softNameDir),
@@ -78,22 +87,46 @@ class AppPathManager {
7887
rootPath = p.join(rootPath, sanitizedInstanceId);
7988
}
8089

81-
// 3. 规范化 Windows 的物理路径字符串(消除 pure_live 和 PURE_LIVE 的大小写异同带来的冲突)
90+
// 3. 规范化 Windows 的物理路径字符串
8291
final String canonicalOldRoot = p.canonicalize(oldRootPath);
8392
final String canonicalNewRoot = p.canonicalize(rootPath);
8493

8594
// 4. 迁移保险锁文件
8695
final lockFile = File(p.join(rootPath, 'migrated.lock'));
8796
final bool isAlreadyMigrated = !kIsWeb && await lockFile.exists();
8897

89-
// 只有在“新旧物理路径不同”且“历史上从未成功迁移过”时才复制文件
90-
if (!kIsWeb && canonicalOldRoot != canonicalNewRoot && !isAlreadyMigrated) {
91-
await _migrateHiveFiles(oldRootPath, rootPath, lockFile);
98+
// 核心修改:非 Web 平台 + 新旧路径不同 + 未迁移过 +【非 C 盘运行】时才触发迁移
99+
if (!kIsWeb && canonicalOldRoot != canonicalNewRoot && !isAlreadyMigrated && !isRunningOnCDrive) {
100+
// 验证目标目录是否有写入权限
101+
final bool hasWritePermission = await _checkDirectoryWritable(rootPath);
102+
if (hasWritePermission) {
103+
await _migrateHiveFiles(oldRootPath, rootPath, lockFile);
104+
} else {
105+
log('迁移终止:目标新目录 $rootPath 没有写入权限!');
106+
}
92107
}
93108

94109
_basePath = rootPath;
95110
}
96111

112+
/// 新增:测试目标目录是否真正具备物理写入权限
113+
Future<bool> _checkDirectoryWritable(String path) async {
114+
try {
115+
final testDir = Directory(path);
116+
if (!await testDir.exists()) {
117+
await testDir.create(recursive: true);
118+
}
119+
// 创建一个临时测试文件
120+
final testFile = File(p.join(path, '.permission_test_${DateTime.now().millisecondsSinceEpoch}'));
121+
await testFile.writeAsString('test');
122+
await testFile.delete(); // 成功写入后立即删除
123+
return true;
124+
} catch (e) {
125+
log('目录写入权限检测失败 ($path): $e');
126+
return false;
127+
}
128+
}
129+
97130
Future<void> _migrateHiveFiles(String oldRoot, String rootPath, File lockFile) async {
98131
final oldDir = Directory(oldRoot);
99132
if (!await oldDir.exists()) return;
@@ -109,7 +142,6 @@ class AppPathManager {
109142

110143
if (await oldFile.exists()) {
111144
try {
112-
// 只复制,不执行旧文件的 delete 操作,确保原始数据 100% 完好留存
113145
await oldFile.copy(newFile.path);
114146
log('数据迁移成功 (原文件已保留): $name');
115147
} catch (e) {
@@ -118,7 +150,6 @@ class AppPathManager {
118150
}
119151
}
120152

121-
// 迁移成功后生成锁文件,下次打开应用直接跳过此段逻辑
122153
try {
123154
await lockFile.create(recursive: true);
124155
await lockFile.writeAsString('Migrated on ${DateTime.now()}');

0 commit comments

Comments
 (0)