-
Notifications
You must be signed in to change notification settings - Fork 63
/
directory.go
207 lines (168 loc) · 5.01 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// 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 (
"compress/gzip"
"context"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/cavaliergopher/cpio"
"kraftkit.sh/log"
)
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 := filepath.WalkDir(initrd.path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("received error before parsing path: %w", err)
}
internal := strings.TrimPrefix(path, filepath.Clean(initrd.path))
if internal == "" {
return nil // Do not archive empty paths
}
internal = "." + filepath.ToSlash(internal)
info, err := d.Info()
if err != nil {
return fmt.Errorf("could not get directory entry info: %w", err)
}
if d.Type().IsDir() {
if err := writer.WriteHeader(&cpio.Header{
Name: internal,
Mode: cpio.FileMode(info.Mode().Perm()) | cpio.TypeDir,
}); err != nil {
return fmt.Errorf("could not write CPIO header: %w", err)
}
return nil
}
initrd.files = append(initrd.files, internal)
log.G(ctx).
WithField("file", internal).
Trace("archiving")
var data []byte
targetLink := ""
if info.Mode()&os.ModeSymlink != 0 {
targetLink, err = os.Readlink(path)
data = []byte(targetLink)
} else if d.Type().IsRegular() {
data, err = os.ReadFile(path)
} else {
log.G(ctx).Warnf("unsupported file: %s", path)
return nil
}
if err != nil {
return fmt.Errorf("could not read file: %w", err)
}
header := &cpio.Header{
Name: internal,
Mode: cpio.FileMode(info.Mode().Perm()),
ModTime: info.ModTime(),
Size: info.Size(),
}
switch {
case info.Mode().IsDir():
header.Mode |= cpio.TypeDir
case info.Mode().IsRegular():
header.Mode |= cpio.TypeReg
case info.Mode()&fs.ModeSymlink != 0:
header.Mode |= cpio.TypeSymlink
header.Linkname = targetLink
}
if err := writer.WriteHeader(header); err != nil {
return fmt.Errorf("writing cpio header for %q: %w", internal, err)
}
if _, err := writer.Write(data); err != nil {
return fmt.Errorf("could not write CPIO data for %s: %w", internal, err)
}
return nil
}); err != nil {
return "", fmt.Errorf("could not walk output path: %w", err)
}
if initrd.opts.compress {
err := writer.Close()
if err != nil {
return "", fmt.Errorf("could not close CPIO writer: %w", err)
}
_, err = f.Seek(0, io.SeekStart)
if err != nil {
return "", fmt.Errorf("could not seek to start of file: %w", err)
}
fw, err := os.OpenFile(initrd.opts.output+".gz", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return "", fmt.Errorf("could not open initramfs file: %w", err)
}
gw := gzip.NewWriter(fw)
if _, err := io.Copy(gw, f); err != nil {
return "", fmt.Errorf("could not compress initramfs file: %w", err)
}
err = gw.Close()
if err != nil {
return "", fmt.Errorf("could not close gzip writer: %w", err)
}
err = fw.Close()
if err != nil {
return "", fmt.Errorf("could not close compressed initramfs file: %w", err)
}
if err := os.Remove(initrd.opts.output); err != nil {
return "", fmt.Errorf("could not remove uncompressed initramfs: %w", err)
}
if err := os.Rename(initrd.opts.output+".gz", initrd.opts.output); err != nil {
return "", fmt.Errorf("could not rename compressed initramfs: %w", err)
}
}
return initrd.opts.output, nil
}
// Files implements Initrd.
func (initrd *directory) Files() []string {
return initrd.files
}