hakurei/system/link.go
Ophestra 024d2ff782
All checks were successful
Test / Create distribution (push) Successful in 35s
Test / Sandbox (push) Successful in 2m5s
Test / Hakurei (push) Successful in 3m20s
Test / Hpkg (push) Successful in 3m57s
Test / Sandbox (race detector) (push) Successful in 4m41s
Test / Hakurei (race detector) (push) Successful in 5m25s
Test / Flake checks (push) Successful in 1m39s
system: improve tests of the I struct
This cleans up for the test overhaul of this package.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2025-09-03 02:16:10 +09:00

47 lines
1.2 KiB
Go

package system
import (
"fmt"
"os"
)
// Link appends [HardlinkOp] to [I] the [Process] criteria.
func (sys *I) Link(oldname, newname string) *I { return sys.LinkFileType(Process, oldname, newname) }
// LinkFileType appends [HardlinkOp] to [I].
func (sys *I) LinkFileType(et Enablement, oldname, newname string) *I {
sys.ops = append(sys.ops, &HardlinkOp{et, newname, oldname})
return sys
}
// HardlinkOp maintains a hardlink until its [Enablement] is no longer satisfied.
type HardlinkOp struct {
et Enablement
dst, src string
}
func (l *HardlinkOp) Type() Enablement { return l.et }
func (l *HardlinkOp) apply(*I) error {
msg.Verbose("linking", l)
return newOpError("hardlink", os.Link(l.src, l.dst), false)
}
func (l *HardlinkOp) revert(_ *I, ec *Criteria) error {
if ec.hasType(l.Type()) {
msg.Verbosef("removing hard link %q", l.dst)
return newOpError("hardlink", os.Remove(l.dst), true)
} else {
msg.Verbosef("skipping hard link %q", l.dst)
return nil
}
}
func (l *HardlinkOp) Is(o Op) bool {
target, ok := o.(*HardlinkOp)
return ok && l != nil && target != nil && *l == *target
}
func (l *HardlinkOp) Path() string { return l.src }
func (l *HardlinkOp) String() string { return fmt.Sprintf("%q from %q", l.dst, l.src) }