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 map[string]bool = make(map[string]bool)
51 FileExistsCache map[string]bool = 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 os.IsNotExist(err) {
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 cwdMustRel(paths ...string) string {
83 rel, err := filepath.Rel(Cwd, path.Join(paths...))
90 func cwdAndTgt(tgt string) (string, string) {
91 cwd, tgt := path.Split(tgt)
92 cwd, err := filepath.Abs(cwd)
99 func isSrc(cwd, tgt string) bool {
100 d, f := path.Split(path.Join(cwd, tgt))
101 if !FileExists(path.Join(d, f)) {
104 if FileExists(path.Join(d, f+".do")) {
107 if FileExists(path.Join(d, RedoDir, f+DepSuffix)) {
113 func isOODByBuildUUID(cwd, tgtOrig string) bool {
114 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
115 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
116 fdDep, err := os.Open(depPath)
120 depInfo, err := depRead(fdDep)
122 if err != nil || depInfo.build != BuildUUID {
128 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
129 indent := strings.Repeat(". ", level)
130 tracef(CDebug, "ood: %s%s checking", indent, tgtOrig)
131 cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
132 ood, cached := OODCache[path.Join(cwd, tgt)]
134 tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgtOrig, ood)
137 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
138 fdDep, err := os.Open(depPath)
142 tracef(CDebug, "ood: %s%s -> is source", indent, tgtOrig)
145 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
147 OODCache[path.Join(cwd, tgt)] = ood
150 depInfo, err := depRead(fdDep)
153 return true, TgtError{tgtOrig, err}
156 if depInfo.build == BuildUUID {
157 tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
158 OODCache[path.Join(cwd, tgt)] = false
161 if !FileExists(path.Join(cwd, tgt)) {
162 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
163 OODCache[path.Join(cwd, tgt)] = true
167 for _, dep := range depInfo.ifcreates {
168 if FileExists(path.Join(cwd, dep)) {
169 tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
175 for _, m := range depInfo.ifchanges {
178 return ood, TgtError{tgtOrig, ErrMissingTarget}
180 theirInode, err := inodeFromRec(m)
182 return ood, TgtError{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
184 theirHsh := m["Hash"]
185 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
186 ood, cached = OODCache[path.Join(cwd, dep)]
188 tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep, ood)
195 inode, err := inodeFromFileByPath(path.Join(cwd, dep))
197 if os.IsNotExist(err) {
198 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
200 OODCache[path.Join(cwd, dep)] = ood
203 return ood, TgtError{tgtOrig, err}
206 if inode.Size != theirInode.Size {
207 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
209 OODCache[path.Join(cwd, dep)] = ood
212 if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
213 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
215 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
216 fd, err := os.Open(path.Join(cwd, dep))
218 return ood, TgtError{tgtOrig, err}
220 hsh, err := fileHash(fd)
223 return ood, TgtError{tgtOrig, err}
226 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
228 OODCache[path.Join(cwd, dep)] = ood
231 tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
235 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
239 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
240 OODCache[path.Join(cwd, dep)] = false
244 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
245 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
246 OODCache[path.Join(cwd, dep)] = false
250 depOOD, err := isOODWithTrace(cwd, dep, level+1, seen)
252 return ood, TgtError{tgtOrig, err}
255 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
259 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
263 tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
264 OODCache[path.Join(cwd, tgt)] = ood
271 seen map[string]struct{},
273 p, err := filepath.Abs(path.Join(cwd, tgtOrig))
279 if !isOODByBuildUUID(cwd, tgtOrig) {
282 "ood: %s%s -> already built",
283 strings.Repeat(". ", level), tgtOrig,
289 "ood: %s%s true, external decision",
290 strings.Repeat(". ", level), tgtOrig,
294 ood, err = isOOD(cwd, tgtOrig, level, seen)
299 flock := unix.Flock_t{
301 Whence: io.SeekStart,
303 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
306 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
309 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
312 flock.Type = unix.F_UNLCK
313 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
319 func oodTgtsClear() {
321 flock := unix.Flock_t{
323 Whence: io.SeekStart,
325 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
328 if err = FdOODTgts.Truncate(0); err != nil {
331 flock.Type = unix.F_UNLCK
332 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {