2 goredo -- redo implementation on pure Go
3 Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // Out-of-date determination
31 "go.cypherpunks.ru/recfile"
34 func cwdAndTgt(tgt string) (string, string) {
35 cwd, tgt := path.Split(tgt)
36 cwd, err := filepath.Abs(cwd)
43 func isSrc(cwd, tgt string) bool {
44 d, f := path.Split(path.Join(cwd, tgt))
45 if _, err := os.Stat(path.Join(d, f)); err != nil {
48 if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
54 func isBuiltNow(fdDep *os.File) (bool, *recfile.Reader, error) {
55 r := recfile.NewReader(fdDep)
58 return false, nil, err
61 return false, r, errors.New("dep. missing Build:")
63 return m["Build"] == BuildUUID, r, nil
66 func isOOD(cwd, tgt string, level int) (bool, error) {
68 indent := strings.Repeat(". ", level)
69 trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
70 cwd, tgt = cwdAndTgt(path.Join(cwd, tgt))
71 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
72 fdDep, err := os.Open(depPath)
74 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
80 builtNow, r, err := isBuiltNow(fdDep)
85 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
100 trace(CDebug, "ood: %s%s -> always", indent, tgtOrig)
103 theirTgt := m["Target"]
105 return ood, errors.New("invalid format of dep." + tgtOrig)
107 if _, err = os.Stat(path.Join(cwd, theirTgt)); err == nil {
108 trace(CDebug, "ood: %s%s -> created", indent, tgtOrig)
113 theirTs := m["Ctime"]
114 theirHsh := m["Hash"]
115 if dep == "" || theirTs == "" {
116 return ood, errors.New("invalid format of dep." + tgtOrig)
118 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
119 fd, err := os.Open(path.Join(cwd, dep))
121 if os.IsNotExist(err) {
122 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
129 ts, err := fileCtime(fd)
134 trace(CDebug, "ood: %s%s -> %s: same ctime", indent, tgtOrig, dep)
135 } else if NoHash || theirHsh == "" {
136 trace(CDebug, "ood: %s%s -> %s: ctime differs", indent, tgtOrig, dep)
139 hsh, err := fileHash(fd)
144 trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
146 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
155 trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
159 trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
162 depOod, err := isOOD(cwd, dep, level+1)
165 trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
167 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
175 trace(CDebug, "ood: %s%s -> stamped: %s", indent, tgtOrig, stampOur)
177 return ood, errors.New("invalid format of dep." + tgtOrig)
180 if ood && stampOur != "" {
181 errs := make(chan error, 0)
182 trace(CDebug, "ood: %s%s running, because stamped", indent, tgtOrig)
183 relTgt, err := filepath.Rel(Cwd, path.Join(cwd, tgt))
187 if err = runScript(relTgt, errs); err != nil {
190 if err = <-errs; err != nil {
193 fdDep, err := os.Open(depPath)
198 builtNow, r, err := isBuiltNow(fdDep)
203 return true, fmt.Errorf("%s is not built", tgtOrig)
205 var stampTheir string
207 m, err := r.NextMap()
214 if m["Type"] == "stamp" {
215 stampTheir = m["Hash"]
219 trace(CDebug, "ood: %s%s -> stamp: %s %s", indent, tgtOrig, stampOur, stampTheir)
220 if stampOur == stampTheir {
224 trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)