]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
redo-sh tests under github.com/chriscool/sharness
[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         "os"
26         "path"
27         "path/filepath"
28         "strings"
29 )
30
31 const (
32         DepTypeIfcreate = "ifcreate"
33         DepTypeIfchange = "ifchange"
34         DepTypeAlways   = "always"
35         DepTypeStamp    = "stamp"
36 )
37
38 type TgtErr struct {
39         Tgt string
40         Err error
41 }
42
43 func (e TgtErr) Unwrap() error { return e.Err }
44
45 func (e TgtErr) Error() string {
46         return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
47 }
48
49 func cwdMustRel(paths ...string) string {
50         rel, err := filepath.Rel(Cwd, path.Join(paths...))
51         if err != nil {
52                 panic(err)
53         }
54         return rel
55 }
56
57 func cwdAndTgt(tgt string) (string, string) {
58         cwd, tgt := path.Split(tgt)
59         cwd, err := filepath.Abs(cwd)
60         if err != nil {
61                 panic(err)
62         }
63         return cwd, tgt
64 }
65
66 func isSrc(cwd, tgt string) bool {
67         d, f := path.Split(path.Join(cwd, tgt))
68         if _, err := os.Stat(path.Join(d, f)); err != nil {
69                 return false
70         }
71         if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
72                 return false
73         }
74         return true
75 }
76
77 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
78         indent := strings.Repeat(". ", level)
79         trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
80         cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
81         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
82         fdDep, err := os.Open(depPath)
83         if err != nil {
84                 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
85                 return true, nil
86         }
87         depInfo, err := depRead(fdDep)
88         fdDep.Close()
89         if err != nil {
90                 return true, TgtErr{tgtOrig, err}
91         }
92
93         if depInfo.build == BuildUUID {
94                 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
95                 return false, nil
96         }
97         ood := false
98
99         for _, dep := range depInfo.ifcreates {
100                 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
101                         trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
102                         ood = true
103                         goto Done
104                 }
105         }
106
107         for _, m := range depInfo.ifchanges {
108                 dep := m["Target"]
109                 theirTs := m["Ctime"]
110                 theirHsh := m["Hash"]
111                 if dep == "" || theirTs == "" {
112                         return ood, TgtErr{tgtOrig, errors.New("invalid format of .dep")}
113                 }
114                 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
115
116                 fd, err := os.Open(path.Join(cwd, dep))
117                 if err != nil {
118                         if os.IsNotExist(err) {
119                                 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
120                                 ood = true
121                                 goto Done
122                         }
123                         return ood, TgtErr{tgtOrig, err}
124                 }
125                 defer fd.Close()
126
127                 ts, err := fileCtime(fd)
128                 if err != nil {
129                         return ood, TgtErr{tgtOrig, err}
130                 }
131                 if theirTs == ts {
132                         trace(CDebug, "ood: %s%s -> %s: same ctime", indent, tgtOrig, dep)
133                 } else {
134                         trace(CDebug, "ood: %s%s -> %s: ctime differs", indent, tgtOrig, dep)
135                         hsh, err := fileHash(fd)
136                         if err != nil {
137                                 return ood, TgtErr{tgtOrig, err}
138                         }
139                         if theirHsh != hsh {
140                                 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
141                                 ood = true
142                                 goto Done
143                         }
144                         trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
145                 }
146                 fd.Close() // optimization not to hold it for long
147
148                 if dep == tgt {
149                         trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
150                         continue
151                 }
152                 if isSrc(cwd, dep) {
153                         trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
154                         continue
155                 }
156
157                 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
158                         trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
159                         continue
160                 }
161
162                 depOod, err := isOOD(cwd, dep, level+1, seen)
163                 if err != nil {
164                         return ood, TgtErr{tgtOrig, err}
165                 }
166                 if depOod {
167                         trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
168                         ood = true
169                         goto Done
170                 }
171                 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
172         }
173
174 Done:
175         trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
176         return ood, nil
177 }