| 1 | package mounts |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | |
| 11 | vm "github.com/sylvinus/agent-vm/internal/mounts/spec" |
| 12 | "github.com/sylvinus/agent-vm/internal/paths" |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | type FileMount struct { |
| 19 | Src, Staging, BindSrc, BindDst string |
| 20 | } |
| 21 | |
| 22 | |
| 23 | func (f FileMount) Line() string { |
| 24 | return strings.Join([]string{f.Src, f.Staging, f.BindSrc, f.BindDst}, "|") |
| 25 | } |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | func CacheOn() bool { return os.Getenv("AGENT_VM_SSHFS_CACHE") == "1" } |
| 33 | |
| 34 | |
| 35 | type Shares struct { |
| 36 | VM string |
| 37 | Dir string |
| 38 | Writable bool |
| 39 | |
| 40 | |
| 41 | |
| 42 | Names []string |
| 43 | Entries []Entry |
| 44 | StateDir string |
| 45 | Warn io.Writer |
| 46 | } |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | func Build(s Shares) ([]vm.Mount, []FileMount) { |
| 52 | w := func(format string, a ...any) { fmt.Fprintf(s.Warn, format+"\n", a...) } |
| 53 | opts := func(writable bool) *vm.SSHFS { |
| 54 | o := &vm.SSHFS{SFTPDriver: "builtin", ReadonlyNames: append([]string(nil), s.Names...)} |
| 55 | if writable && !CacheOn() { |
| 56 | off := false |
| 57 | o.Cache = &off |
| 58 | } |
| 59 | return o |
| 60 | } |
| 61 | out := []vm.Mount{{Location: s.Dir, MountPoint: paths.Guest(s.Dir), Writable: s.Writable, SSHFS: opts(s.Writable)}} |
| 62 | var files []FileMount |
| 63 | idx := 0 |
| 64 | for _, e := range s.Entries { |
| 65 | dst := e.Dst |
| 66 | if dst != "" && !strings.HasPrefix(dst, "/") { |
| 67 | p, ok := ProjectMountpoint(s.Dir, dst, e.Src, s.Warn) |
| 68 | if !ok { |
| 69 | continue |
| 70 | } |
| 71 | dst = paths.Guest(p) |
| 72 | } |
| 73 | if isFile(e.Src) { |
| 74 | if e.Mode == "rw" { |
| 75 | w("Warning: Mount entry '%s' (from ~/.agent-vm/volumes) requests rw on a file; only directories support rw. Mount the parent directory instead. Skipping.", e.Line) |
| 76 | continue |
| 77 | } |
| 78 | dir := paths.Host(filepath.Join(s.StateDir, "file-mounts", s.VM, strconv.Itoa(idx))) |
| 79 | staged := dir + "/" + filepath.Base(e.Src) |
| 80 | if !Stage(e.Src, staged, s.Warn) { |
| 81 | w("Warning: Failed to stage '%s', skipping.", e.Src) |
| 82 | os.RemoveAll(dir) |
| 83 | continue |
| 84 | } |
| 85 | mp := "/tmp/.agent-vm-file-mounts/" + strconv.Itoa(idx) |
| 86 | bind := dst |
| 87 | if bind == "" { |
| 88 | bind = paths.Guest(e.Src) |
| 89 | } |
| 90 | files = append(files, FileMount{Src: e.Src, Staging: staged, BindSrc: mp + "/" + filepath.Base(e.Src), BindDst: bind}) |
| 91 | out = append(out, vm.Mount{Location: dir, MountPoint: mp, SSHFS: opts(false)}) |
| 92 | idx++ |
| 93 | continue |
| 94 | } |
| 95 | if !isDir(e.Src) { |
| 96 | w("Warning: Mount path '%s' (from ~/.agent-vm/volumes) is not a regular file or directory, skipping.", e.Src) |
| 97 | continue |
| 98 | } |
| 99 | writable := false |
| 100 | if e.Mode == "rw" { |
| 101 | if s.Writable { |
| 102 | writable = true |
| 103 | } else { |
| 104 | w("Note: --readonly: '%s' (rw in ~/.agent-vm/volumes) is mounted read-only too.", e.Src) |
| 105 | } |
| 106 | } |
| 107 | mp := dst |
| 108 | if mp == "" { |
| 109 | mp = paths.Guest(e.Src) |
| 110 | } |
| 111 | out = append(out, vm.Mount{Location: e.Src, MountPoint: mp, Writable: writable, SSHFS: opts(writable)}) |
| 112 | } |
| 113 | return out, files |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | func Stage(src, dst string, warn io.Writer) bool { |
| 120 | if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { |
| 121 | return false |
| 122 | } |
| 123 | os.Remove(dst) |
| 124 | |
| 125 | |
| 126 | |
| 127 | physical, err := paths.Resolve(src) |
| 128 | if err != nil { |
| 129 | return false |
| 130 | } |
| 131 | src = physical |
| 132 | if os.Link(src, dst) == nil { |
| 133 | return true |
| 134 | } |
| 135 | in, err := os.Open(src) |
| 136 | if err != nil { |
| 137 | return false |
| 138 | } |
| 139 | defer in.Close() |
| 140 | fi, err := in.Stat() |
| 141 | if err != nil { |
| 142 | return false |
| 143 | } |
| 144 | out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode().Perm()) |
| 145 | if err != nil { |
| 146 | return false |
| 147 | } |
| 148 | _, cerr := io.Copy(out, in) |
| 149 | if err := out.Close(); cerr != nil || err != nil { |
| 150 | return false |
| 151 | } |
| 152 | os.Chtimes(dst, fi.ModTime(), fi.ModTime()) |
| 153 | fmt.Fprintf(warn, "Warning: Staged '%s' via copy (cross-filesystem hardlink failed); live host changes will not propagate until VM (re)start.\n", src) |
| 154 | return true |
| 155 | } |