aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/system/link.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-02-16 12:15:26 +0900
committerOphestra <cat@gensokyo.uk>2025-02-16 12:15:26 +0900
commitc667b13a00bd2995751640322a94b81c2f7f4d6d (patch)
tree4d714d4b67e992dd6529c6ebed064af977ecd656 /internal/system/link.go
parent90b86a5531c2683e8348e7766486e684efd92bb9 (diff)
system: separate link Op implementation
This Op would still be useful after replacing the Tmpfiles interface, so isolate it here. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/system/link.go')
-rw-r--r--internal/system/link.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/system/link.go b/internal/system/link.go
new file mode 100644
index 00000000..cbde3a1f
--- /dev/null
+++ b/internal/system/link.go
@@ -0,0 +1,53 @@
+package system
+
+import (
+ "fmt"
+ "os"
+
+ "git.gensokyo.uk/security/fortify/internal/fmsg"
+)
+
+// Link registers an Op that links dst to src.
+func (sys *I) Link(oldname, newname string) *I { return sys.LinkFileType(Process, oldname, newname) }
+
+// LinkFileType registers a file linking Op labelled with type et.
+func (sys *I) LinkFileType(et Enablement, oldname, newname string) *I {
+ sys.lock.Lock()
+ defer sys.lock.Unlock()
+
+ sys.ops = append(sys.ops, &Hardlink{et, newname, oldname})
+
+ return sys
+}
+
+type Hardlink struct {
+ et Enablement
+ dst, src string
+}
+
+func (l *Hardlink) Type() Enablement { return l.et }
+
+func (l *Hardlink) apply(_ *I) error {
+ fmsg.VPrintln("linking ", l)
+ return fmsg.WrapErrorSuffix(os.Link(l.src, l.dst),
+ fmt.Sprintf("cannot link %q:", l.dst))
+}
+
+func (l *Hardlink) revert(_ *I, ec *Criteria) error {
+ if ec.hasType(l) {
+ fmsg.VPrintf("removing hard link %q", l.dst)
+ return fmsg.WrapErrorSuffix(os.Remove(l.dst),
+ fmt.Sprintf("cannot remove hard link %q:", l.dst))
+ } else {
+ fmsg.VPrintf("skipping hard link %q", l.dst)
+ return nil
+ }
+}
+
+func (l *Hardlink) Is(o Op) bool {
+ l0, ok := o.(*Hardlink)
+ return ok && l0 != nil && *l == *l0
+}
+
+func (l *Hardlink) Path() string { return l.src }
+func (l *Hardlink) String() string { return fmt.Sprintf("%q from %q", l.dst, l.src) }