-
Notifications
You must be signed in to change notification settings - Fork 0
/
deps.go
405 lines (379 loc) · 8.39 KB
/
deps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
)
// A node in the dependency graph
type Node struct {
// Filesystem attributes of the node
Dir, File, DoScript string
IsTarget, Exists, IsDir, UsesDefaultDo bool
// internal state
lockFile *os.File
}
// Takes a path to a node and returns a *Node
func NewNode(path string) (n *Node, err error) {
n = &Node{}
n.Dir, n.File = filepath.Split(path)
var s os.FileInfo
if s, err = os.Stat(path + ".prereqs"); err == nil {
n.IsTarget = true
} else if os.IsNotExist(err) {
err = nil
} else {
return
}
if s, err = os.Stat(path); err == nil {
n.Exists = true
n.IsDir = s.IsDir()
} else if os.IsNotExist(err) {
err = nil
} else {
return
}
if s, err = os.Stat(path + ".do"); err == nil {
n.IsTarget = true
n.DoScript = n.File + ".do"
} else if os.IsNotExist(err) {
ext := filepath.Ext(n.File)
if s, err = os.Stat(n.Dir + "default" + ext + ".do"); err == nil {
n.UsesDefaultDo = true
n.DoScript = "default" + ext + ".do"
n.IsTarget = true
} else if os.IsNotExist(err) {
err = nil
} else {
return
}
} else {
return
}
if n.IsTarget && n.DoScript == "" {
err = fmt.Errorf("file %s has .prereqs but no do exec", path)
return
}
return
}
// Walk through the dependency tree and update deps as necessary
// Returns true if changed
func (n *Node) RedoIfChange(ctx context.Context, cancelCause context.CancelCauseFunc) (changed bool, err error) {
select {
case <-ctx.Done():
return false, context.Cause(ctx)
default:
}
if !n.IsTarget {
return
}
if !n.Exists {
return true, n.build(ctx)
}
f, err := os.Open(n.Dir + n.File + ".prereqs")
if err != nil {
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
var line []string
var o *Node
var hashChanged bool
var wg sync.WaitGroup
for scanner.Scan() {
line = strings.Split(scanner.Text(), " ")
if line[1] == "ifcreate" {
o, err = NewNode(n.Dir + line[0])
if err != nil {
return
}
created, err := o.RedoIfCreate()
if err != nil {
return false, err
}
if created {
changed = true
}
continue
}
if line[1] != "ifchange" {
err = fmt.Errorf("Unknown dependency type: %s", line[1])
return
}
o, err = NewNode(n.Dir + line[0])
if err != nil {
return
}
if !o.IsTarget {
hashChanged, err = o.HashChanged(line[2])
if err != nil {
return
}
changed = changed || hashChanged
continue
}
if !o.Exists {
changed = true
if err = o.build(ctx); err != nil {
return
}
continue
}
hashChanged, err = o.HashChanged(line[2])
if err != nil {
return
}
if hashChanged {
err = fmt.Errorf("hash changed since last build")
return
}
wg.Add(1)
go func(n *Node) {
defer wg.Done()
_, err = n.RedoIfChange(ctx, cancelCause)
if err != nil {
cancelCause(err)
return
}
}(o)
}
wg.Wait()
if err = scanner.Err(); err != nil {
return
}
if changed {
err = n.build(ctx)
}
return
}
// Checks if file has been created
// Returns true if it has been created
func (n *Node) RedoIfCreate() (bool, error) {
if _, err := os.Stat(n.Dir + n.File); err == nil {
log.Printf("%s created since last run\n", n.Dir+n.File)
return true, nil
} else if os.IsNotExist(err) {
return false, nil
} else {
return false, err
}
}
func (n *Node) StopIfChange() (err error) {
if !n.Exists {
err = fmt.Errorf("file does not exist")
return
}
new_hash, err := n.Hash()
if _, err = os.Stat(n.Dir + n.File + ".md5"); err == nil {
old_hash, err := os.ReadFile(n.Dir + n.File + ".md5")
if err != nil {
return err
}
if strings.HasPrefix(string(old_hash), new_hash) {
return nil
} else {
err = fmt.Errorf("Hash changed since last build: %s", n.Dir+n.File)
return err
}
} else if os.IsNotExist(err) {
log.Printf("hashing \"%s\" for the first time, integrity will be preserved hereafter.\n", n.Dir+n.File)
md5File, err := os.Create(n.Dir + n.File + ".md5")
if err != nil {
return fmt.Errorf("unable to write hash for %s:", n.Dir+n.File, err)
}
defer md5File.Close()
_, err = fmt.Fprintf(md5File, "%s %s\n", new_hash, n.File)
if err != nil {
return fmt.Errorf("unable to write hash for %s:", n.Dir+n.File, err)
}
return nil
} else {
return err
}
}
// Check if hash has changed since last build
func (n *Node) HashChanged(lastHash string) (bool, error) {
h, err := n.Hash()
if err != nil {
return false, err
}
return h != lastHash, nil
}
func (n *Node) Hash() (string, error) {
if n.IsDir {
return MD5SumDir(n.Dir + n.File)
} else {
return MD5SumFile(n.Dir + n.File)
}
}
// Run n.DoScript to build target
func (n *Node) build(ctx context.Context) (err error) {
done, err := n.Lock()
if done || err != nil {
return
}
defer n.UnLock()
fmt.Fprintln(os.Stderr, "redo", n.Dir+n.File)
prereqsFile, err := os.Create(n.Dir + n.File + ".prereqs")
if err != nil {
return fmt.Errorf("could not create prereqs file: %v", err)
}
defer prereqsFile.Close()
do, err := NewNode(n.Dir + n.DoScript)
if err != nil {
return fmt.Errorf("unable to open do script: %v", err)
}
h, err := do.Hash()
if err != nil {
return fmt.Errorf("unable to hash do exec: %v", err)
}
_, err = fmt.Fprintf(prereqsFile, "%s ifchange %s\n",
do.File,
h)
if err != nil {
return
}
if n.UsesDefaultDo {
_, err = fmt.Fprintf(prereqsFile, "%s ifcreate\n", n.File+".do")
if err != nil {
err = fmt.Errorf("unable to add ifcreate dep for non-default do: %v", err)
return
}
}
// Set RedoParentEnv
parent, err := filepath.Abs(n.Dir + n.File)
if err != nil {
return
}
env := os.Environ()
var inserted bool
for i, e := range env {
if strings.HasPrefix(e, RedoParentEnv) {
env[i] = RedoParentEnv + "=" + parent
inserted = true
}
}
if !inserted {
env = append(env, RedoParentEnv+"="+parent)
}
tmpStdout := n.Dir + "redo-stdout---" + n.File
redoArg3 := "redo-redoArg3---" + n.File
tmpStdoutFile, err := os.Create(tmpStdout)
if err != nil {
return
}
defer tmpStdoutFile.Close()
defer os.Remove(tmpStdout)
c := exec.CommandContext(
ctx,
"./"+n.DoScript,
n.File,
n.File[:len(n.File)-len(filepath.Ext(n.File))],
redoArg3)
c.Dir = n.Dir
c.Stdout = tmpStdoutFile
c.Stderr = os.Stderr
c.Env = env
c.Cancel = func() error {
return c.Process.Signal(os.Interrupt)
}
if err = c.Run(); err != nil {
return fmt.Errorf("failed while rebuilding: %v", err)
}
stdoutStat, err := os.Stat(tmpStdout)
if err != nil {
return
}
stdoutSize := stdoutStat.Size()
arg3 := false
if _, err = os.Stat(n.Dir + redoArg3); err == nil {
arg3 = true
} else if os.IsNotExist(err) {
err = nil
} else {
return
}
if arg3 && stdoutSize > 0 {
return fmt.Errorf("do program wrote to stdout and to $3")
}
if stdoutSize > 0 {
if err = tmpStdoutFile.Sync(); err != nil {
return
}
return os.Rename(tmpStdout, n.Dir+n.File)
}
if arg3 {
if n.Exists && n.IsDir {
if err = os.RemoveAll(n.Dir + n.File); err != nil {
return
}
}
err = filepath.Walk(n.Dir+redoArg3, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
if err = file.Sync(); err != nil {
return err
}
return nil
})
if err != nil {
return
}
err = os.Rename(n.Dir+redoArg3, n.Dir+n.File)
}
return
}
func (n *Node) Lock() (done bool, err error) {
if _, err = os.Stat(n.Dir + n.File + ".lock"); err == nil {
for {
time.Sleep(time.Second)
if _, err = os.Stat(n.Dir + n.File + ".lock"); os.IsNotExist(err) {
return true, nil
}
log.Printf("waiting for %s...\n", n.Dir+n.File)
}
} else if os.IsNotExist(err) {
var prereqsStat os.FileInfo
if prereqsStat, err = os.Stat(n.Dir + n.File + ".prereqs"); err == nil {
if prereqsStat.ModTime().After(RedoTreeTime) {
return true, nil
}
} else if !os.IsNotExist(err) {
return
}
n.lockFile, err = os.Create(n.Dir + n.File + ".lock")
if err != nil {
return false, fmt.Errorf("could not create lock file: %v", err)
}
}
return
}
func (n *Node) UnLock() (err error) {
err = n.lockFile.Close()
if err != nil {
return
}
return os.Remove(n.Dir + n.File + ".lock")
}
func (n *Node) AddDep(prereqsFile *os.File) (err error) {
h, err := n.Hash()
if err != nil {
return fmt.Errorf("unable to hash: %v", err)
}
_, err = fmt.Fprintf(prereqsFile, "%s ifchange %s\n",
n.Dir+n.File,
h)
return
}