]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
Fix workability under GNU/Linux and other systems because of different syscall
[goredo.git] / ood.go
1 /*
2 goredo -- 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                 theirTs := m["Ctime"]
113                 theirHsh := m["Hash"]
114                 if dep == "" || theirTs == "" {
115                         return ood, TgtErr{tgtOrig, errors.New("invalid format of .dep")}
116                 }
117                 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
118
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                                 goto Done
125                         }
126                         return ood, TgtErr{tgtOrig, err}
127                 }
128                 defer fd.Close()
129
130                 ts, err := fileCtime(fd)
131                 if err != nil {
132                         return ood, TgtErr{tgtOrig, err}
133                 }
134                 if theirTs == ts {
135                         trace(CDebug, "ood: %s%s -> %s: same ctime", indent, tgtOrig, dep)
136                 } else {
137                         trace(CDebug, "ood: %s%s -> %s: ctime differs", indent, tgtOrig, dep)
138                         hsh, err := fileHash(fd)
139                         if err != nil {
140                                 return ood, TgtErr{tgtOrig, err}
141                         }
142                         if theirHsh != hsh {
143                                 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
144                                 ood = true
145                                 goto Done
146                         }
147                         trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
148                 }
149                 fd.Close() // optimization not to hold it for long
150
151                 if dep == tgt {
152                         trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
153                         continue
154                 }
155                 if isSrc(cwd, dep) {
156                         trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
157                         continue
158                 }
159
160                 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
161                         trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
162                         continue
163                 }
164
165                 depOod, err := isOOD(cwd, dep, level+1, seen)
166                 if err != nil {
167                         return ood, TgtErr{tgtOrig, err}
168                 }
169                 if depOod {
170                         trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
171                         ood = true
172                         goto Done
173                 }
174                 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
175         }
176
177 Done:
178         trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
179         return ood, nil
180 }