]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
a7c2d22422443c75b3d4c84d5f876ed0e5fc483a
[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 func cwdAndTgt(tgt string) (string, string) {
35         cwd, tgt := path.Split(tgt)
36         cwd, err := filepath.Abs(cwd)
37         if err != nil {
38                 panic(err)
39         }
40         return cwd, tgt
41 }
42
43 func isSrc(cwd, tgt string) bool {
44         d, f := path.Split(path.Join(cwd, tgt))
45         if _, err := os.Stat(path.Join(d, f)); err != nil {
46                 return false
47         }
48         if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
49                 return false
50         }
51         return true
52 }
53
54 func isBuiltNow(fdDep *os.File) (bool, *recfile.Reader, error) {
55         r := recfile.NewReader(fdDep)
56         m, err := r.NextMap()
57         if err != nil {
58                 return false, nil, err
59         }
60         if m["Build"] == "" {
61                 return false, r, errors.New("dep. missing Build:")
62         }
63         return m["Build"] == BuildUUID, r, nil
64 }
65
66 func isOOD(cwd, tgt string, level int) (bool, error) {
67         tgtOrig := tgt
68         indent := strings.Repeat(". ", level)
69         trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
70         cwd, tgt = cwdAndTgt(path.Join(cwd, tgt))
71         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
72         fdDep, err := os.Open(depPath)
73         if err != nil {
74                 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
75                 return true, nil
76         }
77         defer fdDep.Close()
78         ood := false
79
80         builtNow, r, err := isBuiltNow(fdDep)
81         if err != nil {
82                 return true, err
83         }
84         if builtNow {
85                 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
86                 return false, nil
87         }
88
89         var stampOur string
90         for {
91                 m, err := r.NextMap()
92                 if err != nil {
93                         if err == io.EOF {
94                                 break
95                         }
96                         return true, err
97                 }
98                 switch m["Type"] {
99                 case "always":
100                         trace(CDebug, "ood: %s%s -> always", indent, tgtOrig)
101                         ood = true
102                 case "ifcreate":
103                         theirTgt := m["Target"]
104                         if theirTgt == "" {
105                                 return ood, errors.New("invalid format of dep." + tgtOrig)
106                         }
107                         if _, err = os.Stat(path.Join(cwd, theirTgt)); err == nil {
108                                 trace(CDebug, "ood: %s%s -> created", indent, tgtOrig)
109                                 ood = true
110                         }
111                 case "ifchange":
112                         dep := m["Target"]
113                         theirTs := m["Ctime"]
114                         theirHsh := m["Hash"]
115                         if dep == "" || theirTs == "" {
116                                 return ood, errors.New("invalid format of dep." + tgtOrig)
117                         }
118                         trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
119                         fd, err := os.Open(path.Join(cwd, dep))
120                         if err != nil {
121                                 if os.IsNotExist(err) {
122                                         trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
123                                         ood = true
124                                         continue
125                                 }
126                                 return ood, err
127                         }
128                         defer fd.Close()
129                         ts, err := fileCtime(fd)
130                         if err != nil {
131                                 return ood, err
132                         }
133                         if theirTs == ts {
134                                 trace(CDebug, "ood: %s%s -> %s: same ctime", indent, tgtOrig, dep)
135                         } else if NoHash || theirHsh == "" {
136                                 trace(CDebug, "ood: %s%s -> %s: ctime differs", indent, tgtOrig, dep)
137                                 ood = true
138                         } else {
139                                 hsh, err := fileHash(fd)
140                                 if err != nil {
141                                         return ood, err
142                                 }
143                                 if theirHsh == hsh {
144                                         trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
145                                 } else {
146                                         trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
147                                         ood = true
148                                 }
149                         }
150                         fd.Close()
151                         if ood {
152                                 continue
153                         }
154                         if dep == tgt {
155                                 trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
156                                 continue
157                         }
158                         if isSrc(cwd, dep) {
159                                 trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
160                                 continue
161                         } else {
162                                 depOod, err := isOOD(cwd, dep, level+1)
163                                 if depOod {
164                                         ood = true
165                                         trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
166                                 } else {
167                                         trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
168                                 }
169                                 if err != nil {
170                                         return ood, err
171                                 }
172                         }
173                 case "stamp":
174                         stampOur = m["Hash"]
175                         trace(CDebug, "ood: %s%s -> stamped: %s", indent, tgtOrig, stampOur)
176                 default:
177                         return ood, errors.New("invalid format of dep." + tgtOrig)
178                 }
179         }
180         if ood && stampOur != "" {
181                 errs := make(chan error, 0)
182                 trace(CDebug, "ood: %s%s running, because stamped", indent, tgtOrig)
183                 relTgt, err := filepath.Rel(Cwd, path.Join(cwd, tgt))
184                 if err != nil {
185                         return true, err
186                 }
187                 if err = runScript(relTgt, errs); err != nil {
188                         return true, err
189                 }
190                 if err = <-errs; err != nil {
191                         return true, err
192                 }
193                 fdDep, err := os.Open(depPath)
194                 if err != nil {
195                         return true, err
196                 }
197                 defer fdDep.Close()
198                 builtNow, r, err := isBuiltNow(fdDep)
199                 if err != nil {
200                         return true, err
201                 }
202                 if !builtNow {
203                         return true, fmt.Errorf("%s is not built", tgtOrig)
204                 }
205                 var stampTheir string
206                 for {
207                         m, err := r.NextMap()
208                         if err != nil {
209                                 if err == io.EOF {
210                                         break
211                                 }
212                                 return true, err
213                         }
214                         if m["Type"] == "stamp" {
215                                 stampTheir = m["Hash"]
216                                 break
217                         }
218                 }
219                 trace(CDebug, "ood: %s%s -> stamp: %s %s", indent, tgtOrig, stampOur, stampTheir)
220                 if stampOur == stampTheir {
221                         ood = false
222                 }
223         }
224         trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
225         return ood, nil
226 }