From e0a7e4af12c3e18229f2b42583ae27961eaf3d68 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Tue, 6 Aug 2024 18:24:27 +0300 Subject: [PATCH] Hash type for convenience --- dep.go | 12 ++++++------ depfix.go | 4 ++-- hash.go | 6 ++++-- run.go | 4 ++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/dep.go b/dep.go index 8fe4fff..b43a583 100644 --- a/dep.go +++ b/dep.go @@ -78,9 +78,9 @@ func (ifchange *Ifchange) Hash() Hash { } type Dep struct { - stamp Hash ifcreates []*Tgt ifchanges []*Ifchange + stamp Hash build uuid.UUID always bool } @@ -104,7 +104,7 @@ func always(w io.Writer, fdDepName string) (err error) { func stamp(w io.Writer, fdDepName string, hsh Hash) (err error) { tracef(CDebug, "stamp: %s <- %s", fdDepName, hsh) _, err = io.Copy(w, bytes.NewBuffer(chunkWrite(append( - []byte{DepTypeStamp}, []byte(hsh)..., + []byte{DepTypeStamp}, hsh[:]..., )))) return } @@ -112,7 +112,7 @@ func stamp(w io.Writer, fdDepName string, hsh Hash) (err error) { func fileHash(fd io.Reader) (Hash, error) { h := blake3.New(HashLen, nil) if _, err := io.Copy(h, bufio.NewReader(fd)); err != nil { - return "", err + return HashNull, err } return Hash(h.Sum(nil)), nil } @@ -131,7 +131,7 @@ func depWrite(w io.Writer, fdDepName, cwd string, tgt *Tgt, hsh Hash) (err error if isDir { return nil } - if hsh == "" { + if hsh == HashNull { hsh, err = fileHash(fd) if err != nil { return ErrLine(err) @@ -140,7 +140,7 @@ func depWrite(w io.Writer, fdDepName, cwd string, tgt *Tgt, hsh Hash) (err error _, err = io.Copy(w, bytes.NewBuffer(chunkWrite(bytes.Join([][]byte{ {DepTypeIfchange}, inode[:], - []byte(hsh), + hsh[:], []byte(tgt.RelTo(cwd)), }, nil)))) return @@ -170,7 +170,7 @@ func depsWrite(fdDep *os.File, tgts []*Tgt) error { } tgtDir := path.Join(cwd, DirPrefix) if _, errStat := os.Stat(tgt.a); errStat == nil { - err = ErrLine(depWrite(fdDepW, fdDep.Name(), tgtDir, tgt, "")) + err = ErrLine(depWrite(fdDepW, fdDep.Name(), tgtDir, tgt, HashNull)) } else { tgtRel := tgt.RelTo(tgtDir) err = ErrLine(depWriteNonex(fdDepW, fdDep.Name(), tgtRel)) diff --git a/depfix.go b/depfix.go index bbe2ae8..585e93c 100644 --- a/depfix.go +++ b/depfix.go @@ -177,7 +177,7 @@ func depFix(root string) error { chunkWrite(bytes.Join([][]byte{ {DepTypeIfchange}, inode[:], - []byte(hsh), + hsh[:], []byte(name), }, nil)))) case DepTypeIfchangeNonex: @@ -266,7 +266,7 @@ func depFix(root string) error { chunkWrite(bytes.Join([][]byte{ {DepTypeIfchange}, inode[:], - []byte(hsh), + hsh[:], []byte(name), }, nil)))) } diff --git a/hash.go b/hash.go index 420937d..6c0e6d1 100644 --- a/hash.go +++ b/hash.go @@ -6,8 +6,10 @@ import ( const HashLen = 32 -type Hash string +type Hash [HashLen]byte + +var HashNull Hash func (h Hash) String() string { - return hex.EncodeToString([]byte(h)) + return hex.EncodeToString(h[:]) } diff --git a/run.go b/run.go index 4921548..035cd59 100644 --- a/run.go +++ b/run.go @@ -359,7 +359,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error { } } - if err = depWrite(fdDepW, fdDep.Name(), tgtH, doFile, ""); err != nil { + if err = depWrite(fdDepW, fdDep.Name(), tgtH, doFile, HashNull); err != nil { cleanup() return TgtError{tgt, ErrLine(err)} } @@ -739,7 +739,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error { } } else { var hsh Hash - if hshPrev != "" { + if hshPrev != HashNull { _, err = fd.Seek(0, io.SeekStart) if err != nil { err = ErrLine(err) -- 2.48.1