github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/builder/remotecontext/filehash.go (about)

     1  package remotecontext
     2  
     3  import (
     4  	"archive/tar"
     5  	"crypto/sha256"
     6  	"hash"
     7  	"os"
     8  
     9  	"github.com/docker/docker/pkg/archive"
    10  	"github.com/docker/docker/pkg/tarsum"
    11  )
    12  
    13  // NewFileHash returns new hash that is used for the builder cache keys
    14  func NewFileHash(path, name string, fi os.FileInfo) (hash.Hash, error) {
    15  	hdr, err := archive.FileInfoHeader(path, name, fi)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	tsh := &tarsumHash{hdr: hdr, Hash: sha256.New()}
    20  	tsh.Reset() // initialize header
    21  	return tsh, nil
    22  }
    23  
    24  type tarsumHash struct {
    25  	hash.Hash
    26  	hdr *tar.Header
    27  }
    28  
    29  // Reset resets the Hash to its initial state.
    30  func (tsh *tarsumHash) Reset() {
    31  	// comply with hash.Hash and reset to the state hash had before any writes
    32  	tsh.Hash.Reset()
    33  	tarsum.WriteV1Header(tsh.hdr, tsh.Hash)
    34  }