github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/pkg/uroot/dir.go (about)

     1  package uroot
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  
    10  	"github.com/u-root/u-root/pkg/cpio"
    11  )
    12  
    13  // DirArchiver implements Archiver for a directory.
    14  type DirArchiver struct{}
    15  
    16  // Reader implements Archiver.Reader.
    17  //
    18  // Currently unsupported for directories.
    19  func (da DirArchiver) Reader(io.ReaderAt) ArchiveReader {
    20  	return nil
    21  }
    22  
    23  // OpenWriter implements Archiver.OpenWriter.
    24  func (da DirArchiver) OpenWriter(path, goos, goarch string) (ArchiveWriter, error) {
    25  	if len(path) == 0 {
    26  		var err error
    27  		path, err = ioutil.TempDir("", "u-root")
    28  		if err != nil {
    29  			return nil, err
    30  		}
    31  	} else {
    32  		if _, err := os.Stat(path); os.IsExist(err) {
    33  			return nil, fmt.Errorf("path %q already exists", path)
    34  		}
    35  		if err := os.MkdirAll(path, 0755); err != nil {
    36  			return nil, err
    37  		}
    38  	}
    39  	log.Printf("Path is %s", path)
    40  	return dirWriter{path}, nil
    41  }
    42  
    43  // dirWriter implements ArchiveWriter.
    44  type dirWriter struct {
    45  	dir string
    46  }
    47  
    48  // WriteRecord implements ArchiveWriter.WriteRecord.
    49  func (dw dirWriter) WriteRecord(r cpio.Record) error {
    50  	return cpio.CreateFileInRoot(r, dw.dir)
    51  }
    52  
    53  // Finish implements ArchiveWriter.Finish.
    54  func (dw dirWriter) Finish() error {
    55  	return nil
    56  }