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"
40 func recfileWrite(fdDep *os.File, fields ...recfile.Field) error {
41 w := recfile.NewWriter(fdDep)
42 if _, err := w.RecordStart(); err != nil {
45 if _, err := w.WriteFields(fields...); err != nil {
51 func ifcreate(fdDep *os.File, tgt string) error {
52 trace(CDebug, "ifcreate: %s <- %s", fdDep.Name(), tgt)
55 recfile.Field{Name: "Type", Value: DepTypeIfcreate},
56 recfile.Field{Name: "Target", Value: tgt},
60 func always(fdDep *os.File) error {
61 trace(CDebug, "always: %s", fdDep.Name())
62 return recfileWrite(fdDep, recfile.Field{Name: "Type", Value: DepTypeAlways})
65 func stamp(fdDep, src *os.File) error {
67 hsh, err := fileHash(os.Stdin)
71 trace(CDebug, "stamp: %s <- %s", fdDep.Name(), hsh)
74 recfile.Field{Name: "Type", Value: DepTypeStamp},
75 recfile.Field{Name: "Hash", Value: hsh},
79 func fileHash(fd *os.File) (string, error) {
80 h := blake3.New(32, nil)
81 if _, err := io.Copy(h, bufio.NewReader(fd)); err != nil {
84 return hex.EncodeToString(h.Sum(nil)), nil
87 func writeDep(fdDep *os.File, cwd, tgt string) error {
88 trace(CDebug, "ifchange: %s <- %s", fdDep.Name(), tgt)
89 fd, err := os.Open(path.Join(cwd, tgt))
101 inode, err := inodeFromFile(fd)
105 hsh, err := fileHash(fd)
109 fields := []recfile.Field{
110 {Name: "Type", Value: DepTypeIfchange},
111 {Name: "Target", Value: tgt},
112 {Name: "Hash", Value: hsh},
114 fields = append(fields, inode.RecfileFields()...)
115 return recfileWrite(fdDep, fields...)
118 func writeDeps(fdDep *os.File, tgts []string) (err error) {
120 trace(CDebug, "no opened fdDep: %s", tgts)
123 for _, tgt := range tgts {
124 tgtAbs, err := filepath.Abs(tgt)
129 if DepCwd != "" && Cwd != DepCwd {
132 tgtDir := path.Join(cwd, DirPrefix)
133 tgtRel, err := filepath.Rel(tgtDir, tgtAbs)
137 if _, errStat := os.Stat(tgt); errStat == nil {
138 err = writeDep(fdDep, tgtDir, tgtRel)
140 trace(CDebug, "ifchange: %s <- %s (non-existing)", fdDep.Name(), tgtRel)
141 fields := []recfile.Field{
142 {Name: "Type", Value: DepTypeIfchange},
143 {Name: "Target", Value: tgtRel},
145 inodeDummy := Inode{}
146 fields = append(fields, inodeDummy.RecfileFields()...)
147 err = recfileWrite(fdDep, fields...)
153 type DepInfo struct {
158 ifchanges []map[string]string
161 func depRead(fdDep *os.File) (*DepInfo, error) {
162 r := recfile.NewReader(fdDep)
163 m, err := r.NextMap()
168 if b := m["Build"]; b == "" {
169 return nil, errors.New(".rec missing Build:")
174 m, err := r.NextMap()
183 depInfo.always = true
184 case DepTypeIfcreate:
187 return nil, errors.New("invalid format of .rec")
189 depInfo.ifcreates = append(depInfo.ifcreates, dep)
190 case DepTypeIfchange:
192 depInfo.ifchanges = append(depInfo.ifchanges, m)
196 return nil, errors.New("invalid format of .rec")
200 return nil, errors.New("invalid format of .rec")