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 map[string]bool = make(map[string]bool)
50 FileExistsCache map[string]bool = make(map[string]bool)
52 ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
55 func FileExists(p string) bool {
56 if exists, known := FileExistsCache[p]; known {
61 FileExistsCache[p] = true
64 if os.IsNotExist(err) {
65 FileExistsCache[p] = false
70 type TgtError struct {
75 func (e TgtError) Unwrap() error { return e.Err }
77 func (e TgtError) Error() string {
78 return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
81 func isSrc(cwd, tgt string) bool {
82 d, f := path.Split(path.Join(cwd, tgt))
83 if !FileExists(path.Join(d, f)) {
86 if FileExists(path.Join(d, f+".do")) {
89 if FileExists(path.Join(d, RedoDir, f+DepSuffix)) {
95 func isOODByBuildUUID(cwd, tgtOrig string) bool {
96 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
97 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
98 fdDep, err := os.Open(depPath)
102 depInfo, err := depRead(fdDep)
104 if err != nil || depInfo.build != BuildUUID {
110 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
111 indent := strings.Repeat(". ", level)
112 tracef(CDebug, "ood: %s%s checking", indent, tgtOrig)
113 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
114 ood, cached := OODCache[path.Join(cwd, tgt)]
116 tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgtOrig, ood)
119 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
120 fdDep, err := os.Open(depPath)
124 tracef(CDebug, "ood: %s%s -> is source", indent, tgtOrig)
127 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
129 OODCache[path.Join(cwd, tgt)] = ood
132 depInfo, err := depRead(fdDep)
135 return true, TgtError{tgtOrig, ErrLine(err)}
138 if depInfo.build == BuildUUID {
139 tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
140 OODCache[path.Join(cwd, tgt)] = false
143 if !FileExists(path.Join(cwd, tgt)) {
144 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
145 OODCache[path.Join(cwd, tgt)] = true
149 for _, dep := range depInfo.ifcreates {
150 if FileExists(path.Join(cwd, dep)) {
151 tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
157 for _, m := range depInfo.ifchanges {
160 return ood, TgtError{tgtOrig, ErrMissingTarget}
162 theirInode, err := inodeFromRec(m)
164 return ood, TgtError{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
166 theirHsh := m["Hash"]
167 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
168 ood, cached = OODCache[path.Join(cwd, dep)]
170 tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep, ood)
177 inode, err := inodeFromFileByPath(path.Join(cwd, dep))
179 if os.IsNotExist(err) {
180 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
182 OODCache[path.Join(cwd, dep)] = ood
185 return ood, TgtError{tgtOrig, ErrLine(err)}
188 if inode.Size != theirInode.Size {
189 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
191 OODCache[path.Join(cwd, dep)] = ood
194 if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
195 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
197 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
198 fd, err := os.Open(path.Join(cwd, dep))
200 return ood, TgtError{tgtOrig, ErrLine(err)}
202 hsh, err := fileHash(fd)
205 return ood, TgtError{tgtOrig, ErrLine(err)}
208 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
210 OODCache[path.Join(cwd, dep)] = ood
213 tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
217 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
221 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
222 OODCache[path.Join(cwd, dep)] = false
226 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
227 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
228 OODCache[path.Join(cwd, dep)] = false
232 depOOD, err := isOODWithTrace(cwd, dep, level+1, seen)
234 return ood, TgtError{tgtOrig, err}
237 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
241 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
245 tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
246 OODCache[path.Join(cwd, tgt)] = ood
253 seen map[string]struct{},
255 p := mustAbs(path.Join(cwd, tgtOrig))
259 if !isOODByBuildUUID(cwd, tgtOrig) {
262 "ood: %s%s -> already built",
263 strings.Repeat(". ", level), tgtOrig,
269 "ood: %s%s true, external decision",
270 strings.Repeat(". ", level), tgtOrig,
274 ood, err = isOOD(cwd, tgtOrig, level, seen)
279 flock := unix.Flock_t{
281 Whence: io.SeekStart,
283 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
286 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
289 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
292 flock.Type = unix.F_UNLCK
293 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
299 func oodTgtsClear() {
301 flock := unix.Flock_t{
303 Whence: io.SeekStart,
305 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
308 if err = FdOODTgts.Truncate(0); err != nil {
311 flock.Type = unix.F_UNLCK
312 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {