]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
Optimise redo-sources
[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         "io/fs"
27         "log"
28         "os"
29         "path"
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        = make(map[string]bool)
51         FileExistsCache = 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 errors.Is(err, fs.ErrNotExist) {
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 isSrc(cwd, tgt string) bool {
83         d, f := path.Split(path.Join(cwd, tgt))
84         if !FileExists(path.Join(d, f)) {
85                 return false
86         }
87         if FileExists(path.Join(d, f+".do")) {
88                 return false
89         }
90         if FileExists(path.Join(d, RedoDir, f+DepSuffix)) {
91                 return false
92         }
93         return true
94 }
95
96 func isOODByBuildUUID(cwd, tgtOrig string) bool {
97         cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
98         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
99         build, err := depReadBuild(depPath)
100         return err != nil || build != BuildUUID
101 }
102
103 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
104         indent := strings.Repeat(". ", level)
105         tracef(CDebug, "ood: %s%s checking", indent, tgtOrig)
106         cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
107         ood, cached := OODCache[path.Join(cwd, tgt)]
108         if cached {
109                 tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgtOrig, ood)
110                 return ood, nil
111         }
112         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
113         fdDep, err := os.Open(depPath)
114         if err != nil {
115                 if isSrc(cwd, tgt) {
116                         ood = false
117                         tracef(CDebug, "ood: %s%s -> is source", indent, tgtOrig)
118                 } else {
119                         ood = true
120                         tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
121                 }
122                 OODCache[path.Join(cwd, tgt)] = ood
123                 return ood, nil
124         }
125         depInfo, err := depRead(fdDep)
126         fdDep.Close()
127         if err != nil {
128                 return true, TgtError{tgtOrig, ErrLine(err)}
129         }
130
131         if depInfo.build == BuildUUID {
132                 tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
133                 OODCache[path.Join(cwd, tgt)] = false
134                 return false, nil
135         }
136         if !FileExists(path.Join(cwd, tgt)) {
137                 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
138                 OODCache[path.Join(cwd, tgt)] = true
139                 return true, nil
140         }
141
142         for _, dep := range depInfo.ifcreates {
143                 if FileExists(path.Join(cwd, dep)) {
144                         tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
145                         ood = true
146                         goto Done
147                 }
148         }
149
150         for _, m := range depInfo.ifchanges {
151                 dep := m["Target"]
152                 if dep == "" {
153                         return ood, TgtError{tgtOrig, ErrMissingTarget}
154                 }
155                 theirInode, err := inodeFromRec(m)
156                 if err != nil {
157                         return ood, TgtError{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
158                 }
159                 theirHsh := m["Hash"]
160                 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
161                 ood, cached = OODCache[path.Join(cwd, dep)]
162                 if cached {
163                         tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep, ood)
164                         if ood {
165                                 goto Done
166                         }
167                         continue
168                 }
169
170                 inode, err := inodeFromFileByPath(path.Join(cwd, dep))
171                 if err != nil {
172                         if errors.Is(err, fs.ErrNotExist) {
173                                 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
174                                 ood = true
175                                 OODCache[path.Join(cwd, dep)] = ood
176                                 goto Done
177                         }
178                         return ood, TgtError{tgtOrig, ErrLine(err)}
179                 }
180
181                 if inode.Size != theirInode.Size {
182                         tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
183                         ood = true
184                         OODCache[path.Join(cwd, dep)] = ood
185                         goto Done
186                 }
187                 if InodeTrust != InodeTrustNone && inode.Equals(theirInode) {
188                         tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
189                 } else {
190                         tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
191                         fd, err := os.Open(path.Join(cwd, dep))
192                         if err != nil {
193                                 return ood, TgtError{tgtOrig, ErrLine(err)}
194                         }
195                         hsh, err := fileHash(fd)
196                         fd.Close()
197                         if err != nil {
198                                 return ood, TgtError{tgtOrig, ErrLine(err)}
199                         }
200                         if theirHsh != hsh {
201                                 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
202                                 ood = true
203                                 OODCache[path.Join(cwd, dep)] = ood
204                                 goto Done
205                         }
206                         tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
207                 }
208
209                 if dep == tgt {
210                         tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
211                         continue
212                 }
213                 if isSrc(cwd, dep) {
214                         tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
215                         OODCache[path.Join(cwd, dep)] = false
216                         continue
217                 }
218
219                 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
220                         tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
221                         OODCache[path.Join(cwd, dep)] = false
222                         continue
223                 }
224
225                 depOOD, err := isOODWithTrace(cwd, dep, level+1, seen)
226                 if err != nil {
227                         return ood, TgtError{tgtOrig, err}
228                 }
229                 if depOOD {
230                         tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
231                         ood = true
232                         goto Done
233                 }
234                 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
235         }
236
237 Done:
238         tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
239         OODCache[path.Join(cwd, tgt)] = ood
240         return ood, nil
241 }
242
243 func isOODWithTrace(
244         cwd, tgtOrig string,
245         level int,
246         seen map[string]struct{},
247 ) (bool, error) {
248         p := mustAbs(path.Join(cwd, tgtOrig))
249         _, ood := OODTgts[p]
250         var err error
251         if ood {
252                 if !isOODByBuildUUID(cwd, tgtOrig) {
253                         tracef(
254                                 CDebug,
255                                 "ood: %s%s -> already built",
256                                 strings.Repeat(". ", level), tgtOrig,
257                         )
258                         return false, nil
259                 }
260                 tracef(
261                         CDebug,
262                         "ood: %s%s true, external decision",
263                         strings.Repeat(". ", level), tgtOrig,
264                 )
265                 goto RecordOODTgt
266         }
267         ood, err = isOOD(cwd, tgtOrig, level, seen)
268         if !ood {
269                 return ood, err
270         }
271 RecordOODTgt:
272         flock := unix.Flock_t{
273                 Type:   unix.F_WRLCK,
274                 Whence: io.SeekStart,
275         }
276         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
277                 log.Fatal(err)
278         }
279         if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
280                 log.Fatal(err)
281         }
282         if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
283                 log.Fatal(err)
284         }
285         flock.Type = unix.F_UNLCK
286         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
287                 log.Fatal(err)
288         }
289         return true, nil
290 }
291
292 func oodTgtsClear() {
293         var err error
294         flock := unix.Flock_t{
295                 Type:   unix.F_WRLCK,
296                 Whence: io.SeekStart,
297         }
298         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
299                 log.Fatal(err)
300         }
301         if err = FdOODTgts.Truncate(0); err != nil {
302                 log.Fatal(err)
303         }
304         flock.Type = unix.F_UNLCK
305         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
306                 log.Fatal(err)
307         }
308 }