]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
Up to date recfile
[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         "os"
26         "path"
27         "path/filepath"
28         "strings"
29
30         "golang.org/x/sys/unix"
31 )
32
33 const (
34         DepTypeIfcreate = "ifcreate"
35         DepTypeIfchange = "ifchange"
36         DepTypeAlways   = "always"
37         DepTypeStamp    = "stamp"
38
39         EnvOODTgtsFd     = "REDO_OOD_TGTS_FD"
40         EnvOODTgtsLockFd = "REDO_OOD_TGTS_LOCK_FD"
41 )
42
43 var (
44         OODTgts       map[string]struct{}
45         FdOODTgts     *os.File
46         FdOODTgtsLock *os.File
47 )
48
49 type TgtErr struct {
50         Tgt string
51         Err error
52 }
53
54 func (e TgtErr) Unwrap() error { return e.Err }
55
56 func (e TgtErr) Error() string {
57         return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
58 }
59
60 func cwdMustRel(paths ...string) string {
61         rel, err := filepath.Rel(Cwd, path.Join(paths...))
62         if err != nil {
63                 panic(err)
64         }
65         return rel
66 }
67
68 func cwdAndTgt(tgt string) (string, string) {
69         cwd, tgt := path.Split(tgt)
70         cwd, err := filepath.Abs(cwd)
71         if err != nil {
72                 panic(err)
73         }
74         return cwd, tgt
75 }
76
77 func isSrc(cwd, tgt string) bool {
78         d, f := path.Split(path.Join(cwd, tgt))
79         if _, err := os.Stat(path.Join(d, f)); err != nil {
80                 return false
81         }
82         if _, err := os.Stat(path.Join(d, f+".do")); err == nil {
83                 return false
84         }
85         if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
86                 return false
87         }
88         return true
89 }
90
91 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
92         indent := strings.Repeat(". ", level)
93         trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
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                 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
99                 return true, nil
100         }
101         depInfo, err := depRead(fdDep)
102         fdDep.Close()
103         if err != nil {
104                 return true, TgtErr{tgtOrig, err}
105         }
106
107         if depInfo.build == BuildUUID {
108                 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
109                 return false, nil
110         }
111         ood := false
112
113         for _, dep := range depInfo.ifcreates {
114                 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
115                         trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
116                         ood = true
117                         goto Done
118                 }
119         }
120
121         for _, m := range depInfo.ifchanges {
122                 dep := m["Target"]
123                 if dep == "" {
124                         return ood, TgtErr{tgtOrig, errors.New("invalid format of .rec: missing Target")}
125                 }
126                 theirInode, err := inodeFromRec(m)
127                 if err != nil {
128                         return ood, TgtErr{tgtOrig, fmt.Errorf("invalid format of .rec: %v", err)}
129                 }
130                 theirHsh := m["Hash"]
131                 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
132
133                 fd, err := os.Open(path.Join(cwd, dep))
134                 if err != nil {
135                         if os.IsNotExist(err) {
136                                 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
137                                 ood = true
138                                 goto Done
139                         }
140                         return ood, TgtErr{tgtOrig, err}
141                 }
142                 defer fd.Close()
143
144                 inode, err := inodeFromFile(fd)
145                 if err != nil {
146                         return ood, TgtErr{tgtOrig, err}
147                 }
148                 if inode.Size != theirInode.Size {
149                         trace(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
150                         ood = true
151                         goto Done
152                 }
153                 if InodeTrust && inode.Equals(theirInode) {
154                         trace(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
155                 } else {
156                         trace(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
157                         hsh, err := fileHash(fd)
158                         if err != nil {
159                                 return ood, TgtErr{tgtOrig, err}
160                         }
161                         if theirHsh != hsh {
162                                 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
163                                 ood = true
164                                 goto Done
165                         }
166                         trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
167                 }
168                 fd.Close() // optimization not to hold it for long
169
170                 if dep == tgt {
171                         trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
172                         continue
173                 }
174                 if isSrc(cwd, dep) {
175                         trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
176                         continue
177                 }
178
179                 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
180                         trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
181                         continue
182                 }
183
184                 depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
185                 if err != nil {
186                         return ood, TgtErr{tgtOrig, err}
187                 }
188                 if depOod {
189                         trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
190                         ood = true
191                         goto Done
192                 }
193                 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
194         }
195
196 Done:
197         trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
198         return ood, nil
199 }
200
201 func isOODWithTrace(
202         cwd, tgtOrig string,
203         level int,
204         seen map[string]struct{},
205 ) (bool, error) {
206         p, err := filepath.Abs(path.Join(cwd, tgtOrig))
207         if err != nil {
208                 panic(err)
209         }
210         _, ood := OODTgts[p]
211         if ood {
212                 trace(
213                         CDebug,
214                         "ood: %s%s true, external decision",
215                         strings.Repeat(". ", level), tgtOrig,
216                 )
217                 goto RecordOODTgt
218         }
219         ood, err = isOOD(cwd, tgtOrig, level, seen)
220         if !ood {
221                 return ood, err
222         }
223 RecordOODTgt:
224         if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
225                 panic(err)
226         }
227         if _, err = FdOODTgts.Seek(0, os.SEEK_END); err != nil {
228                 panic(err)
229         }
230         if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
231                 panic(err)
232         }
233         unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
234         return true, nil
235 }
236
237 func oodTgtsClear() {
238         if err := unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
239                 panic(err)
240         }
241         if err := FdOODTgts.Truncate(0); err != nil {
242                 panic(err)
243         }
244         unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
245 }