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)
140 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
141 OODCache[path.Join(cwd, tgt)] = true
144 depInfo, err := depRead(fdDep)
147 return true, TgtError{tgtOrig, err}
150 if depInfo.build == BuildUUID {
151 tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
152 OODCache[path.Join(cwd, tgt)] = false
155 if !FileExists(path.Join(cwd, tgt)) {
156 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
157 OODCache[path.Join(cwd, tgt)] = true
161 for _, dep := range depInfo.ifcreates {
162 if FileExists(path.Join(cwd, dep)) {
163 tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
169 for _, m := range depInfo.ifchanges {
172 return ood, TgtError{tgtOrig, ErrMissingTarget}
174 theirInode, err := inodeFromRec(m)
176 return ood, TgtError{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
178 theirHsh := m["Hash"]
179 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
180 ood, cached = OODCache[path.Join(cwd, dep)]
182 tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep, ood)
189 inode, err := inodeFromFileByPath(path.Join(cwd, dep))
191 if os.IsNotExist(err) {
192 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
194 OODCache[path.Join(cwd, dep)] = ood
197 return ood, TgtError{tgtOrig, err}
200 if inode.Size != theirInode.Size {
201 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
203 OODCache[path.Join(cwd, dep)] = ood
206 if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
207 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
209 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
210 fd, err := os.Open(path.Join(cwd, dep))
212 return ood, TgtError{tgtOrig, err}
214 hsh, err := fileHash(fd)
217 return ood, TgtError{tgtOrig, err}
220 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
222 OODCache[path.Join(cwd, dep)] = ood
225 tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
229 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
233 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
234 OODCache[path.Join(cwd, dep)] = false
238 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
239 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
240 OODCache[path.Join(cwd, dep)] = false
244 depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
246 return ood, TgtError{tgtOrig, err}
249 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
253 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
257 tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
258 OODCache[path.Join(cwd, tgt)] = ood
265 seen map[string]struct{},
267 p, err := filepath.Abs(path.Join(cwd, tgtOrig))
273 if !isOODByBuildUUID(cwd, tgtOrig) {
276 "ood: %s%s -> already built",
277 strings.Repeat(". ", level), tgtOrig,
283 "ood: %s%s true, external decision",
284 strings.Repeat(". ", level), tgtOrig,
288 ood, err = isOOD(cwd, tgtOrig, level, seen)
293 flock := unix.Flock_t{
295 Whence: io.SeekStart,
297 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
300 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
303 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
306 flock.Type = unix.F_UNLCK
307 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
313 func oodTgtsClear() {
315 flock := unix.Flock_t{
317 Whence: io.SeekStart,
319 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
322 if err = FdOODTgts.Truncate(0); err != nil {
325 flock.Type = unix.F_UNLCK
326 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {