跳到主要内容

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

浏览文件

0004-664-sequential.patch

240 行 · 7.8 KB历史下载
1diff -ruN a/pkg/reversesshfs/rooted.go b/pkg/reversesshfs/rooted.go
2--- a/pkg/reversesshfs/rooted.go 2026-10-02 20:31:16.091506951 +0200
3+++ b/pkg/reversesshfs/rooted.go 2026-10-02 20:33:59.398020816 +0200
4@@ -56,7 +56,9 @@
5 noopRemovals: make(map[string]time.Time),
6 }
7 handlers := sftp.Handlers{FileGet: h, FilePut: h, FileCmd: h, FileList: h}
8- srv := sftp.NewRequestServer(rwc, handlers, sftp.WithStartDirectory(startDirectory(h.rootPath)))
9+ // Sequential, as OpenSSH's sftp-server: otherwise two writes to the same range, or a write
10+ // and the FSTAT or FSETSTAT sent after it, can run in either order.
11+ srv := sftp.NewRequestServer(rwc, handlers, sftp.WithStartDirectory(startDirectory(h.rootPath)), sftp.WithRSSequential())
12 return srv, h, nil
13 }
14
15@@ -159,14 +161,27 @@
16 return h.root.Open(rel)
17 }
18
19+// writableFile is a file opened by openFile: checked against readonlyNames, without following symlinks.
20+// FSETSTAT applies to it directly. Files opened for reading only may have been opened through a symlink
21+// to a read-only name, so FSETSTAT on them goes through the path checks of setstat.
22+type writableFile struct{ *os.File }
23+
24 // Filewrite implements sftp.FileWriter.
25 func (h *rootedHandlers) Filewrite(r *sftp.Request) (io.WriterAt, error) {
26- return h.openFile(r)
27+ f, err := h.openFile(r)
28+ if err != nil {
29+ return nil, err
30+ }
31+ return writableFile{f}, nil
32 }
33
34 // OpenFile implements sftp.OpenFileWriter.
35 func (h *rootedHandlers) OpenFile(r *sftp.Request) (sftp.WriterAtReaderAt, error) {
36- return h.openFile(r)
37+ f, err := h.openFile(r)
38+ if err != nil {
39+ return nil, err
40+ }
41+ return writableFile{f}, nil
42 }
43
44 // openMode returns the permissions sent with an Open request.
45@@ -184,6 +199,10 @@
46 func (h *rootedHandlers) Filecmd(r *sftp.Request) error {
47 switch r.Method {
48 case "Setstat":
49+ // FSETSTAT: r.Filepath is where the file was opened, which may now be another file.
50+ if f, ok := r.HandleFile().(writableFile); ok {
51+ return h.fsetstat(f.File, r)
52+ }
53 if h.isNoopTimes(r) {
54 return nil
55 }
56@@ -254,6 +273,21 @@
57 }
58 return listerAt(fis), nil
59 case "Stat":
60+ // FSTAT: r.Filepath is where the file was opened, which may now be another file.
61+ var f *os.File
62+ switch hf := r.HandleFile().(type) {
63+ case writableFile:
64+ f = hf.File
65+ case *os.File:
66+ f = hf
67+ }
68+ if f != nil {
69+ fi, err := f.Stat()
70+ if err != nil {
71+ return nil, err
72+ }
73+ return listerAt{fi}, nil
74+ }
75 fi, err := h.root.Stat(rel)
76 if err != nil {
77 return nil, err
78diff -ruN a/pkg/reversesshfs/rooted_test.go b/pkg/reversesshfs/rooted_test.go
79--- a/pkg/reversesshfs/rooted_test.go 2026-10-02 20:31:16.091506951 +0200
80+++ b/pkg/reversesshfs/rooted_test.go 2026-10-02 20:33:59.398020816 +0200
81@@ -233,6 +233,67 @@
82 }
83 }
84
85+// FSTAT and FSETSTAT apply to the open file, not to whatever is now at the path it was opened with.
86+func TestRootedFstatFsetstatAfterRename(t *testing.T) {
87+ c, root := setupRooted(t, false)
88+ p := func(s string) string { return filepath.Join(root, s) }
89+ f, err := c.Create(p("src/a"))
90+ if err != nil {
91+ t.Fatal(err)
92+ }
93+ defer f.Close()
94+ if _, err := f.Write([]byte("hello")); err != nil {
95+ t.Fatal(err)
96+ }
97+ if err := c.PosixRename(p("src/a"), p("src/b")); err != nil {
98+ t.Fatal(err)
99+ }
100+ if err := os.WriteFile(p("src/a"), []byte("other file"), 0o644); err != nil {
101+ t.Fatal(err)
102+ }
103+ if fi, err := f.Stat(); err != nil || fi.Size() != 5 {
104+ t.Fatalf("Stat: %v, %v", fi, err)
105+ }
106+ if err := f.Truncate(2); err != nil {
107+ t.Fatal(err)
108+ }
109+ if err := f.Chmod(0o600); err != nil {
110+ t.Fatal(err)
111+ }
112+ if b, err := os.ReadFile(p("src/b")); err != nil || string(b) != "he" {
113+ t.Fatalf("src/b: %q, %v", b, err)
114+ }
115+ if b, err := os.ReadFile(p("src/a")); err != nil || string(b) != "other file" {
116+ t.Fatalf("src/a: %q, %v", b, err)
117+ }
118+ if runtime.GOOS != "windows" {
119+ if fi, err := os.Stat(p("src/b")); err != nil || fi.Mode().Perm() != 0o600 {
120+ t.Fatalf("src/b: %v, %v", fi, err)
121+ }
122+ if fi, err := os.Stat(p("src/a")); err != nil || fi.Mode().Perm() != 0o644 {
123+ t.Fatalf("src/a: %v, %v", fi, err)
124+ }
125+ }
126+}
127+
128+// A handle opened for reading may point into a read-only name through a symlink:
129+// FSETSTAT on it must keep going through the path checks.
130+func TestRootedFsetstatReadHandle(t *testing.T) {
131+ c, root := setupRooted(t, false)
132+ f, err := c.Open(filepath.Join(root, "configlink"))
133+ if err != nil {
134+ t.Fatal(err)
135+ }
136+ defer f.Close()
137+ if err := f.Truncate(0); err == nil {
138+ t.Fatal("Truncate succeeded")
139+ }
140+ if err := f.Chmod(0o777); err == nil {
141+ t.Fatal("Chmod succeeded")
142+ }
143+ assertUnchanged(t, root)
144+}
145+
146 // sshfs sends its open flags with the permissions alone as attributes.
147 func TestRootedOpenMode(t *testing.T) {
148 if runtime.GOOS == "windows" {
149diff -ruN a/pkg/reversesshfs/rooted_unix.go b/pkg/reversesshfs/rooted_unix.go
150--- a/pkg/reversesshfs/rooted_unix.go 2026-10-02 20:31:16.091506951 +0200
151+++ b/pkg/reversesshfs/rooted_unix.go 2026-10-02 20:33:59.398020816 +0200
152@@ -251,6 +251,41 @@
153 return nil
154 }
155
156+// fsetstat applies a FSETSTAT to the file f of the handle.
157+func (h *rootedHandlers) fsetstat(f *os.File, r *sftp.Request) error {
158+ flags := r.AttrFlags()
159+ attrs := r.Attributes()
160+ if attrs == nil {
161+ return unix.EINVAL
162+ }
163+ fd := int(f.Fd())
164+ if flags.Size {
165+ if err := unix.Ftruncate(fd, int64(attrs.Size)); err != nil {
166+ return &os.PathError{Op: "truncate", Path: f.Name(), Err: err}
167+ }
168+ }
169+ if flags.Permissions {
170+ if err := unix.Fchmod(fd, attrs.Mode&0o7777); err != nil {
171+ return &os.PathError{Op: "chmod", Path: f.Name(), Err: err}
172+ }
173+ }
174+ if flags.UidGid {
175+ if err := unix.Fchown(fd, int(attrs.UID), int(attrs.GID)); err != nil {
176+ return &os.PathError{Op: "chown", Path: f.Name(), Err: err}
177+ }
178+ }
179+ if flags.Acmodtime {
180+ tv := []unix.Timeval{
181+ unix.NsecToTimeval(int64(attrs.Atime) * 1e9),
182+ unix.NsecToTimeval(int64(attrs.Mtime) * 1e9),
183+ }
184+ if err := unix.Futimes(fd, tv); err != nil {
185+ return &os.PathError{Op: "chtimes", Path: f.Name(), Err: err}
186+ }
187+ }
188+ return nil
189+}
190+
191 // StatVFS implements sftp.StatVFSFileCmder.
192 func (h *rootedHandlers) StatVFS(r *sftp.Request) (*sftp.StatVFS, error) {
193 if _, err := h.rel(r.Filepath); err != nil {
194diff -ruN a/pkg/reversesshfs/rooted_windows.go b/pkg/reversesshfs/rooted_windows.go
195--- a/pkg/reversesshfs/rooted_windows.go 2026-10-02 20:31:16.091506951 +0200
196+++ b/pkg/reversesshfs/rooted_windows.go 2026-10-02 20:33:59.398020816 +0200
197@@ -234,7 +234,8 @@
198 access = windows.FILE_GENERIC_READ | windows.FILE_GENERIC_WRITE
199 options = windows.FILE_NON_DIRECTORY_FILE
200 case pf.Write || pf.Append:
201- access = windows.FILE_GENERIC_WRITE
202+ // FILE_READ_ATTRIBUTES: for fsetstat, as FILE_GENERIC_WRITE lacks it.
203+ access = windows.FILE_GENERIC_WRITE | windows.FILE_READ_ATTRIBUTES
204 options = windows.FILE_NON_DIRECTORY_FILE
205 default:
206 access = windows.FILE_GENERIC_READ
207@@ -439,7 +440,6 @@
208
209 func (h *rootedHandlers) setstat(r *sftp.Request) error {
210 flags := r.AttrFlags()
211- attrs := r.Attributes()
212 if flags.UidGid {
213 return &os.PathError{Op: "chown", Path: r.Filepath, Err: syscall.EWINDOWS}
214 }
215@@ -453,6 +453,25 @@
216 }
217 defer windows.CloseHandle(parent)
218 defer windows.CloseHandle(fh)
219+ return setstatHandle(fh, r)
220+}
221+
222+// fsetstat applies a FSETSTAT to the file f of the handle.
223+func (h *rootedHandlers) fsetstat(f *os.File, r *sftp.Request) error {
224+ if r.AttrFlags().UidGid {
225+ return &os.PathError{Op: "chown", Path: f.Name(), Err: syscall.EWINDOWS}
226+ }
227+ return setstatHandle(windows.Handle(f.Fd()), r)
228+}
229+
230+// setstatHandle applies the attributes of r to fh. r.Filepath is only used in errors.
231+
232+func setstatHandle(fh windows.Handle, r *sftp.Request) error {
233+ flags := r.AttrFlags()
234+ attrs := r.Attributes()
235+ if attrs == nil {
236+ return windows.ERROR_INVALID_PARAMETER
237+ }
238 if flags.Size {
239 if err := windows.Ftruncate(fh, int64(attrs.Size)); err != nil {
240 return &os.PathError{Op: "truncate", Path: r.Filepath, Err: err}