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 "golang.org/x/sys/unix"
36 DepTypeIfcreate = "ifcreate"
37 DepTypeIfchange = "ifchange"
38 DepTypeAlways = "always"
39 DepTypeStamp = "stamp"
41 EnvOODTgtsFd = "REDO_OOD_TGTS_FD"
42 EnvOODTgtsLockFd = "REDO_OOD_TGTS_LOCK_FD"
46 OODTgts map[string]struct{}
48 FdOODTgtsLock *os.File
56 func (e TgtErr) Unwrap() error { return e.Err }
58 func (e TgtErr) Error() string {
59 return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
62 func cwdMustRel(paths ...string) string {
63 rel, err := filepath.Rel(Cwd, path.Join(paths...))
70 func cwdAndTgt(tgt string) (string, string) {
71 cwd, tgt := path.Split(tgt)
72 cwd, err := filepath.Abs(cwd)
79 func isSrc(cwd, tgt string) bool {
80 d, f := path.Split(path.Join(cwd, tgt))
81 if _, err := os.Stat(path.Join(d, f)); err != nil {
84 if _, err := os.Stat(path.Join(d, f+".do")); err == nil {
87 if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
93 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
94 indent := strings.Repeat(". ", level)
95 trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
96 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
97 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
98 fdDep, err := os.Open(depPath)
100 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
103 depInfo, err := depRead(fdDep)
106 return true, TgtErr{tgtOrig, err}
109 if depInfo.build == BuildUUID {
110 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
115 for _, dep := range depInfo.ifcreates {
116 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
117 trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
123 for _, m := range depInfo.ifchanges {
126 return ood, TgtErr{tgtOrig, errors.New("invalid format of .rec: missing Target")}
128 theirInode, err := inodeFromRec(m)
130 return ood, TgtErr{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
132 theirHsh := m["Hash"]
133 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
135 fd, err := os.Open(path.Join(cwd, dep))
137 if os.IsNotExist(err) {
138 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
142 return ood, TgtErr{tgtOrig, err}
146 inode, err := inodeFromFile(fd)
148 return ood, TgtErr{tgtOrig, err}
150 if inode.Size != theirInode.Size {
151 trace(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
155 if InodeTrust && inode.Equals(theirInode) {
156 trace(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
158 trace(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
159 hsh, err := fileHash(fd)
161 return ood, TgtErr{tgtOrig, err}
164 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
168 trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
170 fd.Close() // optimization not to hold it for long
173 trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
177 trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
181 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
182 trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
186 depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
188 return ood, TgtErr{tgtOrig, err}
191 trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
195 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
199 trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
206 seen map[string]struct{},
208 p, err := filepath.Abs(path.Join(cwd, tgtOrig))
216 "ood: %s%s true, external decision",
217 strings.Repeat(". ", level), tgtOrig,
221 ood, err = isOOD(cwd, tgtOrig, level, seen)
226 if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
229 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
232 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
235 unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
239 func oodTgtsClear() {
240 if err := unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
243 if err := FdOODTgts.Truncate(0); err != nil {
246 unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)