跳到主要内容

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

浏览文件

storage_runtime.go

80 行 · 2.8 KB历史下载
1package cage
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "os"
8 "path/filepath"
9)
10
11// StorageTransition identifies one cold move. Runtime state is captured before
12// the journal is published and survives process termination with that journal.
13type StorageTransition struct {
14 ID string `json:"id"`
15 Source string `json:"source"`
16 Destination string `json:"destination"`
17 Backup string `json:"backup"`
18 State json.RawMessage `json:"state,omitempty"`
19}
20
21type StorageRuntimeSnapshot struct {
22 State json.RawMessage
23 Backups []string
24 Excluded []string
25}
26
27// StorageRuntime coordinates an independently registered VM with the shared
28// filesystem transaction. Begin prevents VM starts until Recover completes.
29// Switch runs after the verified copy and before the stable directory changes.
30// Recovery restores the directory first, then resolves the runtime, and only
31// then removes a failed copy. The original data is retained after commit.
32type StorageRuntime interface {
33 Name() string
34 Snapshot(context.Context, string) (StorageRuntimeSnapshot, error)
35 Begin(context.Context, StorageTransition) error
36 Switch(context.Context, StorageTransition) error
37 Recover(context.Context, StorageTransition, bool) error
38}
39
40var ErrStorageRuntimeRecovery = errors.New("环境迁移需要客机管理助手完成恢复,请重新打开原工作台")
41
42// RecoverStorage resolves the durable journal using its owning runtime. Plain
43// filesystem callers leave a native VM transaction intact until this succeeds.
44func RecoverStorage(ctx context.Context, profile string, runtime StorageRuntime) error {
45 if _, err := os.Stat(filepath.Join(profile, pendingFile)); errors.Is(err, os.ErrNotExist) {
46 return nil
47 } else if err != nil {
48 return err
49 }
50 lock, err := openStorageLockWithRuntime(ctx, profile, true, runtime)
51 if err != nil {
52 return err
53 }
54 return lock.Close()
55}
56
57// CopyStorage copies an additional runtime-owned directory into a fresh private
58// directory. It uses the same sparse-file and digest verification as the move.
59func CopyStorage(ctx context.Context, source, target string, excluded []string, report func(int64, int64)) error {
60 for _, path := range excluded {
61 if !filepath.IsAbs(path) || sameStoragePath(path, source) || !StorageContains(source, path) {
62 return errors.New("客机管理文件超出当前存储目录")
63 }
64 }
65 if err := os.Mkdir(target, 0700); err != nil {
66 return err
67 }
68 return copyStorageExcluding(ctx, source, target, excluded, report)
69}
70
71func CopyStorageFile(ctx context.Context, source, target string) error {
72 info, err := os.Lstat(source)
73 if err != nil {
74 return err
75 }
76 if !info.Mode().IsRegular() {
77 return errors.New("环境磁盘需要真实本地文件")
78 }
79 return copyStorageFile(ctx, source, target, info, make([]byte, 1<<20), make([]byte, 1<<20), func(int) {})
80}