2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2021 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, f+".do")); err == nil {
74 if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
80 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
81 indent := strings.Repeat(". ", level)
82 trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
83 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
84 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
85 fdDep, err := os.Open(depPath)
87 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
90 depInfo, err := depRead(fdDep)
93 return true, TgtErr{tgtOrig, err}
96 if depInfo.build == BuildUUID {
97 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
102 for _, dep := range depInfo.ifcreates {
103 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
104 trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
110 for _, m := range depInfo.ifchanges {
113 return ood, TgtErr{tgtOrig, errors.New("invalid format of .dep: missing Target")}
115 theirInode, err := inodeFromRec(m)
117 return ood, TgtErr{tgtOrig, fmt.Errorf("invalid format of .dep: %v", err)}
119 theirHsh := m["Hash"]
120 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
122 fd, err := os.Open(path.Join(cwd, dep))
124 if os.IsNotExist(err) {
125 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
129 return ood, TgtErr{tgtOrig, err}
133 inode, err := inodeFromFile(fd)
135 return ood, TgtErr{tgtOrig, err}
137 if inode.Size != theirInode.Size {
138 trace(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
142 if InodeTrust && inode.Equals(theirInode) {
143 trace(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
145 trace(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
146 hsh, err := fileHash(fd)
148 return ood, TgtErr{tgtOrig, err}
151 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
155 trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
157 fd.Close() // optimization not to hold it for long
160 trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
164 trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
168 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
169 trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
173 depOod, err := isOOD(cwd, dep, level+1, seen)
175 return ood, TgtErr{tgtOrig, err}
178 trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
182 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
186 trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)