github.com/andrewsun2898/u-root@v6.0.1-0.20200616011413-4b2895c1b815+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 // 24 // If `path` is empty, a default path of /tmp/initramfs.GOOS_GOARCH.cpio is 25 // used. 26 func (ca CPIOArchiver) OpenWriter(l ulog.Logger, path, goos, goarch string) (Writer, error) { 27 if len(path) == 0 && len(goos) == 0 && len(goarch) == 0 { 28 return nil, fmt.Errorf("passed no path, GOOS, and GOARCH to CPIOArchiver.OpenWriter") 29 } 30 if len(path) == 0 { 31 path = fmt.Sprintf("/tmp/initramfs.%s_%s.cpio", goos, goarch) 32 } 33 f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) 34 if err != nil { 35 return nil, err 36 } 37 l.Printf("Filename is %s", path) 38 return osWriter{ca.RecordFormat.Writer(f), f}, nil 39 } 40 41 // osWriter implements Writer. 42 type osWriter struct { 43 cpio.RecordWriter 44 45 f *os.File 46 } 47 48 // Finish implements Writer.Finish. 49 func (o osWriter) Finish() error { 50 err := cpio.WriteTrailer(o) 51 o.f.Close() 52 return err 53 } 54 55 // Reader implements Archiver.Reader. 56 func (ca CPIOArchiver) Reader(r io.ReaderAt) Reader { 57 return ca.RecordFormat.Reader(r) 58 }