forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ramfs.go
90 lines (74 loc) · 1.87 KB
/
ramfs.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
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package test
import (
"fmt"
"os"
"github.com/u-root/u-root/pkg/cpio"
"github.com/u-root/u-root/pkg/uio"
)
type ArchiveValidator interface {
Validate(a *cpio.Archive) error
}
type HasRecord struct {
R cpio.Record
}
func (hr HasRecord) Validate(a *cpio.Archive) error {
r, ok := a.Get(hr.R.Name)
if !ok {
return fmt.Errorf("archive does not contain %v", hr.R)
}
if !cpio.Equal(r, hr.R) {
return fmt.Errorf("archive does not contain %v; instead has %v", hr.R, r)
}
return nil
}
type HasFile struct {
Path string
}
func (hf HasFile) Validate(a *cpio.Archive) error {
if _, ok := a.Get(hf.Path); ok {
return nil
}
return fmt.Errorf("archive does not contain %s, but should", hf.Path)
}
type HasContent struct {
Path string
Content string
}
func (hc HasContent) Validate(a *cpio.Archive) error {
r, ok := a.Get(hc.Path)
if !ok {
return fmt.Errorf("archive does not contain %s, but should", hc.Path)
}
if c, err := uio.ReadAll(r); err != nil {
return fmt.Errorf("reading record %s failed: %v", hc.Path, err)
} else if string(c) != hc.Content {
return fmt.Errorf("content of %s is %s, want %s", hc.Path, string(c), hc.Content)
}
return nil
}
type MissingFile struct {
Path string
}
func (mf MissingFile) Validate(a *cpio.Archive) error {
if _, ok := a.Get(mf.Path); ok {
return fmt.Errorf("archive contains %s, but shouldn't", mf.Path)
}
return nil
}
type IsEmpty struct{}
func (IsEmpty) Validate(a *cpio.Archive) error {
if empty := a.Empty(); !empty {
return fmt.Errorf("expected archive to be empty")
}
return nil
}
func ReadArchive(path string) (*cpio.Archive, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
return cpio.ArchiveFromReader(cpio.Newc.Reader(f))
}