Skip to content

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

:art: Improve workspace dir checking https://github.com/siyuan-note/siyuan/issues/13789

Daniel010139be3f16
diff --git a/kernel/api/workspace.go b/kernel/api/workspace.go
index 323019243a..34a75fd0d8 100644
--- a/kernel/api/workspace.go
+++ b/kernel/api/workspace.go
@@ -300,7 +300,7 @@ func setWorkspaceDir(c *gin.Context) {
// 检查路径是否在已有的工作空间路径中
pathIsWorkspace := util.IsWorkspaceDir(path)
if !pathIsWorkspace {
- for p := filepath.Dir(path); "" != p; p = filepath.Dir(p) {
+ for p := filepath.Dir(path); !util.IsRootPath(p); p = filepath.Dir(p) {
if util.IsWorkspaceDir(p) {
ret.Code = -1
ret.Msg = fmt.Sprintf(model.Conf.Language(256), path, p)
diff --git a/kernel/util/path.go b/kernel/util/path.go
index 6c81828f30..b41a962a47 100644
--- a/kernel/util/path.go
+++ b/kernel/util/path.go
@@ -23,6 +23,7 @@ import (
"os"
"path"
"path/filepath"
+ "runtime"
"sort"
"strings"
"time"
@@ -351,3 +352,22 @@ func IsWorkspaceDir(dir string) bool {
}
return strings.Contains(string(data), "kernelVersion")
}
+
+// IsRootPath checks if the given path is a root path.
+func IsRootPath(path string) bool {
+ if path == "" {
+ return false
+ }
+
+ // Clean the path to remove any trailing slashes
+ cleanPath := filepath.Clean(path)
+
+ // Check if the path is the root path based on the operating system
+ if runtime.GOOS == "windows" {
+ // On Windows, root paths are like "C:\", "D:\", etc.
+ return len(cleanPath) == 3 && cleanPath[1] == ':' && cleanPath[2] == '\\'
+ } else {
+ // On Unix-like systems, the root path is "/"
+ return cleanPath == "/"
+ }
+}