/
cpio.go
275 lines (240 loc) · 6.56 KB
/
cpio.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2013-2017 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 cpio
import (
"fmt"
"io"
"path/filepath"
)
var (
formatMap = make(map[string]RecordFormat)
Debug = func(string, ...interface{}) {}
)
// A RecordReader reads one record from an archive.
type RecordReader interface {
ReadRecord() (Record, error)
}
// A RecordWriter writes on record to an archive.
type RecordWriter interface {
WriteRecord(Record) error
}
// A RecordFormat gives readers and writers for dealing with archives.
type RecordFormat interface {
Reader(r io.ReaderAt) RecordReader
Writer(w io.Writer) RecordWriter
}
// Format returns the RecordFormat with that name, if it exists.
func Format(name string) (RecordFormat, error) {
op, ok := formatMap[name]
if !ok {
return nil, fmt.Errorf("%q is not in cpio format map %v", name, formatMap)
}
return op, nil
}
// EOFReader is a RecordReader that converts the Trailer record to io.EOF.
type EOFReader struct {
RecordReader
}
// ReadRecord implements RecordReader.
//
// ReadRecord returns io.EOF when the record name is TRAILER!!!.
func (r EOFReader) ReadRecord() (Record, error) {
rec, err := r.RecordReader.ReadRecord()
if err != nil {
return Record{}, err
}
// The end of a CPIO archive is marked by a record whose name is
// "TRAILER!!!".
if rec.Name == Trailer {
return Record{}, io.EOF
}
return rec, nil
}
// DedupWriter is a RecordWriter that does not write more than one record with
// the same path.
//
// There seems to be no harm done in stripping duplicate names when the record
// is written, and lots of harm done if we don't do it.
type DedupWriter struct {
rw RecordWriter
// alreadyWritten keeps track of paths already written to rw.
alreadyWritten map[string]struct{}
}
// NewDedupWriter returns a new deduplicating rw.
func NewDedupWriter(rw RecordWriter) RecordWriter {
return &DedupWriter{
rw: rw,
alreadyWritten: make(map[string]struct{}),
}
}
// Passthrough copies from a RecordReader to a RecordWriter
// It processes one record at a time to minimize the memory footprint.
func Passthrough(r RecordReader, w RecordWriter) error {
if err := Concat(w, r, nil); err != nil {
return err
}
if err := WriteTrailer(w); err != nil {
return err
}
return nil
}
// WriteRecord implements RecordWriter.
//
// If rec.Name was already seen once before, it will not be written again and
// WriteRecord returns nil.
func (dw *DedupWriter) WriteRecord(rec Record) error {
rec.Name = Normalize(rec.Name)
if _, ok := dw.alreadyWritten[rec.Name]; ok {
return nil
}
dw.alreadyWritten[rec.Name] = struct{}{}
return dw.rw.WriteRecord(rec)
}
// WriteRecords writes multiple records.
func WriteRecords(w RecordWriter, files []Record) error {
for _, f := range files {
if err := w.WriteRecord(f); err != nil {
return fmt.Errorf("WriteRecords: writing %q got %v", f.Info.Name, err)
}
}
return nil
}
// WriteTrailer writes the trailer record.
func WriteTrailer(w RecordWriter) error {
return w.WriteRecord(TrailerRecord)
}
// Concat reads files from r one at a time, and writes them to w.
func Concat(w RecordWriter, r RecordReader, transform func(Record) Record) error {
// Read and write one file at a time. We don't want all that in memory.
for {
f, err := r.ReadRecord()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
if transform != nil {
f = transform(f)
}
if err := w.WriteRecord(f); err != nil {
return err
}
}
}
// Archive implements RecordWriter and is an in-memory list of files.
//
// Archive.Reader returns a RecordReader for this archive.
type Archive struct {
// Files is a map of relative archive path -> record.
Files map[string]Record
// Order is a list of relative archive paths and represents the order
// in which Files were added.
Order []string
}
// InMemArchive returns an in-memory file archive.
func InMemArchive() *Archive {
return &Archive{
Files: make(map[string]Record),
}
}
// WriteRecord implements RecordWriter and adds a record to the archive.
func (a *Archive) WriteRecord(r Record) error {
r.Name = Normalize(r.Name)
a.Files[r.Name] = r
a.Order = append(a.Order, r.Name)
return nil
}
type archiveReader struct {
a *Archive
pos int
}
// Reader returns a RecordReader for the archive.
func (a *Archive) Reader() RecordReader {
return &EOFReader{&archiveReader{a: a}}
}
// ReadRecord implements RecordReader.
func (ar *archiveReader) ReadRecord() (Record, error) {
if ar.pos >= len(ar.a.Order) {
return Record{}, io.EOF
}
path := ar.a.Order[ar.pos]
ar.pos++
return ar.a.Files[path], nil
}
// Contains returns true if a record matching r is in the archive.
func (a *Archive) Contains(r Record) bool {
r.Name = Normalize(r.Name)
if s, ok := a.Files[r.Name]; ok {
return Equal(r, s)
}
return false
}
// ReadArchive reads the entire archive in-memory and makes it accessible by
// paths.
func ReadArchive(rr RecordReader) (*Archive, error) {
a := &Archive{
Files: make(map[string]Record),
}
err := ForEachRecord(rr, func(r Record) error {
return a.WriteRecord(r)
})
return a, err
}
// ReadAllRecords returns all records in `r` in the order in which they were
// read.
func ReadAllRecords(rr RecordReader) ([]Record, error) {
var files []Record
err := ForEachRecord(rr, func(r Record) error {
files = append(files, r)
return nil
})
return files, err
}
// ForEachRecord reads every record from r and applies f.
func ForEachRecord(rr RecordReader, fun func(Record) error) error {
for {
rec, err := rr.ReadRecord()
switch err {
case io.EOF:
return nil
case nil:
if err := fun(rec); err != nil {
return err
}
default:
return err
}
}
}
// Normalize normalizes path to be relative to /.
func Normalize(path string) string {
if filepath.IsAbs(path) {
rel, err := filepath.Rel("/", path)
if err != nil {
panic("absolute filepath must be relative to /")
}
return rel
}
return path
}
// MakeReproducible changes any fields in a Record such that if we run cpio
// again, with the same files presented to it in the same order, and those
// files have unchanged contents, the cpio file it produces will be bit-for-bit
// identical. This is an essential property for firmware-embedded payloads.
func MakeReproducible(r Record) Record {
r.Ino = 0
r.Name = Normalize(r.Name)
r.MTime = 0
r.UID = 0
r.GID = 0
return r
}
// MakeAllReproducible makes all given records reproducible as in
// MakeReproducible.
func MakeAllReproducible(files []Record) {
for i := range files {
files[i] = MakeReproducible(files[i])
}
}