From 02a2934909dcea8146300ca46123d7a243eb17b7 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 4 Jan 2023 19:33:02 +0800 Subject: [PATCH 1/4] =?UTF-8?q?:art:=20=E7=A7=BB=E5=8A=A8=E7=AB=AF?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E5=B7=A5=E4=BD=9C=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=20https://github.com/siyuan-note/siyuan/issues/4642?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/mobile/kernel.go | 6 +++--- kernel/util/working.go | 3 --- kernel/util/working_mobile.go | 7 +++---- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/kernel/mobile/kernel.go b/kernel/mobile/kernel.go index 58c2101ef..411c6a15a 100644 --- a/kernel/mobile/kernel.go +++ b/kernel/mobile/kernel.go @@ -32,16 +32,16 @@ import ( _ "golang.org/x/mobile/bind" ) -func StartKernelFast(container, appDir, workspaceDir, nativeLibDir, privateDataDir, localIP string) { +func StartKernelFast(container, appDir, workspaceBaseDir, localIPs string) { go server.Serve(true) } -func StartKernel(container, appDir, workspaceDir, nativeLibDir, privateDataDir, timezoneID, localIPs, lang string) { +func StartKernel(container, appDir, workspaceBaseDir, timezoneID, localIPs, lang string) { SetTimezone(container, appDir, timezoneID) util.Mode = "prod" util.LocalIPs = strings.Split(localIPs, ",") - util.BootMobile(container, appDir, workspaceDir, nativeLibDir, privateDataDir, lang) + util.BootMobile(container, appDir, workspaceBaseDir, lang) model.InitConf() go server.Serve(false) diff --git a/kernel/util/working.go b/kernel/util/working.go index c48d5e6f4..e84f8c524 100644 --- a/kernel/util/working.go +++ b/kernel/util/working.go @@ -176,9 +176,6 @@ var ( IconsPath string // 配置目录下的外观目录下的 icons/ 路径 SnippetsPath string // 数据目录下的 snippets/ 路径 - AndroidNativeLibDir string // Android 库路径 - AndroidPrivateDataDir string // Android 私有数据路径 - UIProcessIDs = sync.Map{} // UI 进程 ID IsNewbie bool // 是否是第一次安装 diff --git a/kernel/util/working_mobile.go b/kernel/util/working_mobile.go index 8af63af6f..de565f00f 100644 --- a/kernel/util/working_mobile.go +++ b/kernel/util/working_mobile.go @@ -28,13 +28,14 @@ import ( "github.com/siyuan-note/logging" ) -func BootMobile(container, appDir, workspaceDir, nativeLibDir, privateDataDir, lang string) { +func BootMobile(container, appDir, workspaceBaseDir, lang string) { IncBootProgress(3, "Booting...") rand.Seed(time.Now().UTC().UnixNano()) initMime() initHttpClient() - HomeDir = filepath.Join(workspaceDir, "home") + workspaceDir := filepath.Join(workspaceBaseDir, "siyuan") + HomeDir = filepath.Join(workspaceBaseDir, "home") userHomeConfDir := filepath.Join(HomeDir, ".config", "siyuan") if !gulu.File.IsExist(userHomeConfDir) { os.MkdirAll(userHomeConfDir, 0755) @@ -54,8 +55,6 @@ func BootMobile(container, appDir, workspaceDir, nativeLibDir, privateDataDir, l DBPath = filepath.Join(TempDir, DBName) HistoryDBPath = filepath.Join(TempDir, "history.db") BlockTreePath = filepath.Join(TempDir, "blocktree.msgpack") - AndroidNativeLibDir = nativeLibDir - AndroidPrivateDataDir = privateDataDir LogPath = filepath.Join(TempDir, "siyuan.log") logging.SetLogPath(LogPath) AppearancePath = filepath.Join(ConfDir, "appearance") From fb7361d98b5fa4563218fd4413fed574f619ffa4 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 4 Jan 2023 20:12:58 +0800 Subject: [PATCH 2/4] :art: Clean code --- kernel/util/runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/util/runtime.go b/kernel/util/runtime.go index 72850d31e..4b971c619 100644 --- a/kernel/util/runtime.go +++ b/kernel/util/runtime.go @@ -36,7 +36,7 @@ const ( ExitCodeUnavailablePort = 21 // 端口不可用 ExitCodeCreateConfDirErr = 22 // 创建配置目录失败 ExitCodeBlockTreeErr = 23 // 无法读写 blocktree.msgpack 文件 - ExitCodeWorkspaceLocked = 24 // 工作区已被锁定 + ExitCodeWorkspaceLocked = 24 // 工作空间已被锁定 ExitCodeOk = 0 // 正常退出 ExitCodeFatal = 1 // 致命错误 ) From a9cb87787e7db0b1f1c4520293c0b04996accc29 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 4 Jan 2023 20:13:17 +0800 Subject: [PATCH 3/4] =?UTF-8?q?:art:=20=E7=A7=BB=E5=8A=A8=E7=AB=AF?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E5=B7=A5=E4=BD=9C=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=20https://github.com/siyuan-note/siyuan/issues/4642?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/util/working.go | 2 +- kernel/util/working_mobile.go | 122 ++++++++++++++++++++++++++-------- 2 files changed, 95 insertions(+), 29 deletions(-) diff --git a/kernel/util/working.go b/kernel/util/working.go index e84f8c524..ed91959cc 100644 --- a/kernel/util/working.go +++ b/kernel/util/working.go @@ -217,7 +217,7 @@ func initWorkspaceDir(workspaceArg string) { log.Printf("unmarshal workspace conf [%s] failed: %s", workspaceConf, err) } - tmp := workspacePaths[:0] + var tmp []string for _, d := range workspacePaths { d = strings.TrimRight(d, " \t\n") // 去掉工作空间路径尾部空格 https://github.com/siyuan-note/siyuan/issues/6353 if gulu.File.IsDir(d) { diff --git a/kernel/util/working_mobile.go b/kernel/util/working_mobile.go index de565f00f..465468f5c 100644 --- a/kernel/util/working_mobile.go +++ b/kernel/util/working_mobile.go @@ -17,9 +17,11 @@ package util import ( + "log" "math/rand" "os" "path/filepath" + "strings" "time" "github.com/88250/gulu" @@ -33,41 +35,105 @@ func BootMobile(container, appDir, workspaceBaseDir, lang string) { rand.Seed(time.Now().UTC().UnixNano()) initMime() initHttpClient() - - workspaceDir := filepath.Join(workspaceBaseDir, "siyuan") - HomeDir = filepath.Join(workspaceBaseDir, "home") - userHomeConfDir := filepath.Join(HomeDir, ".config", "siyuan") - if !gulu.File.IsExist(userHomeConfDir) { - os.MkdirAll(userHomeConfDir, 0755) - } - WorkingDir = filepath.Join(appDir, "app") - WorkspaceDir = workspaceDir - ConfDir = filepath.Join(workspaceDir, "conf") - DataDir = filepath.Join(workspaceDir, "data") - HistoryDir = filepath.Join(workspaceDir, "history") - RepoDir = filepath.Join(WorkspaceDir, "repo") - TempDir = filepath.Join(workspaceDir, "temp") - osTmpDir := filepath.Join(TempDir, "os") - os.RemoveAll(osTmpDir) - os.MkdirAll(osTmpDir, 0755) - os.RemoveAll(filepath.Join(TempDir, "repo")) - os.Setenv("TMPDIR", osTmpDir) - DBPath = filepath.Join(TempDir, DBName) - HistoryDBPath = filepath.Join(TempDir, "history.db") - BlockTreePath = filepath.Join(TempDir, "blocktree.msgpack") - LogPath = filepath.Join(TempDir, "siyuan.log") - logging.SetLogPath(LogPath) - AppearancePath = filepath.Join(ConfDir, "appearance") - ThemesPath = filepath.Join(AppearancePath, "themes") - IconsPath = filepath.Join(AppearancePath, "icons") - SnippetsPath = filepath.Join(DataDir, "snippets") ServerPort = FixedPort Container = container UserAgent = UserAgent + " " + Container httpclient.SetUserAgent(UserAgent) Lang = lang + + WorkingDir = filepath.Join(appDir, "app") + HomeDir = filepath.Join(workspaceBaseDir, "home") + userHomeConfDir := filepath.Join(HomeDir, ".config", "siyuan") + if !gulu.File.IsExist(userHomeConfDir) { + os.MkdirAll(userHomeConfDir, 0755) + } + + initWorkspaceDirMobile(workspaceBaseDir) + initPathDir() bootBanner := figure.NewFigure("SiYuan", "", true) logging.LogInfof("\n" + bootBanner.String()) logBootInfo() } + +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) + } + } + + defaultWorkspaceDir := filepath.Join(workspaceBaseDir, "siyuan") + var workspacePaths []string + if !gulu.File.IsExist(workspaceConf) { + WorkspaceDir = defaultWorkspaceDir + if !gulu.File.IsDir(WorkspaceDir) { + log.Printf("use the default workspace [%s] since the specified workspace [%s] is not a dir", WorkspaceDir, defaultWorkspaceDir) + WorkspaceDir = defaultWorkspaceDir + } + workspacePaths = append(workspacePaths, WorkspaceDir) + } else { + data, err := os.ReadFile(workspaceConf) + if err = gulu.JSON.UnmarshalJSON(data, &workspacePaths); nil != err { + log.Printf("unmarshal workspace conf [%s] failed: %s", workspaceConf, err) + } + + var tmp []string + for _, d := range workspacePaths { + d = strings.TrimRight(d, " \t\n") // 去掉工作空间路径尾部空格 https://github.com/siyuan-note/siyuan/issues/6353 + if gulu.File.IsDir(d) { + tmp = append(tmp, d) + } + } + workspacePaths = tmp + + if 0 < len(workspacePaths) { + WorkspaceDir = workspacePaths[len(workspacePaths)-1] + if !gulu.File.IsDir(WorkspaceDir) { + log.Printf("use the default workspace [%s] since the specified workspace [%s] is not a dir", WorkspaceDir, defaultWorkspaceDir) + WorkspaceDir = defaultWorkspaceDir + } + workspacePaths[len(workspacePaths)-1] = WorkspaceDir + } else { + WorkspaceDir = defaultWorkspaceDir + workspacePaths = append(workspacePaths, WorkspaceDir) + } + } + + if data, err := gulu.JSON.MarshalJSON(workspacePaths); nil == err { + if err = os.WriteFile(workspaceConf, data, 0644); nil != err { + log.Fatalf("write workspace conf [%s] failed: %s", workspaceConf, err) + } + } else { + log.Fatalf("marshal workspace conf [%s] failed: %s", workspaceConf, err) + } + + ConfDir = filepath.Join(WorkspaceDir, "conf") + DataDir = filepath.Join(WorkspaceDir, "data") + RepoDir = filepath.Join(WorkspaceDir, "repo") + HistoryDir = filepath.Join(WorkspaceDir, "history") + TempDir = filepath.Join(WorkspaceDir, "temp") + osTmpDir := filepath.Join(TempDir, "os") + os.RemoveAll(osTmpDir) + if err := os.MkdirAll(osTmpDir, 0755); nil != err { + log.Fatalf("create os tmp dir [%s] failed: %s", osTmpDir, err) + } + os.RemoveAll(filepath.Join(TempDir, "repo")) + os.Setenv("TMPDIR", osTmpDir) + os.Setenv("TEMP", osTmpDir) + os.Setenv("TMP", osTmpDir) + DBPath = filepath.Join(TempDir, DBName) + HistoryDBPath = filepath.Join(TempDir, "history.db") + BlockTreePath = filepath.Join(TempDir, "blocktree.msgpack") + SnippetsPath = filepath.Join(DataDir, "snippets") + + AppearancePath = filepath.Join(ConfDir, "appearance") + ThemesPath = filepath.Join(AppearancePath, "themes") + IconsPath = filepath.Join(AppearancePath, "icons") + + LogPath = filepath.Join(TempDir, "siyuan.log") + logging.SetLogPath(LogPath) +} From ceac827a7f6a99730dc9f3318c4b7a2a1e3030b9 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 4 Jan 2023 20:41:33 +0800 Subject: [PATCH 4/4] =?UTF-8?q?:art:=20=E7=A7=BB=E5=8A=A8=E7=AB=AF?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E5=B7=A5=E4=BD=9C=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=20https://github.com/siyuan-note/siyuan/issues/4642?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/util/working.go | 2 +- kernel/util/working_mobile.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/util/working.go b/kernel/util/working.go index ed91959cc..50cfc1011 100644 --- a/kernel/util/working.go +++ b/kernel/util/working.go @@ -207,7 +207,7 @@ func initWorkspaceDir(workspaceArg string) { WorkspaceDir = workspaceArg } if !gulu.File.IsDir(WorkspaceDir) { - log.Printf("use the default workspace [%s] since the specified workspace [%s] is not a dir", WorkspaceDir, defaultWorkspaceDir) + log.Printf("use the default workspace [%s] since the specified workspace [%s] is not a dir", defaultWorkspaceDir, WorkspaceDir) WorkspaceDir = defaultWorkspaceDir } workspacePaths = append(workspacePaths, WorkspaceDir) diff --git a/kernel/util/working_mobile.go b/kernel/util/working_mobile.go index 465468f5c..9b6d3561e 100644 --- a/kernel/util/working_mobile.go +++ b/kernel/util/working_mobile.go @@ -93,7 +93,7 @@ func initWorkspaceDirMobile(workspaceBaseDir string) { if 0 < len(workspacePaths) { WorkspaceDir = workspacePaths[len(workspacePaths)-1] if !gulu.File.IsDir(WorkspaceDir) { - log.Printf("use the default workspace [%s] since the specified workspace [%s] is not a dir", WorkspaceDir, defaultWorkspaceDir) + log.Printf("use the default workspace [%s] since the specified workspace [%s] is not a dir", defaultWorkspaceDir, WorkspaceDir) WorkspaceDir = defaultWorkspaceDir } workspacePaths[len(workspacePaths)-1] = WorkspaceDir