github.com/jlowellwofford/u-root@v1.0.0/xcmds/archive/encode.go (about)

     1  // Copyright 2013 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  // Archive archives files. The VTOC is at the front; we're not modeling tape drives or
     6  // streams as in tar and cpio. This will greatly speed up listing the archive,
     7  // modifying it, and so on. We think.
     8  // Why a new tool?
     9  package main
    10  
    11  import (
    12  	"io"
    13  	"os"
    14  )
    15  
    16  func encodeOne(out io.Writer, f *file) error {
    17  	if f.Size == 0 || !f.Mode.IsRegular() {
    18  		return nil
    19  	}
    20  
    21  	in, err := os.Open(f.Name)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	defer in.Close()
    26  	amt, err := io.Copy(out, in)
    27  	debug("%s: wrote %d bytes", f.Name, amt)
    28  	return err
    29  }
    30  
    31  func encode(out io.Writer, dirs ...string) error {
    32  	var vtoc []*file
    33  	if len(dirs) == 0 {
    34  		dirs = []string{"."}
    35  	}
    36  
    37  	vtoc, err := buildVTOC(dirs)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	amt, err := writeVTOC(out, vtoc)
    43  	debug("Wrote %d bytes of vtoc", amt)
    44  
    45  	for _, v := range vtoc {
    46  		if err = encodeOne(out, v); err != nil {
    47  			break
    48  		}
    49  	}
    50  
    51  	return err
    52  }