]> Cypherpunks.ru repositories - goredo.git/blobdiff - ood.go
Cache file existence state
[goredo.git] / ood.go
diff --git a/ood.go b/ood.go
index fdb0d846b219534549ac2ac31842bbef0a56fb6c..3ce732f10ec70f6526131b5febeb484a88dc3acb 100644 (file)
--- a/ood.go
+++ b/ood.go
@@ -1,6 +1,6 @@
 /*
 goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2022 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
@@ -46,8 +46,27 @@ var (
        OODTgts       map[string]struct{}
        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
@@ -78,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
@@ -125,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
@@ -142,7 +161,7 @@ func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, erro
        for _, m := range depInfo.ifchanges {
                dep := m["Target"]
                if dep == "" {
-                       return ood, TgtError{tgtOrig, errors.New("invalid format of .rec: missing Target")}
+                       return ood, TgtError{tgtOrig, ErrMissingTarget}
                }
                theirInode, err := inodeFromRec(m)
                if err != nil {
@@ -151,7 +170,7 @@ func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, erro
                theirHsh := m["Hash"]
                tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
 
-               fd, err := os.Open(path.Join(cwd, dep))
+               inode, err := inodeFromFileByPath(path.Join(cwd, dep))
                if err != nil {
                        if os.IsNotExist(err) {
                                tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
@@ -160,22 +179,22 @@ func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, erro
                        }
                        return ood, TgtError{tgtOrig, err}
                }
-               defer fd.Close()
 
-               inode, err := inodeFromFile(fd)
-               if err != nil {
-                       return ood, TgtError{tgtOrig, err}
-               }
                if inode.Size != theirInode.Size {
                        tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
                        ood = true
                        goto Done
                }
-               if InodeTrust && inode.Equals(theirInode) {
+               if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
                        tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
                } else {
                        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}
+                       }
                        hsh, err := fileHash(fd)
+                       fd.Close()
                        if err != nil {
                                return ood, TgtError{tgtOrig, err}
                        }
@@ -186,7 +205,6 @@ func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, erro
                        }
                        tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
                }
-               fd.Close() // optimization not to hold it for long
 
                if dep == tgt {
                        tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
@@ -250,7 +268,11 @@ func isOODWithTrace(
                return ood, err
        }
 RecordOODTgt:
-       if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
+       flock := unix.Flock_t{
+               Type:   unix.F_WRLCK,
+               Whence: io.SeekStart,
+       }
+       if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
                log.Fatalln(err)
        }
        if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
@@ -259,7 +281,8 @@ RecordOODTgt:
        if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
                log.Fatalln(err)
        }
-       if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN); err != nil {
+       flock.Type = unix.F_UNLCK
+       if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
                log.Fatalln(err)
        }
        return true, nil
@@ -267,13 +290,18 @@ RecordOODTgt:
 
 func oodTgtsClear() {
        var err error
-       if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
+       flock := unix.Flock_t{
+               Type:   unix.F_WRLCK,
+               Whence: io.SeekStart,
+       }
+       if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
                log.Fatalln(err)
        }
        if err = FdOODTgts.Truncate(0); err != nil {
                log.Fatalln(err)
        }
-       if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN); err != nil {
+       flock.Type = unix.F_UNLCK
+       if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
                log.Fatalln(err)
        }
 }