2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2022 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 ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
53 type TgtError struct {
58 func (e TgtError) Unwrap() error { return e.Err }
60 func (e TgtError) Error() string {
61 return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
64 func cwdMustRel(paths ...string) string {
65 rel, err := filepath.Rel(Cwd, path.Join(paths...))
72 func cwdAndTgt(tgt string) (string, string) {
73 cwd, tgt := path.Split(tgt)
74 cwd, err := filepath.Abs(cwd)
81 func isSrc(cwd, tgt string) bool {
82 d, f := path.Split(path.Join(cwd, tgt))
83 if _, err := os.Stat(path.Join(d, f)); err != nil {
86 if _, err := os.Stat(path.Join(d, f+".do")); err == nil {
89 if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
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 depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
115 fdDep, err := os.Open(depPath)
117 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
120 depInfo, err := depRead(fdDep)
123 return true, TgtError{tgtOrig, err}
126 if depInfo.build == BuildUUID {
127 tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
130 if _, err := os.Stat(path.Join(cwd, tgt)); err != nil && os.IsNotExist(err) {
131 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
136 for _, dep := range depInfo.ifcreates {
137 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
138 tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
144 for _, m := range depInfo.ifchanges {
147 return ood, TgtError{tgtOrig, ErrMissingTarget}
149 theirInode, err := inodeFromRec(m)
151 return ood, TgtError{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
153 theirHsh := m["Hash"]
154 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
156 fd, err := os.Open(path.Join(cwd, dep))
158 if os.IsNotExist(err) {
159 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
163 return ood, TgtError{tgtOrig, err}
167 inode, err := inodeFromFile(fd)
169 return ood, TgtError{tgtOrig, err}
171 if inode.Size != theirInode.Size {
172 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
176 if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
177 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
179 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
180 hsh, err := fileHash(fd)
182 return ood, TgtError{tgtOrig, err}
185 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
189 tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
194 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
198 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
202 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
203 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
207 depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
209 return ood, TgtError{tgtOrig, err}
212 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
216 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
220 tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
227 seen map[string]struct{},
229 p, err := filepath.Abs(path.Join(cwd, tgtOrig))
235 if !isOODByBuildUUID(cwd, tgtOrig) {
238 "ood: %s%s -> already built",
239 strings.Repeat(". ", level), tgtOrig,
245 "ood: %s%s true, external decision",
246 strings.Repeat(". ", level), tgtOrig,
250 ood, err = isOOD(cwd, tgtOrig, level, seen)
255 flock := unix.Flock_t{
257 Whence: io.SeekStart,
259 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
262 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
265 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
268 flock.Type = unix.F_UNLCK
269 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
275 func oodTgtsClear() {
277 flock := unix.Flock_t{
279 Whence: io.SeekStart,
281 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
284 if err = FdOODTgts.Truncate(0); err != nil {
287 flock.Type = unix.F_UNLCK
288 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {