-
Notifications
You must be signed in to change notification settings - Fork 64
/
directory.go
101 lines (83 loc) · 2.39 KB
/
directory.go
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package initrd
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cavaliergopher/cpio"
)
type directory struct {
opts InitrdOptions
path string
files []string
}
// NewFromDirectory returns an instantiated Initrd interface which is is able to
// serialize a rootfs from a given directory.
func NewFromDirectory(_ context.Context, path string, opts ...InitrdOption) (Initrd, error) {
path = strings.TrimRight(path, string(filepath.Separator))
rootfs := directory{
opts: InitrdOptions{},
path: path,
}
for _, opt := range opts {
if err := opt(&rootfs.opts); err != nil {
return nil, err
}
}
fi, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
return nil, fmt.Errorf("file does not exist: %s", path)
} else if err != nil {
return nil, fmt.Errorf("could not check path: %w", err)
} else if !fi.IsDir() {
return nil, fmt.Errorf("supplied path is not a directory: %s", path)
}
return &rootfs, nil
}
// Build implements Initrd.
func (initrd *directory) Build(ctx context.Context) (string, error) {
if initrd.opts.output == "" {
fi, err := os.CreateTemp("", "")
if err != nil {
return "", fmt.Errorf("could not make temporary file: %w", err)
}
initrd.opts.output = fi.Name()
err = fi.Close()
if err != nil {
return "", fmt.Errorf("could not close temporary file: %w", err)
}
}
f, err := os.OpenFile(initrd.opts.output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return "", fmt.Errorf("could not open initramfs file: %w", err)
}
defer f.Close()
writer := cpio.NewWriter(f)
defer writer.Close()
if err := walkFiles(ctx, initrd.path, writer, &initrd.files); err != nil {
return "", fmt.Errorf("could not walk output path: %w", err)
}
if initrd.opts.compress {
if err := compressFiles(initrd.opts.output, writer, f); err != nil {
return "", fmt.Errorf("could not compress files: %w", err)
}
}
return initrd.opts.output, nil
}
// Files implements Initrd.
func (initrd *directory) Files() []string {
return initrd.files
}
// Env implements Initrd.
func (initrd *directory) Env() []string {
return nil
}
// Args implements Initrd.
func (initrd *directory) Args() []string {
return nil
}