-
Notifications
You must be signed in to change notification settings - Fork 64
/
file.go
79 lines (64 loc) · 1.49 KB
/
file.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
// 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"
"io"
"os"
"github.com/cavaliergopher/cpio"
)
type file struct {
opts InitrdOptions
path string
files []string
}
// NewFromFile accepts an input file which already represents a CPIO archive and
// is provided as a mechanism for satisfying the Initrd interface.
func NewFromFile(_ context.Context, path string, opts ...InitrdOption) (Initrd, error) {
fi, err := os.Open(path)
if err != nil {
return nil, err
}
defer fi.Close()
initrd := file{
opts: InitrdOptions{},
path: path,
}
for _, opt := range opts {
if err := opt(&initrd.opts); err != nil {
return nil, err
}
}
reader := cpio.NewReader(fi)
// Iterate through the files in the archive.
for {
hdr, err := reader.Next()
if err == io.EOF {
// end of cpio archive
break
}
if err != nil {
return nil, err
}
initrd.files = append(initrd.files, hdr.Name)
}
return &initrd, nil
}
// Build implements Initrd.
func (initrd *file) Build(_ context.Context) (string, error) {
return initrd.path, nil
}
// Files implements Initrd.
func (initrd *file) Files() []string {
return initrd.files
}
// Env implements Initrd.
func (initrd *file) Env() []string {
return nil
}
// Args implements Initrd.
func (initrd *file) Args() []string {
return nil
}