This commit is contained in:
Daniel 2025-02-10 11:00:03 +08:00
parent e01b7256b5
commit 010139be3f
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 21 additions and 1 deletions

View file

@ -300,7 +300,7 @@ func setWorkspaceDir(c *gin.Context) {
// 检查路径是否在已有的工作空间路径中 // 检查路径是否在已有的工作空间路径中
pathIsWorkspace := util.IsWorkspaceDir(path) pathIsWorkspace := util.IsWorkspaceDir(path)
if !pathIsWorkspace { 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) { if util.IsWorkspaceDir(p) {
ret.Code = -1 ret.Code = -1
ret.Msg = fmt.Sprintf(model.Conf.Language(256), path, p) ret.Msg = fmt.Sprintf(model.Conf.Language(256), path, p)

View file

@ -23,6 +23,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"runtime"
"sort" "sort"
"strings" "strings"
"time" "time"
@ -351,3 +352,22 @@ func IsWorkspaceDir(dir string) bool {
} }
return strings.Contains(string(data), "kernelVersion") 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 == "/"
}
}