diff --git a/kernel/api/workspace.go b/kernel/api/workspace.go index 323019243..34a75fd0d 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 6c81828f3..b41a962a4 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 == "/" + } +}