Skip to content

Commit dab9764

Browse files
committed
fingerprint: checksum cleanups
- 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
1 parent dc2b6ae commit dab9764

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

internal/fingerprint/sources_checksum.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,31 @@ func (c *ChecksumChecker) checksum(t *ast.Task, globs []*ast.Glob) (string, erro
118118
buf := make([]byte, 128*1024)
119119
for _, f := range sources {
120120
// also sum the filename, so checksum changes for renaming a file
121-
if _, err := io.CopyBuffer(h, strings.NewReader(filepath.Base(f)), buf); err != nil {
122-
return "", err
121+
if rel, err := filepath.Rel(t.Dir, f); err == nil {
122+
h.WriteString(rel)
123+
} else {
124+
// couldn't make a relative path, use the full path to be safe
125+
h.WriteString(f)
123126
}
124-
f, err := os.Open(f)
125-
if err != nil {
126-
return "", err
127+
// if we have a symlink here: we hash the link and *not* the target content
128+
if fi, err := os.Stat(f); err == nil && fi.Mode()&os.ModeSymlink != 0 {
129+
link, err := os.Readlink(f)
130+
if err != nil {
131+
return "", err
132+
}
133+
h.WriteString(link)
134+
} else {
135+
f, err := os.Open(f)
136+
if err != nil {
137+
return "", err
138+
}
139+
_, err = io.CopyBuffer(h, f, buf)
140+
f.Close()
141+
if err != nil {
142+
return "", err
143+
}
127144
}
128-
if _, err = io.CopyBuffer(h, f, buf); err != nil {
129-
return "", err
130-
}
131-
f.Close()
132145
}
133-
134146
hash := h.Sum128()
135147
return fmt.Sprintf("%x%x", hash.Hi, hash.Lo), nil
136148
}

0 commit comments

Comments
 (0)