Skip to content

Commit

Permalink
fingerprint: checksum cleanups
Browse files Browse the repository at this point in the history
- use the full relative path and not only the base name for hashing
- if we encounter a symlink, hash the link and not the target path
- properly close file on error
  • Loading branch information
vanackere committed Dec 17, 2024
1 parent dc2b6ae commit dab9764
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions internal/fingerprint/sources_checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,31 @@ func (c *ChecksumChecker) checksum(t *ast.Task, globs []*ast.Glob) (string, erro
buf := make([]byte, 128*1024)
for _, f := range sources {
// also sum the filename, so checksum changes for renaming a file
if _, err := io.CopyBuffer(h, strings.NewReader(filepath.Base(f)), buf); err != nil {
return "", err
if rel, err := filepath.Rel(t.Dir, f); err == nil {
h.WriteString(rel)
} else {
// couldn't make a relative path, use the full path to be safe
h.WriteString(f)
}
f, err := os.Open(f)
if err != nil {
return "", err
// if we have a symlink here: we hash the link and *not* the target content
if fi, err := os.Stat(f); err == nil && fi.Mode()&os.ModeSymlink != 0 {
link, err := os.Readlink(f)
if err != nil {
return "", err
}
h.WriteString(link)
} else {
f, err := os.Open(f)
if err != nil {
return "", err
}
_, err = io.CopyBuffer(h, f, buf)
f.Close()
if err != nil {
return "", err
}
}
if _, err = io.CopyBuffer(h, f, buf); err != nil {
return "", err
}
f.Close()
}

hash := h.Sum128()
return fmt.Sprintf("%x%x", hash.Hi, hash.Lo), nil
}
Expand Down

0 comments on commit dab9764

Please sign in to comment.