]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
Lock file must stay opened till the very end of the program
[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         "bytes"
24         "errors"
25         "fmt"
26         "io"
27         "io/fs"
28         "log"
29         "os"
30         "path"
31         "strings"
32
33         "golang.org/x/sys/unix"
34 )
35
36 const (
37         DepTypeIfcreate = "ifcreate"
38         DepTypeIfchange = "ifchange"
39         DepTypeAlways   = "always"
40         DepTypeStamp    = "stamp"
41
42         EnvOODTgtsFd     = "REDO_OOD_TGTS_FD"
43         EnvOODTgtsLockFd = "REDO_OOD_TGTS_LOCK_FD"
44 )
45
46 var (
47         OODTgts       map[string]struct{}
48         FdOODTgts     *os.File
49         FdOODTgtsLock *os.File
50
51         OODCache        = make(map[string]bool)
52         FileExistsCache = make(map[string]bool)
53
54         ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
55 )
56
57 func FileExists(p string) bool {
58         if exists, known := FileExistsCache[p]; known {
59                 return exists
60         }
61         _, err := os.Stat(p)
62         if err == nil {
63                 FileExistsCache[p] = true
64                 return true
65         }
66         if errors.Is(err, fs.ErrNotExist) {
67                 FileExistsCache[p] = false
68         }
69         return false
70 }
71
72 type TgtError struct {
73         Tgt string
74         Err error
75 }
76
77 func (e TgtError) Unwrap() error { return e.Err }
78
79 func (e TgtError) Error() string {
80         return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
81 }
82
83 func isSrc(cwd, tgt string) bool {
84         d, f := path.Split(path.Join(cwd, tgt))
85         if !FileExists(path.Join(d, f)) {
86                 return false
87         }
88         if FileExists(path.Join(d, f+".do")) {
89                 return false
90         }
91         if FileExists(path.Join(d, RedoDir, f+DepSuffix)) {
92                 return false
93         }
94         return true
95 }
96
97 func isOODByBuildUUID(cwd, tgtOrig string) bool {
98         cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
99         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
100         build, err := depReadBuild(depPath)
101         return err != nil || build != BuildUUID
102 }
103
104 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
105         indent := strings.Repeat(". ", level)
106         tracef(CDebug, "ood: %s%s checking", indent, tgtOrig)
107         cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
108         ood, cached := OODCache[path.Join(cwd, tgt)]
109         if cached {
110                 tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgtOrig, ood)
111                 return ood, nil
112         }
113         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
114         depInfo, err := depRead(depPath)
115         if err != nil {
116                 if errors.Is(err, fs.ErrNotExist) {
117                         if isSrc(cwd, tgt) {
118                                 ood = false
119                                 tracef(CDebug, "ood: %s%s -> is source", indent, tgtOrig)
120                         } else {
121                                 ood = true
122                                 tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
123                         }
124                         OODCache[path.Join(cwd, tgt)] = ood
125                         return ood, nil
126                 }
127                 if err != nil {
128                         return true, TgtError{tgtOrig, ErrLine(err)}
129                 }
130         }
131
132         if depInfo.build == BuildUUID {
133                 tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
134                 OODCache[path.Join(cwd, tgt)] = false
135                 return false, nil
136         }
137         if !FileExists(path.Join(cwd, tgt)) {
138                 tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
139                 OODCache[path.Join(cwd, tgt)] = true
140                 return true, nil
141         }
142
143         for _, dep := range depInfo.ifcreates {
144                 if FileExists(path.Join(cwd, dep)) {
145                         tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
146                         ood = true
147                         goto Done
148                 }
149         }
150
151         for _, dep := range depInfo.ifchanges {
152                 tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep.tgt)
153                 ood, cached = OODCache[path.Join(cwd, dep.tgt)]
154                 if cached {
155                         tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep.tgt, ood)
156                         if ood {
157                                 goto Done
158                         }
159                         continue
160                 }
161
162                 inode, err := inodeFromFileByPath(path.Join(cwd, dep.tgt))
163                 if err != nil {
164                         if errors.Is(err, fs.ErrNotExist) {
165                                 tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep.tgt)
166                                 ood = true
167                                 OODCache[path.Join(cwd, dep.tgt)] = ood
168                                 goto Done
169                         }
170                         return ood, TgtError{tgtOrig, ErrLine(err)}
171                 }
172
173                 if inode.Size != dep.inode.Size {
174                         tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep.tgt)
175                         ood = true
176                         OODCache[path.Join(cwd, dep.tgt)] = ood
177                         goto Done
178                 }
179                 if InodeTrust != InodeTrustNone && inode.Equals(dep.inode) {
180                         tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep.tgt)
181                 } else {
182                         tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep.tgt)
183                         fd, err := os.Open(path.Join(cwd, dep.tgt))
184                         if err != nil {
185                                 return ood, TgtError{tgtOrig, ErrLine(err)}
186                         }
187                         hsh, err := fileHash(fd)
188                         fd.Close()
189                         if err != nil {
190                                 return ood, TgtError{tgtOrig, ErrLine(err)}
191                         }
192                         if !bytes.Equal(dep.hash, hsh) {
193                                 tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep.tgt)
194                                 ood = true
195                                 OODCache[path.Join(cwd, dep.tgt)] = ood
196                                 goto Done
197                         }
198                         tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep.tgt)
199                 }
200
201                 if dep.tgt == tgt {
202                         tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep.tgt)
203                         continue
204                 }
205                 if isSrc(cwd, dep.tgt) {
206                         tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep.tgt)
207                         OODCache[path.Join(cwd, dep.tgt)] = false
208                         continue
209                 }
210
211                 if _, ok := seen[cwdMustRel(cwd, dep.tgt)]; ok {
212                         tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep.tgt)
213                         OODCache[path.Join(cwd, dep.tgt)] = false
214                         continue
215                 }
216
217                 depOOD, err := isOODWithTrace(cwd, dep.tgt, level+1, seen)
218                 if err != nil {
219                         return ood, TgtError{tgtOrig, err}
220                 }
221                 if depOOD {
222                         tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep.tgt)
223                         ood = true
224                         goto Done
225                 }
226                 tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep.tgt)
227         }
228
229 Done:
230         tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
231         OODCache[path.Join(cwd, tgt)] = ood
232         return ood, nil
233 }
234
235 func isOODWithTrace(
236         cwd, tgtOrig string,
237         level int,
238         seen map[string]struct{},
239 ) (bool, error) {
240         p := mustAbs(path.Join(cwd, tgtOrig))
241         _, ood := OODTgts[p]
242         var err error
243         if ood {
244                 if !isOODByBuildUUID(cwd, tgtOrig) {
245                         tracef(
246                                 CDebug,
247                                 "ood: %s%s -> already built",
248                                 strings.Repeat(". ", level), tgtOrig,
249                         )
250                         return false, nil
251                 }
252                 tracef(
253                         CDebug,
254                         "ood: %s%s true, external decision",
255                         strings.Repeat(". ", level), tgtOrig,
256                 )
257                 goto RecordOODTgt
258         }
259         ood, err = isOOD(cwd, tgtOrig, level, seen)
260         if !ood {
261                 return ood, err
262         }
263 RecordOODTgt:
264         flock := unix.Flock_t{
265                 Type:   unix.F_WRLCK,
266                 Whence: io.SeekStart,
267         }
268         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
269                 log.Fatal(err)
270         }
271         if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
272                 log.Fatal(err)
273         }
274         if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
275                 log.Fatal(err)
276         }
277         flock.Type = unix.F_UNLCK
278         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
279                 log.Fatal(err)
280         }
281         return true, nil
282 }
283
284 func oodTgtsClear() {
285         var err error
286         flock := unix.Flock_t{
287                 Type:   unix.F_WRLCK,
288                 Whence: io.SeekStart,
289         }
290         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
291                 log.Fatal(err)
292         }
293         if err = FdOODTgts.Truncate(0); err != nil {
294                 log.Fatal(err)
295         }
296         flock.Type = unix.F_UNLCK
297         if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
298                 log.Fatal(err)
299         }
300 }