mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 16:27:33 -10:00
macOS numbers its disks' devices in the order they appear at boot, so after a restart an unchanged project folder's identity (dev:ino:btime) can have another device. The finder then refused its VM as made for an earlier folder (vm_other_folder), a stopped VM's start refused it as replaced (project_folder_changed), and the next .git check called it replaced (git_replaced). platform.SameFolder is now the one comparison of two folder identities: the same ID, or at the same path the same inode and birth time on another device. On another device the ID must also be the one read from the folder opened at the path with no link followed (OpenNoLinks, then fstat and fstatfs on it: openFolderID), on a file system whose IDs are their folder's alone, so a link swapped in while the caller read the ID can't pass a copy's (a snapshot's, a cloned disk's) off as the folder's. A folder at another path (moved) matches only exactly. platform.Renumbered says when the record needs the folder's ID as it is now. The finder's sameFolder, here.movedFrom, gitprotect's protectRepo and the strict conversion's checks use it. A start records the project folder's identity as it is now under the VM's state lock (renumbered, in bringUp) before the backend mounts it by Want.ProjectID, so vz's /.vol/<dev>/<ino> mount names the current boot's device, in the one limactl edit; folderChanged's checks after it then compare exactly. A .git check that accepts a renumbered .git records its current identity. The move's check stays exact, as both its IDs are read in one run. The structure test's folder-ids rule fails on == or != on a field named Folder or GitID, or a call of FolderID or folderID, outside internal/platform and tests, unless exactFolderIDs lists the function with why: the agents' and exports' Folder paths, recordMove's unchanged-state check and folderChanged. Tests: TestSameFolder (platform), TestRenumberedFolder (vm), TestReportsRenumbered (gitprotect), a renumbered vz case in TestProjectMountByInode (lima), existing.txtar's renumbered folder, and fixture_v010_renumbered.txtar on the frozen v0.1.0 state, through the new renumber script command and stubtest.Renumbered. Co-Authored-By: Claude Opus 5.5 <[email protected]>
91 lines
3.1 KiB
Go
91 lines
3.1 KiB
Go
package platform
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// FolderID returns what identifies the folder at path itself, not its
|
|
// path: its device, inode and birth time. ext4 gives a folder made again
|
|
// the inode number the deleted one had, so the birth time (where the file
|
|
// system keeps one) tells them apart.
|
|
func FolderID(path string) (string, error) {
|
|
return statxID(unix.AT_FDCWD, path, 0)
|
|
}
|
|
|
|
// openFolderID is the FolderID of the open folder f, and whether it's
|
|
// its alone (UniqueFolderID), both read from f itself, so nothing put at
|
|
// its path since it was opened counts.
|
|
func openFolderID(f *os.File) (string, bool, error) {
|
|
id, err := statxID(int(f.Fd()), "", unix.AT_EMPTY_PATH)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
var fs unix.Statfs_t
|
|
if err := unix.Fstatfs(int(f.Fd()), &fs); err != nil {
|
|
return "", false, err
|
|
}
|
|
return id, strings.Count(id, ":") == 2 && uniqueFS(fs.Type), nil
|
|
}
|
|
|
|
// statxID is the FolderID of what statx finds at dirfd, path and flags.
|
|
func statxID(dirfd int, path string, flags int) (string, error) {
|
|
var st unix.Statx_t
|
|
if err := unix.Statx(dirfd, path, flags, unix.STATX_INO|unix.STATX_BTIME, &st); err != nil {
|
|
return "", err
|
|
}
|
|
id := fmt.Sprintf("%d:%d", unix.Mkdev(st.Dev_major, st.Dev_minor), st.Ino)
|
|
if st.Mask&unix.STATX_BTIME != 0 {
|
|
id += fmt.Sprintf(":%d.%09d", st.Btime.Sec, st.Btime.Nsec)
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
// zfsSuperMagic is OpenZFS's statfs type, which golang.org/x/sys lacks.
|
|
const zfsSuperMagic = 0x2fc12fc1
|
|
|
|
// UniqueFolderID reports whether id, the FolderID of the folder at path,
|
|
// is that folder's alone, never a deleted one's or another file system's:
|
|
// it has the folder's birth time, as an inode number alone is used again,
|
|
// and the folder is on a local file system with stable inode numbers
|
|
// (uniqueFS). FUSE, network shares and FAT may make inode numbers up.
|
|
func UniqueFolderID(path, id string) bool {
|
|
var st unix.Statfs_t
|
|
if strings.Count(id, ":") != 2 || unix.Statfs(path, &st) != nil {
|
|
return false
|
|
}
|
|
return uniqueFS(st.Type)
|
|
}
|
|
|
|
// uniqueFS reports whether a statfs type is a local file system that keeps
|
|
// each inode number for its file's life and reports a birth time through
|
|
// statx: ext4, XFS, Btrfs, tmpfs, OpenZFS, F2FS and bcachefs.
|
|
func uniqueFS(fsType int64) bool {
|
|
switch fsType {
|
|
case unix.EXT4_SUPER_MAGIC, unix.XFS_SUPER_MAGIC, unix.BTRFS_SUPER_MAGIC, unix.TMPFS_MAGIC,
|
|
zfsSuperMagic, unix.F2FS_SUPER_MAGIC, unix.BCACHEFS_SUPER_MAGIC:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// HasImmutableFlag reports whether this system has a user-immutable flag:
|
|
// Linux's immutable attribute needs root.
|
|
const HasImmutableFlag = false
|
|
|
|
// FoldsCase reports whether file names here may match whatever their
|
|
// case: not on Linux's file systems, as devvm's project folders use them.
|
|
const FoldsCase = false
|
|
|
|
// Chflags sets file flags: Linux has no unprivileged immutable flag.
|
|
func Chflags(context.Context, string, string) error { return errors.ErrUnsupported }
|
|
|
|
// Immutable reports whether a path has the user-immutable flag: Linux has
|
|
// none.
|
|
func Immutable(string) bool { return false }
|