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 build, err := depReadBuild(depPath)
100 return err != nil || build != BuildUUID
103 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
104 indent := strings.Repeat(". ", level)
105 tracef(CDebug, "ood: %s%s checking", indent, tgtOrig)
106 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
107 ood, cached := OODCache[path.Join(cwd, tgt)]
109 tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgtOrig, ood)
112 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
113 fdDep, err := os.Open(depPath)
117 tracef(CDebug, "ood: %s%s -> is source", indent, tgtOrig)
120 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
122 OODCache[path.Join(cwd, tgt)] = ood
125 depInfo, err := depRead(fdDep)
128 return true, TgtError{tgtOrig, ErrLine(err)}
131 if depInfo.build == BuildUUID {
132 tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
133 OODCache[path.Join(cwd, tgt)] = false
136 if !FileExists(path.Join(cwd, tgt)) {
137 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
138 OODCache[path.Join(cwd, tgt)] = true
142 for _, dep := range depInfo.ifcreates {
143 if FileExists(path.Join(cwd, dep)) {
144 tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
150 for _, m := range depInfo.ifchanges {
153 return ood, TgtError{tgtOrig, ErrMissingTarget}
155 theirInode, err := inodeFromRec(m)
157 return ood, TgtError{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
159 theirHsh := m["Hash"]
160 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
161 ood, cached = OODCache[path.Join(cwd, dep)]
163 tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep, ood)
170 inode, err := inodeFromFileByPath(path.Join(cwd, dep))
172 if errors.Is(err, fs.ErrNotExist) {
173 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
175 OODCache[path.Join(cwd, dep)] = ood
178 return ood, TgtError{tgtOrig, ErrLine(err)}
181 if inode.Size != theirInode.Size {
182 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
184 OODCache[path.Join(cwd, dep)] = ood
187 if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
188 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
190 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
191 fd, err := os.Open(path.Join(cwd, dep))
193 return ood, TgtError{tgtOrig, ErrLine(err)}
195 hsh, err := fileHash(fd)
198 return ood, TgtError{tgtOrig, ErrLine(err)}
201 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
203 OODCache[path.Join(cwd, dep)] = ood
206 tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
210 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
214 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
215 OODCache[path.Join(cwd, dep)] = false
219 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
220 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
221 OODCache[path.Join(cwd, dep)] = false
225 depOOD, err := isOODWithTrace(cwd, dep, level+1, seen)
227 return ood, TgtError{tgtOrig, err}
230 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
234 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
238 tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
239 OODCache[path.Join(cwd, tgt)] = ood
246 seen map[string]struct{},
248 p := mustAbs(path.Join(cwd, tgtOrig))
252 if !isOODByBuildUUID(cwd, tgtOrig) {
255 "ood: %s%s -> already built",
256 strings.Repeat(". ", level), tgtOrig,
262 "ood: %s%s true, external decision",
263 strings.Repeat(". ", level), tgtOrig,
267 ood, err = isOOD(cwd, tgtOrig, level, seen)
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.Seek(0, io.SeekEnd); err != nil {
282 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
285 flock.Type = unix.F_UNLCK
286 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
292 func oodTgtsClear() {
294 flock := unix.Flock_t{
296 Whence: io.SeekStart,
298 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
301 if err = FdOODTgts.Truncate(0); err != nil {
304 flock.Type = unix.F_UNLCK
305 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {