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
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
49 OODCache = make(map[string]bool)
50 FileExistsCache = make(map[string]bool)
51 DepInfoCache = make(map[string]*DepInfo)
53 ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
56 func FileExists(p string) bool {
57 if exists, known := FileExistsCache[p]; known {
62 FileExistsCache[p] = true
65 if errors.Is(err, fs.ErrNotExist) {
66 FileExistsCache[p] = false
71 type TgtError struct {
76 func (e TgtError) Unwrap() error { return e.Err }
78 func (e TgtError) Error() string {
79 return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
82 func isSrc(tgt *Tgt) bool {
83 if !FileExists(tgt.a) {
86 if FileExists(tgt.a + ".do") {
89 if FileExists(tgt.Dep()) {
95 func isOODByBuildUUID(tgt *Tgt) bool {
96 build, err := depReadBuild(tgt.Dep())
97 return err != nil || build != BuildUUID
100 func isOOD(tgt *Tgt, level int, seen map[string]*Tgt) (bool, error) {
101 indent := strings.Repeat(". ", level)
102 tracef(CDebug, "ood: %s%s checking", indent, tgt)
103 ood, cached := OODCache[tgt.a]
105 tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgt, ood)
108 depInfo := DepInfoCache[tgt.Dep()]
111 depInfo, err = depRead(tgt)
113 if errors.Is(err, fs.ErrNotExist) {
116 tracef(CDebug, "ood: %s%s -> is source", indent, tgt)
119 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgt, tgt.Dep())
121 OODCache[tgt.a] = ood
125 return true, TgtError{tgt, ErrLine(err)}
128 DepInfoCache[tgt.Dep()] = depInfo
131 if depInfo.build == BuildUUID {
132 tracef(CDebug, "ood: %s%s -> already built", indent, tgt)
133 OODCache[tgt.a] = false
136 if !FileExists(tgt.a) {
137 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgt)
138 OODCache[tgt.a] = true
142 for _, dep := range depInfo.ifcreates {
143 if FileExists(dep.a) {
144 tracef(CDebug, "ood: %s%s -> %s created", indent, tgt, dep)
150 for _, dep := range depInfo.ifchanges {
151 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgt, dep.tgt)
152 ood, cached = OODCache[dep.tgt.a]
154 tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgt, dep.tgt, ood)
161 inode, err := inodeFromFileByPath(dep.tgt.a)
163 if errors.Is(err, fs.ErrNotExist) {
164 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgt, dep.tgt)
166 OODCache[dep.tgt.a] = ood
169 return ood, TgtError{tgt, ErrLine(err)}
172 if inode.Size != dep.inode.Size {
173 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgt, dep.tgt)
175 OODCache[dep.tgt.a] = ood
178 if InodeTrust != InodeTrustNone && inode.Equals(dep.inode) {
179 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgt, dep.tgt)
181 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgt, dep.tgt)
182 fd, err := os.Open(dep.tgt.a)
184 return ood, TgtError{tgt, ErrLine(err)}
186 hsh, err := fileHash(fd)
189 return ood, TgtError{tgt, ErrLine(err)}
192 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgt, dep.tgt)
194 OODCache[dep.tgt.a] = ood
197 tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgt, dep.tgt)
200 if dep.tgt.a == tgt.a {
201 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgt, dep.tgt)
205 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgt, dep.tgt)
206 OODCache[dep.tgt.a] = false
210 if _, ok := seen[dep.tgt.a]; ok {
211 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgt, dep.tgt)
212 OODCache[dep.tgt.a] = false
216 depOOD, err := isOODWithTrace(dep.tgt, level+1, seen)
218 return ood, TgtError{tgt, err}
221 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgt, dep.tgt)
225 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgt, dep.tgt)
229 tracef(CDebug, "ood: %s%s: %v", indent, tgt, ood)
230 OODCache[tgt.a] = ood
234 func isOODWithTrace(tgt *Tgt, level int, seen map[string]*Tgt) (bool, error) {
235 _, ood := OODTgts[tgt.a]
238 if !isOODByBuildUUID(tgt) {
239 tracef(CDebug, "ood: %s%s -> already built", strings.Repeat(". ", level), tgt)
242 tracef(CDebug, "ood: %s%s true, external decision", strings.Repeat(". ", level), tgt)
245 ood, err = isOOD(tgt, level, seen)
250 flock := unix.Flock_t{
252 Whence: io.SeekStart,
254 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
257 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
260 if _, err := FdOODTgts.WriteString(tgt.a + "\x00"); err != nil {
263 flock.Type = unix.F_UNLCK
264 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
270 func oodTgtsClear() {
272 flock := unix.Flock_t{
274 Whence: io.SeekStart,
276 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
279 if err = FdOODTgts.Truncate(0); err != nil {
282 flock.Type = unix.F_UNLCK
283 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {