package dirview
import (
"crypto/rand"
"errors"
"fmt"
"io"
"io/fs"
"net/netip"
"os"
"path/filepath"
"strings"
"syscall"
)
// A registry directory joins trees to the server of one endpoint, eg
// ~/.local/state/drvu/127.31.1.1-8080/, with one symlink per lock. A
// joining drvu adds a link; the server sees it through inotify, writes
// the lock and serves the tree. An flock on the directory orders a join
// against the server's start and exit, so no tree joins a dying server.
type registry struct {
dir string
f *os.File
}
func stateDir() (string, error) {
if d := os.Getenv("XDG_STATE_HOME"); filepath.IsAbs(d) {
return d, nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".local", "state"), nil
}
func registryDir(ep netip.AddrPort) (string, error) {
base, err := stateDir()
if err != nil {
return "", err
}
return filepath.Join(base, progName, endpointName(ep)), nil
}
func openRegistry(ep netip.AddrPort) (*registry, error) {
dir, err := registryDir(ep)
if err != nil {
return nil, err
}
if err := os.MkdirAll(dir, 0o700); err != nil {
return nil, err
}
f, err := os.Open(dir)
if err != nil {
return nil, err
}
return ®istry{dir: dir, f: f}, nil
}
func (r *registry) lock() error {
return flock(r.f, syscall.LOCK_EX)
}
func (r *registry) unlock() {
flock(r.f, syscall.LOCK_UN)
}
// close also releases the flock.
func (r *registry) close() {
r.f.Close()
}
func flock(f *os.File, how int) error {
for {
err := syscall.Flock(int(f.Fd()), how)
if !errors.Is(err, syscall.EINTR) {
return err
}
}
}
// linkName turns a directory into one file name for its link: % and /
// become %25 and %2F, cut short to keep within NAME_MAX, and a random
// suffix follows. So every join makes a link of a new name, which the
// server tells from the link it already has without trusting the inode,
// which a new file may reuse. The name only helps a reader of the
// registry; drvu reads the tree from the link's target.
func linkName(dir string) string {
name := strings.NewReplacer("%", "%25", "/", "%2F").Replace(dir)
if len(name) > linkNameMax {
name = name[:linkNameMax]
}
return name + "~" + rand.Text()[:8]
}
// linkNameMax leaves room under NAME_MAX, 255 bytes, for the suffix.
const linkNameMax = 200
// add links lockPath into the registry. Any older link for the same tree
// goes, and the new one takes a new name, so the server can tell a new
// join from the link it already has.
// A shared tree's secret token follows the lock path after a #, which the
// lock name never holds; the registry is private, so the token stays so.
func (r *registry) add(lockPath, token string) (string, os.FileInfo, error) {
links, err := r.links()
if err != nil {
return "", nil, err
}
for link, target := range links {
if old, _ := splitTarget(target); filepath.Dir(old) == filepath.Dir(lockPath) {
if err := os.Remove(link); err != nil && !errors.Is(err, fs.ErrNotExist) {
return "", nil, err
}
}
}
target := lockPath
if token != "" {
target += "#" + token
}
link := filepath.Join(r.dir, linkName(filepath.Dir(lockPath)))
if err := os.Symlink(target, link); err != nil {
return "", nil, err
}
info, err := os.Lstat(link)
if err != nil {
os.Remove(link)
return "", nil, err
}
return link, info, nil
}
// splitTarget parses a link's target into the lock path and the token.
func splitTarget(target string) (lockPath, token string) {
if strings.HasSuffix(target, lockSuffix) || strings.HasSuffix(target, persistSuffix) {
return target, ""
}
i := strings.LastIndex(target, "#")
if i < 0 {
return target, ""
}
return target[:i], target[i+1:]
}
// serves reports whether a live server holds a tree in the registry of ep.
// The caller holds the lock.
func (r *registry) serves(ep netip.AddrPort) bool {
// The server on the default address stays up with no tree, so its
// record alone says it serves.
if _, ok := webEndpoint(ep, os.Getuid()); ok {
if srv, up := serverOf(ep); up && !srv.foreign() {
return true
}
}
links, _ := r.links()
for _, target := range links {
if _, ok := readTree(ep, target); ok {
return true
}
}
return false
}
// links maps every link in the registry to its target, which splitTarget
// parses.
func (r *registry) links() (map[string]string, error) {
return readLinks(r.dir)
}
func readLinks(dir string) (map[string]string, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
m := make(map[string]string, len(entries))
for _, e := range entries {
if e.Type()&fs.ModeSymlink == 0 {
continue
}
link := filepath.Join(dir, e.Name())
if target, err := os.Readlink(link); err == nil {
m[link] = target
}
}
return m, nil
}
// serverFile, in a registry, names the server that runs on its endpoint:
// its PID and boot, as a lock does. A persistent lock holds no PID, so this
// is how to tell whether its tree is served and which process to stop.
const serverFile = "server"
// pagesFile, in a registry, lists the path of each page that held a live
// stream when its server stopped, one per line. --restart creates it empty
// before it stops the server and reads it after, to say which pages
// reload onto the new server and whether a tree needs a browser opened. A
// server stopped any other way finds no file and writes none.
const pagesFile = "pages"
// writePages records the pages open on a stopping server, when a restart
// has asked for them.
func (r *registry) writePages(paths []string) {
f, err := os.OpenFile(filepath.Join(r.dir, pagesFile), os.O_WRONLY|os.O_TRUNC, 0)
if err != nil {
return
}
defer f.Close()
for _, p := range paths {
f.WriteString(p + "\n")
}
}
// binaryFile, in a registry, names the binary of the last server on its
// endpoint. It outlives the server, unlike serverFile, so --restart can
// tell which binary persistent trees wait for, and leaves the servers of
// another install alone.
const binaryFile = "binary"
// baseFile, in a registry, holds the base path of the last server on its
// endpoint, or is missing for a server at the root. It outlives the
// server, as binaryFile does, so --restart serves the trees under the
// same URLs again.
const baseFile = "base"
// baseOf reads the base path the last server on ep had, or "".
func baseOf(ep netip.AddrPort) string {
dir, err := registryDir(ep)
if err != nil {
return ""
}
b, err := os.ReadFile(filepath.Join(dir, baseFile))
if err != nil {
return ""
}
base, err := checkBase(strings.TrimSuffix(string(b), "\n"))
if err != nil {
return ""
}
return base
}
// markServer records this process as the server of the registry, its
// binary and its base path.
func (r *registry) markServer() error {
if exe, err := os.Executable(); err == nil {
if err := os.WriteFile(filepath.Join(r.dir, binaryFile), []byte(exe+"\n"), 0o600); err != nil {
return err
}
}
if basePath == "" {
os.Remove(filepath.Join(r.dir, baseFile))
} else if err := os.WriteFile(filepath.Join(r.dir, baseFile), []byte(basePath+"\n"), 0o600); err != nil {
return err
}
return os.WriteFile(filepath.Join(r.dir, serverFile),
[]byte(fmt.Sprintf("pid: %d\nboot_id: %s\npid_ns: %s\n", os.Getpid(), bootID(), pidNS())), 0o600)
}
// binaryOf reads which binary last served ep, or "" for a registry with no
// record, eg one older than the record.
func binaryOf(ep netip.AddrPort) string {
dir, err := registryDir(ep)
if err != nil {
return ""
}
b, err := os.ReadFile(filepath.Join(dir, binaryFile))
if err != nil {
return ""
}
return strings.TrimSuffix(string(b), "\n")
}
// otherBinary names the binary that last served ep when that is not this
// binary and still exists. A registry with no record, or whose binary is
// gone, belongs to whichever binary restarts it.
func otherBinary(ep netip.AddrPort) string {
b := binaryOf(ep)
exe, err := os.Executable()
if b == "" || err != nil || b == exe {
return ""
}
if _, err := os.Stat(b); err != nil {
return ""
}
return b
}
// unmarkServer removes the record, unless another server has written its
// own. A PID repeats across namespaces, so the record is this server's
// only if it names this namespace too. The binary record stays while
// persistent trees wait for a server, and goes with the last link.
func (r *registry) unmarkServer() {
p := filepath.Join(r.dir, serverFile)
if b, err := os.ReadFile(p); err == nil {
if l := parseLock(b); l.pid == os.Getpid() && l.pidNS == pidNS() {
os.Remove(p)
}
}
if links, err := r.links(); err == nil && len(links) == 0 {
os.Remove(filepath.Join(r.dir, binaryFile))
}
}
// serverOf reads the server record of ep, and reports whether that server
// still runs.
func serverOf(ep netip.AddrPort) (lockInfo, bool) {
dir, err := registryDir(ep)
if err != nil {
return lockInfo{}, false
}
f, err := os.Open(filepath.Join(dir, serverFile))
if err != nil {
return lockInfo{}, false
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return lockInfo{}, false
}
b, err := io.ReadAll(f)
if err != nil {
return lockInfo{}, false
}
l := parseLock(b)
l.since = info.ModTime()
return l, l.running()
}