reuse hash code

This commit is contained in:
marco 2024-04-05 13:24:16 +02:00
parent 3bd5eac61a
commit f602c8c31e

View file

@ -1,10 +1,7 @@
package cwhub
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"slices"
@ -12,6 +9,7 @@ import (
"strings"
"github.com/Masterminds/semver/v3"
"github.com/crowdsecurity/go-cs-lib/downloader"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
@ -38,22 +36,6 @@ func linkTarget(path string, logger *logrus.Logger) (string, error) {
return hubpath, nil
}
func getSHA256(filepath string) (string, error) {
f, err := os.Open(filepath)
if err != nil {
return "", fmt.Errorf("unable to open '%s': %w", filepath, err)
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", fmt.Errorf("unable to calculate sha256 of '%s': %w", filepath, err)
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// information used to create a new Item, from a file path.
type itemFileInfo struct {
fname string
@ -466,7 +448,7 @@ func (h *Hub) localSync() error {
func (i *Item) setVersionState(path string, inhub bool) error {
var err error
i.State.LocalHash, err = getSHA256(path)
i.State.LocalHash, err = downloader.SHA256(path)
if err != nil {
return fmt.Errorf("failed to get sha256 of %s: %w", path, err)
}