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
32 "golang.org/x/sys/unix"
36 DepTypeIfcreate = "ifcreate"
37 DepTypeIfchange = "ifchange"
38 DepTypeAlways = "always"
39 DepTypeStamp = "stamp"
41 EnvOODTgtsFd = "REDO_OOD_TGTS_FD"
42 EnvOODTgtsLockFd = "REDO_OOD_TGTS_LOCK_FD"
46 OODTgts map[string]struct{}
48 FdOODTgtsLock *os.File
50 OODCache = make(map[string]bool)
51 FileExistsCache = make(map[string]bool)
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, err := depRead(tgt)
110 if errors.Is(err, fs.ErrNotExist) {
113 tracef(CDebug, "ood: %s%s -> is source", indent, tgt)
116 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgt, tgt.Dep())
118 OODCache[tgt.a] = ood
122 return true, TgtError{tgt, ErrLine(err)}
126 if depInfo.build == BuildUUID {
127 tracef(CDebug, "ood: %s%s -> already built", indent, tgt)
128 OODCache[tgt.a] = false
131 if !FileExists(tgt.a) {
132 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgt)
133 OODCache[tgt.a] = true
137 for _, dep := range depInfo.ifcreates {
138 if FileExists(dep.a) {
139 tracef(CDebug, "ood: %s%s -> %s created", indent, tgt, dep)
145 for _, dep := range depInfo.ifchanges {
146 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgt, dep.tgt)
147 ood, cached = OODCache[dep.tgt.a]
149 tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgt, dep.tgt, ood)
156 inode, err := inodeFromFileByPath(dep.tgt.a)
158 if errors.Is(err, fs.ErrNotExist) {
159 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgt, dep.tgt)
161 OODCache[dep.tgt.a] = ood
164 return ood, TgtError{tgt, ErrLine(err)}
167 if inode.Size != dep.inode.Size {
168 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgt, dep.tgt)
170 OODCache[dep.tgt.a] = ood
173 if InodeTrust != InodeTrustNone && inode.Equals(dep.inode) {
174 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgt, dep.tgt)
176 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgt, dep.tgt)
177 fd, err := os.Open(dep.tgt.a)
179 return ood, TgtError{tgt, ErrLine(err)}
181 hsh, err := fileHash(fd)
184 return ood, TgtError{tgt, ErrLine(err)}
186 if !bytes.Equal(dep.hash, hsh) {
187 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgt, dep.tgt)
189 OODCache[dep.tgt.a] = ood
192 tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgt, dep.tgt)
195 if dep.tgt.a == tgt.a {
196 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgt, dep.tgt)
200 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgt, dep.tgt)
201 OODCache[dep.tgt.a] = false
205 if _, ok := seen[dep.tgt.a]; ok {
206 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgt, dep.tgt)
207 OODCache[dep.tgt.a] = false
211 depOOD, err := isOODWithTrace(dep.tgt, level+1, seen)
213 return ood, TgtError{tgt, err}
216 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgt, dep.tgt)
220 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgt, dep.tgt)
224 tracef(CDebug, "ood: %s%s: %v", indent, tgt, ood)
225 OODCache[tgt.a] = ood
229 func isOODWithTrace(tgt *Tgt, level int, seen map[string]*Tgt) (bool, error) {
230 _, ood := OODTgts[tgt.a]
233 if !isOODByBuildUUID(tgt) {
234 tracef(CDebug, "ood: %s%s -> already built", strings.Repeat(". ", level), tgt)
237 tracef(CDebug, "ood: %s%s true, external decision", strings.Repeat(". ", level), tgt)
240 ood, err = isOOD(tgt, level, seen)
245 flock := unix.Flock_t{
247 Whence: io.SeekStart,
249 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
252 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
255 if _, err := FdOODTgts.WriteString(tgt.a + "\x00"); err != nil {
258 flock.Type = unix.F_UNLCK
259 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
265 func oodTgtsClear() {
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.Truncate(0); err != nil {
277 flock.Type = unix.F_UNLCK
278 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {