/* goredo -- djb's redo implementation on pure Go Copyright (C) 2020-2021 Sergey Matveev 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 the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ // Out-of-date determination package main import ( "errors" "fmt" "os" "path" "path/filepath" "strings" "golang.org/x/sys/unix" ) const ( DepTypeIfcreate = "ifcreate" DepTypeIfchange = "ifchange" DepTypeAlways = "always" DepTypeStamp = "stamp" EnvOODTgtsFd = "REDO_OOD_TGTS_FD" EnvOODTgtsLockFd = "REDO_OOD_TGTS_LOCK_FD" ) var ( OODTgts map[string]struct{} FdOODTgts *os.File FdOODTgtsLock *os.File ) type TgtErr struct { Tgt string Err error } func (e TgtErr) Unwrap() error { return e.Err } func (e TgtErr) Error() string { 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 { return false } if _, err := os.Stat(path.Join(d, f+".do")); err == nil { return false } if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil { return false } return true } func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) { indent := strings.Repeat(". ", level) trace(CDebug, "ood: %s%s checking", indent, tgtOrig) cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig)) depPath := path.Join(cwd, RedoDir, tgt+DepSuffix) fdDep, err := os.Open(depPath) if err != nil { trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath) return true, nil } depInfo, err := depRead(fdDep) fdDep.Close() if err != nil { return true, TgtErr{tgtOrig, err} } if depInfo.build == BuildUUID { trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig) return false, nil } ood := false for _, dep := range depInfo.ifcreates { if _, err := os.Stat(path.Join(cwd, dep)); err == nil { trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep) ood = true goto Done } } for _, m := range depInfo.ifchanges { dep := m["Target"] if dep == "" { return ood, TgtErr{tgtOrig, errors.New("invalid format of .rec: missing Target")} } theirInode, err := inodeFromRec(m) if err != nil { return ood, TgtErr{tgtOrig, fmt.Errorf("invalid format of .rec: %v", err)} } theirHsh := m["Hash"] trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep) fd, err := os.Open(path.Join(cwd, dep)) if err != nil { if os.IsNotExist(err) { trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep) ood = true goto Done } return ood, TgtErr{tgtOrig, err} } defer fd.Close() inode, err := inodeFromFile(fd) if err != nil { return ood, TgtErr{tgtOrig, err} } if inode.Size != theirInode.Size { trace(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep) ood = true goto Done } if InodeTrust && inode.Equals(theirInode) { trace(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep) } else { trace(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep) hsh, err := fileHash(fd) if err != nil { return ood, TgtErr{tgtOrig, err} } if theirHsh != hsh { trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep) ood = true goto Done } trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep) } fd.Close() // optimization not to hold it for long if dep == tgt { trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep) continue } if isSrc(cwd, dep) { trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep) continue } if _, ok := seen[cwdMustRel(cwd, dep)]; ok { trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep) continue } depOod, err := isOODWithTrace(cwd, dep, level+1, seen) if err != nil { return ood, TgtErr{tgtOrig, err} } if depOod { trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep) ood = true goto Done } trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep) } Done: trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood) return ood, nil } func isOODWithTrace( cwd, tgtOrig string, level int, seen map[string]struct{}, ) (bool, error) { p, err := filepath.Abs(path.Join(cwd, tgtOrig)) if err != nil { panic(err) } _, ood := OODTgts[p] if ood { trace( CDebug, "ood: %s%s true, external decision", strings.Repeat(". ", level), tgtOrig, ) goto RecordOODTgt } ood, err = isOOD(cwd, tgtOrig, level, seen) if !ood { return ood, err } RecordOODTgt: if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil { panic(err) } if _, err = FdOODTgts.Seek(0, os.SEEK_END); err != nil { panic(err) } if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil { panic(err) } unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN) return true, nil } func oodTgtsClear() { if err := unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil { panic(err) } if err := FdOODTgts.Truncate(0); err != nil { panic(err) } unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN) }