aboutsummaryrefslogtreecommitdiffhomepage
path: root/container/initsymlink.go
blob: 09e74ec22da520d4ec94f8f120d995e8240f9769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package container

import (
	"fmt"
	"path/filepath"

	"hakurei.app/check"
)

// Link appends an [Op] that creates a symlink in the container filesystem.
func (f *Ops) Link(target *check.Absolute, linkName string, dereference bool) *Ops {
	*f = append(*f, &SymlinkOp{target, linkName, dereference})
	return f
}

// SymlinkOp optionally dereferences LinkName and creates a symlink at container path Target.
type SymlinkOp struct {
	Target *check.Absolute
	// LinkName is an arbitrary uninterpreted pathname.
	LinkName string

	// Dereference causes LinkName to be dereferenced during early.
	Dereference bool
}

func (l *SymlinkOp) Valid() bool { return l != nil && l.Target != nil && l.LinkName != zeroString }

func (l *SymlinkOp) early(_ *setupState, k syscallDispatcher) error {
	if l.Dereference {
		if !filepath.IsAbs(l.LinkName) {
			return check.AbsoluteError(l.LinkName)
		}
		if name, err := k.readlink(l.LinkName); err != nil {
			return err
		} else {
			l.LinkName = name
		}
	}
	return nil
}

func (l *SymlinkOp) apply(state *setupState, k syscallDispatcher) error {
	target := toSysroot(l.Target.String())
	if err := k.mkdirAll(filepath.Dir(target), state.ParentPerm); err != nil {
		return err
	}
	return k.symlink(l.LinkName, target)
}

func (l *SymlinkOp) late(*setupState, syscallDispatcher) error { return nil }

func (l *SymlinkOp) Is(op Op) bool {
	vl, ok := op.(*SymlinkOp)
	return ok && l.Valid() && vl.Valid() &&
		l.Target.Is(vl.Target) &&
		l.LinkName == vl.LinkName &&
		l.Dereference == vl.Dereference
}
func (*SymlinkOp) prefix() (string, bool) { return "creating", true }
func (l *SymlinkOp) String() string {
	return fmt.Sprintf("symlink on %q linkname %q", l.Target, l.LinkName)
}