| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | |
| 8 | "github.com/sylvinus/agent-vm/internal/cage" |
| 9 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 10 | "github.com/sylvinus/agent-vm/internal/mounts" |
| 11 | "github.com/sylvinus/agent-vm/internal/paths" |
| 12 | ) |
| 13 | |
| 14 | func (s *Server) validateStoragePlacement(directory string) error { |
| 15 | target, err := cage.StoragePhysicalPath(directory) |
| 16 | if err != nil { |
| 17 | return err |
| 18 | } |
| 19 | projects, err := s.projectPaths() |
| 20 | if err != nil { |
| 21 | return err |
| 22 | } |
| 23 | for _, project := range projects { |
| 24 | physical, err := cage.StoragePhysicalPath(project) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | if cage.StoragePathsOverlap(physical, target) { |
| 29 | return errors.New("环境存储目录与已共享的项目重叠,请选择项目以外的专用文件夹") |
| 30 | } |
| 31 | } |
| 32 | return nil |
| 33 | } |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | func privateProjectDirectories(profile string) ([]string, error) { |
| 39 | directories, err := cage.StoragePrivatePaths(profile) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | directories = append(directories, profile) |
| 44 | directories = append(directories, guestos.WindowsPrivateStoragePaths()...) |
| 45 | for _, directory := range directories { |
| 46 | physical, err := cage.StoragePhysicalPath(directory) |
| 47 | if os.IsNotExist(err) { |
| 48 | |
| 49 | continue |
| 50 | } |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | if !paths.Equal(physical, directory) { |
| 55 | directories = append(directories, physical) |
| 56 | } |
| 57 | } |
| 58 | return directories, nil |
| 59 | } |
| 60 | |
| 61 | func validateHostProjectData(root, profile string) error { |
| 62 | home, err := os.UserHomeDir() |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | defaultProfile := filepath.Join(home, ".claude-cage") |
| 67 | |
| 68 | if !paths.Equal(profile, defaultProfile) { |
| 69 | private, err := privateProjectDirectories(defaultProfile) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | for _, dir := range private { |
| 74 | if cage.StoragePathsOverlap(root, dir) { |
| 75 | return errors.New("Cage 配置、环境存储及备份不能共享") |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | refs := mounts.Refs(paths.Host(home), "", paths.Host(defaultProfile)) |
| 80 | if _, unsafe := mounts.UnsafeProject(paths.Host(root), refs); unsafe { |
| 81 | return errors.New("此目录包含或属于凭据、用户资料或版本控制数据,无法共享") |
| 82 | } |
| 83 | return nil |
| 84 | } |