github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/remotecontext/filehash.go (about) 1 package remotecontext // import "github.com/demonoid81/moby/builder/remotecontext" 2 3 import ( 4 "archive/tar" 5 "crypto/sha256" 6 "hash" 7 "os" 8 9 "github.com/demonoid81/moby/pkg/archive" 10 "github.com/demonoid81/moby/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 var link string 16 if fi.Mode()&os.ModeSymlink != 0 { 17 var err error 18 link, err = os.Readlink(path) 19 if err != nil { 20 return nil, err 21 } 22 } 23 hdr, err := archive.FileInfoHeader(name, fi, link) 24 if err != nil { 25 return nil, err 26 } 27 if err := archive.ReadSecurityXattrToTarHeader(path, hdr); err != nil { 28 return nil, err 29 } 30 tsh := &tarsumHash{hdr: hdr, Hash: sha256.New()} 31 tsh.Reset() // initialize header 32 return tsh, nil 33 } 34 35 type tarsumHash struct { 36 hash.Hash 37 hdr *tar.Header 38 } 39 40 // Reset resets the Hash to its initial state. 41 func (tsh *tarsumHash) Reset() { 42 // comply with hash.Hash and reset to the state hash had before any writes 43 tsh.Hash.Reset() 44 tarsum.WriteV1Header(tsh.hdr, tsh.Hash) 45 }