Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Prevent mobile workspace migration from moving sibling workspaces

Danield5e28cff4da0
diff --git a/kernel/util/working_mobile.go b/kernel/util/working_mobile.go
index 37d61249d7..e4e903de29 100644
--- a/kernel/util/working_mobile.go
+++ b/kernel/util/working_mobile.go
@@ -17,6 +17,9 @@
package util
import (
+ "errors"
+ "fmt"
+ "io/fs"
"os"
"path/filepath"
"runtime"
@@ -66,46 +69,9 @@ func BootMobile(container, appDir, workspaceBaseDir, lang string) {
}
func initWorkspaceDirMobile(workspaceBaseDir string) {
- if gulu.File.IsDir(workspaceBaseDir) {
- entries, err := os.ReadDir(workspaceBaseDir)
- if err != nil {
- logging.LogErrorf("read workspace dir [%s] failed: %s", workspaceBaseDir, err)
- } else {
- // 旧版 iOS 端会在 workspaceBaseDir 下直接创建工作空间,这里需要将数据迁移到 workspaceBaseDir/siyuan/ 文件夹下
- var oldConf, oldData, oldTemp bool
- for _, entry := range entries {
- if entry.IsDir() && "conf" == entry.Name() {
- oldConf = true
- continue
- }
- if entry.IsDir() && "data" == entry.Name() {
- oldData = true
- continue
- }
- if entry.IsDir() && "temp" == entry.Name() {
- oldTemp = true
- continue
- }
- }
- if oldConf && oldData && oldTemp {
- for _, entry := range entries {
- if "home" == entry.Name() || "siyuan" == entry.Name() {
- continue
- }
-
- from := filepath.Join(workspaceBaseDir, entry.Name())
- to := filepath.Join(workspaceBaseDir, "siyuan", entry.Name())
- if err = os.Rename(from, to); err != nil {
- logging.LogErrorf("move workspace dir [%s] failed: %s", workspaceBaseDir, err)
- } else {
- logging.LogInfof("moved workspace dir [fomr=%s, to=%s]", from, to)
- }
- }
-
- os.RemoveAll(filepath.Join(workspaceBaseDir, "sync"))
- os.RemoveAll(filepath.Join(workspaceBaseDir, "backup"))
- }
- }
+ migrated, err := migrateLegacyIOSWorkspace(workspaceBaseDir)
+ if err != nil {
+ logging.LogErrorf("migrate legacy iOS workspace [%s] failed: %s", workspaceBaseDir, err)
}
userHomeConfDir := filepath.Join(HomeDir, ".config", "siyuan")
@@ -123,6 +89,9 @@ func initWorkspaceDirMobile(workspaceBaseDir string) {
workspacePaths = append(workspacePaths, WorkspaceDir)
} else {
workspacePaths, _ = ReadWorkspacePaths()
+ if migrated {
+ workspacePaths = replaceLegacyIOSWorkspacePath(workspacePaths, workspaceBaseDir, defaultWorkspaceDir)
+ }
if 0 < len(workspacePaths) {
WorkspaceDir = workspacePaths[len(workspacePaths)-1]
@@ -173,3 +142,81 @@ func initWorkspaceDirMobile(workspaceBaseDir string) {
LogPath = filepath.Join(TempDir, "siyuan.log")
logging.SetLogPath(LogPath)
}
+
+// 仅迁移工作空间规范定义的目录,保留基目录下的其他工作空间和用户文件。
+var legacyIOSWorkspaceEntries = []string{"conf", "data", "repo", "history", "corrupted", "temp", "sync", "backup"}
+
+type workspaceDirMove struct {
+ from string
+ to string
+}
+
+func migrateLegacyIOSWorkspace(workspaceBaseDir string) (migrated bool, err error) {
+ if ContainerIOS != Container || !gulu.File.IsDir(workspaceBaseDir) {
+ return false, nil
+ }
+
+ for _, name := range []string{"conf", "data", "temp"} {
+ if !gulu.File.IsDir(filepath.Join(workspaceBaseDir, name)) {
+ return false, nil
+ }
+ }
+
+ defaultWorkspaceDir := filepath.Join(workspaceBaseDir, "siyuan")
+ destinationEntries, readErr := os.ReadDir(defaultWorkspaceDir)
+ if readErr != nil {
+ return false, fmt.Errorf("read destination [%s] failed: %w", defaultWorkspaceDir, readErr)
+ }
+ if 0 < len(destinationEntries) {
+ return false, fmt.Errorf("destination [%s] is not empty", defaultWorkspaceDir)
+ }
+
+ var moves []workspaceDirMove
+ for _, name := range legacyIOSWorkspaceEntries {
+ from := filepath.Join(workspaceBaseDir, name)
+ if _, statErr := os.Lstat(from); statErr != nil {
+ if errors.Is(statErr, fs.ErrNotExist) {
+ continue
+ }
+ return false, fmt.Errorf("stat source [%s] failed: %w", from, statErr)
+ }
+
+ to := filepath.Join(defaultWorkspaceDir, name)
+ if _, statErr := os.Lstat(to); statErr == nil {
+ return false, fmt.Errorf("destination [%s] already exists", to)
+ } else if !errors.Is(statErr, fs.ErrNotExist) {
+ return false, fmt.Errorf("stat destination [%s] failed: %w", to, statErr)
+ }
+ moves = append(moves, workspaceDirMove{from: from, to: to})
+ }
+
+ var completed []workspaceDirMove
+ for _, move := range moves {
+ if renameErr := os.Rename(move.from, move.to); renameErr != nil {
+ var rollbackErrors []error
+ for i := len(completed) - 1; 0 <= i; i-- {
+ completedMove := completed[i]
+ if rollbackErr := os.Rename(completedMove.to, completedMove.from); rollbackErr != nil {
+ rollbackErrors = append(rollbackErrors, fmt.Errorf("rollback [%s] to [%s] failed: %w",
+ completedMove.to, completedMove.from, rollbackErr))
+ }
+ }
+ return false, errors.Join(append([]error{fmt.Errorf("move [%s] to [%s] failed: %w", move.from, move.to, renameErr)}, rollbackErrors...)...)
+ }
+ completed = append(completed, move)
+ }
+
+ for _, move := range completed {
+ logging.LogInfof("moved legacy iOS workspace dir [from=%s, to=%s]", move.from, move.to)
+ }
+ return true, nil
+}
+
+func replaceLegacyIOSWorkspacePath(workspacePaths []string, workspaceBaseDir, defaultWorkspaceDir string) []string {
+ for i, workspacePath := range workspacePaths {
+ if filepath.Clean(workspacePath) == filepath.Clean(workspaceBaseDir) {
+ workspacePaths[i] = defaultWorkspaceDir
+ }
+ }
+ return DeduplicateWorkspacePaths(workspacePaths)
+}
diff --git a/kernel/util/working_mobile_test.go b/kernel/util/working_mobile_test.go
new file mode 100644
index 0000000000..450196655d
--- /dev/null
+++ b/kernel/util/working_mobile_test.go
@@ -0,0 +1,154 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package util
+
+import (
+ "os"
+ "path/filepath"
+ "reflect"
+ "testing"
+)
+
+func TestMigrateLegacyIOSWorkspaceSkipsAndroid(t *testing.T) {
+ setMobileMigrationContainer(t, ContainerAndroid)
+ workspaceBaseDir := prepareLegacyMobileWorkspace(t)
+ siblingWorkspace := filepath.Join(workspaceBaseDir, "Todo")
+ if err := os.MkdirAll(siblingWorkspace, 0755); err != nil {
+ t.Fatal(err)
+ }
+
+ migrated, err := migrateLegacyIOSWorkspace(workspaceBaseDir)
+ if err != nil {
+ t.Fatalf("migrateLegacyIOSWorkspace() returned an error: %s", err)
+ }
+ if migrated {
+ t.Fatal("Android workspace must not run the legacy iOS migration")
+ }
+ for _, path := range []string{filepath.Join(workspaceBaseDir, "conf"), filepath.Join(workspaceBaseDir, "data"),
+ filepath.Join(workspaceBaseDir, "temp"), siblingWorkspace} {
+ if _, err = os.Stat(path); err != nil {
+ t.Fatalf("source path [%s] must remain unchanged: %s", path, err)
+ }
+ }
+}
+
+func TestMigrateLegacyIOSWorkspaceMovesKnownEntries(t *testing.T) {
+ setMobileMigrationContainer(t, ContainerIOS)
+ workspaceBaseDir := prepareLegacyMobileWorkspace(t)
+ for _, name := range []string{"repo", "history", "corrupted", "sync", "backup"} {
+ writeMigrationMarker(t, filepath.Join(workspaceBaseDir, name))
+ }
+ siblingWorkspace := filepath.Join(workspaceBaseDir, "Todo")
+ writeMigrationMarker(t, siblingWorkspace)
+
+ migrated, err := migrateLegacyIOSWorkspace(workspaceBaseDir)
+ if err != nil {
+ t.Fatalf("migrateLegacyIOSWorkspace() returned an error: %s", err)
+ }
+ if !migrated {
+ t.Fatal("legacy iOS workspace should be migrated")
+ }
+ for _, name := range legacyIOSWorkspaceEntries {
+ from := filepath.Join(workspaceBaseDir, name)
+ if _, err = os.Stat(from); !os.IsNotExist(err) {
+ t.Fatalf("source path [%s] should be moved", from)
+ }
+ marker := filepath.Join(workspaceBaseDir, "siyuan", name, "marker")
+ if _, err = os.Stat(marker); err != nil {
+ t.Fatalf("migration marker [%s] is missing: %s", marker, err)
+ }
+ }
+ if _, err = os.Stat(filepath.Join(siblingWorkspace, "marker")); err != nil {
+ t.Fatalf("sibling workspace must not be moved: %s", err)
+ }
+
+ migrated, err = migrateLegacyIOSWorkspace(workspaceBaseDir)
+ if err != nil {
+ t.Fatalf("repeated migrateLegacyIOSWorkspace() returned an error: %s", err)
+ }
+ if migrated {
+ t.Fatal("repeated migration should be a no-op")
+ }
+}
+
+func TestMigrateLegacyIOSWorkspaceRejectsDestinationConflicts(t *testing.T) {
+ setMobileMigrationContainer(t, ContainerIOS)
+ workspaceBaseDir := prepareLegacyMobileWorkspace(t)
+ writeMigrationMarker(t, filepath.Join(workspaceBaseDir, "sync"))
+ destinationData := filepath.Join(workspaceBaseDir, "siyuan", "data")
+ writeMigrationMarker(t, destinationData)
+
+ migrated, err := migrateLegacyIOSWorkspace(workspaceBaseDir)
+ if err == nil {
+ t.Fatal("destination conflict should abort migration")
+ }
+ if migrated {
+ t.Fatal("conflicting migration must not be reported as successful")
+ }
+ for _, name := range []string{"conf", "data", "temp", "sync"} {
+ marker := filepath.Join(workspaceBaseDir, name, "marker")
+ if _, statErr := os.Stat(marker); statErr != nil {
+ t.Fatalf("source marker [%s] must remain after preflight failure: %s", marker, statErr)
+ }
+ }
+ if _, err = os.Stat(filepath.Join(destinationData, "marker")); err != nil {
+ t.Fatalf("destination data must remain unchanged: %s", err)
+ }
+}
+
+func TestReplaceLegacyIOSWorkspacePath(t *testing.T) {
+ workspaceBaseDir := filepath.Join(t.TempDir(), "Documents")
+ defaultWorkspaceDir := filepath.Join(workspaceBaseDir, "siyuan")
+ otherWorkspaceDir := filepath.Join(workspaceBaseDir, "Todo")
+ paths := []string{workspaceBaseDir, otherWorkspaceDir, workspaceBaseDir + string(os.PathSeparator)}
+ want := []string{defaultWorkspaceDir, otherWorkspaceDir}
+
+ if got := replaceLegacyIOSWorkspacePath(paths, workspaceBaseDir, defaultWorkspaceDir); !reflect.DeepEqual(got, want) {
+ t.Fatalf("unexpected workspace paths: got %v, want %v", got, want)
+ }
+}
+
+func prepareLegacyMobileWorkspace(t *testing.T) string {
+ t.Helper()
+ workspaceBaseDir := t.TempDir()
+ if err := os.MkdirAll(filepath.Join(workspaceBaseDir, "siyuan"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ for _, name := range []string{"conf", "data", "temp"} {
+ writeMigrationMarker(t, filepath.Join(workspaceBaseDir, name))
+ }
+ return workspaceBaseDir
+}
+
+func writeMigrationMarker(t *testing.T, dir string) {
+ t.Helper()
+ if err := os.MkdirAll(dir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "marker"), []byte("test"), 0644); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func setMobileMigrationContainer(t *testing.T, container string) {
+ t.Helper()
+ originalContainer := Container
+ Container = container
+ t.Cleanup(func() {
+ Container = originalContainer
+ })
+}