"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 {
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 {
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...)
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:")
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 {
if hsh == "" {
return nil, ErrBadRecFormat
}
- depInfo.stamp = string(mustHexDecode(hsh))
+ depInfo.stamp = mustHashDecode(hsh)
default:
return nil, ErrBadRecFormat
}