瀏覽代碼

Merge remote-tracking branch 'origin/dev' into dev

Vanessa 2 年之前
父節點
當前提交
ed2ce5bf29
共有 3 個文件被更改,包括 65 次插入14 次删除
  1. 8 7
      kernel/util/runtime.go
  2. 4 0
      kernel/util/working.go
  3. 53 7
      kernel/util/working_mobile.go

+ 8 - 7
kernel/util/runtime.go

@@ -32,13 +32,14 @@ import (
 const DatabaseVer = "20220501" // 修改表结构的话需要修改这里
 const DatabaseVer = "20220501" // 修改表结构的话需要修改这里
 
 
 const (
 const (
-	ExitCodeReadOnlyDatabase = 20 // 数据库文件被锁
-	ExitCodeUnavailablePort  = 21 // 端口不可用
-	ExitCodeCreateConfDirErr = 22 // 创建配置目录失败
-	ExitCodeBlockTreeErr     = 23 // 无法读写 blocktree.msgpack 文件
-	ExitCodeWorkspaceLocked  = 24 // 工作空间已被锁定
-	ExitCodeOk               = 0  // 正常退出
-	ExitCodeFatal            = 1  // 致命错误
+	ExitCodeReadOnlyDatabase      = 20 // 数据库文件被锁
+	ExitCodeUnavailablePort       = 21 // 端口不可用
+	ExitCodeCreateConfDirErr      = 22 // 创建配置目录失败
+	ExitCodeBlockTreeErr          = 23 // 无法读写 blocktree.msgpack 文件
+	ExitCodeWorkspaceLocked       = 24 // 工作空间已被锁定
+	ExitCodeCreateWorkspaceDirErr = 25 // 创建工作空间失败
+	ExitCodeOk                    = 0  // 正常退出
+	ExitCodeFatal                 = 1  // 致命错误
 )
 )
 
 
 func logBootInfo() {
 func logBootInfo() {

+ 4 - 0
kernel/util/working.go

@@ -199,6 +199,10 @@ func initWorkspaceDir(workspaceArg string) {
 			defaultWorkspaceDir = filepath.Join(userProfile, "Documents", "SiYuan")
 			defaultWorkspaceDir = filepath.Join(userProfile, "Documents", "SiYuan")
 		}
 		}
 	}
 	}
+	if err := os.MkdirAll(defaultWorkspaceDir, 0755); nil != err && !os.IsExist(err) {
+		log.Printf("create default workspace folder [%s] failed: %s", defaultWorkspaceDir, err)
+		os.Exit(ExitCodeCreateWorkspaceDirErr)
+	}
 
 
 	var workspacePaths []string
 	var workspacePaths []string
 	if !gulu.File.IsExist(workspaceConf) {
 	if !gulu.File.IsExist(workspaceConf) {

+ 53 - 7
kernel/util/working_mobile.go

@@ -45,7 +45,17 @@ func BootMobile(container, appDir, workspaceBaseDir, lang string) {
 	HomeDir = filepath.Join(workspaceBaseDir, "home")
 	HomeDir = filepath.Join(workspaceBaseDir, "home")
 	userHomeConfDir := filepath.Join(HomeDir, ".config", "siyuan")
 	userHomeConfDir := filepath.Join(HomeDir, ".config", "siyuan")
 	if !gulu.File.IsExist(userHomeConfDir) {
 	if !gulu.File.IsExist(userHomeConfDir) {
-		os.MkdirAll(userHomeConfDir, 0755)
+		if err := os.MkdirAll(userHomeConfDir, 0755); nil != err && !os.IsExist(err) {
+			log.Printf("create user home conf folder [%s] failed: %s", userHomeConfDir, err)
+			os.Exit(ExitCodeCreateConfDirErr)
+		}
+
+	}
+
+	defaultWorkspaceDir := filepath.Join(workspaceBaseDir, "siyuan")
+	if err := os.MkdirAll(defaultWorkspaceDir, 0755); nil != err && !os.IsExist(err) {
+		log.Printf("create default workspace folder [%s] failed: %s", defaultWorkspaceDir, err)
+		os.Exit(ExitCodeCreateWorkspaceDirErr)
 	}
 	}
 
 
 	initWorkspaceDirMobile(workspaceBaseDir)
 	initWorkspaceDirMobile(workspaceBaseDir)
@@ -57,16 +67,52 @@ func BootMobile(container, appDir, workspaceBaseDir, lang string) {
 }
 }
 
 
 func initWorkspaceDirMobile(workspaceBaseDir string) {
 func initWorkspaceDirMobile(workspaceBaseDir string) {
-	userHomeConfDir := filepath.Join(HomeDir, ".config", "siyuan")
-	workspaceConf := filepath.Join(userHomeConfDir, "workspace.json")
-	if !gulu.File.IsExist(workspaceConf) {
-		if err := os.MkdirAll(userHomeConfDir, 0755); nil != err && !os.IsExist(err) {
-			log.Printf("create user home conf folder [%s] failed: %s", userHomeConfDir, err)
-			os.Exit(ExitCodeCreateConfDirErr)
+	if gulu.File.IsDir(workspaceBaseDir) {
+		entries, err := os.ReadDir(workspaceBaseDir)
+		if nil != err {
+			log.Printf("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() {
+						continue
+					}
+
+					from := filepath.Join(workspaceBaseDir, entry.Name())
+					to := filepath.Join(workspaceBaseDir, "siyuan", entry.Name())
+					if err = os.Rename(from, to); nil != err {
+						log.Printf("move workspace dir [%s] failed: %s", workspaceBaseDir, err)
+					} else {
+						log.Printf("moved workspace dir [fomr=%s, to=%s]", from, to)
+					}
+				}
+
+				os.RemoveAll(filepath.Join(workspaceBaseDir, "sync"))
+				os.RemoveAll(filepath.Join(workspaceBaseDir, "backup"))
+			}
 		}
 		}
 	}
 	}
 
 
+	userHomeConfDir := filepath.Join(HomeDir, ".config", "siyuan")
+	workspaceConf := filepath.Join(userHomeConfDir, "workspace.json")
 	defaultWorkspaceDir := filepath.Join(workspaceBaseDir, "siyuan")
 	defaultWorkspaceDir := filepath.Join(workspaceBaseDir, "siyuan")
+
 	var workspacePaths []string
 	var workspacePaths []string
 	if !gulu.File.IsExist(workspaceConf) {
 	if !gulu.File.IsExist(workspaceConf) {
 		WorkspaceDir = defaultWorkspaceDir
 		WorkspaceDir = defaultWorkspaceDir