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(cwd, tgt string) bool {
83 d, f := path.Split(path.Join(cwd, tgt))
84 if !FileExists(path.Join(d, f)) {
87 if FileExists(path.Join(d, f+".do")) {
90 if FileExists(path.Join(d, RedoDir, f+DepSuffix)) {
96 func isOODByBuildUUID(cwd, tgtOrig string) bool {
97 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
98 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
99 fdDep, err := os.Open(depPath)
103 depInfo, err := depRead(fdDep)
105 if err != nil || depInfo.build != BuildUUID {
111 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
112 indent := strings.Repeat(". ", level)
113 tracef(CDebug, "ood: %s%s checking", indent, tgtOrig)
114 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
115 ood, cached := OODCache[path.Join(cwd, tgt)]
117 tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgtOrig, ood)
120 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
121 fdDep, err := os.Open(depPath)
125 tracef(CDebug, "ood: %s%s -> is source", indent, tgtOrig)
128 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
130 OODCache[path.Join(cwd, tgt)] = ood
133 depInfo, err := depRead(fdDep)
136 return true, TgtError{tgtOrig, ErrLine(err)}
139 if depInfo.build == BuildUUID {
140 tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
141 OODCache[path.Join(cwd, tgt)] = false
144 if !FileExists(path.Join(cwd, tgt)) {
145 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
146 OODCache[path.Join(cwd, tgt)] = true
150 for _, dep := range depInfo.ifcreates {
151 if FileExists(path.Join(cwd, dep)) {
152 tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
158 for _, m := range depInfo.ifchanges {
161 return ood, TgtError{tgtOrig, ErrMissingTarget}
163 theirInode, err := inodeFromRec(m)
165 return ood, TgtError{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
167 theirHsh := m["Hash"]
168 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
169 ood, cached = OODCache[path.Join(cwd, dep)]
171 tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep, ood)
178 inode, err := inodeFromFileByPath(path.Join(cwd, dep))
180 if errors.Is(err, fs.ErrNotExist) {
181 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
183 OODCache[path.Join(cwd, dep)] = ood
186 return ood, TgtError{tgtOrig, ErrLine(err)}
189 if inode.Size != theirInode.Size {
190 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
192 OODCache[path.Join(cwd, dep)] = ood
195 if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
196 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
198 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
199 fd, err := os.Open(path.Join(cwd, dep))
201 return ood, TgtError{tgtOrig, ErrLine(err)}
203 hsh, err := fileHash(fd)
206 return ood, TgtError{tgtOrig, ErrLine(err)}
209 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
211 OODCache[path.Join(cwd, dep)] = ood
214 tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
218 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
222 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
223 OODCache[path.Join(cwd, dep)] = false
227 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
228 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
229 OODCache[path.Join(cwd, dep)] = false
233 depOOD, err := isOODWithTrace(cwd, dep, level+1, seen)
235 return ood, TgtError{tgtOrig, err}
238 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
242 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
246 tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
247 OODCache[path.Join(cwd, tgt)] = ood
254 seen map[string]struct{},
256 p := mustAbs(path.Join(cwd, tgtOrig))
260 if !isOODByBuildUUID(cwd, tgtOrig) {
263 "ood: %s%s -> already built",
264 strings.Repeat(". ", level), tgtOrig,
270 "ood: %s%s true, external decision",
271 strings.Repeat(". ", level), tgtOrig,
275 ood, err = isOOD(cwd, tgtOrig, level, seen)
280 flock := unix.Flock_t{
282 Whence: io.SeekStart,
284 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
287 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
290 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
293 flock.Type = unix.F_UNLCK
294 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
300 func oodTgtsClear() {
302 flock := unix.Flock_t{
304 Whence: io.SeekStart,
306 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
309 if err = FdOODTgts.Truncate(0); err != nil {
312 flock.Type = unix.F_UNLCK
313 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {