github.com/rck/u-root@v0.0.0-20180106144920-7eb602e381bb/pkg/uroot/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 uroot 6 7 import ( 8 "github.com/u-root/u-root/pkg/cpio" 9 _ "github.com/u-root/u-root/pkg/cpio/newc" 10 "github.com/u-root/u-root/pkg/ramfs" 11 ) 12 13 // CPIOArchiver is an implementation of Archiver for the cpio format. 14 type CPIOArchiver struct { 15 // Format is the name of the cpio format to use. 16 Format string 17 } 18 19 // DefaultExtension implements Archiver.DefaultExtension. 20 func (ca CPIOArchiver) DefaultExtension() string { 21 return "cpio" 22 } 23 24 // Archive implements Archiver.Archive. 25 func (ca CPIOArchiver) Archive(opts ArchiveOpts) error { 26 archiver, err := cpio.Format(ca.Format) 27 if err != nil { 28 return err 29 } 30 31 init, err := ramfs.NewInitramfs(archiver.Writer(opts.OutputFile)) 32 if err != nil { 33 return err 34 } 35 36 if opts.BaseArchive != nil { 37 transform := cpio.MakeReproducible 38 39 // Rename init to inito if there is another init. 40 if !opts.UseExistingInit && opts.Contains("init") { 41 transform = func(r cpio.Record) cpio.Record { 42 if r.Name == "init" { 43 r.Name = "inito" 44 } 45 return cpio.MakeReproducible(r) 46 } 47 } 48 49 if err := init.Concat(archiver.Reader(opts.BaseArchive), transform); err != nil { 50 return err 51 } 52 } 53 54 // Reproducible builds: Files should be added to the archive in the 55 // same order. 56 for _, path := range opts.ArchiveFiles.SortedKeys() { 57 if record, ok := opts.ArchiveFiles.Records[path]; ok { 58 if err := init.WriteRecord(record); err != nil { 59 return err 60 } 61 } 62 if src, ok := opts.ArchiveFiles.Files[path]; ok { 63 if err := init.WriteFile(src, path); err != nil { 64 return err 65 } 66 } 67 } 68 return init.WriteTrailer() 69 }