1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00
goreleaser/internal/gio/hash.go
Carlos Alexandro Becker f37c045052
fix: copying file on docker pkg instead of hard linking (#2362)
* refactor: move copyfile to gio package

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* refactor: copying file on docker pkg instead of hard linking

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* chore: fmt

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* test: fixex

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: one todo

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* test: copy with mode

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* chore: fmt

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: errors

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
2021-07-24 19:59:43 -03:00

44 lines
811 B
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
}
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
}