github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/layer/empty.go (about)

     1  package layer // import "github.com/demonoid81/moby/layer"
     2  
     3  import (
     4  	"archive/tar"
     5  	"bytes"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  )
    10  
    11  // DigestSHA256EmptyTar is the canonical sha256 digest of empty tar file -
    12  // (1024 NULL bytes)
    13  const DigestSHA256EmptyTar = DiffID("sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef")
    14  
    15  type emptyLayer struct{}
    16  
    17  // EmptyLayer is a layer that corresponds to empty tar.
    18  var EmptyLayer = &emptyLayer{}
    19  
    20  func (el *emptyLayer) TarStream() (io.ReadCloser, error) {
    21  	buf := new(bytes.Buffer)
    22  	tarWriter := tar.NewWriter(buf)
    23  	tarWriter.Close()
    24  	return ioutil.NopCloser(buf), nil
    25  }
    26  
    27  func (el *emptyLayer) TarStreamFrom(p ChainID) (io.ReadCloser, error) {
    28  	if p == "" {
    29  		return el.TarStream()
    30  	}
    31  	return nil, fmt.Errorf("can't get parent tar stream of an empty layer")
    32  }
    33  
    34  func (el *emptyLayer) ChainID() ChainID {
    35  	return ChainID(DigestSHA256EmptyTar)
    36  }
    37  
    38  func (el *emptyLayer) DiffID() DiffID {
    39  	return DigestSHA256EmptyTar
    40  }
    41  
    42  func (el *emptyLayer) Parent() Layer {
    43  	return nil
    44  }
    45  
    46  func (el *emptyLayer) Size() (size int64, err error) {
    47  	return 0, nil
    48  }
    49  
    50  func (el *emptyLayer) DiffSize() (size int64, err error) {
    51  	return 0, nil
    52  }
    53  
    54  func (el *emptyLayer) Metadata() (map[string]string, error) {
    55  	return make(map[string]string), nil
    56  }
    57  
    58  // IsEmpty returns true if the layer is an EmptyLayer
    59  func IsEmpty(diffID DiffID) bool {
    60  	return diffID == DigestSHA256EmptyTar
    61  }