github.com/jlowellwofford/u-root@v1.0.0/pkg/uroot/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 uroot
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"io/ioutil"
    11  	"log"
    12  	"os"
    13  
    14  	"github.com/u-root/u-root/pkg/cpio"
    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) ArchiveReader {
    24  	return nil
    25  }
    26  
    27  // OpenWriter implements Archiver.OpenWriter.
    28  func (da DirArchiver) OpenWriter(path, goos, goarch string) (ArchiveWriter, 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  	log.Printf("Path is %s", path)
    44  	return dirWriter{path}, nil
    45  }
    46  
    47  // dirWriter implements ArchiveWriter.
    48  type dirWriter struct {
    49  	dir string
    50  }
    51  
    52  // WriteRecord implements ArchiveWriter.WriteRecord.
    53  func (dw dirWriter) WriteRecord(r cpio.Record) error {
    54  	return cpio.CreateFileInRoot(r, dw.dir)
    55  }
    56  
    57  // Finish implements ArchiveWriter.Finish.
    58  func (dw dirWriter) Finish() error {
    59  	return nil
    60  }