hasher with logs and deterministic delimiter

This commit is contained in:
Jakub
2020-12-31 08:10:00 +01:00
committed by Jakub Cuth
parent d9c9edf4d7
commit 0cde1ab801
2 changed files with 21 additions and 8 deletions

View File

@ -19,51 +19,63 @@ package sum
import ( import (
"crypto/sha512" "crypto/sha512"
"encoding/base64"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"github.com/sirupsen/logrus"
) )
// RecursiveSum computes the sha512 sum of all files in the root directory and descendents. // RecursiveSum computes the sha512 sum of all files in the root directory and descendents.
// If a skipFile is provided (e.g. the path of a checksum file relative to rootDir), it (and its signature) is ignored. // If a skipFile is provided (e.g. the path of a checksum file relative to
// rootDir), it (and its signature) is ignored.
func RecursiveSum(rootDir, skipFileName string) ([]byte, error) { func RecursiveSum(rootDir, skipFileName string) ([]byte, error) {
hash := sha512.New() hash := sha512.New()
// In windows filepath accepts both delimiters `\` and `/`. In order to
// to properly skip file we have to choose one native delimiter.
rootDir = filepath.FromSlash(rootDir)
skipFile := filepath.Join(rootDir, skipFileName) skipFile := filepath.Join(rootDir, skipFileName)
skipFileSig := skipFile + ".sig" skipFileSig := skipFile + ".sig"
if err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { if err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
log := logrus.
WithField("path", path).
WithField("sum", base64.StdEncoding.EncodeToString(hash.Sum([]byte{})))
log.Debug("Next file")
if err != nil { if err != nil {
log.WithError(err).Error("Walk failed")
return err return err
} }
if info.IsDir() { if info.IsDir() {
log.Debug("Skip dir")
return nil return nil
} }
// The hashfile itself isn't included in the hash. // The hashfile itself isn't included in the hash.
if path == skipFile || path == skipFileSig { if path == skipFile || path == skipFileSig {
log.Debug("Skip file")
return nil return nil
} }
rel, err := filepath.Rel(rootDir, path) rel, err := filepath.Rel(rootDir, path)
if err != nil { if err != nil {
log.WithError(err).Error("Failed to find relative path")
return err return err
} }
if _, err := hash.Write([]byte(rel)); err != nil { if _, err := hash.Write([]byte(rel)); err != nil {
log.WithError(err).Error("Failed to write path")
return err return err
} }
f, err := os.Open(path) // nolint[gosec] f, err := os.Open(path) // nolint[gosec]
if err != nil { if err != nil {
log.WithError(err).Error("Failed to open file")
return err return err
} }
if _, err := io.Copy(hash, f); err != nil { if _, err := io.Copy(hash, f); err != nil {
log.WithError(err).Error("Copy to hash failed")
return err return err
} }
return f.Close() return f.Close()
}); err != nil { }); err != nil {
return nil, err return nil, err

View File

@ -26,6 +26,7 @@ import (
) )
func main() { func main() {
logrus.SetLevel(logrus.DebugLevel)
if err := createApp().Run(os.Args); err != nil { if err := createApp().Run(os.Args); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
@ -56,7 +57,7 @@ func createApp() *cli.App { // nolint[funlen]
} }
func computeSum(c *cli.Context) error { func computeSum(c *cli.Context) error {
b, err := sum.RecursiveSum(c.String("root"), "") b, err := sum.RecursiveSum(c.String("root"), c.String("output"))
if err != nil { if err != nil {
return err return err
} }