跳到主要内容

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

浏览文件

0002-expect-remove.patch

333 行 · 10.8 KB历史下载
1From 3f1a12816251bee5b7cc5307a092220422c77be1 Mon Sep 17 00:00:00 2001
2From: Sylvain Zimmer <[email protected]>
3Date: Wed, 23 Sep 2026 22:54:09 +0200
4Subject: [PATCH 2/2] reversesshfs: add ExpectRemove
5
6ExpectRemove(hostPath) makes the next Remove or Rmdir of hostPath from
7the guest succeed without touching the host, if it arrives within 5
8seconds. It lets the caller relay a deletion done on the host: the guest
9removes the path in the mount, so the guest kernel emits IN_DELETE,
10while the host keeps the path if it was created again in the meantime.
11
12It returns false when unsupported, i.e. without the rooted server of the
13builtin driver.
14
15Assisted-by: Claude Opus 5.5 (1M context) <[email protected]>
16Signed-off-by: Sylvain Zimmer <[email protected]>
17---
18 pkg/reversesshfs/reversesshfs.go | 17 +++++++
19 pkg/reversesshfs/rooted.go | 41 ++++++++++++++++
20 pkg/reversesshfs/rooted_e2e_test.go | 75 +++++++++++++++++++++++++++++
21 pkg/reversesshfs/rooted_others.go | 2 +
22 pkg/reversesshfs/rooted_test.go | 61 ++++++++++++++++++++++-
23 5 files changed, 195 insertions(+), 1 deletion(-)
24
25diff --git a/pkg/reversesshfs/reversesshfs.go b/pkg/reversesshfs/reversesshfs.go
26index 4fce6e2..d59def7 100644
27--- a/pkg/reversesshfs/reversesshfs.go
28+++ b/pkg/reversesshfs/reversesshfs.go
29@@ -13,6 +13,7 @@ import (
30 "runtime"
31 "strconv"
32 "strings"
33+ "sync/atomic"
34
35 "github.com/lima-vm/sshocker/pkg/ssh"
36 "github.com/lima-vm/sshocker/pkg/util"
37@@ -43,6 +44,21 @@ type ReverseSSHFS struct {
38 // ReadonlyNames makes a path read-only when any of its components is one of these names
39 // (compared case-insensitively). Requires DriverBuiltin on Linux, macOS, or Windows.
40 ReadonlyNames []string
41+
42+ rooted atomic.Pointer[rootedHandlers]
43+}
44+
45+// ExpectRemove makes the next removal of hostPath requested by the remote, within a few seconds,
46+// succeed without touching hostPath. It is used to relay a local deletion to the remote,
47+// by removing the path there, which emits an inotify event on the remote.
48+// It returns false when unsupported, i.e., when not using DriverBuiltin on Linux, macOS, or Windows.
49+func (rsf *ReverseSSHFS) ExpectRemove(hostPath string) bool {
50+ h := rsf.rooted.Load()
51+ if h == nil {
52+ return false
53+ }
54+ h.expectRemove(hostPath)
55+ return true
56 }
57
58 func (rsf *ReverseSSHFS) Prepare() error {
59@@ -196,6 +212,7 @@ func (rsf *ReverseSSHFS) Start() error {
60 WriteCloser: stdinPipe,
61 }
62 builtinSftpServer, rooted, err = newRootedServer(stdio, rsf.LocalPath, rsf.Readonly, rsf.ReadonlyNames)
63+ rsf.rooted.Store(rooted)
64 if err != nil {
65 return err
66 }
67diff --git a/pkg/reversesshfs/rooted.go b/pkg/reversesshfs/rooted.go
68index e4517c9..fb39721 100644
69--- a/pkg/reversesshfs/rooted.go
70+++ b/pkg/reversesshfs/rooted.go
71@@ -8,6 +8,8 @@ import (
72 "os"
73 "path"
74 "strings"
75+ "sync"
76+ "time"
77
78 "github.com/pkg/sftp"
79 )
80@@ -26,8 +28,14 @@ type rootedHandlers struct {
81 readonly bool
82 readonlyNames []string
83 rootedSys
84+
85+ mu sync.Mutex
86+ noopRemovals map[string]time.Time // expiry, keyed by request path
87 }
88
89+// noopRemovalTTL bounds how long an ExpectRemove token waits for the guest.
90+const noopRemovalTTL = 5 * time.Second
91+
92 func newRootedServer(rwc io.ReadWriteCloser, localPath string, readonly bool, readonlyNames []string) (*sftp.RequestServer, *rootedHandlers, error) {
93 root, err := os.OpenRoot(localPath)
94 if err != nil {
95@@ -44,6 +52,7 @@ func newRootedServer(rwc io.ReadWriteCloser, localPath string, readonly bool, re
96 readonly: readonly,
97 readonlyNames: readonlyNames,
98 rootedSys: sys,
99+ noopRemovals: make(map[string]time.Time),
100 }
101 handlers := sftp.Handlers{FileGet: h, FilePut: h, FileCmd: h, FileList: h}
102 srv := sftp.NewRequestServer(rwc, handlers, sftp.WithStartDirectory(startDirectory(h.rootPath)))
103@@ -54,6 +63,33 @@ func (h *rootedHandlers) Close() error {
104 return errors.Join(h.root.Close(), h.rootedSys.close())
105 }
106
107+// expectRemove makes the next Remove or Rmdir request for p, within noopRemovalTTL,
108+// succeed without touching the host. p is a host path under the root.
109+func (h *rootedHandlers) expectRemove(p string) {
110+ p = slashPath(p)
111+ now := time.Now()
112+ h.mu.Lock()
113+ defer h.mu.Unlock()
114+ for k, expiry := range h.noopRemovals {
115+ if now.After(expiry) {
116+ delete(h.noopRemovals, k)
117+ }
118+ }
119+ h.noopRemovals[p] = now.Add(noopRemovalTTL)
120+}
121+
122+func (h *rootedHandlers) consumeNoopRemoval(p string) bool {
123+ p = path.Clean(p)
124+ h.mu.Lock()
125+ defer h.mu.Unlock()
126+ expiry, ok := h.noopRemovals[p]
127+ if !ok {
128+ return false
129+ }
130+ delete(h.noopRemovals, p)
131+ return time.Now().Before(expiry)
132+}
133+
134 // rel maps a request path to a path relative to the root.
135 // The result has no "." or ".." component, except "." for the root itself.
136 func (h *rootedHandlers) rel(p string) (string, error) {
137@@ -145,6 +181,11 @@ func (h *rootedHandlers) Filecmd(r *sftp.Request) error {
138 case "Link":
139 return h.link(r)
140 case "Remove", "Rmdir":
141+ // The guest agent removes a path deleted on the host, so that the guest emits IN_DELETE.
142+ // The path may have been created again on the host since, so it must not be removed.
143+ if h.consumeNoopRemoval(r.Filepath) {
144+ return nil
145+ }
146 return h.remove(r)
147 case "Mkdir":
148 return h.mkdir(r)
149diff --git a/pkg/reversesshfs/rooted_e2e_test.go b/pkg/reversesshfs/rooted_e2e_test.go
150index d572646..5f6d8a6 100644
151--- a/pkg/reversesshfs/rooted_e2e_test.go
152+++ b/pkg/reversesshfs/rooted_e2e_test.go
153@@ -146,6 +146,81 @@ func TestRootedSSHFS(t *testing.T) {
154 t.Errorf("%s: no IN_ATTRIB event in the mount", f)
155 }
156 }
157+
158+ // Relay a host deletion: the hostagent calls ExpectRemove, then the guest agent removes the path.
159+ for _, tc := range []struct {
160+ name string
161+ recreate bool
162+ guestSeen bool
163+ guestListed bool
164+ delay time.Duration
165+ }{
166+ {name: "src/gone.txt", guestSeen: true},
167+ {name: "src/listed.txt", guestListed: true},
168+ {name: ".git/index.lock", guestSeen: true},
169+ {name: "src/later.txt", guestSeen: true, delay: 2 * time.Second},
170+ {name: "src/again.txt", guestSeen: true, recreate: true},
171+ {name: "src/unseen.txt"},
172+ } {
173+ hostPath := filepath.Join(root, tc.name)
174+ if err := os.WriteFile(hostPath, []byte("old"), 0o644); err != nil {
175+ t.Fatal(err)
176+ }
177+ if tc.guestSeen {
178+ if _, err := os.Stat(filepath.Join(mnt, tc.name)); err != nil {
179+ t.Fatal(err)
180+ }
181+ }
182+ if tc.guestListed {
183+ if _, err := os.ReadDir(filepath.Dir(filepath.Join(mnt, tc.name))); err != nil {
184+ t.Fatal(err)
185+ }
186+ }
187+ if err := os.Remove(hostPath); err != nil {
188+ t.Fatal(err)
189+ }
190+ if tc.recreate {
191+ if err := os.WriteFile(hostPath, []byte("new"), 0o644); err != nil {
192+ t.Fatal(err)
193+ }
194+ }
195+ time.Sleep(tc.delay)
196+ h.expectRemove(hostPath)
197+ got := gotDeleteEvent(t, filepath.Join(mnt, tc.name))
198+ t.Logf("%s (seen: %v, listed: %v, delay: %v): IN_DELETE: %v", tc.name, tc.guestSeen, tc.guestListed, tc.delay, got)
199+ if tc.guestSeen && !got {
200+ t.Errorf("%s: no IN_DELETE event in the mount", tc.name)
201+ }
202+ if tc.recreate {
203+ if b, err := os.ReadFile(hostPath); err != nil || string(b) != "new" {
204+ t.Errorf("%s: the recreated file was modified: %q, %v", tc.name, b, err)
205+ }
206+ }
207+ }
208+}
209+
210+func gotDeleteEvent(t *testing.T, p string) bool {
211+ t.Helper()
212+ fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK)
213+ if err != nil {
214+ t.Fatal(err)
215+ }
216+ defer unix.Close(fd)
217+ if _, err := unix.InotifyAddWatch(fd, filepath.Dir(p), unix.IN_DELETE); err != nil {
218+ t.Fatal(err)
219+ }
220+ if err := os.Remove(p); err != nil {
221+ t.Logf("remove %s: %v", p, err)
222+ return false
223+ }
224+ buf := make([]byte, 4096)
225+ for range 20 {
226+ if n, err := unix.Read(fd, buf); err == nil && n > 0 {
227+ return true
228+ }
229+ time.Sleep(50 * time.Millisecond)
230+ }
231+ return false
232 }
233
234 func gotAttribEvent(t *testing.T, p string, mtime time.Time) bool {
235diff --git a/pkg/reversesshfs/rooted_others.go b/pkg/reversesshfs/rooted_others.go
236index a2dc295..5669be5 100644
237--- a/pkg/reversesshfs/rooted_others.go
238+++ b/pkg/reversesshfs/rooted_others.go
239@@ -16,3 +16,5 @@ func newRootedServer(io.ReadWriteCloser, string, bool, []string) (*sftp.RequestS
240 }
241
242 func (*rootedHandlers) Close() error { return nil }
243+
244+func (*rootedHandlers) expectRemove(string) {}
245diff --git a/pkg/reversesshfs/rooted_test.go b/pkg/reversesshfs/rooted_test.go
246index 124100a..bfd630e 100644
247--- a/pkg/reversesshfs/rooted_test.go
248+++ b/pkg/reversesshfs/rooted_test.go
249@@ -25,6 +25,12 @@ var (
250 // that plays the role of a compromised guest sending arbitrary requests.
251 // It also creates the symlinks "gitlink" -> ".git" and "configlink" -> ".git/config".
252 func setupRooted(t *testing.T, readonly bool) (*sftp.Client, string) {
253+ t.Helper()
254+ c, root, _ := setupRootedHandlers(t, readonly)
255+ return c, root
256+}
257+
258+func setupRootedHandlers(t *testing.T, readonly bool) (*sftp.Client, string, *rootedHandlers) {
259 t.Helper()
260 tmp := t.TempDir()
261 root := filepath.Join(tmp, "root")
262@@ -65,7 +71,7 @@ func setupRooted(t *testing.T, readonly bool) (*sftp.Client, string) {
263 client.Close()
264 <-done
265 })
266- return client, root
267+ return client, root, h
268 }
269
270 func assertUnchanged(t *testing.T, root string) {
271@@ -301,6 +307,59 @@ func TestRootedNoopTimes(t *testing.T) {
272 assertUnchanged(t, root)
273 }
274
275+// TestRootedNoopRemoval checks the removal used to relay host deletions.
276+func TestRootedNoopRemoval(t *testing.T) {
277+ c, root, h := setupRootedHandlers(t, false)
278+ p := func(s string) string { return filepath.Join(root, s) }
279+
280+ // The path was created again on the host before the guest removed it.
281+ h.expectRemove(p("src/main.go"))
282+ if err := c.Remove(p("src/main.go")); err != nil {
283+ t.Fatal(err)
284+ }
285+ if _, err := os.Stat(p("src/main.go")); err != nil {
286+ t.Fatalf("the file was removed: %v", err)
287+ }
288+ // The token is consumed.
289+ if err := c.Remove(p("src/main.go")); err != nil {
290+ t.Fatal(err)
291+ }
292+ if _, err := os.Stat(p("src/main.go")); err == nil {
293+ t.Fatal("the file was not removed")
294+ }
295+
296+ h.expectRemove(p("src"))
297+ if err := c.RemoveDirectory(p("src")); err != nil {
298+ t.Fatal(err)
299+ }
300+ if _, err := os.Stat(p("src")); err != nil {
301+ t.Fatalf("the directory was removed: %v", err)
302+ }
303+
304+ // A git lock file deleted on the host.
305+ h.expectRemove(p(".git/index.lock"))
306+ if err := c.Remove(p(".git/index.lock")); err != nil {
307+ t.Fatal(err)
308+ }
309+ if err := c.Remove(p(".git/index.lock")); err == nil {
310+ t.Fatal("expected an error")
311+ }
312+ h.expectRemove(p(".git/config"))
313+ if err := c.Remove(p(".git/config")); err != nil {
314+ t.Fatal(err)
315+ }
316+
317+ // An expired token.
318+ h.expectRemove(p(".git/config"))
319+ h.mu.Lock()
320+ h.noopRemovals[slashPath(p(".git/config"))] = time.Now().Add(-time.Second)
321+ h.mu.Unlock()
322+ if err := c.Remove(p(".git/config")); err == nil {
323+ t.Fatal("expected an error")
324+ }
325+ assertUnchanged(t, root)
326+}
327+
328 func TestRootedReadonly(t *testing.T) {
329 c, root := setupRooted(t, true)
330 if _, err := c.Create(filepath.Join(root, "src", "new.go")); err == nil {
331--
3322.47.3
333