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
31 "golang.org/x/sys/unix"
35 DepTypeIfcreate = "ifcreate"
36 DepTypeIfchange = "ifchange"
37 DepTypeAlways = "always"
38 DepTypeStamp = "stamp"
40 EnvOODTgtsFd = "REDO_OOD_TGTS_FD"
41 EnvOODTgtsLockFd = "REDO_OOD_TGTS_LOCK_FD"
45 OODTgts map[string]struct{}
47 FdOODTgtsLock *os.File
55 func (e TgtErr) Unwrap() error { return e.Err }
57 func (e TgtErr) Error() string {
58 return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
61 func cwdMustRel(paths ...string) string {
62 rel, err := filepath.Rel(Cwd, path.Join(paths...))
69 func cwdAndTgt(tgt string) (string, string) {
70 cwd, tgt := path.Split(tgt)
71 cwd, err := filepath.Abs(cwd)
78 func isSrc(cwd, tgt string) bool {
79 d, f := path.Split(path.Join(cwd, tgt))
80 if _, err := os.Stat(path.Join(d, f)); err != nil {
83 if _, err := os.Stat(path.Join(d, f+".do")); err == nil {
86 if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
92 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
93 indent := strings.Repeat(". ", level)
94 trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
95 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
96 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
97 fdDep, err := os.Open(depPath)
99 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
102 depInfo, err := depRead(fdDep)
105 return true, TgtErr{tgtOrig, err}
108 if depInfo.build == BuildUUID {
109 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
114 for _, dep := range depInfo.ifcreates {
115 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
116 trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
122 for _, m := range depInfo.ifchanges {
125 return ood, TgtErr{tgtOrig, errors.New("invalid format of .rec: missing Target")}
127 theirInode, err := inodeFromRec(m)
129 return ood, TgtErr{tgtOrig, fmt.Errorf("invalid format of .rec: %v", err)}
131 theirHsh := m["Hash"]
132 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
134 fd, err := os.Open(path.Join(cwd, dep))
136 if os.IsNotExist(err) {
137 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
141 return ood, TgtErr{tgtOrig, err}
145 inode, err := inodeFromFile(fd)
147 return ood, TgtErr{tgtOrig, err}
149 if inode.Size != theirInode.Size {
150 trace(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
154 if InodeTrust && inode.Equals(theirInode) {
155 trace(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
157 trace(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
158 hsh, err := fileHash(fd)
160 return ood, TgtErr{tgtOrig, err}
163 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
167 trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
169 fd.Close() // optimization not to hold it for long
172 trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
176 trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
180 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
181 trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
185 depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
187 return ood, TgtErr{tgtOrig, err}
190 trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
194 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
198 trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
205 seen map[string]struct{},
207 p, err := filepath.Abs(path.Join(cwd, tgtOrig))
215 "ood: %s%s true, external decision",
216 strings.Repeat(". ", level), tgtOrig,
220 ood, err = isOOD(cwd, tgtOrig, level, seen)
225 if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
228 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
231 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
234 unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
238 func oodTgtsClear() {
239 if err := unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
242 if err := FdOODTgts.Truncate(0); err != nil {
245 unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)