aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/mbf/cache.go
blob: adbc84e2dad62272e499a25c5440ed9af7cb5eac (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
package main

import (
	"context"
	"os"
	"path/filepath"
	"runtime"
	"testing"

	"hakurei.app/check"
	"hakurei.app/container"
	"hakurei.app/internal/pkg"
	"hakurei.app/internal/rosa"
	"hakurei.app/message"
)

// cache refers to an instance of [pkg.Cache] that might be open.
type cache struct {
	ctx context.Context
	msg message.Msg

	// Should generally not be used directly.
	c *pkg.Cache

	cures, jobs        int
	hostAbstract, idle bool
	verboseInit        bool
	arch               string

	base string
}

// open opens the underlying [pkg.Cache].
func (cache *cache) open() (err error) {
	if cache.c != nil {
		return os.ErrInvalid
	}

	var base *check.Absolute
	if cache.base, err = filepath.Abs(cache.base); err != nil {
		return
	} else if base, err = check.NewAbs(cache.base); err != nil {
		return
	}

	var flags int
	if cache.idle {
		flags |= pkg.CSchedIdle
	}
	if cache.hostAbstract {
		flags |= pkg.CHostAbstract
	}
	if !cache.verboseInit {
		flags |= pkg.CSuppressInit
	}

	done := make(chan struct{})
	defer close(done)
	go func() {
		select {
		case <-cache.ctx.Done():
			if testing.Testing() {
				return
			}
			os.Exit(2)

		case <-done:
			return
		}
	}()

	cache.msg.Verbosef("opening cache at %s", base)
	cache.c, err = pkg.Open(
		cache.ctx,
		cache.msg,
		flags,
		cache.cures,
		cache.jobs,
		base,
	)
	if err != nil {
		return
	}
	done <- struct{}{}

	if cache.arch != "" && cache.arch != runtime.GOARCH {
		var (
			name   string
			offset byte
			magic  string
			mask   string
		)
		switch cache.arch {
		case "riscv64":
			name = "riscv64"
			offset = 0
			magic = "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00"
			mask = "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"

		default:
			cache.c.Close()
			err = pkg.UnsupportedArchError(cache.arch)
			return
		}

		var pathname *check.Absolute
		pathname, _, err = cache.c.Cure(rosa.Std.Load(rosa.QEMU))
		if err != nil {
			cache.c.Close()
			return
		}

		pkg.RegisterArch(cache.arch, container.BinfmtEntry{
			Offset: offset,
			Magic:  magic,
			Mask:   mask,
			Interpreter: pathname.Append(
				"system/bin",
				"qemu-"+name,
			),
		})
		rosa.DropCaches(cache.arch, rosa.Flags())
	}

	return
}

// Close closes the underlying [pkg.Cache] if it is open.
func (cache *cache) Close() {
	if cache.c != nil {
		cache.c.Close()
	}
}

// Do calls f on the underlying cache and returns its error value.
func (cache *cache) Do(f func(cache *pkg.Cache) error) error {
	if cache.c == nil {
		if err := cache.open(); err != nil {
			return err
		}
	}
	return f(cache.c)
}