MCPcopy
hub / github.com/cortexlabs/cortex / HashFile

Function HashFile

pkg/lib/files/files.go:993–1024  ·  view source on GitHub ↗
(path string, paths ...string)

Source from the content-addressed store, hash-verified

991}
992
993func HashFile(path string, paths ...string) (string, error) {
994 md5Hash := md5.New()
995
996 allPaths := append(paths, path)
997 for _, path := range allPaths {
998 f, err := Open(path)
999 if err != nil {
1000 return "", err
1001 }
1002
1003 // Skip directories
1004 fileInfo, err := f.Stat()
1005 if err != nil {
1006 f.Close()
1007 return "", errors.WithStack(err)
1008 }
1009 if fileInfo.IsDir() {
1010 f.Close()
1011 continue
1012 }
1013
1014 if _, err := io.Copy(md5Hash, f); err != nil {
1015 f.Close()
1016 return "", errors.Wrap(err, path)
1017 }
1018
1019 io.WriteString(md5Hash, path)
1020 f.Close()
1021 }
1022
1023 return hex.EncodeToString(md5Hash.Sum(nil)), nil
1024}
1025
1026func HashDirectory(dir string, ignoreFns ...IgnoreFn) (string, error) {
1027 md5Hash := md5.New()

Callers

nothing calls this directly

Calls 4

WithStackFunction · 0.92
WrapFunction · 0.92
OpenFunction · 0.85
CopyMethod · 0.45

Tested by

no test coverage detected