Skip to content

Commit 2b4bab3

Browse files
committed
🐛 fix: AppDataDirectory migration not effective on Linux
1 parent 64a0714 commit 2b4bab3

File tree

4 files changed

+62
-16
lines changed

4 files changed

+62
-16
lines changed

src/SwashbucklerDiary.Gtk/App.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public App(IServiceProvider serviceProvider)
4444
_masaBlazor = _serviceProvider.GetRequiredService<Masa.Blazor.MasaBlazor>();
4545
_appLifecycle = _serviceProvider.GetRequiredService<IAppLifecycle>();
4646
InitTheme();
47+
MigrateAppDataDirectory();
4748
AppActionsHelper.AddMainOptionEntries(app);
4849
}
4950

@@ -127,5 +128,14 @@ private void ThemeChanged(Theme theme)
127128
{
128129
_masaBlazor.SetTheme(theme == Theme.Dark);
129130
}
131+
132+
private void MigrateAppDataDirectory()
133+
{
134+
var versionUpdataManager = _serviceProvider.GetRequiredService<IVersionUpdataManager>();
135+
if (versionUpdataManager is SwashbucklerDiary.Gtk.Services.VersionUpdataManager gtkVersionUpdataManager)
136+
{
137+
gtkVersionUpdataManager.MigrateAppDataDirectory();
138+
}
139+
}
130140
}
131141
}

src/SwashbucklerDiary.Gtk/Services/VersionUpdataManager/VersionUpdataManager.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using SwashbucklerDiary.Rcl.Essentials;
23
using SwashbucklerDiary.Rcl.Services;
34

@@ -29,18 +30,43 @@ public override async Task ToUpdate()
2930
await _platformIntegration.OpenBrowser("https://github.com/Yu-Core/SwashbucklerDiary/releases");
3031
}
3132

32-
protected override void InitializeVersionHandlers()
33+
public void MigrateAppDataDirectory()
3334
{
34-
base.InitializeVersionHandlers();
35+
if (!_versionTracking.IsFirstLaunchEver)
36+
{
37+
return;
38+
}
3539

36-
AddVersionHandler("1.11.7", HandleVersionUpdate1117);
37-
}
40+
string _oldAppDataParentDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AppInfo.PackageName);
41+
if (!Path.Exists(_oldAppDataParentDirectory))
42+
{
43+
return;
44+
}
3845

39-
private Task HandleVersionUpdate1117()
40-
{
41-
string _oldAppDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AppInfo.PackageName, "Data");
42-
_appFileSystem.MoveFolder(_oldAppDataDirectory, FileSystem.AppDataDirectory, SearchOption.AllDirectories);
43-
return Task.CompletedTask;
46+
_appFileSystem.MoveFolder(_oldAppDataParentDirectory, Path.Combine(FileSystem.AppDataDirectory, ".."), SearchOption.AllDirectories, true);
47+
Directory.Delete(_oldAppDataParentDirectory, true);
48+
49+
string command = $"sleep 1 && {Environment.ProcessPath} &";
50+
51+
// 创建一个 ProcessStartInfo 对象来配置进程
52+
ProcessStartInfo processStartInfo = new ProcessStartInfo
53+
{
54+
FileName = "/bin/bash", // 使用 bash 来执行命令
55+
Arguments = $"-c \"{command}\"", // -c 表示执行后面的字符串作为命令
56+
RedirectStandardOutput = true, // 重定向标准输出
57+
RedirectStandardError = true, // 重定向标准错误
58+
UseShellExecute = false, // 不使用系统 shell 执行
59+
CreateNoWindow = true // 不创建窗口
60+
};
61+
62+
// 创建并启动进程
63+
using Process process = new Process();
64+
process.StartInfo = processStartInfo;
65+
66+
// 启动进程
67+
process.Start();
68+
69+
global::Gtk.Application.GetDefault()?.Quit();
4470
}
4571
}
4672
}

src/SwashbucklerDiary.Rcl/Essentials/AppFileSystem/AppFileSystem.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SwashbucklerDiary.Rcl.Essentials
1+
namespace SwashbucklerDiary.Rcl.Essentials
22
{
33
public abstract class AppFileSystem : IAppFileSystem
44
{
@@ -47,7 +47,8 @@ public async Task FileCopyAsync(string targetFilePath, Stream sourceStream)
4747
using (FileStream localFileStream = File.OpenWrite(targetFilePath))
4848
{
4949
await sourceStream.CopyToAsync(localFileStream, 1024 * 1024);
50-
};
50+
}
51+
;
5152
}
5253

5354
public void ClearFolder(string folderPath, List<string>? exceptPaths = null)
@@ -174,7 +175,7 @@ public void CopyFolder(string sourceFolder, string destinationFolder, SearchOpti
174175
}
175176
}
176177

177-
public void MoveFolder(string sourceFolder, string destinationFolder, SearchOption searchOption)
178+
public void MoveFolder(string sourceFolder, string destinationFolder, SearchOption searchOption, bool fileOverwrite = false)
178179
{
179180
string[] files = Directory.GetFiles(sourceFolder, "*", searchOption);
180181

@@ -186,10 +187,19 @@ public void MoveFolder(string sourceFolder, string destinationFolder, SearchOpti
186187

187188
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!);
188189

189-
if (!File.Exists(destinationPath))
190+
if (File.Exists(destinationPath))
190191
{
191-
File.Move(file, destinationPath);
192+
if (fileOverwrite)
193+
{
194+
File.Delete(destinationPath);
195+
}
196+
else
197+
{
198+
continue;
199+
}
192200
}
201+
202+
File.Move(file, destinationPath);
193203
}
194204
}
195205

src/SwashbucklerDiary.Rcl/Essentials/AppFileSystem/IAppFileSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SwashbucklerDiary.Rcl.Essentials
1+
namespace SwashbucklerDiary.Rcl.Essentials
22
{
33
public interface IAppFileSystem
44
{
@@ -24,7 +24,7 @@ public interface IAppFileSystem
2424

2525
void CopyFolder(string sourceFolder, string destinationFolder, SearchOption searchOption);
2626

27-
void MoveFolder(string sourceFolder, string destinationFolder, SearchOption searchOption);
27+
void MoveFolder(string sourceFolder, string destinationFolder, SearchOption searchOption, bool fileOverwrite = false);
2828

2929
/// <summary>
3030
/// 清除缓存

0 commit comments

Comments
 (0)