mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
internal/safefile (standard library only) owns every read of a file a VM can write or swap: Read and Open never block on a FIFO, follow a final link only when asked, and check the opened file is regular, within its cap and, with OneLink, singly linked; OpenRoot opens beneath a folder. The proxy, secrets, agentlogin, devcontainer, detect, agent save and gitprotect read through it, fixing three hangs on a FIFO the VM leaves, and forbidigo holds those callers to it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
110 lines
3.1 KiB
Go
110 lines
3.1 KiB
Go
package vm
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/devvmhq/devvm/internal/image"
|
|
"github.com/devvmhq/devvm/internal/safefile"
|
|
)
|
|
|
|
// nixMarkers are a Nix project's own files, which call for the nix layer.
|
|
var nixMarkers = []string{"flake.nix", "shell.nix", "devenv.nix"}
|
|
|
|
// marker is a part and the files in a folder's top level that call for it.
|
|
type marker struct {
|
|
part string
|
|
files []string
|
|
}
|
|
|
|
// markers are the files that call for each toolchain (image.SharedTools'
|
|
// Detect), then for the nix layer, in the order detectFiles lists them:
|
|
// the same mix always gives the same image.
|
|
func markers() []marker {
|
|
var ms []marker
|
|
for _, d := range image.SharedTools {
|
|
if len(d.Detect) > 0 {
|
|
ms = append(ms, marker{d.Name, d.Detect})
|
|
}
|
|
}
|
|
return append(ms, marker{image.NixLayer, nixMarkers})
|
|
}
|
|
|
|
// nixShellMax is how much of a default.nix nixShell reads.
|
|
const nixShellMax = 64 << 10
|
|
|
|
// nixShell reports whether dir's default.nix makes a shell (mkShell, or
|
|
// mkShellNoCC), as one for nix-shell does; most build a package instead.
|
|
// It reads the first nixShellMax bytes of a regular file, not a link
|
|
// (safefile's), so a FIFO the VM swaps in can't hold it.
|
|
func nixShell(dir string) bool {
|
|
f, err := safefile.Open(filepath.Join(dir, "default.nix"), safefile.Opts{})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer func() { _ = f.Close() }()
|
|
data, _ := io.ReadAll(io.LimitReader(f, nixShellMax))
|
|
return strings.Contains(string(data), "mkShell")
|
|
}
|
|
|
|
// detected is a toolchain, or the nix layer, that a folder calls for, and
|
|
// the first of its marker files that calls for it.
|
|
type detected struct{ Name, File string }
|
|
|
|
// detectFiles returns the toolchains and the nix layer whose marker files
|
|
// are in dir's top level, in a fixed order, each with the file that calls
|
|
// for it: nix also for a default.nix that makes a shell. A new VM's
|
|
// default parts (defaults) start from them.
|
|
func detectFiles(dir string) []detected {
|
|
var found []detected
|
|
f := folder{dir: dir}
|
|
for _, m := range markers() {
|
|
file := f.first(m.files)
|
|
if file == "" && m.part == image.NixLayer && nixShell(dir) {
|
|
file = "default.nix"
|
|
}
|
|
if file != "" {
|
|
found = append(found, detected{m.part, file})
|
|
}
|
|
}
|
|
return found
|
|
}
|
|
|
|
// folder is a project folder's top level, as detectFiles looks in it:
|
|
// its names are read once, for the first pattern that needs them.
|
|
type folder struct {
|
|
dir string
|
|
names []string
|
|
read bool
|
|
}
|
|
|
|
// first is the first of a marker's files in the folder: a name that's
|
|
// there, or the first name a pattern matches (path.Match's), in name
|
|
// order; "" when none is.
|
|
func (f *folder) first(files []string) string {
|
|
for _, file := range files {
|
|
if !strings.Contains(file, "*") {
|
|
if _, err := os.Lstat(filepath.Join(f.dir, file)); err == nil {
|
|
return file
|
|
}
|
|
continue
|
|
}
|
|
if !f.read {
|
|
entries, _ := os.ReadDir(f.dir) // sorted by name; none when it can't be read
|
|
for _, e := range entries {
|
|
f.names = append(f.names, e.Name())
|
|
}
|
|
f.read = true
|
|
}
|
|
for _, n := range f.names {
|
|
if ok, _ := path.Match(file, n); ok {
|
|
return n
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|