]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
isModified check must only look at ifchanges
[goredo.git] / ood.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2021 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
51 type TgtErr struct {
52         Tgt string
53         Err error
54 }
55
56 func (e TgtErr) Unwrap() error { return e.Err }
57
58 func (e TgtErr) Error() string {
59         return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
60 }
61
62 func cwdMustRel(paths ...string) string {
63         rel, err := filepath.Rel(Cwd, path.Join(paths...))
64         if err != nil {
65                 panic(err)
66         }
67         return rel
68 }
69
70 func cwdAndTgt(tgt string) (string, string) {
71         cwd, tgt := path.Split(tgt)
72         cwd, err := filepath.Abs(cwd)
73         if err != nil {
74                 panic(err)
75         }
76         return cwd, tgt
77 }
78
79 func isSrc(cwd, tgt string) bool {
80         d, f := path.Split(path.Join(cwd, tgt))
81         if _, err := os.Stat(path.Join(d, f)); err != nil {
82                 return false
83         }
84         if _, err := os.Stat(path.Join(d, f+".do")); err == nil {
85                 return false
86         }
87         if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
88                 return false
89         }
90         return true
91 }
92
93 func isOODByBuildUUID(cwd, tgtOrig string) bool {
94         cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
95         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
96         fdDep, err := os.Open(depPath)
97         if err != nil {
98                 return true
99         }
100         depInfo, err := depRead(fdDep)
101         fdDep.Close()
102         if err != nil || depInfo.build != BuildUUID {
103                 return true
104         }
105         return false
106 }
107
108 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
109         indent := strings.Repeat(". ", level)
110         trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
111         cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
112         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
113         fdDep, err := os.Open(depPath)
114         if err != nil {
115                 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
116                 return true, nil
117         }
118         depInfo, err := depRead(fdDep)
119         fdDep.Close()
120         if err != nil {
121                 return true, TgtErr{tgtOrig, err}
122         }
123
124         if depInfo.build == BuildUUID {
125                 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
126                 return false, nil
127         }
128         if _, err := os.Stat(path.Join(cwd, tgt)); err != nil && os.IsNotExist(err) {
129                 trace(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
130                 return true, nil
131         }
132         ood := false
133
134         for _, dep := range depInfo.ifcreates {
135                 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
136                         trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
137                         ood = true
138                         goto Done
139                 }
140         }
141
142         for _, m := range depInfo.ifchanges {
143                 dep := m["Target"]
144                 if dep == "" {
145                         return ood, TgtErr{tgtOrig, errors.New("invalid format of .rec: missing Target")}
146                 }
147                 theirInode, err := inodeFromRec(m)
148                 if err != nil {
149                         return ood, TgtErr{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
150                 }
151                 theirHsh := m["Hash"]
152                 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
153
154                 fd, err := os.Open(path.Join(cwd, dep))
155                 if err != nil {
156                         if os.IsNotExist(err) {
157                                 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
158                                 ood = true
159                                 goto Done
160                         }
161                         return ood, TgtErr{tgtOrig, err}
162                 }
163                 defer fd.Close()
164
165                 inode, err := inodeFromFile(fd)
166                 if err != nil {
167                         return ood, TgtErr{tgtOrig, err}
168                 }
169                 if inode.Size != theirInode.Size {
170                         trace(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
171                         ood = true
172                         goto Done
173                 }
174                 if InodeTrust && inode.Equals(theirInode) {
175                         trace(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
176                 } else {
177                         trace(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
178                         hsh, err := fileHash(fd)
179                         if err != nil {
180                                 return ood, TgtErr{tgtOrig, err}
181                         }
182                         if theirHsh != hsh {
183                                 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
184                                 ood = true
185                                 goto Done
186                         }
187                         trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
188                 }
189                 fd.Close() // optimization not to hold it for long
190
191                 if dep == tgt {
192                         trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
193                         continue
194                 }
195                 if isSrc(cwd, dep) {
196                         trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
197                         continue
198                 }
199
200                 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
201                         trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
202                         continue
203                 }
204
205                 depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
206                 if err != nil {
207                         return ood, TgtErr{tgtOrig, err}
208                 }
209                 if depOod {
210                         trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
211                         ood = true
212                         goto Done
213                 }
214                 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
215         }
216
217 Done:
218         trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
219         return ood, nil
220 }
221
222 func isOODWithTrace(
223         cwd, tgtOrig string,
224         level int,
225         seen map[string]struct{},
226 ) (bool, error) {
227         p, err := filepath.Abs(path.Join(cwd, tgtOrig))
228         if err != nil {
229                 panic(err)
230         }
231         _, ood := OODTgts[p]
232         if ood {
233                 if !isOODByBuildUUID(cwd, tgtOrig) {
234                         trace(
235                                 CDebug,
236                                 "ood: %s%s -> already built",
237                                 strings.Repeat(". ", level), tgtOrig,
238                         )
239                         return false, nil
240                 }
241                 trace(
242                         CDebug,
243                         "ood: %s%s true, external decision",
244                         strings.Repeat(". ", level), tgtOrig,
245                 )
246                 goto RecordOODTgt
247         }
248         ood, err = isOOD(cwd, tgtOrig, level, seen)
249         if !ood {
250                 return ood, err
251         }
252 RecordOODTgt:
253         if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
254                 log.Fatalln(err)
255         }
256         if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
257                 log.Fatalln(err)
258         }
259         if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
260                 log.Fatalln(err)
261         }
262         unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
263         return true, nil
264 }
265
266 func oodTgtsClear() {
267         if err := unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
268                 log.Fatalln(err)
269         }
270         if err := FdOODTgts.Truncate(0); err != nil {
271                 log.Fatalln(err)
272         }
273         unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
274 }