]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
DOT generation
[goredo.git] / ood.go
1 /*
2 goredo -- redo implementation on pure Go
3 Copyright (C) 2020 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         "os"
27         "path"
28         "path/filepath"
29         "strings"
30
31         "go.cypherpunks.ru/recfile"
32 )
33
34 const (
35         DepTypeIfcreate = "ifcreate"
36         DepTypeIfchange = "ifchange"
37         DepTypeAlways   = "always"
38         DepTypeStamp    = "stamp"
39 )
40
41 type TgtErr struct {
42         Tgt string
43         Err error
44 }
45
46 func (e TgtErr) Unwrap() error { return e.Err }
47
48 func (e TgtErr) Error() string {
49         return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
50 }
51
52 func cwdMustRel(paths ...string) string {
53         rel, err := filepath.Rel(Cwd, path.Join(paths...))
54         if err != nil {
55                 panic(err)
56         }
57         return rel
58 }
59
60 func cwdAndTgt(tgt string) (string, string) {
61         cwd, tgt := path.Split(tgt)
62         cwd, err := filepath.Abs(cwd)
63         if err != nil {
64                 panic(err)
65         }
66         return cwd, tgt
67 }
68
69 func isSrc(cwd, tgt string) bool {
70         d, f := path.Split(path.Join(cwd, tgt))
71         if _, err := os.Stat(path.Join(d, f)); err != nil {
72                 return false
73         }
74         if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
75                 return false
76         }
77         return true
78 }
79
80 func isBuiltNow(fdDep *os.File) (bool, *recfile.Reader, error) {
81         r := recfile.NewReader(fdDep)
82         m, err := r.NextMap()
83         if err != nil {
84                 return false, nil, err
85         }
86         if m["Build"] == "" {
87                 return false, r, errors.New(".dep missing Build:")
88         }
89         return m["Build"] == BuildUUID, r, nil
90 }
91
92 func rebuildStamped(cwd, tgt, depPath string) (string, error) {
93         relTgt := cwdMustRel(cwd, tgt)
94         errs := make(chan error, 1)
95         if err := runScript(relTgt, errs); err != nil {
96                 return "", err
97         }
98         if err := <-errs; !isOkRun(err) {
99                 return "", errors.New("build failed")
100         }
101         fdDep, err := os.Open(depPath)
102         if err != nil {
103                 return "", err
104         }
105         defer fdDep.Close()
106         builtNow, r, err := isBuiltNow(fdDep)
107         if err != nil {
108                 return "", err
109         }
110         if !builtNow {
111                 return "", errors.New("is not built")
112         }
113         var stampTheir string
114         for {
115                 m, err := r.NextMap()
116                 if err != nil {
117                         if err == io.EOF {
118                                 break
119                         }
120                         return "", err
121                 }
122                 if m["Type"] == DepTypeStamp {
123                         stampTheir = m["Hash"]
124                         break
125                 }
126         }
127         return stampTheir, nil
128 }
129
130 func isOOD(cwd, tgtOrig string, level int) (bool, error) {
131         indent := strings.Repeat(". ", level)
132         trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
133         cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
134         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
135         fdDep, err := os.Open(depPath)
136         if err != nil {
137                 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
138                 return true, nil
139         }
140         defer fdDep.Close()
141         ood := false
142
143         builtNow, r, err := isBuiltNow(fdDep)
144         if err != nil {
145                 return true, TgtErr{tgtOrig, err}
146         }
147         if builtNow {
148                 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
149                 return false, nil
150         }
151
152         var stampOur string
153         ifcreates := []map[string]string{}
154         ifchanges := []map[string]string{}
155         for {
156                 m, err := r.NextMap()
157                 if err != nil {
158                         if err == io.EOF {
159                                 break
160                         }
161                         return true, TgtErr{tgtOrig, err}
162                 }
163                 switch m["Type"] {
164                 case DepTypeAlways:
165                         trace(CDebug, "ood: %s%s -> always", indent, tgtOrig)
166                         ood = true
167                 case DepTypeIfcreate:
168                         ifcreates = append(ifcreates, m)
169                 case DepTypeIfchange:
170                         ifchanges = append(ifchanges, m)
171                 case DepTypeStamp:
172                         stampOur = m["Hash"]
173                         trace(CDebug, "ood: %s%s -> stamped: %s", indent, tgtOrig, stampOur)
174                 default:
175                         return ood, TgtErr{tgtOrig, errors.New("invalid format of .dep")}
176                 }
177         }
178         if ood {
179                 goto StampCheck
180         }
181
182         for _, m := range ifcreates {
183                 theirTgt := m["Target"]
184                 if theirTgt == "" {
185                         return ood, TgtErr{tgtOrig, errors.New("invalid format of .dep")}
186                 }
187                 if _, err = os.Stat(path.Join(cwd, theirTgt)); err == nil {
188                         trace(CDebug, "ood: %s%s -> created", indent, tgtOrig)
189                         ood = true
190                         goto StampCheck
191                 }
192         }
193
194         for _, m := range ifchanges {
195                 dep := m["Target"]
196                 theirTs := m["Ctime"]
197                 theirHsh := m["Hash"]
198                 if dep == "" || theirTs == "" {
199                         return ood, TgtErr{tgtOrig, errors.New("invalid format of .dep")}
200                 }
201                 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
202                 fd, err := os.Open(path.Join(cwd, dep))
203                 if err != nil {
204                         if os.IsNotExist(err) {
205                                 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
206                                 ood = true
207                                 goto StampCheck
208                         }
209                         return ood, TgtErr{tgtOrig, err}
210                 }
211                 defer fd.Close()
212                 ts, err := fileCtime(fd)
213                 if err != nil {
214                         return ood, TgtErr{tgtOrig, err}
215                 }
216                 if theirTs == ts {
217                         trace(CDebug, "ood: %s%s -> %s: same ctime", indent, tgtOrig, dep)
218                 } else if NoHash || theirHsh == "" {
219                         trace(CDebug, "ood: %s%s -> %s: ctime differs", indent, tgtOrig, dep)
220                         ood = true
221                         goto StampCheck
222                 } else {
223                         hsh, err := fileHash(fd)
224                         if err != nil {
225                                 return ood, TgtErr{tgtOrig, err}
226                         }
227                         if theirHsh != hsh {
228                                 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
229                                 ood = true
230                                 goto StampCheck
231                         }
232                         trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
233                 }
234                 fd.Close()
235                 if dep == tgt {
236                         trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
237                         continue
238                 }
239                 if isSrc(cwd, dep) {
240                         trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
241                         continue
242                 }
243                 depOod, err := isOOD(cwd, dep, level+1)
244                 if err != nil {
245                         return ood, TgtErr{tgtOrig, err}
246                 }
247                 if depOod {
248                         trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
249                         ood = true
250                         goto StampCheck
251                 }
252                 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
253         }
254
255 StampCheck:
256         if ood && stampOur != "" {
257                 trace(CDebug, "ood: %s%s run, because stamped", indent, tgtOrig)
258                 stampTheir, err := rebuildStamped(cwd, tgt, depPath)
259                 if err != nil {
260                         return true, TgtErr{tgtOrig, err}
261                 }
262                 trace(CDebug, "ood: %s%s -> stamp: %s %s", indent, tgtOrig, stampOur, stampTheir)
263                 if stampOur == stampTheir {
264                         ood = false
265                 }
266         }
267         trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
268         return ood, nil
269 }