github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/uroot/initramfs/cpio.go (about) 1 // Copyright 2015-2017 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package initramfs 6 7 import ( 8 "fmt" 9 "io" 10 "os" 11 12 "github.com/u-root/u-root/pkg/cpio" 13 "github.com/u-root/u-root/pkg/ulog" 14 ) 15 16 // CPIOArchiver is an implementation of Archiver for the cpio format. 17 type CPIOArchiver struct { 18 cpio.RecordFormat 19 } 20 21 // OpenWriter opens `path` as the correct file type and returns an 22 // Writer pointing to `path`. 23 func (ca CPIOArchiver) OpenWriter(l ulog.Logger, path string) (Writer, error) { 24 if len(path) == 0 { 25 return nil, fmt.Errorf("path is required") 26 } 27 f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) 28 if err != nil { 29 return nil, err 30 } 31 return osWriter{ca.RecordFormat.Writer(f), f}, nil 32 } 33 34 // osWriter implements Writer. 35 type osWriter struct { 36 cpio.RecordWriter 37 38 f *os.File 39 } 40 41 // Finish implements Writer.Finish. 42 func (o osWriter) Finish() error { 43 err := cpio.WriteTrailer(o) 44 o.f.Close() 45 return err 46 } 47 48 // Reader implements Archiver.Reader. 49 func (ca CPIOArchiver) Reader(r io.ReaderAt) Reader { 50 return ca.RecordFormat.Reader(r) 51 }