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