2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2023 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
33 "golang.org/x/sys/unix"
37 DepTypeIfcreate = "ifcreate"
38 DepTypeIfchange = "ifchange"
39 DepTypeAlways = "always"
40 DepTypeStamp = "stamp"
42 EnvOODTgtsFd = "REDO_OOD_TGTS_FD"
43 EnvOODTgtsLockFd = "REDO_OOD_TGTS_LOCK_FD"
47 OODTgts map[string]struct{}
49 FdOODTgtsLock *os.File
51 OODCache = make(map[string]bool)
52 FileExistsCache = make(map[string]bool)
54 ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
57 func FileExists(p string) bool {
58 if exists, known := FileExistsCache[p]; known {
63 FileExistsCache[p] = true
66 if errors.Is(err, fs.ErrNotExist) {
67 FileExistsCache[p] = false
72 type TgtError struct {
77 func (e TgtError) Unwrap() error { return e.Err }
79 func (e TgtError) Error() string {
80 return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
83 func isSrc(cwd, tgt string) bool {
84 d, f := path.Split(path.Join(cwd, tgt))
85 if !FileExists(path.Join(d, f)) {
88 if FileExists(path.Join(d, f+".do")) {
91 if FileExists(path.Join(d, RedoDir, f+DepSuffix)) {
97 func isOODByBuildUUID(cwd, tgtOrig string) bool {
98 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
99 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
100 build, err := depReadBuild(depPath)
101 return err != nil || build != BuildUUID
104 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
105 indent := strings.Repeat(". ", level)
106 tracef(CDebug, "ood: %s%s checking", indent, tgtOrig)
107 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
108 ood, cached := OODCache[path.Join(cwd, tgt)]
110 tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgtOrig, ood)
113 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
114 depInfo, err := depRead(depPath)
116 if errors.Is(err, fs.ErrNotExist) {
119 tracef(CDebug, "ood: %s%s -> is source", indent, tgtOrig)
122 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
124 OODCache[path.Join(cwd, tgt)] = ood
128 return true, TgtError{tgtOrig, ErrLine(err)}
132 if depInfo.build == BuildUUID {
133 tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
134 OODCache[path.Join(cwd, tgt)] = false
137 if !FileExists(path.Join(cwd, tgt)) {
138 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
139 OODCache[path.Join(cwd, tgt)] = true
143 for _, dep := range depInfo.ifcreates {
144 if FileExists(path.Join(cwd, dep)) {
145 tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
151 for _, dep := range depInfo.ifchanges {
153 return ood, TgtError{tgtOrig, ErrMissingTarget}
155 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep.tgt)
156 ood, cached = OODCache[path.Join(cwd, dep.tgt)]
158 tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep.tgt, ood)
165 inode, err := inodeFromFileByPath(path.Join(cwd, dep.tgt))
167 if errors.Is(err, fs.ErrNotExist) {
168 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep.tgt)
170 OODCache[path.Join(cwd, dep.tgt)] = ood
173 return ood, TgtError{tgtOrig, ErrLine(err)}
176 if inode.Size != dep.inode.Size {
177 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep.tgt)
179 OODCache[path.Join(cwd, dep.tgt)] = ood
182 if InodeTrust != InodeTrustNone && inode.Equals(dep.inode) {
183 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep.tgt)
185 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep.tgt)
186 fd, err := os.Open(path.Join(cwd, dep.tgt))
188 return ood, TgtError{tgtOrig, ErrLine(err)}
190 hsh, err := fileHash(fd)
193 return ood, TgtError{tgtOrig, ErrLine(err)}
195 if !bytes.Equal(dep.hash, hsh) {
196 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep.tgt)
198 OODCache[path.Join(cwd, dep.tgt)] = ood
201 tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep.tgt)
205 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep.tgt)
208 if isSrc(cwd, dep.tgt) {
209 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep.tgt)
210 OODCache[path.Join(cwd, dep.tgt)] = false
214 if _, ok := seen[cwdMustRel(cwd, dep.tgt)]; ok {
215 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep.tgt)
216 OODCache[path.Join(cwd, dep.tgt)] = false
220 depOOD, err := isOODWithTrace(cwd, dep.tgt, level+1, seen)
222 return ood, TgtError{tgtOrig, err}
225 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep.tgt)
229 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep.tgt)
233 tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
234 OODCache[path.Join(cwd, tgt)] = ood
241 seen map[string]struct{},
243 p := mustAbs(path.Join(cwd, tgtOrig))
247 if !isOODByBuildUUID(cwd, tgtOrig) {
250 "ood: %s%s -> already built",
251 strings.Repeat(". ", level), tgtOrig,
257 "ood: %s%s true, external decision",
258 strings.Repeat(". ", level), tgtOrig,
262 ood, err = isOOD(cwd, tgtOrig, level, seen)
267 flock := unix.Flock_t{
269 Whence: io.SeekStart,
271 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
274 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
277 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
280 flock.Type = unix.F_UNLCK
281 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
287 func oodTgtsClear() {
289 flock := unix.Flock_t{
291 Whence: io.SeekStart,
293 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
296 if err = FdOODTgts.Truncate(0); err != nil {
299 flock.Type = unix.F_UNLCK
300 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {