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 inode, err := inodeFromFileByPath(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}
166 if inode.Size != theirInode.Size {
167 tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
171 if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
172 tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
174 tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
175 fd, err := os.Open(path.Join(cwd, dep))
177 return ood, TgtError{tgtOrig, err}
179 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)
193 tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
197 tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
201 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
202 tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
206 depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
208 return ood, TgtError{tgtOrig, err}
211 tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
215 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
219 tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
226 seen map[string]struct{},
228 p, err := filepath.Abs(path.Join(cwd, tgtOrig))
234 if !isOODByBuildUUID(cwd, tgtOrig) {
237 "ood: %s%s -> already built",
238 strings.Repeat(". ", level), tgtOrig,
244 "ood: %s%s true, external decision",
245 strings.Repeat(". ", level), tgtOrig,
249 ood, err = isOOD(cwd, tgtOrig, level, seen)
254 flock := unix.Flock_t{
256 Whence: io.SeekStart,
258 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
261 if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
264 if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
267 flock.Type = unix.F_UNLCK
268 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
274 func oodTgtsClear() {
276 flock := unix.Flock_t{
278 Whence: io.SeekStart,
280 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
283 if err = FdOODTgts.Truncate(0); err != nil {
286 flock.Type = unix.F_UNLCK
287 if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {