跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

paths.go

176 行 · 4.5 KB历史下载
1// Package paths compares and joins host paths as agent-vm's checks need:
2// textually, as `cd` does, and whatever the case where the file system
3// ignores it.
4//
5// Host paths are spelled with / separators everywhere: on Windows, C:/Users/me
6// (Host), which Go's file functions take as they take C:\Users\me.
7package paths
8
9import (
10 "os"
11 "runtime"
12 "strings"
13 "unicode/utf8"
14)
15
16// Windows reports whether host paths are Windows ones, with a drive. A
17// variable, for tests.
18var Windows = func() bool { return runtime.GOOS == "windows" }
19
20// Host is p, as the OS gave it, in agent-vm's spelling: / separators on
21// Windows, as is elsewhere.
22func Host(p string) string {
23 if Windows() {
24 return strings.ReplaceAll(p, `\`, "/")
25 }
26 return p
27}
28
29// Home is the user's home folder, spelled as Host does; "" when unknown.
30func Home() string {
31 h, err := os.UserHomeDir()
32 if err != nil {
33 return ""
34 }
35 return Host(h)
36}
37
38// Real is p with its links resolved (including directory junctions), spelled as
39// Host does.
40func Real(p string) (string, error) {
41 r, err := Resolve(p)
42 return Host(r), err
43}
44
45// volume is the length of p's drive ("C:"), 0 when it has none or this is
46// not Windows.
47func volume(p string) int {
48 if Windows() && len(p) >= 2 && p[1] == ':' && ('a' <= p[0]|0x20 && p[0]|0x20 <= 'z') {
49 return 2
50 }
51 return 0
52}
53
54// IsAbs reports whether p is absolute: it starts with /, after its drive
55// on Windows (C:/x).
56func IsAbs(p string) bool {
57 return strings.HasPrefix(p[volume(p):], "/")
58}
59
60// Root is p's root: "/", or its drive's ("C:/") on Windows.
61func Root(p string) string {
62 return p[:volume(p)] + "/"
63}
64
65// Join is dir/p, with "." and ".." resolved in the text, as `cd` does: a
66// path that only goes through dir on its way out of it (../x) names a file
67// outside. The root ("/", "C:/") when nothing is left: ".." stops there.
68func Join(dir, p string) string {
69 v := volume(dir)
70 drive, out := dir[:v], strings.TrimSuffix(dir[v:], "/")
71 for _, comp := range strings.Split(p, "/") {
72 switch comp {
73 case "", ".":
74 case "..":
75 if i := strings.LastIndexByte(out, '/'); i >= 0 {
76 out = out[:i]
77 } else {
78 out = ""
79 }
80 default:
81 out += "/" + comp
82 }
83 }
84 if out == "" {
85 return drive + "/"
86 }
87 return drive + out
88}
89
90// Abs is p made absolute against dir, as Join, an absolute p against its
91// root.
92func Abs(dir, p string) string {
93 if IsAbs(p) {
94 r := Root(p)
95 return Join(r, p[len(r):])
96 }
97 return Join(dir, p)
98}
99
100// Guest is the host path p as 0.2 spelled it, which is the path the VM sees
101// it at, and which VM names hash: as is, but for Windows, where Git Bash
102// spelled C:\Users\x as /c/Users/x.
103func Guest(p string) string {
104 if volume(p) == 0 {
105 return p
106 }
107 return "/" + strings.ToLower(p[:1]) + strings.ReplaceAll(p[2:], `\`, "/")
108}
109
110// FromGitBash is p, on Windows, with Git Bash's spelling of a drive (/c/x,
111// as 0.2's volumes file has it) made a drive again (C:/x); as is
112// elsewhere.
113func FromGitBash(p string) string {
114 if !Windows() || len(p) < 2 || p[0] != '/' || !('a' <= p[1]|0x20 && p[1]|0x20 <= 'z') || (len(p) > 2 && p[2] != '/') {
115 return p
116 }
117 return strings.ToUpper(p[1:2]) + ":" + Join("/", p[2:])
118}
119
120// NoCase reports whether this host's file systems ignore case by default:
121// macOS, Windows. A variable, for tests.
122var NoCase = func() bool {
123 return runtime.GOOS == "darwin" || Windows()
124}
125
126// Fold is s as the file system compares it: lowercased where case is
127// ignored, as is elsewhere.
128func Fold(s string) string {
129 if NoCase() {
130 return strings.ToLower(s)
131 }
132 return s
133}
134
135// Equal compares two paths as the file system does.
136func Equal(a, b string) bool {
137 if NoCase() {
138 return strings.EqualFold(a, b)
139 }
140 return a == b
141}
142
143// CutPrefix is s without prefix, compared as the file system does, and
144// whether s starts with it. The rest is s's own spelling: case folding can
145// change the length of a string, so it is cut where the match ends in s.
146func CutPrefix(s, prefix string) (string, bool) {
147 if !NoCase() {
148 return strings.CutPrefix(s, prefix)
149 }
150 i := 0
151 for _, pr := range prefix {
152 if i >= len(s) {
153 return s, false
154 }
155 sr, n := utf8.DecodeRuneInString(s[i:])
156 if !strings.EqualFold(string(sr), string(pr)) {
157 return s, false
158 }
159 i += n
160 }
161 return s[i:], true
162}
163
164// In is p relative to dir, "." for dir itself, compared as the file system
165// does; false when p is not inside dir.
166func In(p, dir string) (string, bool) {
167 p, dir = strings.TrimSuffix(p, "/"), strings.TrimSuffix(dir, "/")
168 if Equal(p, dir) {
169 return ".", true
170 }
171 if dir == "" {
172 return "", false
173 }
174 rest, ok := CutPrefix(p, dir+"/")
175 return rest, ok
176}