]> Cypherpunks.ru repositories - goredo.git/blob - ood.go
Do not use deprecated os.SEEK_*
[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         "io"
26         "os"
27         "path"
28         "path/filepath"
29         "strings"
30
31         "golang.org/x/sys/unix"
32 )
33
34 const (
35         DepTypeIfcreate = "ifcreate"
36         DepTypeIfchange = "ifchange"
37         DepTypeAlways   = "always"
38         DepTypeStamp    = "stamp"
39
40         EnvOODTgtsFd     = "REDO_OOD_TGTS_FD"
41         EnvOODTgtsLockFd = "REDO_OOD_TGTS_LOCK_FD"
42 )
43
44 var (
45         OODTgts       map[string]struct{}
46         FdOODTgts     *os.File
47         FdOODTgtsLock *os.File
48 )
49
50 type TgtErr struct {
51         Tgt string
52         Err error
53 }
54
55 func (e TgtErr) Unwrap() error { return e.Err }
56
57 func (e TgtErr) Error() string {
58         return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
59 }
60
61 func cwdMustRel(paths ...string) string {
62         rel, err := filepath.Rel(Cwd, path.Join(paths...))
63         if err != nil {
64                 panic(err)
65         }
66         return rel
67 }
68
69 func cwdAndTgt(tgt string) (string, string) {
70         cwd, tgt := path.Split(tgt)
71         cwd, err := filepath.Abs(cwd)
72         if err != nil {
73                 panic(err)
74         }
75         return cwd, tgt
76 }
77
78 func isSrc(cwd, tgt string) bool {
79         d, f := path.Split(path.Join(cwd, tgt))
80         if _, err := os.Stat(path.Join(d, f)); err != nil {
81                 return false
82         }
83         if _, err := os.Stat(path.Join(d, f+".do")); err == nil {
84                 return false
85         }
86         if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
87                 return false
88         }
89         return true
90 }
91
92 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
93         indent := strings.Repeat(". ", level)
94         trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
95         cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
96         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
97         fdDep, err := os.Open(depPath)
98         if err != nil {
99                 trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
100                 return true, nil
101         }
102         depInfo, err := depRead(fdDep)
103         fdDep.Close()
104         if err != nil {
105                 return true, TgtErr{tgtOrig, err}
106         }
107
108         if depInfo.build == BuildUUID {
109                 trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
110                 return false, nil
111         }
112         ood := false
113
114         for _, dep := range depInfo.ifcreates {
115                 if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
116                         trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
117                         ood = true
118                         goto Done
119                 }
120         }
121
122         for _, m := range depInfo.ifchanges {
123                 dep := m["Target"]
124                 if dep == "" {
125                         return ood, TgtErr{tgtOrig, errors.New("invalid format of .rec: missing Target")}
126                 }
127                 theirInode, err := inodeFromRec(m)
128                 if err != nil {
129                         return ood, TgtErr{tgtOrig, fmt.Errorf("invalid format of .rec: %v", err)}
130                 }
131                 theirHsh := m["Hash"]
132                 trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
133
134                 fd, err := os.Open(path.Join(cwd, dep))
135                 if err != nil {
136                         if os.IsNotExist(err) {
137                                 trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
138                                 ood = true
139                                 goto Done
140                         }
141                         return ood, TgtErr{tgtOrig, err}
142                 }
143                 defer fd.Close()
144
145                 inode, err := inodeFromFile(fd)
146                 if err != nil {
147                         return ood, TgtErr{tgtOrig, err}
148                 }
149                 if inode.Size != theirInode.Size {
150                         trace(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
151                         ood = true
152                         goto Done
153                 }
154                 if InodeTrust && inode.Equals(theirInode) {
155                         trace(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
156                 } else {
157                         trace(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
158                         hsh, err := fileHash(fd)
159                         if err != nil {
160                                 return ood, TgtErr{tgtOrig, err}
161                         }
162                         if theirHsh != hsh {
163                                 trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
164                                 ood = true
165                                 goto Done
166                         }
167                         trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
168                 }
169                 fd.Close() // optimization not to hold it for long
170
171                 if dep == tgt {
172                         trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
173                         continue
174                 }
175                 if isSrc(cwd, dep) {
176                         trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
177                         continue
178                 }
179
180                 if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
181                         trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
182                         continue
183                 }
184
185                 depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
186                 if err != nil {
187                         return ood, TgtErr{tgtOrig, err}
188                 }
189                 if depOod {
190                         trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
191                         ood = true
192                         goto Done
193                 }
194                 trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
195         }
196
197 Done:
198         trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
199         return ood, nil
200 }
201
202 func isOODWithTrace(
203         cwd, tgtOrig string,
204         level int,
205         seen map[string]struct{},
206 ) (bool, error) {
207         p, err := filepath.Abs(path.Join(cwd, tgtOrig))
208         if err != nil {
209                 panic(err)
210         }
211         _, ood := OODTgts[p]
212         if ood {
213                 trace(
214                         CDebug,
215                         "ood: %s%s true, external decision",
216                         strings.Repeat(". ", level), tgtOrig,
217                 )
218                 goto RecordOODTgt
219         }
220         ood, err = isOOD(cwd, tgtOrig, level, seen)
221         if !ood {
222                 return ood, err
223         }
224 RecordOODTgt:
225         if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
226                 panic(err)
227         }
228         if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
229                 panic(err)
230         }
231         if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
232                 panic(err)
233         }
234         unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
235         return true, nil
236 }
237
238 func oodTgtsClear() {
239         if err := unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
240                 panic(err)
241         }
242         if err := FdOODTgts.Truncate(0); err != nil {
243                 panic(err)
244         }
245         unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
246 }