mirror of
https://github.com/goreleaser/goreleaser.git
synced 2024-12-29 01:44:39 +02:00
440e9a63b9
Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package gio
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
)
|
|
|
|
// EqualFiles returns true if both files sha256sums and their modes are equal.
|
|
func EqualFiles(a, b string) (bool, error) {
|
|
am, as, err := sha256sum(a)
|
|
if err != nil {
|
|
return false, fmt.Errorf("could not hash %s: %w", a, err)
|
|
}
|
|
bm, bs, err := sha256sum(b)
|
|
if err != nil {
|
|
return false, fmt.Errorf("could not hash %s: %w", b, err)
|
|
}
|
|
return as == bs && am == bm, nil
|
|
}
|
|
|
|
// EqualFileContents returns true if both files contents are equal.
|
|
func EqualFileContents(a, b string) (bool, error) {
|
|
_, as, err := sha256sum(a)
|
|
if err != nil {
|
|
return false, fmt.Errorf("could not hash %s: %w", a, err)
|
|
}
|
|
_, bs, err := sha256sum(b)
|
|
if err != nil {
|
|
return false, fmt.Errorf("could not hash %s: %w", b, err)
|
|
}
|
|
return as == bs, nil
|
|
}
|
|
|
|
func sha256sum(path string) (fs.FileMode, string, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return 0, "", err
|
|
}
|
|
defer f.Close()
|
|
|
|
h := sha256.New()
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
return 0, "", err
|
|
}
|
|
|
|
st, err := f.Stat()
|
|
if err != nil {
|
|
return 0, "", err
|
|
}
|
|
|
|
return st.Mode(), hex.EncodeToString(h.Sum(nil)), nil
|
|
}
|