]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
Raise copyright years
[goredo.git] / ood.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 // Out-of-date determination
19
20 package main
21
22 import (
23         "errors"
24         "fmt"
25         "io"
26         "log"
27         "os"
28         "path"
29         "path/filepath"
30         "strings"
31
32         "golang.org/x/sys/unix"
33 )
34
35 const (
36         DepTypeIfcreate = "ifcreate"
37         DepTypeIfchange = "ifchange"
38         DepTypeAlways   = "always"
39         DepTypeStamp    = "stamp"
40
41         EnvOODTgtsFd     = "REDO_OOD_TGTS_FD"
42         EnvOODTgtsLockFd = "REDO_OOD_TGTS_LOCK_FD"
43 )
44
45 var (
46         OODTgts       map[string]struct{}
47         FdOODTgts     *os.File
48         FdOODTgtsLock *os.File
49
50         OODCache        map[string]bool = make(map[string]bool)
51         FileExistsCache map[string]bool = make(map[string]bool)
52
53         ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
54 )
55
56 func FileExists(p string) bool {
57         if exists, known := FileExistsCache[p]; known {
58                 return exists
59         }
60         _, err := os.Stat(p)
61         if err == nil {
62                 FileExistsCache[p] = true
63                 return true
64         }
65         if os.IsNotExist(err) {
66                 FileExistsCache[p] = false
67         }
68         return false
69 }
70
71 type TgtError struct {
72         Tgt string
73         Err error
74 }
75
76 func (e TgtError) Unwrap() error { return e.Err }
77
78 func (e TgtError) Error() string {
79         return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
80 }
81
82 func cwdMustRel(paths ...string) string {
83         rel, err := filepath.Rel(Cwd, path.Join(paths...))
84         if err != nil {
85                 panic(err)
86         }
87         return rel
88 }
89
90 func cwdAndTgt(tgt string) (string, string) {
91         cwd, tgt := path.Split(tgt)
92         cwd, err := filepath.Abs(cwd)
93         if err != nil {
94                 panic(err)
95         }
96         return cwd, tgt
97 }
98
99 func isSrc(cwd, tgt string) bool {
100         d, f := path.Split(path.Join(cwd, tgt))
101         if !FileExists(path.Join(d, f)) {
102                 return false
103         }
104         if FileExists(path.Join(d, f+".do")) {
105                 return false
106         }
107         if FileExists(path.Join(d, RedoDir, f+DepSuffix)) {
108                 return false
109         }
110         return true
111 }
112
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)
117         if err != nil {
118                 return true
119         }
120         depInfo, err := depRead(fdDep)
121         fdDep.Close()
122         if err != nil || depInfo.build != BuildUUID {
123                 return true
124         }
125         return false
126 }
127
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)]
133         if cached {
134                 tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgtOrig, ood)
135                 return ood, nil
136         }
137         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
138         fdDep, err := os.Open(depPath)
139         if err != nil {
140                 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
141                 OODCache[path.Join(cwd, tgt)] = true
142                 return true, nil
143         }
144         depInfo, err := depRead(fdDep)
145         fdDep.Close()
146         if err != nil {
147                 return true, TgtError{tgtOrig, err}
148         }
149
150         if depInfo.build == BuildUUID {
151                 tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
152                 OODCache[path.Join(cwd, tgt)] = false
153                 return false, nil
154         }
155         if !FileExists(path.Join(cwd, tgt)) {
156                 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
157                 OODCache[path.Join(cwd, tgt)] = true
158                 return true, nil
159         }
160
161         for _, dep := range depInfo.ifcreates {
162                 if FileExists(path.Join(cwd, dep)) {
163                         tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
164                         ood = true
165                         goto Done
166                 }
167         }
168
169         for _, m := range depInfo.ifchanges {
170                 dep := m["Target"]
171                 if dep == "" {
172                         return ood, TgtError{tgtOrig, ErrMissingTarget}
173                 }
174                 theirInode, err := inodeFromRec(m)
175                 if err != nil {
176                         return ood, TgtError{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
177                 }
178                 theirHsh := m["Hash"]
179                 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
180                 ood, cached = OODCache[path.Join(cwd, dep)]
181                 if cached {
182                         tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep, ood)
183                         if ood {
184                                 goto Done
185                         }
186                         continue
187                 }
188
189                 inode, err := inodeFromFileByPath(path.Join(cwd, dep))
190                 if err != nil {
191                         if os.IsNotExist(err) {
192                                 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
193                                 ood = true
194                                 OODCache[path.Join(cwd, dep)] = ood
195                                 goto Done
196                         }
197                         return ood, TgtError{tgtOrig, err}
198                 }
199
200                 if inode.Size != theirInode.Size {
201                         tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
202                         ood = true
203                         OODCache[path.Join(cwd, dep)] = ood
204                         goto Done
205                 }
206                 if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
207                         tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
208                 } else {
209                         tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
210                         fd, err := os.Open(path.Join(cwd, dep))
211                         if err != nil {
212                                 return ood, TgtError{tgtOrig, err}
213                         }
214                         hsh, err := fileHash(fd)
215                         fd.Close()
216                         if err != nil {
217                                 return ood, TgtError{tgtOrig, err}
218                         }
219                         if theirHsh != hsh {
220                                 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
221                                 ood = true
222                                 OODCache[path.Join(cwd, dep)] = ood
223                                 goto Done
224                         }
225                         tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
226                 }
227
228                 if dep == tgt {
229                         tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
230                         continue
231                 }
232                 if isSrc(cwd, dep) {
233                         tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
234                         OODCache[path.Join(cwd, dep)] = false
235                         continue
236                 }
237
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
241                         continue
242                 }
243
244                 depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
245                 if err != nil {
246                         return ood, TgtError{tgtOrig, err}
247                 }
248                 if depOod {
249                         tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
250                         ood = true
251                         goto Done
252                 }
253                 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
254         }
255
256 Done:
257         tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
258         OODCache[path.Join(cwd, tgt)] = ood
259         return ood, nil
260 }
261
262 func isOODWithTrace(
263         cwd, tgtOrig string,
264         level int,
265         seen map[string]struct{},
266 ) (bool, error) {
267         p, err := filepath.Abs(path.Join(cwd, tgtOrig))
268         if err != nil {
269                 panic(err)
270         }
271         _, ood := OODTgts[p]
272         if ood {
273                 if !isOODByBuildUUID(cwd, tgtOrig) {
274                         tracef(
275                                 CDebug,
276                                 "ood: %s%s -> already built",
277                                 strings.Repeat(". ", level), tgtOrig,
278                         )
279                         return false, nil
280                 }
281                 tracef(
282                         CDebug,
283                         "ood: %s%s true, external decision",
284                         strings.Repeat(". ", level), tgtOrig,
285                 )
286                 goto RecordOODTgt
287         }
288         ood, err = isOOD(cwd, tgtOrig, level, seen)
289         if !ood {
290                 return ood, err
291         }
292 RecordOODTgt:
293         flock := unix.Flock_t{
294                 Type:   unix.F_WRLCK,
295                 Whence: io.SeekStart,
296         }
297         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
298                 log.Fatalln(err)
299         }
300         if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
301                 log.Fatalln(err)
302         }
303         if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
304                 log.Fatalln(err)
305         }
306         flock.Type = unix.F_UNLCK
307         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
308                 log.Fatalln(err)
309         }
310         return true, nil
311 }
312
313 func oodTgtsClear() {
314         var err error
315         flock := unix.Flock_t{
316                 Type:   unix.F_WRLCK,
317                 Whence: io.SeekStart,
318         }
319         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
320                 log.Fatalln(err)
321         }
322         if err = FdOODTgts.Truncate(0); err != nil {
323                 log.Fatalln(err)
324         }
325         flock.Type = unix.F_UNLCK
326         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
327                 log.Fatalln(err)
328         }
329 }