From d50ba0bfdabd90a8d472ed57a5afcffd7a893340 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Wed, 4 Oct 2023 19:46:40 +0300 Subject: [PATCH] Hash explicit type --- dep.go | 36 ++++++++++++++++++++++-------------- depfix.go | 7 +++---- run.go | 4 ++-- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/dep.go b/dep.go index 94daa2a..a1e45ae 100644 --- a/dep.go +++ b/dep.go @@ -33,15 +33,23 @@ import ( "lukechampine.com/blake3" ) +const HashLen = 32 + var ( DirPrefix string DepCwd string ErrBadRecFormat = errors.New("invalid format of .rec") InodeCache = make(map[string][]*Inode) - HashCache = make(map[string][]string) + HashCache = make(map[string][]Hash) ) +type Hash string + +func (h Hash) String() string { + return hex.EncodeToString([]byte(h)) +} + func recfileWrite(fdDep io.StringWriter, fields ...recfile.Field) error { w := recfile.NewWriter(fdDep) if _, err := w.RecordStart(); err != nil { @@ -72,23 +80,23 @@ func stamp(fdDep, src *os.File) error { if err != nil { return err } - tracef(CDebug, "stamp: %s <- %s", fdDep.Name(), hex.EncodeToString([]byte(hsh))) + tracef(CDebug, "stamp: %s <- %s", fdDep.Name(), hsh) return recfileWrite( fdDep, recfile.Field{Name: "Type", Value: DepTypeStamp}, - recfile.Field{Name: "Hash", Value: hex.EncodeToString([]byte(hsh))}, + recfile.Field{Name: "Hash", Value: hsh.String()}, ) } -func fileHash(fd *os.File) (string, error) { - h := blake3.New(32, nil) +func fileHash(fd *os.File) (Hash, error) { + h := blake3.New(HashLen, nil) if _, err := io.Copy(h, bufio.NewReader(fd)); err != nil { return "", err } - return string(h.Sum(nil)), nil + return Hash(h.Sum(nil)), nil } -func depWrite(fdDep *os.File, cwd string, tgt *Tgt, hsh string) error { +func depWrite(fdDep *os.File, cwd string, tgt *Tgt, hsh Hash) error { tracef(CDebug, "ifchange: %s <- %s", fdDep.Name(), tgt) fd, err := os.Open(tgt.a) if err != nil { @@ -111,7 +119,7 @@ func depWrite(fdDep *os.File, cwd string, tgt *Tgt, hsh string) error { fields := []recfile.Field{ {Name: "Type", Value: DepTypeIfchange}, {Name: "Target", Value: tgt.RelTo(cwd)}, - {Name: "Hash", Value: hex.EncodeToString([]byte(hsh))}, + {Name: "Hash", Value: hsh.String()}, } fields = append(fields, inode.RecfileFields()...) return recfileWrite(fdDep, fields...) @@ -153,23 +161,23 @@ func depsWrite(fdDep *os.File, tgts []*Tgt) error { type DepInfoIfchange struct { tgt *Tgt inode *Inode - hash string + hash Hash } type DepInfo struct { build string always bool - stamp string + stamp Hash ifcreates []*Tgt ifchanges []DepInfoIfchange } -func mustHexDecode(s string) []byte { +func mustHashDecode(s string) Hash { b, err := hex.DecodeString(s) if err != nil { log.Fatal(err) } - return b + return Hash(b) } var missingBuild = errors.New(".rec missing Build:") @@ -232,7 +240,7 @@ func depRead(tgt *Tgt) (*DepInfo, error) { InodeCache[dep.a] = append(InodeCache[dep.a], inode) } - hsh := string(mustHexDecode(m["Hash"])) + hsh := mustHashDecode(m["Hash"]) cachedFound = false for _, cachedHash := range HashCache[dep.a] { if hsh == cachedHash { @@ -253,7 +261,7 @@ func depRead(tgt *Tgt) (*DepInfo, error) { if hsh == "" { return nil, ErrBadRecFormat } - depInfo.stamp = string(mustHexDecode(hsh)) + depInfo.stamp = mustHashDecode(hsh) default: return nil, ErrBadRecFormat } diff --git a/depfix.go b/depfix.go index 87ecd88..bc4452c 100644 --- a/depfix.go +++ b/depfix.go @@ -18,7 +18,6 @@ along with this program. If not, see . package main import ( - "encoding/hex" "errors" "io" "io/fs" @@ -111,7 +110,7 @@ func depFix(root string) error { if err != nil { return ErrLine(err) } - theirHsh := mustHexDecode(m["Hash"]) + theirHsh := mustHashDecode(m["Hash"]) fd, err := os.Open(path.Join(root, dep)) if err != nil { if errors.Is(err, fs.ErrNotExist) { @@ -149,7 +148,7 @@ func depFix(root string) error { if err != nil { return ErrLine(err) } - if hsh != string(theirHsh) { + if hsh != theirHsh { tracef( CDebug, "depfix: %s/%s -> %s: hash differs", root, entry.Name(), dep, @@ -159,7 +158,7 @@ func depFix(root string) error { fields = []recfile.Field{ {Name: "Type", Value: DepTypeIfchange}, {Name: "Target", Value: dep}, - {Name: "Hash", Value: hex.EncodeToString([]byte(hsh))}, + {Name: "Hash", Value: hsh.String()}, } fields = append(fields, inode.RecfileFields()...) fieldses[len(fieldses)-1] = fields diff --git a/run.go b/run.go index 7a6ac37..48c2f10 100644 --- a/run.go +++ b/run.go @@ -133,7 +133,7 @@ func mkdirs(pth string) error { } func isModified(depInfo *DepInfo, tgt *Tgt) ( - modified bool, ourInode *Inode, hshPrev string, err error, + modified bool, ourInode *Inode, hshPrev Hash, err error, ) { if depInfo == nil { return @@ -721,7 +721,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error { goto Finish } } else { - var hsh string + var hsh Hash if hshPrev != "" { _, err = fd.Seek(0, io.SeekStart) if err != nil { -- 2.44.0