]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
Download link for 1.2.0 release
[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
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, f+".do")); 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 isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
81         indent := strings.Repeat(". ", level)
82         trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
83         cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
84         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
85         fdDep, err := os.Open(depPath)
86         if err != nil {
87                 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
88                 return true, nil
89         }
90         depInfo, err := depRead(fdDep)
91         fdDep.Close()
92         if err != nil {
93                 return true, TgtErr{tgtOrig, err}
94         }
95
96         if depInfo.build == BuildUUID {
97                 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
98                 return false, nil
99         }
100         ood := false
101
102         for _, dep := range depInfo.ifcreates {
103                 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
104                         trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
105                         ood = true
106                         goto Done
107                 }
108         }
109
110         for _, m := range depInfo.ifchanges {
111                 dep := m["Target"]
112                 if dep == "" {
113                         return ood, TgtErr{tgtOrig, errors.New("invalid format of .rec: missing Target")}
114                 }
115                 theirInode, err := inodeFromRec(m)
116                 if err != nil {
117                         return ood, TgtErr{tgtOrig, fmt.Errorf("invalid format of .rec: %v", err)}
118                 }
119                 theirHsh := m["Hash"]
120                 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
121
122                 fd, err := os.Open(path.Join(cwd, dep))
123                 if err != nil {
124                         if os.IsNotExist(err) {
125                                 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
126                                 ood = true
127                                 goto Done
128                         }
129                         return ood, TgtErr{tgtOrig, err}
130                 }
131                 defer fd.Close()
132
133                 inode, err := inodeFromFile(fd)
134                 if err != nil {
135                         return ood, TgtErr{tgtOrig, err}
136                 }
137                 if inode.Size != theirInode.Size {
138                         trace(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
139                         ood = true
140                         goto Done
141                 }
142                 if InodeTrust && inode.Equals(theirInode) {
143                         trace(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
144                 } else {
145                         trace(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
146                         hsh, err := fileHash(fd)
147                         if err != nil {
148                                 return ood, TgtErr{tgtOrig, err}
149                         }
150                         if theirHsh != hsh {
151                                 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
152                                 ood = true
153                                 goto Done
154                         }
155                         trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
156                 }
157                 fd.Close() // optimization not to hold it for long
158
159                 if dep == tgt {
160                         trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
161                         continue
162                 }
163                 if isSrc(cwd, dep) {
164                         trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
165                         continue
166                 }
167
168                 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
169                         trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
170                         continue
171                 }
172
173                 depOod, err := isOOD(cwd, dep, level+1, seen)
174                 if err != nil {
175                         return ood, TgtErr{tgtOrig, err}
176                 }
177                 if depOod {
178                         trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
179                         ood = true
180                         goto Done
181                 }
182                 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
183         }
184
185 Done:
186         trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
187         return ood, nil
188 }