github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/uroot/initramfs/dir.go (about) 1 // Copyright 2018 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/mvdan/u-root-coreutils/pkg/cpio" 13 "github.com/mvdan/u-root-coreutils/pkg/ulog" 14 ) 15 16 // DirArchiver implements Archiver for a directory. 17 type DirArchiver struct{} 18 19 // Reader implements Archiver.Reader. 20 // 21 // Currently unsupported for directories. 22 func (da DirArchiver) Reader(io.ReaderAt) Reader { 23 return nil 24 } 25 26 // OpenWriter implements Archiver.OpenWriter. 27 func (da DirArchiver) OpenWriter(l ulog.Logger, path string) (Writer, error) { 28 if len(path) == 0 { 29 var err error 30 path, err = os.MkdirTemp("", "u-root") 31 if err != nil { 32 return nil, err 33 } 34 } else { 35 if _, err := os.Stat(path); os.IsExist(err) { 36 return nil, fmt.Errorf("path %q already exists", path) 37 } 38 if err := os.MkdirAll(path, 0o755); err != nil { 39 return nil, err 40 } 41 } 42 l.Printf("Path is %s", path) 43 return dirWriter{path}, nil 44 } 45 46 // dirWriter implements Writer. 47 type dirWriter struct { 48 dir string 49 } 50 51 // WriteRecord implements Writer.WriteRecord. 52 func (dw dirWriter) WriteRecord(r cpio.Record) error { 53 return cpio.CreateFileInRoot(r, dw.dir, false) 54 } 55 56 // Finish implements Writer.Finish. 57 func (dw dirWriter) Finish() error { 58 return nil 59 }