/* goredo -- redo implementation on pure Go Copyright (C) 2020 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" "io" "os" "path" "path/filepath" "strings" "go.cypherpunks.ru/recfile" ) 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, RedoDir, f+DepSuffix)); err == nil { return false } return true } func isBuiltNow(fdDep *os.File) (bool, *recfile.Reader, error) { r := recfile.NewReader(fdDep) m, err := r.NextMap() if err != nil { return false, nil, err } if m["Build"] == "" { return false, r, errors.New("dep. missing Build:") } return m["Build"] == BuildUUID, r, nil } func isOOD(cwd, tgt string, level int) (bool, error) { tgtOrig := tgt indent := strings.Repeat(". ", level) trace(CDebug, "ood: %s%s checking", indent, tgtOrig) cwd, tgt = cwdAndTgt(path.Join(cwd, tgt)) 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 } defer fdDep.Close() ood := false builtNow, r, err := isBuiltNow(fdDep) if err != nil { return true, err } if builtNow { trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig) return false, nil } var stampOur string for { m, err := r.NextMap() if err != nil { if err == io.EOF { break } return true, err } switch m["Type"] { case "always": trace(CDebug, "ood: %s%s -> always", indent, tgtOrig) ood = true case "ifcreate": theirTgt := m["Target"] if theirTgt == "" { return ood, errors.New("invalid format of dep." + tgtOrig) } if _, err = os.Stat(path.Join(cwd, theirTgt)); err == nil { trace(CDebug, "ood: %s%s -> created", indent, tgtOrig) ood = true } case "ifchange": dep := m["Target"] theirTs := m["Ctime"] theirHsh := m["Hash"] if dep == "" || theirTs == "" { return ood, errors.New("invalid format of dep." + tgtOrig) } 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 continue } return ood, err } defer fd.Close() ts, err := fileCtime(fd) if err != nil { return ood, err } if theirTs == ts { trace(CDebug, "ood: %s%s -> %s: same ctime", indent, tgtOrig, dep) } else if NoHash || theirHsh == "" { trace(CDebug, "ood: %s%s -> %s: ctime differs", indent, tgtOrig, dep) ood = true } else { hsh, err := fileHash(fd) if err != nil { return ood, err } if theirHsh == hsh { trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep) } else { trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep) ood = true } } fd.Close() if ood { continue } 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 } else { depOod, err := isOOD(cwd, dep, level+1) if depOod { ood = true trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep) } else { trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep) } if err != nil { return ood, err } } case "stamp": stampOur = m["Hash"] trace(CDebug, "ood: %s%s -> stamped: %s", indent, tgtOrig, stampOur) default: return ood, errors.New("invalid format of dep." + tgtOrig) } } if ood && stampOur != "" { errs := make(chan error, 0) trace(CDebug, "ood: %s%s running, because stamped", indent, tgtOrig) relTgt, err := filepath.Rel(Cwd, path.Join(cwd, tgt)) if err != nil { return true, err } if err = runScript(relTgt, errs); err != nil { return true, err } if err = <-errs; err != nil { return true, err } fdDep, err := os.Open(depPath) if err != nil { return true, err } defer fdDep.Close() builtNow, r, err := isBuiltNow(fdDep) if err != nil { return true, err } if !builtNow { return true, fmt.Errorf("%s is not built", tgtOrig) } var stampTheir string for { m, err := r.NextMap() if err != nil { if err == io.EOF { break } return true, err } if m["Type"] == "stamp" { stampTheir = m["Hash"] break } } trace(CDebug, "ood: %s%s -> stamp: %s %s", indent, tgtOrig, stampOur, stampTheir) if stampOur == stampTheir { ood = false } } trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood) return ood, nil }