/*
goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2022 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
"errors"
"fmt"
"io"
+ "io/fs"
"log"
"os"
"path"
- "path/filepath"
"strings"
"golang.org/x/sys/unix"
FdOODTgts *os.File
FdOODTgtsLock *os.File
+ OODCache map[string]bool = make(map[string]bool)
+ FileExistsCache map[string]bool = make(map[string]bool)
+
ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
)
+func FileExists(p string) bool {
+ if exists, known := FileExistsCache[p]; known {
+ return exists
+ }
+ _, err := os.Stat(p)
+ if err == nil {
+ FileExistsCache[p] = true
+ return true
+ }
+ if errors.Is(err, fs.ErrNotExist) {
+ FileExistsCache[p] = false
+ }
+ return false
+}
+
type TgtError struct {
Tgt string
Err error
return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
}
-func cwdMustRel(paths ...string) string {
- rel, err := filepath.Rel(Cwd, path.Join(paths...))
- if err != nil {
- panic(err)
- }
- return rel
-}
-
-func cwdAndTgt(tgt string) (string, string) {
- cwd, tgt := path.Split(tgt)
- cwd, err := filepath.Abs(cwd)
- if err != nil {
- panic(err)
- }
- return cwd, tgt
-}
-
func isSrc(cwd, tgt string) bool {
d, f := path.Split(path.Join(cwd, tgt))
- if _, err := os.Stat(path.Join(d, f)); err != nil {
+ if !FileExists(path.Join(d, f)) {
return false
}
- if _, err := os.Stat(path.Join(d, f+".do")); err == nil {
+ if FileExists(path.Join(d, f+".do")) {
return false
}
- if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
+ if FileExists(path.Join(d, RedoDir, f+DepSuffix)) {
return false
}
return true
indent := strings.Repeat(". ", level)
tracef(CDebug, "ood: %s%s checking", indent, tgtOrig)
cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
+ ood, cached := OODCache[path.Join(cwd, tgt)]
+ if cached {
+ tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgtOrig, ood)
+ return ood, nil
+ }
depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
fdDep, err := os.Open(depPath)
if err != nil {
- tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
- return true, nil
+ if isSrc(cwd, tgt) {
+ ood = false
+ tracef(CDebug, "ood: %s%s -> is source", indent, tgtOrig)
+ } else {
+ ood = true
+ tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
+ }
+ OODCache[path.Join(cwd, tgt)] = ood
+ return ood, nil
}
depInfo, err := depRead(fdDep)
fdDep.Close()
if err != nil {
- return true, TgtError{tgtOrig, err}
+ return true, TgtError{tgtOrig, ErrLine(err)}
}
if depInfo.build == BuildUUID {
tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
+ OODCache[path.Join(cwd, tgt)] = false
return false, nil
}
- if _, err := os.Stat(path.Join(cwd, tgt)); err != nil && os.IsNotExist(err) {
+ if !FileExists(path.Join(cwd, tgt)) {
tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
+ OODCache[path.Join(cwd, tgt)] = true
return true, nil
}
- ood := false
for _, dep := range depInfo.ifcreates {
- if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
+ if FileExists(path.Join(cwd, dep)) {
tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
ood = true
goto Done
}
theirHsh := m["Hash"]
tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
+ ood, cached = OODCache[path.Join(cwd, dep)]
+ if cached {
+ tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep, ood)
+ if ood {
+ goto Done
+ }
+ continue
+ }
inode, err := inodeFromFileByPath(path.Join(cwd, dep))
if err != nil {
- if os.IsNotExist(err) {
+ if errors.Is(err, fs.ErrNotExist) {
tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
ood = true
+ OODCache[path.Join(cwd, dep)] = ood
goto Done
}
- return ood, TgtError{tgtOrig, err}
+ return ood, TgtError{tgtOrig, ErrLine(err)}
}
if inode.Size != theirInode.Size {
tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
ood = true
+ OODCache[path.Join(cwd, dep)] = ood
goto Done
}
if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
fd, err := os.Open(path.Join(cwd, dep))
if err != nil {
- return ood, TgtError{tgtOrig, err}
+ return ood, TgtError{tgtOrig, ErrLine(err)}
}
hsh, err := fileHash(fd)
fd.Close()
if err != nil {
- return ood, TgtError{tgtOrig, err}
+ return ood, TgtError{tgtOrig, ErrLine(err)}
}
if theirHsh != hsh {
tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
ood = true
+ OODCache[path.Join(cwd, dep)] = ood
goto Done
}
tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
}
if isSrc(cwd, dep) {
tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
+ OODCache[path.Join(cwd, dep)] = false
continue
}
if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
+ OODCache[path.Join(cwd, dep)] = false
continue
}
- depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
+ depOOD, err := isOODWithTrace(cwd, dep, level+1, seen)
if err != nil {
return ood, TgtError{tgtOrig, err}
}
- if depOod {
+ if depOOD {
tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
ood = true
goto Done
Done:
tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
+ OODCache[path.Join(cwd, tgt)] = ood
return ood, nil
}
level int,
seen map[string]struct{},
) (bool, error) {
- p, err := filepath.Abs(path.Join(cwd, tgtOrig))
- if err != nil {
- panic(err)
- }
+ p := mustAbs(path.Join(cwd, tgtOrig))
_, ood := OODTgts[p]
+ var err error
if ood {
if !isOODByBuildUUID(cwd, tgtOrig) {
tracef(
Whence: io.SeekStart,
}
if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
- log.Fatalln(err)
+ log.Fatal(err)
}
if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
- log.Fatalln(err)
+ log.Fatal(err)
}
if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
- log.Fatalln(err)
+ log.Fatal(err)
}
flock.Type = unix.F_UNLCK
if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
- log.Fatalln(err)
+ log.Fatal(err)
}
return true, nil
}
Whence: io.SeekStart,
}
if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
- log.Fatalln(err)
+ log.Fatal(err)
}
if err = FdOODTgts.Truncate(0); err != nil {
- log.Fatalln(err)
+ log.Fatal(err)
}
flock.Type = unix.F_UNLCK
if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
- log.Fatalln(err)
+ log.Fatal(err)
}
}