paths: config lookup -config → $VCOM_CONFIG → XDG → ~/.config (never the working directory; StrayCwdConfig only reports one), state/cache/data dirs of the effective user under sudo (inherited XDG_* ignored for writes), run-<pid> dir with a sweep of dead processes' dirs (U-P1-15). atomicfile: temp + fsync + rename + dir fsync; keeps the old file on any error, writes through symlinks and keeps mode/owner of an existing file. The trash now takes its home from paths.DataHome. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
313 lines
8.3 KiB
Go
313 lines
8.3 KiB
Go
// Package paths decides where vcom reads and writes its files (design §9.1).
|
|
//
|
|
// - Config: -config PATH → $VCOM_CONFIG → $XDG_CONFIG_HOME/vcom/vcom.toml →
|
|
// ~/.config/vcom/vcom.toml. Never ./vcom.toml from the working directory.
|
|
// - State (session.toml, vcom.log): $XDG_STATE_HOME/vcom.
|
|
// - Cache and per-process temp files: $XDG_CACHE_HOME/vcom/run-<pid> (0700);
|
|
// run dirs of dead processes are swept on start (U-P1-15).
|
|
// - sudo: config and ~/.ssh are READ from the invoking user's home; state,
|
|
// cache, trash and known_hosts are WRITTEN only under the effective user's
|
|
// home, and inherited XDG_*_HOME variables are ignored for writing, so root
|
|
// never leaves files in ~user.
|
|
package paths
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
// system is the process environment; tests replace it.
|
|
type system struct {
|
|
getenv func(string) string
|
|
geteuid func() int
|
|
getpid func() int
|
|
getwd func() (string, error)
|
|
homeOfUser func(name string) (string, error) // home of a user name
|
|
homeOfUID func(uid int) (string, error)
|
|
alive func(pid int) bool
|
|
}
|
|
|
|
var sys = osSystem()
|
|
|
|
func osSystem() system {
|
|
return system{
|
|
getenv: os.Getenv,
|
|
geteuid: os.Geteuid,
|
|
getpid: os.Getpid,
|
|
getwd: os.Getwd,
|
|
homeOfUser: func(name string) (string, error) {
|
|
u, err := user.Lookup(name)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return u.HomeDir, nil
|
|
},
|
|
homeOfUID: func(uid int) (string, error) {
|
|
u, err := user.LookupId(strconv.Itoa(uid))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return u.HomeDir, nil
|
|
},
|
|
alive: pidAlive,
|
|
}
|
|
}
|
|
|
|
// pidAlive: signal 0 succeeds or fails with EPERM for a live process.
|
|
func pidAlive(pid int) bool {
|
|
err := syscall.Kill(pid, 0)
|
|
return err == nil || errors.Is(err, syscall.EPERM)
|
|
}
|
|
|
|
// SudoUser returns the invoking user's name when running as root via sudo.
|
|
func SudoUser() (string, bool) {
|
|
if sys.geteuid() != 0 {
|
|
return "", false
|
|
}
|
|
u := sys.getenv("SUDO_USER")
|
|
if u == "" || u == "root" {
|
|
return "", false
|
|
}
|
|
return u, true
|
|
}
|
|
|
|
func envHome() (string, error) {
|
|
if h := sys.getenv("HOME"); h != "" {
|
|
return h, nil
|
|
}
|
|
if h, err := sys.homeOfUID(sys.geteuid()); err == nil && h != "" {
|
|
return h, nil
|
|
}
|
|
return "", errors.New("cannot determine home directory")
|
|
}
|
|
|
|
// ReadHome is the home to read user configuration from (vcom.toml, ~/.ssh):
|
|
// the invoking user's home under sudo, $HOME otherwise.
|
|
func ReadHome() (string, error) {
|
|
if name, ok := SudoUser(); ok {
|
|
if h, err := sys.homeOfUser(name); err == nil && h != "" {
|
|
return h, nil
|
|
}
|
|
}
|
|
return envHome()
|
|
}
|
|
|
|
// WriteHome is the home vcom may write under: the effective user's.
|
|
func WriteHome() (string, error) {
|
|
if _, ok := SudoUser(); ok {
|
|
h, err := sys.homeOfUID(sys.geteuid())
|
|
if err != nil {
|
|
return "", fmt.Errorf("home of uid %d: %w", sys.geteuid(), err)
|
|
}
|
|
return h, nil
|
|
}
|
|
return envHome()
|
|
}
|
|
|
|
// xdgWrite returns $env (if absolute and not under sudo) or WriteHome/def.
|
|
func xdgWrite(env string, def ...string) (string, error) {
|
|
if _, sudo := SudoUser(); !sudo {
|
|
if d := sys.getenv(env); filepath.IsAbs(d) {
|
|
return d, nil
|
|
}
|
|
}
|
|
h, err := WriteHome()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(append([]string{h}, def...)...), nil
|
|
}
|
|
|
|
// configHome is the directory holding vcom/ for reading.
|
|
func configHome() (string, error) {
|
|
if d := sys.getenv("XDG_CONFIG_HOME"); filepath.IsAbs(d) {
|
|
return d, nil
|
|
}
|
|
h, err := ReadHome()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(h, ".config"), nil
|
|
}
|
|
|
|
// ConfigFile resolves the config path. explicit is the -config flag. found
|
|
// reports whether the file exists; when it does not, path is where SetKey
|
|
// would create it (DefaultConfigFile), or explicit/$VCOM_CONFIG if given.
|
|
// The working directory is never consulted (see StrayCwdConfig).
|
|
func ConfigFile(explicit string) (path string, found bool, err error) {
|
|
for _, p := range []string{explicit, sys.getenv("VCOM_CONFIG")} {
|
|
if p == "" {
|
|
continue
|
|
}
|
|
abs, err := filepath.Abs(p)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
ok, err := exists(abs)
|
|
return abs, ok, err
|
|
}
|
|
var candidates []string
|
|
if d, err := configHome(); err == nil {
|
|
candidates = append(candidates, filepath.Join(d, "vcom", "vcom.toml"))
|
|
}
|
|
if h, err := ReadHome(); err == nil {
|
|
candidates = append(candidates, filepath.Join(h, ".config", "vcom", "vcom.toml"))
|
|
}
|
|
if _, sudo := SudoUser(); sudo {
|
|
if h, err := WriteHome(); err == nil {
|
|
candidates = append(candidates, filepath.Join(h, ".config", "vcom", "vcom.toml"))
|
|
}
|
|
}
|
|
for _, c := range candidates {
|
|
ok, err := exists(c)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
if ok {
|
|
return c, true, nil
|
|
}
|
|
}
|
|
def, err := DefaultConfigFile()
|
|
return def, false, err
|
|
}
|
|
|
|
// DefaultConfigFile is where a new config file is created. Under sudo it is
|
|
// the effective user's, so root does not create files in ~user.
|
|
func DefaultConfigFile() (string, error) {
|
|
if _, sudo := SudoUser(); sudo {
|
|
d, err := xdgWrite("XDG_CONFIG_HOME", ".config")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(d, "vcom", "vcom.toml"), nil
|
|
}
|
|
d, err := configHome()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(d, "vcom", "vcom.toml"), nil
|
|
}
|
|
|
|
// StrayCwdConfig returns ./vcom.toml (or ./config/vcom.toml) when it exists:
|
|
// older versions loaded it; now it is only worth a hint in the log when no
|
|
// real config was found.
|
|
func StrayCwdConfig() (string, bool) {
|
|
wd, err := sys.getwd()
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
for _, c := range []string{filepath.Join(wd, "vcom.toml"), filepath.Join(wd, "config", "vcom.toml")} {
|
|
if ok, _ := exists(c); ok {
|
|
return c, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// OldSessionFile is the pre-0.3 session location (next to the config), read
|
|
// once for migration.
|
|
func OldSessionFile() (string, error) {
|
|
d, err := configHome()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(d, "vcom", "session.toml"), nil
|
|
}
|
|
|
|
// StateDir is $XDG_STATE_HOME/vcom (effective user). It is not created.
|
|
func StateDir() (string, error) {
|
|
d, err := xdgWrite("XDG_STATE_HOME", ".local", "state")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(d, "vcom"), nil
|
|
}
|
|
|
|
// CacheDir is $XDG_CACHE_HOME/vcom (effective user). It is not created.
|
|
func CacheDir() (string, error) {
|
|
d, err := xdgWrite("XDG_CACHE_HOME", ".cache")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(d, "vcom"), nil
|
|
}
|
|
|
|
// DataHome is $XDG_DATA_HOME (effective user), e.g. for the trash.
|
|
func DataHome() (string, error) { return xdgWrite("XDG_DATA_HOME", ".local", "share") }
|
|
|
|
// EnsureDir creates d and missing parents with mode 0700.
|
|
func EnsureDir(d string) error { return os.MkdirAll(d, 0o700) }
|
|
|
|
// CreateRunDir sweeps run dirs of dead processes from CacheDir and creates
|
|
// CacheDir/run-<pid> (0700) for this process. The caller removes it on exit
|
|
// (RemoveRunDir).
|
|
func CreateRunDir() (string, error) {
|
|
cache, err := CacheDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err := EnsureDir(cache); err != nil {
|
|
return "", err
|
|
}
|
|
SweepRunDirs(cache)
|
|
d := filepath.Join(cache, "run-"+strconv.Itoa(sys.getpid()))
|
|
if err := os.Mkdir(d, 0o700); err != nil && !errors.Is(err, fs.ErrExist) {
|
|
return "", err
|
|
}
|
|
if err := os.Chmod(d, 0o700); err != nil {
|
|
return "", err
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
// SweepRunDirs removes cache/run-<pid> directories whose process is gone
|
|
// (left behind by kill -9 or a crash). It returns what it removed.
|
|
func SweepRunDirs(cache string) []string {
|
|
ents, err := os.ReadDir(cache)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var removed []string
|
|
self := sys.getpid()
|
|
for _, e := range ents {
|
|
rest, ok := strings.CutPrefix(e.Name(), "run-")
|
|
if !ok || !e.IsDir() {
|
|
continue
|
|
}
|
|
pid, err := strconv.Atoi(rest)
|
|
if err != nil || pid <= 0 || pid == self || sys.alive(pid) {
|
|
continue
|
|
}
|
|
p := filepath.Join(cache, e.Name())
|
|
if os.RemoveAll(p) == nil {
|
|
removed = append(removed, p)
|
|
}
|
|
}
|
|
return removed
|
|
}
|
|
|
|
// RemoveRunDir deletes this process's run dir (teardown).
|
|
func RemoveRunDir(d string) error {
|
|
if d == "" || !strings.HasPrefix(filepath.Base(d), "run-") {
|
|
return fmt.Errorf("refusing to remove %q: not a run dir", d)
|
|
}
|
|
return os.RemoveAll(d)
|
|
}
|
|
|
|
func exists(p string) (bool, error) {
|
|
_, err := os.Stat(p)
|
|
if err == nil {
|
|
return true, nil
|
|
}
|
|
if errors.Is(err, fs.ErrNotExist) || errors.Is(err, syscall.ENOTDIR) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|