From 9aec69aeb699839887d0e7a0d1e7b2f4900e83b0 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 28 Aug 2022 17:09:09 +0300 Subject: [PATCH] Cache file existence state --- do.go | 4 ++-- ood.go | 27 ++++++++++++++++++++++----- run.go | 2 +- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/do.go b/do.go index fd3bde0..4fbb6c9 100644 --- a/do.go +++ b/do.go @@ -34,7 +34,7 @@ const ( var TopDir string func existsDo(fdDep *os.File, cwd, pth string) (bool, error) { - if _, err := os.Stat(path.Join(cwd, pth)); err == nil { + if FileExists(path.Join(cwd, pth)) { return true, nil } return false, ifcreate(fdDep, pth) @@ -89,7 +89,7 @@ func findDo(fdDep *os.File, cwd, tgt string) (string, int, error) { if dirAbs == TopDir { break } - if _, err = os.Stat(path.Join(dirAbs, RedoDir, TopFile)); err == nil { + if FileExists(path.Join(dirAbs, RedoDir, TopFile)) { break } if dirAbs == dirAbsPrev { diff --git a/ood.go b/ood.go index 7a31573..3ce732f 100644 --- a/ood.go +++ b/ood.go @@ -47,9 +47,26 @@ var ( FdOODTgts *os.File FdOODTgtsLock *os.File + 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 os.IsNotExist(err) { + FileExistsCache[p] = false + } + return false +} + type TgtError struct { Tgt string Err error @@ -80,13 +97,13 @@ func cwdAndTgt(tgt string) (string, string) { 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 @@ -127,14 +144,14 @@ func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, erro tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig) 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) 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 diff --git a/run.go b/run.go index 5e67c41..985db83 100644 --- a/run.go +++ b/run.go @@ -126,7 +126,7 @@ func (e RunError) Error() string { } func mkdirs(pth string) error { - if _, err := os.Stat(pth); err == nil { + if FileExists(pth) { return nil } return os.MkdirAll(pth, os.FileMode(0777)) -- 2.44.0