DepCwd string
ErrBadRecFormat = errors.New("invalid format of .rec")
+ InodeCache = make(map[string][]*Inode)
+ HashCache = make(map[string][]string)
)
func recfileWrite(fdDep io.StringWriter, fields ...recfile.Field) error {
if err != nil {
return err
}
- tracef(CDebug, "stamp: %s <- %s", fdDep.Name(), hex.EncodeToString(hsh))
+ tracef(CDebug, "stamp: %s <- %s", fdDep.Name(), hex.EncodeToString([]byte(hsh)))
return recfileWrite(
fdDep,
recfile.Field{Name: "Type", Value: DepTypeStamp},
- recfile.Field{Name: "Hash", Value: hex.EncodeToString(hsh)},
+ recfile.Field{Name: "Hash", Value: hex.EncodeToString([]byte(hsh))},
)
}
-func fileHash(fd *os.File) ([]byte, error) {
+func fileHash(fd *os.File) (string, error) {
h := blake3.New(32, nil)
if _, err := io.Copy(h, bufio.NewReader(fd)); err != nil {
- return nil, err
+ return "", err
}
- return h.Sum(nil), nil
+ return string(h.Sum(nil)), nil
}
-func depWrite(fdDep *os.File, cwd string, tgt *Tgt, hsh []byte) error {
+func depWrite(fdDep *os.File, cwd string, tgt *Tgt, hsh string) error {
tracef(CDebug, "ifchange: %s <- %s", fdDep.Name(), tgt)
fd, err := os.Open(tgt.a)
if err != nil {
if isDir {
return nil
}
- if hsh == nil {
+ if hsh == "" {
hsh, err = fileHash(fd)
if err != nil {
return ErrLine(err)
fields := []recfile.Field{
{Name: "Type", Value: DepTypeIfchange},
{Name: "Target", Value: tgt.RelTo(cwd)},
- {Name: "Hash", Value: hex.EncodeToString(hsh)},
+ {Name: "Hash", Value: hex.EncodeToString([]byte(hsh))},
}
fields = append(fields, inode.RecfileFields()...)
return recfileWrite(fdDep, fields...)
}
tgtDir := path.Join(cwd, DirPrefix)
if _, errStat := os.Stat(tgt.a); errStat == nil {
- err = ErrLine(depWrite(fdDep, tgtDir, tgt, nil))
+ err = ErrLine(depWrite(fdDep, tgtDir, tgt, ""))
} else {
tgtRel := tgt.RelTo(tgtDir)
tracef(CDebug, "ifchange: %s <- %s (non-existing)", fdDep.Name(), tgtRel)
type DepInfoIfchange struct {
tgt *Tgt
inode *Inode
- hash []byte
+ hash string
}
type DepInfo struct {
build string
always bool
- stamp []byte
+ stamp string
ifcreates []*Tgt
ifchanges []DepInfoIfchange
}
depInfo.ifcreates = append(depInfo.ifcreates,
NewTgt(path.Join(tgt.h, dep)))
case DepTypeIfchange:
- dep := m["Target"]
- if dep == "" {
+ depRaw := m["Target"]
+ if depRaw == "" {
return nil, ErrBadRecFormat
}
inode, err := inodeFromRec(m)
log.Print(err)
return nil, ErrBadRecFormat
}
- hsh := mustHexDecode(m["Hash"])
+ dep := NewTgt(path.Join(tgt.h, depRaw))
+
+ cachedFound := false
+ for _, cachedInode := range InodeCache[dep.a] {
+ if inode.Equals(cachedInode) {
+ inode = cachedInode
+ cachedFound = true
+ break
+ }
+ }
+ if InodeCache != nil && !cachedFound {
+ InodeCache[dep.a] = append(InodeCache[dep.a], inode)
+ }
+
+ hsh := string(mustHexDecode(m["Hash"]))
+ cachedFound = false
+ for _, cachedHash := range HashCache[dep.a] {
+ if hsh == cachedHash {
+ hsh = cachedHash
+ cachedFound = true
+ break
+ }
+ }
+ if HashCache != nil && !cachedFound {
+ HashCache[dep.a] = append(HashCache[dep.a], hsh)
+ }
+
depInfo.ifchanges = append(depInfo.ifchanges, DepInfoIfchange{
- tgt: NewTgt(path.Join(tgt.h, dep)), inode: inode, hash: hsh,
+ tgt: dep, inode: inode, hash: hsh,
})
case DepTypeStamp:
hsh := m["Hash"]
if hsh == "" {
return nil, ErrBadRecFormat
}
- depInfo.stamp = mustHexDecode(hsh)
+ depInfo.stamp = string(mustHexDecode(hsh))
default:
return nil, ErrBadRecFormat
}
package main
import (
- "bytes"
"errors"
"fmt"
"io"
OODCache = make(map[string]bool)
FileExistsCache = make(map[string]bool)
+ DepInfoCache = make(map[string]*DepInfo)
ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
)
tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgt, ood)
return ood, nil
}
- depInfo, err := depRead(tgt)
- if err != nil {
- if errors.Is(err, fs.ErrNotExist) {
- if isSrc(tgt) {
- ood = false
- tracef(CDebug, "ood: %s%s -> is source", indent, tgt)
- } else {
- ood = true
- tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgt, tgt.Dep())
- }
- OODCache[tgt.a] = ood
- return ood, nil
- }
+ depInfo := DepInfoCache[tgt.Dep()]
+ var err error
+ if depInfo == nil {
+ depInfo, err = depRead(tgt)
if err != nil {
- return true, TgtError{tgt, ErrLine(err)}
+ if errors.Is(err, fs.ErrNotExist) {
+ if isSrc(tgt) {
+ ood = false
+ tracef(CDebug, "ood: %s%s -> is source", indent, tgt)
+ } else {
+ ood = true
+ tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgt, tgt.Dep())
+ }
+ OODCache[tgt.a] = ood
+ return ood, nil
+ }
+ if err != nil {
+ return true, TgtError{tgt, ErrLine(err)}
+ }
}
+ DepInfoCache[tgt.Dep()] = depInfo
}
if depInfo.build == BuildUUID {
if err != nil {
return ood, TgtError{tgt, ErrLine(err)}
}
- if !bytes.Equal(dep.hash, hsh) {
+ if dep.hash != hsh {
tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgt, dep.tgt)
ood = true
OODCache[dep.tgt.a] = ood
import (
"bufio"
- "bytes"
"crypto/rand"
"encoding/hex"
"errors"
}
func isModified(depInfo *DepInfo, tgt *Tgt) (
- modified bool, ourInode *Inode, hshPrev []byte, err error,
+ modified bool, ourInode *Inode, hshPrev string, err error,
) {
if depInfo == nil {
return
}
}
- if err = depWrite(fdDep, tgt.h, doFile, nil); err != nil {
+ if err = depWrite(fdDep, tgt.h, doFile, ""); err != nil {
cleanup()
return TgtError{tgt, ErrLine(err)}
}
goto Finish
}
} else {
- var hsh []byte
- if hshPrev != nil {
+ var hsh string
+ if hshPrev != "" {
_, err = fd.Seek(0, io.SeekStart)
if err != nil {
err = ErrLine(err)
err = ErrLine(err)
goto Finish
}
- if bytes.Equal(hsh, hshPrev) {
+ if hsh == hshPrev {
tracef(CDebug, "%s has same hash, not renaming", tgt)
err = ErrLine(os.Remove(fd.Name()))
if err != nil {