]> Cypherpunks.ru repositories - goredo.git/commitdiff
Hash explicit type
authorSergey Matveev <stargrave@stargrave.org>
Wed, 4 Oct 2023 16:46:40 +0000 (19:46 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 4 Oct 2023 16:46:40 +0000 (19:46 +0300)
dep.go
depfix.go
run.go

diff --git a/dep.go b/dep.go
index 94daa2a7a8be11629ea33715741df2d648a4b6df..a1e45ae62c0d8cb013e1abe26ca5a7b47f375727 100644 (file)
--- 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
                }
index 87ecd882961cf8026d2398dc0dcf4a96bbbac2c9..bc4452cef820c3a40b507bd860ec9094be701d5a 100644 (file)
--- a/depfix.go
+++ b/depfix.go
@@ -18,7 +18,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 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 7a6ac37ed124474f787ccf7b1ef267e909529d46..48c2f10671ab3fa46125879b371e6adf0d34ab11 100644 (file)
--- 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 {