/* 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" ) 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 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 rebuildStamped(cwd, tgt, depPath string) (string, error) { relTgt, err := filepath.Rel(Cwd, path.Join(cwd, tgt)) if err != nil { panic(err) } errs := make(chan error, 1) if err = runScript(relTgt, errs); err != nil { return "", err } if err = <-errs; !isOkRun(err) { return "", errors.New("build failed") } fdDep, err := os.Open(depPath) if err != nil { return "", err } defer fdDep.Close() builtNow, r, err := isBuiltNow(fdDep) if err != nil { return "", err } if !builtNow { return "", errors.New("is not built") } var stampTheir string for { m, err := r.NextMap() if err != nil { if err == io.EOF { break } return "", err } if m["Type"] == "stamp" { stampTheir = m["Hash"] break } } return stampTheir, 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, TgtErr{tgtOrig, err} } if builtNow { trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig) return false, nil } var stampOur string ifcreates := []map[string]string{} ifchanges := []map[string]string{} for { m, err := r.NextMap() if err != nil { if err == io.EOF { break } return true, TgtErr{tgtOrig, err} } switch m["Type"] { case "always": trace(CDebug, "ood: %s%s -> always", indent, tgtOrig) ood = true case "ifcreate": ifcreates = append(ifcreates, m) case "ifchange": ifchanges = append(ifchanges, m) case "stamp": stampOur = m["Hash"] trace(CDebug, "ood: %s%s -> stamped: %s", indent, tgtOrig, stampOur) default: return ood, TgtErr{tgtOrig, errors.New("invalid format of .dep")} } } if ood { goto StampCheck } for _, m := range ifcreates { theirTgt := m["Target"] if theirTgt == "" { return ood, TgtErr{tgtOrig, errors.New("invalid format of .dep")} } if _, err = os.Stat(path.Join(cwd, theirTgt)); err == nil { trace(CDebug, "ood: %s%s -> created", indent, tgtOrig) ood = true goto StampCheck } } for _, m := range ifchanges { dep := m["Target"] theirTs := m["Ctime"] theirHsh := m["Hash"] if dep == "" || theirTs == "" { return ood, TgtErr{tgtOrig, errors.New("invalid format of .dep")} } 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 StampCheck } return ood, TgtErr{tgtOrig, err} } defer fd.Close() ts, err := fileCtime(fd) if err != nil { return ood, TgtErr{tgtOrig, 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 goto StampCheck } else { 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 StampCheck } trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep) } fd.Close() 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 } depOod, err := isOOD(cwd, dep, level+1) if err != nil { return ood, TgtErr{tgtOrig, err} } if depOod { trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep) ood = true goto StampCheck } trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep) } StampCheck: if ood && stampOur != "" { trace(CDebug, "ood: %s%s run, because stamped", indent, tgtOrig) stampTheir, err := rebuildStamped(cwd, tgt, depPath) if err != nil { return true, TgtErr{tgtOrig, err} } 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 }