2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
31 "go.cypherpunks.ru/recfile"
32 "lukechampine.com/blake3"
39 ErrBadRecFormat = errors.New("invalid format of .rec")
42 func recfileWrite(fdDep io.StringWriter, fields ...recfile.Field) error {
43 w := recfile.NewWriter(fdDep)
44 if _, err := w.RecordStart(); err != nil {
47 if _, err := w.WriteFields(fields...); err != nil {
53 func ifcreate(fdDep *os.File, tgt string) error {
54 tracef(CDebug, "ifcreate: %s <- %s", fdDep.Name(), tgt)
57 recfile.Field{Name: "Type", Value: DepTypeIfcreate},
58 recfile.Field{Name: "Target", Value: tgt},
62 func always(fdDep *os.File) error {
63 tracef(CDebug, "always: %s", fdDep.Name())
64 return recfileWrite(fdDep, recfile.Field{Name: "Type", Value: DepTypeAlways})
67 func stamp(fdDep, src *os.File) error {
69 hsh, err := fileHash(src)
73 tracef(CDebug, "stamp: %s <- %s", fdDep.Name(), hsh)
76 recfile.Field{Name: "Type", Value: DepTypeStamp},
77 recfile.Field{Name: "Hash", Value: hsh},
81 func fileHash(fd *os.File) (string, error) {
82 h := blake3.New(32, nil)
83 if _, err := io.Copy(h, bufio.NewReader(fd)); err != nil {
86 return hex.EncodeToString(h.Sum(nil)), nil
89 func depWrite(fdDep *os.File, cwd, tgt string) error {
90 tracef(CDebug, "ifchange: %s <- %s", fdDep.Name(), tgt)
91 fd, err := os.Open(path.Join(cwd, tgt))
103 inode, err := inodeFromFile(fd)
107 hsh, err := fileHash(fd)
111 fields := []recfile.Field{
112 {Name: "Type", Value: DepTypeIfchange},
113 {Name: "Target", Value: tgt},
114 {Name: "Hash", Value: hsh},
116 fields = append(fields, inode.RecfileFields()...)
117 return recfileWrite(fdDep, fields...)
120 func depsWrite(fdDep *os.File, tgts []string) error {
122 tracef(CDebug, "no opened fdDep: %s", tgts)
125 for _, tgt := range tgts {
126 tgtAbs, err := filepath.Abs(tgt)
131 if DepCwd != "" && Cwd != DepCwd {
134 tgtDir := path.Join(cwd, DirPrefix)
135 tgtRel, err := filepath.Rel(tgtDir, tgtAbs)
139 if _, errStat := os.Stat(tgt); errStat == nil {
140 err = depWrite(fdDep, tgtDir, tgtRel)
142 tracef(CDebug, "ifchange: %s <- %s (non-existing)", fdDep.Name(), tgtRel)
143 fields := []recfile.Field{
144 {Name: "Type", Value: DepTypeIfchange},
145 {Name: "Target", Value: tgtRel},
147 inodeDummy := Inode{}
148 fields = append(fields, inodeDummy.RecfileFields()...)
149 err = recfileWrite(fdDep, fields...)
158 type DepInfo struct {
163 ifchanges []map[string]string
166 func depRead(fdDep io.Reader) (*DepInfo, error) {
167 r := recfile.NewReader(fdDep)
168 m, err := r.NextMap()
175 return nil, errors.New(".rec missing Build:")
179 m, err := r.NextMap()
181 if errors.Is(err, io.EOF) {
188 depInfo.always = true
189 case DepTypeIfcreate:
192 return nil, ErrBadRecFormat
194 depInfo.ifcreates = append(depInfo.ifcreates, dep)
195 case DepTypeIfchange:
197 depInfo.ifchanges = append(depInfo.ifchanges, m)
201 return nil, ErrBadRecFormat
205 return nil, ErrBadRecFormat