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
32 DepTypeIfcreate = "ifcreate"
33 DepTypeIfchange = "ifchange"
34 DepTypeAlways = "always"
35 DepTypeStamp = "stamp"
43 func (e TgtErr) Unwrap() error { return e.Err }
45 func (e TgtErr) Error() string {
46 return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
49 func cwdMustRel(paths ...string) string {
50 rel, err := filepath.Rel(Cwd, path.Join(paths...))
57 func cwdAndTgt(tgt string) (string, string) {
58 cwd, tgt := path.Split(tgt)
59 cwd, err := filepath.Abs(cwd)
66 func isSrc(cwd, tgt string) bool {
67 d, f := path.Split(path.Join(cwd, tgt))
68 if _, err := os.Stat(path.Join(d, f)); err != nil {
71 if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
77 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
78 indent := strings.Repeat(". ", level)
79 trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
80 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
81 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
82 fdDep, err := os.Open(depPath)
84 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
87 depInfo, err := depRead(fdDep)
90 return true, TgtErr{tgtOrig, err}
93 if depInfo.build == BuildUUID {
94 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
99 for _, dep := range depInfo.ifcreates {
100 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
101 trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
107 for _, m := range depInfo.ifchanges {
109 theirTs := m["Ctime"]
110 theirHsh := m["Hash"]
111 if dep == "" || theirTs == "" {
112 return ood, TgtErr{tgtOrig, errors.New("invalid format of .dep")}
114 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
116 fd, err := os.Open(path.Join(cwd, dep))
118 if os.IsNotExist(err) {
119 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
123 return ood, TgtErr{tgtOrig, err}
127 ts, err := fileCtime(fd)
129 return ood, TgtErr{tgtOrig, err}
132 trace(CDebug, "ood: %s%s -> %s: same ctime", indent, tgtOrig, dep)
134 trace(CDebug, "ood: %s%s -> %s: ctime differs", indent, tgtOrig, dep)
135 hsh, err := fileHash(fd)
137 return ood, TgtErr{tgtOrig, err}
140 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
144 trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
146 fd.Close() // optimization not to hold it for long
149 trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
153 trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
157 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
158 trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
162 depOod, err := isOOD(cwd, dep, level+1, seen)
164 return ood, TgtErr{tgtOrig, err}
167 trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
171 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
175 trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)