跳到主要内容

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

浏览文件

disk.go

111 行 · 2.5 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package store
5
6import (
7 "errors"
8 "fmt"
9 "io/fs"
10 "os"
11 "path/filepath"
12
13 "github.com/lima-vm/go-qcow2reader"
14
15 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
16)
17
18type Disk struct {
19 Name string `json:"name"`
20 Size int64 `json:"size"`
21 Format string `json:"format"`
22 Dir string `json:"dir"`
23 Instance string `json:"instance"`
24 InstanceDir string `json:"instanceDir"`
25 MountPoint string `json:"mountPoint"`
26
27 // if the Disk is in use and the FSType is specified
28 FSType *string `json:"fsType,omitempty"`
29}
30
31func InspectDisk(diskName string, fsType *string) (*Disk, error) {
32 disk := &Disk{
33 Name: diskName,
34 FSType: fsType,
35 }
36
37 diskDir, err := DiskDir(diskName)
38 if err != nil {
39 return nil, err
40 }
41
42 disk.Dir = diskDir
43 dataDisk := filepath.Join(diskDir, filenames.DataDisk)
44 if _, err := os.Stat(dataDisk); err != nil {
45 return nil, err
46 }
47
48 disk.Size, disk.Format, err = inspectDisk(dataDisk)
49 if err != nil {
50 return nil, err
51 }
52
53 instDir, err := os.Readlink(filepath.Join(diskDir, filenames.InUseBy))
54 if err != nil {
55 if !errors.Is(err, fs.ErrNotExist) {
56 return nil, err
57 }
58 } else {
59 disk.Instance = filepath.Base(instDir)
60 disk.InstanceDir = instDir
61 }
62
63 if disk.FSType != nil && *disk.FSType == "swap" {
64 disk.MountPoint = "[SWAP]" // only used for log message
65 } else {
66 disk.MountPoint = fmt.Sprintf("/mnt/lima-%s", diskName)
67 }
68
69 return disk, nil
70}
71
72// inspectDisk attempts to inspect the disk size and format with qcow2reader.
73func inspectDisk(fName string) (size int64, format string, _ error) {
74 f, err := os.Open(fName)
75 if err != nil {
76 return -1, "", err
77 }
78 defer f.Close()
79 img, err := qcow2reader.Open(f)
80 if err != nil {
81 return -1, "", err
82 }
83 sz := img.Size()
84 if sz < 0 {
85 return -1, "", fmt.Errorf("cannot determine size of %#q", fName)
86 }
87
88 return sz, string(img.Type()), nil
89}
90
91func (d *Disk) Lock(instanceDir string) error {
92 inUseBy := filepath.Join(d.Dir, filenames.InUseBy)
93 return os.Symlink(instanceDir, inUseBy)
94}
95
96func (d *Disk) Unlock() error {
97 inUseBy := filepath.Join(d.Dir, filenames.InUseBy)
98 return os.Remove(inUseBy)
99}
100
101func (d *Disk) LockForInstance(instanceDir string) error {
102 if d.Instance != "" {
103 if d.InstanceDir != instanceDir {
104 return fmt.Errorf("in use by instance %#q", d.Instance)
105 }
106 if err := d.Unlock(); err != nil {
107 return fmt.Errorf("failed to unlock for reuse in the same instance: %w", err)
108 }
109 }
110 return d.Lock(instanceDir)
111}