aboutsummaryrefslogtreecommitdiffhomepage
path: root/system/link.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-02-17 19:00:43 +0900
committerOphestra <cat@gensokyo.uk>2025-02-17 19:00:43 +0900
commit90cb01b2748550228d0bc179691a19ebb996fc36 (patch)
tree06a2e17b578312c3f0734bf4c476e3c2eb5f6908 /system/link.go
parentb1e1d5627e6532ec719c761846d7f38fe93fcadd (diff)
system: move out of internal
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'system/link.go')
-rw-r--r--system/link.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/system/link.go b/system/link.go
new file mode 100644
index 00000000..4182d560
--- /dev/null
+++ b/system/link.go
@@ -0,0 +1,47 @@
+package system
+
+import (
+ "fmt"
+ "os"
+)
+
+// 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(sys *I) error {
+ sys.println("linking", l)
+ return sys.wrapErrSuffix(os.Link(l.src, l.dst),
+ fmt.Sprintf("cannot link %q:", l.dst))
+}
+
+func (l *Hardlink) revert(sys *I, ec *Criteria) error {
+ if ec.hasType(l) {
+ sys.printf("removing hard link %q", l.dst)
+ return sys.wrapErrSuffix(os.Remove(l.dst),
+ fmt.Sprintf("cannot remove hard link %q:", l.dst))
+ } else {
+ sys.printf("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) }