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
30 "golang.org/x/sys/unix"
34 DepTypeIfcreate = "ifcreate"
35 DepTypeIfchange = "ifchange"
36 DepTypeAlways = "always"
37 DepTypeStamp = "stamp"
39 EnvOODTgtsFd = "REDO_OOD_TGTS_FD"
40 EnvOODTgtsLockFd = "REDO_OOD_TGTS_LOCK_FD"
44 OODTgts map[string]struct{}
46 FdOODTgtsLock *os.File
54 func (e TgtErr) Unwrap() error { return e.Err }
56 func (e TgtErr) Error() string {
57 return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
60 func cwdMustRel(paths ...string) string {
61 rel, err := filepath.Rel(Cwd, path.Join(paths...))
68 func cwdAndTgt(tgt string) (string, string) {
69 cwd, tgt := path.Split(tgt)
70 cwd, err := filepath.Abs(cwd)
77 func isSrc(cwd, tgt string) bool {
78 d, f := path.Split(path.Join(cwd, tgt))
79 if _, err := os.Stat(path.Join(d, f)); err != nil {
82 if _, err := os.Stat(path.Join(d, f+".do")); err == nil {
85 if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
91 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
92 indent := strings.Repeat(". ", level)
93 trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
94 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
95 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
96 fdDep, err := os.Open(depPath)
98 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
101 depInfo, err := depRead(fdDep)
104 return true, TgtErr{tgtOrig, err}
107 if depInfo.build == BuildUUID {
108 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
113 for _, dep := range depInfo.ifcreates {
114 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
115 trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
121 for _, m := range depInfo.ifchanges {
124 return ood, TgtErr{tgtOrig, errors.New("invalid format of .rec: missing Target")}
126 theirInode, err := inodeFromRec(m)
128 return ood, TgtErr{tgtOrig, fmt.Errorf("invalid format of .rec: %v", err)}
130 theirHsh := m["Hash"]
131 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
133 fd, err := os.Open(path.Join(cwd, dep))
135 if os.IsNotExist(err) {
136 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
140 return ood, TgtErr{tgtOrig, err}
144 inode, err := inodeFromFile(fd)
146 return ood, TgtErr{tgtOrig, err}
148 if inode.Size != theirInode.Size {
149 trace(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
153 if InodeTrust && inode.Equals(theirInode) {
154 trace(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
156 trace(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
157 hsh, err := fileHash(fd)
159 return ood, TgtErr{tgtOrig, err}
162 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
166 trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
168 fd.Close() // optimization not to hold it for long
171 trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
175 trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
179 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
180 trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
184 depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
186 return ood, TgtErr{tgtOrig, err}
189 trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
193 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
197 trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
204 seen map[string]struct{},
206 p, err := filepath.Abs(path.Join(cwd, tgtOrig))
214 "ood: %s%s true, external decision",
215 strings.Repeat(". ", level), tgtOrig,
219 ood, err = isOOD(cwd, tgtOrig, level, seen)
224 if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
227 if _, err = FdOODTgts.Seek(0, os.SEEK_END); err != nil {
230 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
233 unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
237 func oodTgtsClear() {
238 if err := unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
241 if err := FdOODTgts.Truncate(0); err != nil {
244 unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)