aboutsummaryrefslogtreecommitdiffhomepage
path: root/hst/fsbind.go
blob: f94e467b973d89e35b68f56ecd05fe0f82689370 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package hst

import (
	"encoding/gob"
	"strings"

	"hakurei.app/container/bits"
	"hakurei.app/container/check"
	"hakurei.app/container/fhs"
)

func init() { gob.Register(new(FSBind)) }

// FilesystemBind is the type string of a bind mount point.
const FilesystemBind = "bind"

// FSBind represents a host to container bind mount.
type FSBind struct {
	// mount point in container, same as Source if empty
	Target *check.Absolute `json:"dst,omitempty"`
	// host filesystem path to make available to the container
	Source *check.Absolute `json:"src"`
	// do not mount Target read-only
	Write bool `json:"write,omitempty"`
	// do not disable device files on Target, implies Write
	Device bool `json:"dev,omitempty"`
	// create Source as a directory if it does not exist
	Ensure bool `json:"ensure,omitempty"`
	// skip this mount point if Source does not exist
	Optional bool `json:"optional,omitempty"`

	// enable special behaviour:
	// for autoroot, Target must be set to [fhs.AbsRoot];
	// for autoetc, Target must be set to [fhs.AbsEtc]
	Special bool `json:"special,omitempty"`
}

// IsAutoRoot returns whether this FSBind has autoroot behaviour enabled.
func (b *FSBind) IsAutoRoot() bool {
	return b.Valid() && b.Special && b.Target.String() == fhs.Root
}

// IsAutoEtc returns whether this FSBind has autoetc behaviour enabled.
func (b *FSBind) IsAutoEtc() bool {
	return b.Valid() && b.Special && b.Target.String() == fhs.Etc
}

func (b *FSBind) Valid() bool {
	if b == nil || b.Source == nil {
		return false
	}
	if b.Ensure && b.Optional {
		return false
	}
	if b.Special {
		if b.Target == nil {
			return false
		} else {
			switch b.Target.String() {
			case fhs.Root, fhs.Etc:
				break

			default:
				return false
			}
		}
	}
	return true
}

func (b *FSBind) Path() *check.Absolute {
	if !b.Valid() {
		return nil
	}
	if b.Target == nil {
		return b.Source
	}
	return b.Target
}

func (b *FSBind) Host() []*check.Absolute {
	if !b.Valid() {
		return nil
	}
	return []*check.Absolute{b.Source}
}

func (b *FSBind) Apply(z *ApplyState) {
	if !b.Valid() {
		return
	}

	target := b.Target
	if target == nil {
		target = b.Source
	}
	var flags int
	if b.Write {
		flags |= bits.BindWritable
	}
	if b.Device {
		flags |= bits.BindDevice | bits.BindWritable
	}
	if b.Ensure {
		flags |= bits.BindEnsure
	}
	if b.Optional {
		flags |= bits.BindOptional
	}

	switch {
	case b.IsAutoRoot():
		z.Root(b.Source, flags)

	case b.IsAutoEtc():
		z.Etc(b.Source, z.AutoEtcPrefix)

	default:
		z.Bind(b.Source, target, flags)
	}
}

func (b *FSBind) String() string {
	if !b.Valid() {
		return "<invalid>"
	}

	var flagSym string
	if b.Device {
		flagSym = "d"
	} else if b.Write {
		flagSym = "w"
	}

	if b.Special {
		switch {
		case b.IsAutoRoot():
			prefix := "autoroot"
			if flagSym != "" {
				prefix += ":" + flagSym
			}
			if b.Source.String() != fhs.Root {
				return prefix + ":" + b.Source.String()
			}
			return prefix

		case b.IsAutoEtc():
			return "autoetc:" + b.Source.String()
		}
	}

	g := 4 + len(b.Source.String())
	if b.Target != nil {
		g += len(b.Target.String())
	}

	expr := new(strings.Builder)
	expr.Grow(g)
	expr.WriteString(flagSym)

	switch {
	case b.Ensure:
		expr.WriteString("-")

	case b.Optional:
		expr.WriteString("+")

	default:
		expr.WriteString("*")
	}

	expr.WriteString(b.Source.String())
	if b.Target != nil {
		expr.WriteString(":" + b.Target.String())
	}

	return expr.String()
}