aboutsummaryrefslogtreecommitdiffhomepage
path: root/container/path.go
blob: 8d91abd7691e3b5750b272d8f4fee96ef437a33d (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
package container

import (
	"errors"
	"io/fs"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"

	"hakurei.app/fhs"
	"hakurei.app/vfs"
)

const (
	// Nonexistent is a path that cannot exist.
	//
	// This path can never be presented by the kernel if proc is mounted on
	// /proc/. This can only exist if parts of /proc/ is covered, or proc is not
	// mounted at all. Neither configuration is supported by this package.
	Nonexistent = fhs.Proc + "nonexistent"

	// hostPath is the pathname where the host root filesystem is mounted.
	hostPath = fhs.Root + hostDir
	// hostDir is the name of hostPath.
	hostDir = "host"
	// sysrootPath is the pathname where the new root filesystem is mounted.
	sysrootPath = fhs.Root + sysrootDir
	// sysrootDir is the name of sysrootPath.
	sysrootDir = "sysroot"
)

// toSysroot prefixes name with sysrootPath.
func toSysroot(name string) string {
	name = strings.TrimLeftFunc(name, func(r rune) bool { return r == '/' })
	return filepath.Join(sysrootPath, name)
}

// toHost prefixes name with hostPath.
func toHost(name string) string {
	name = strings.TrimLeftFunc(name, func(r rune) bool { return r == '/' })
	return filepath.Join(hostPath, name)
}

// createFile places a file at name.
func createFile(name string, perm, pperm os.FileMode, content []byte) error {
	if err := os.MkdirAll(filepath.Dir(name), pperm); err != nil {
		return err
	}
	f, err := os.OpenFile(name, syscall.O_CREAT|syscall.O_EXCL|syscall.O_WRONLY, perm)
	if err != nil {
		return err
	}
	if content != nil {
		_, err = f.Write(content)
	}
	return errors.Join(f.Close(), err)
}

// ensureFile ensures the existence of a file at name.
func ensureFile(name string, perm, pperm os.FileMode) error {
	fi, err := os.Stat(name)
	if err != nil {
		if !os.IsNotExist(err) {
			return err
		}
		return createFile(name, perm, pperm, nil)
	}

	if mode := fi.Mode(); mode&fs.ModeDir != 0 || mode&fs.ModeSymlink != 0 {
		err = &os.PathError{Op: "ensure", Path: name, Err: syscall.EISDIR}
	}
	return err
}

// newProcPaths returns a populated procPaths.
func newProcPaths(k syscallDispatcher, prefix string) procPaths {
	return procPaths{k, prefix + "/proc", prefix + "/proc/self"}
}

// procPaths describes [fhs.Proc] paths.
type procPaths struct {
	k      syscallDispatcher
	prefix string
	self   string
}

// stdout returns the pathname of the magic symlink representing standard output.
func (p procPaths) stdout() string { return p.self + "/fd/1" }

// fd returns the pathname of the magic symlink representing the specified file.
func (p procPaths) fd(fd int) string { return p.self + "/fd/" + strconv.Itoa(fd) }

// mountinfo returns the pathname of the mountinfo file.
func (p procPaths) mountinfo(f func(d *vfs.MountInfoDecoder) error) error {
	if r, err := p.k.openNew(p.self + "/mountinfo"); err != nil {
		return err
	} else {
		d := vfs.NewMountInfoDecoder(r)
		err0 := f(d)
		if err = r.Close(); err != nil {
			return err
		} else if err = d.Err(); err != nil {
			return err
		}
		return err0
	}
}

// hostProc is a procPaths instance referring to [fhs.Proc] located in hostPath.
var hostProc = newProcPaths(direct{}, hostPath)