Files
w3cjandClaude Opus 5.5 c41ca82f6e fix(project): devvm's own folders are compared by identity; a launch warns of a project in an agent's folder
project.Unsafe's Holds and Inside compared a project folder with the home
folder and devvm's config, data and Lima folders by name alone
(paths.Within, paths.Below), so on a volume that ignores case, as a
Mac's does by default, a project at ~/.Config/devvm/layers passed both
and its VM got devvm's settings and layer scripts read-write. They now
compare by identity too: paths.In, the vm package's isIn moved to its
owner, finds the path or a folder above it to be the same folder as the
other, by device and inode, and paths.Under is In for the path's
folder. An empty folder holds nothing, so an unset one can't match the
working folder. sshagent's mountedBy, which refuses a key in a folder a
VM mounts, compared by name in the same way and uses paths.In now.

Each launch (Launch.Up, beside warnMounted) warns once, in one line,
when the project folder it mounts is inside an agent's folder in the
home folder or holds one (project_agent_folder), naming every such
folder from the agents' registry, with agent_import_project_inside's
detection (overlapsAgent). A --clone VM mounts nothing and gets no
warning.

Tests: TestUnsafeIgnoresCase, TestInAndUnder, TestMountedKeyIgnoresCase,
TestWarnAgentFolders; the case checks skip on a volume that tells
names apart by case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 09:05:20 -07:00

97 lines
3.2 KiB
Go

package paths
import (
"os"
"path/filepath"
"strings"
)
// A path the host checks against a folder is resolved one of three ways,
// each for its own question:
//
// - Physical: every symlink in it, as far as the path exists. For where a
// folder or a file really is, made yet or not: a project folder against
// devvm's own, a key against the folders VMs mount.
// - PhysicalParent: its folder's symlinks, the last element as named. For
// a path that may itself be a symlink devvm mustn't follow, or that the
// VM could make: a path under .git to lock, a key's recorded path, a
// Seatbelt rule.
// - filepath.EvalSymlinks itself, which fails when the path doesn't
// exist. For a path that must: the folder devvm runs in, a link's
// TARGET, mise's folder.
//
// Then Within and Below compare it with a folder that is resolved the
// same way, by name; In and Under by identity too, for a check that keeps
// a VM away from a folder.
// Physical returns path with its symlinks resolved, as far as it exists:
// the part that doesn't is joined, as named, onto the deepest folder that
// does, so a folder devvm hasn't made yet is where it will be.
func Physical(path string) string {
rest := ""
for p := filepath.Clean(path); ; p = filepath.Dir(p) {
if r, err := filepath.EvalSymlinks(p); err == nil {
return filepath.Join(r, rest)
}
if filepath.Dir(p) == p {
return path
}
rest = filepath.Join(filepath.Base(p), rest)
}
}
// PhysicalParent returns path with its folder's symlinks resolved and its
// last element as named, or path cleaned when the folder doesn't exist
// (yet: a VM could make it).
func PhysicalParent(path string) string {
if d, err := filepath.EvalSymlinks(filepath.Dir(path)); err == nil {
return filepath.Join(d, filepath.Base(path))
}
return filepath.Clean(path)
}
// Within reports whether path is dir or inside it: by name, so both are
// clean and resolved the same way. Every path is within /.
func Within(path, dir string) bool {
return path == dir || Below(path, dir)
}
// Below reports whether path is inside dir, and isn't dir itself: by
// name, so both are clean and resolved the same way.
func Below(path, dir string) bool {
return path != dir && strings.HasPrefix(path, strings.TrimSuffix(dir, "/")+"/")
}
// In reports whether path is dir or inside it: by name, as Within, or by
// identity, path or a folder above it being the same folder as dir, so a
// name that differs only in case on a volume that ignores it, such as
// ~/.Config on macOS, is still dir. A check that keeps a VM away from a
// folder uses In, never Within alone. An empty dir holds nothing.
func In(path, dir string) bool {
if dir == "" {
return false
}
if Within(path, dir) {
return true
}
d, err := os.Stat(dir)
if err != nil {
return false
}
for p := filepath.Clean(path); ; p = filepath.Dir(p) {
if fi, err := os.Stat(p); err == nil && os.SameFile(fi, d) {
return true
}
if filepath.Dir(p) == p {
return false
}
}
}
// Under reports whether path is inside dir, and isn't dir itself, as In
// finds it: its folder is In dir.
func Under(path, dir string) bool {
path = filepath.Clean(path)
return filepath.Dir(path) != path && In(filepath.Dir(path), dir)
}