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 {
152 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep.tgt)
153 ood, cached = OODCache[path.Join(cwd, dep.tgt)]
155 tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep.tgt, ood)
162 inode, err := inodeFromFileByPath(path.Join(cwd, dep.tgt))
164 if errors.Is(err, fs.ErrNotExist) {
165 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep.tgt)
167 OODCache[path.Join(cwd, dep.tgt)] = ood
170 return ood, TgtError{tgtOrig, ErrLine(err)}
173 if inode.Size != dep.inode.Size {
174 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep.tgt)
176 OODCache[path.Join(cwd, dep.tgt)] = ood
179 if InodeTrust != InodeTrustNone && inode.Equals(dep.inode) {
180 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep.tgt)
182 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep.tgt)
183 fd, err := os.Open(path.Join(cwd, dep.tgt))
185 return ood, TgtError{tgtOrig, ErrLine(err)}
187 hsh, err := fileHash(fd)
190 return ood, TgtError{tgtOrig, ErrLine(err)}
192 if !bytes.Equal(dep.hash, hsh) {
193 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep.tgt)
195 OODCache[path.Join(cwd, dep.tgt)] = ood
198 tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep.tgt)
202 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep.tgt)
205 if isSrc(cwd, dep.tgt) {
206 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep.tgt)
207 OODCache[path.Join(cwd, dep.tgt)] = false
211 if _, ok := seen[cwdMustRel(cwd, dep.tgt)]; ok {
212 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep.tgt)
213 OODCache[path.Join(cwd, dep.tgt)] = false
217 depOOD, err := isOODWithTrace(cwd, dep.tgt, level+1, seen)
219 return ood, TgtError{tgtOrig, err}
222 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep.tgt)
226 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep.tgt)
230 tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
231 OODCache[path.Join(cwd, tgt)] = ood
238 seen map[string]struct{},
240 p := mustAbs(path.Join(cwd, tgtOrig))
244 if !isOODByBuildUUID(cwd, tgtOrig) {
247 "ood: %s%s -> already built",
248 strings.Repeat(". ", level), tgtOrig,
254 "ood: %s%s true, external decision",
255 strings.Repeat(". ", level), tgtOrig,
259 ood, err = isOOD(cwd, tgtOrig, level, seen)
264 flock := unix.Flock_t{
266 Whence: io.SeekStart,
268 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
271 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
274 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
277 flock.Type = unix.F_UNLCK
278 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
284 func oodTgtsClear() {
286 flock := unix.Flock_t{
288 Whence: io.SeekStart,
290 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
293 if err = FdOODTgts.Truncate(0); err != nil {
296 flock.Type = unix.F_UNLCK
297 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {