2 goredo -- 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/>.
32 "go.cypherpunks.ru/recfile"
33 "golang.org/x/sys/unix"
34 "lukechampine.com/blake3"
39 func recfileWrite(fdDep *os.File, fields ...recfile.Field) error {
40 w := recfile.NewWriter(fdDep)
41 if _, err := w.RecordStart(); err != nil {
44 if _, err := w.WriteFields(fields...); err != nil {
50 func ifcreate(fdDep *os.File, tgt string) error {
51 trace(CDebug, "ifcreate: %s <- %s", fdDep.Name(), tgt)
54 recfile.Field{Name: "Type", Value: DepTypeIfcreate},
55 recfile.Field{Name: "Target", Value: tgt},
59 func always(fdDep *os.File) error {
60 trace(CDebug, "always: %s", fdDep.Name())
61 return recfileWrite(fdDep, recfile.Field{Name: "Type", Value: DepTypeAlways})
64 func stamp(fdDep, src *os.File) error {
66 hsh, err := fileHash(os.Stdin)
70 trace(CDebug, "stamp: %s <- %s", fdDep.Name(), hsh)
73 recfile.Field{Name: "Type", Value: DepTypeStamp},
74 recfile.Field{Name: "Hash", Value: hsh},
78 func fileCtime(fd *os.File) (string, error) {
80 if err := unix.Fstat(int(fd.Fd()), &stat); err != nil {
83 sec, nsec := stat.Ctim.Unix()
84 return fmt.Sprintf("%d.%d", sec, nsec), nil
87 func fileHash(fd *os.File) (string, error) {
88 h := blake3.New(32, nil)
89 if _, err := io.Copy(h, bufio.NewReader(fd)); err != nil {
92 return hex.EncodeToString(h.Sum(nil)), nil
95 func writeDep(fdDep *os.File, cwd, tgt string) error {
96 trace(CDebug, "ifchange: %s <- %s", fdDep.Name(), tgt)
97 fd, err := os.Open(path.Join(cwd, tgt))
109 ts, err := fileCtime(fd)
113 hsh, err := fileHash(fd)
119 recfile.Field{Name: "Type", Value: DepTypeIfchange},
120 recfile.Field{Name: "Target", Value: tgt},
121 recfile.Field{Name: "Ctime", Value: ts},
122 recfile.Field{Name: "Hash", Value: hsh},
126 func writeDeps(fdDep *os.File, tgts []string) (err error) {
128 trace(CDebug, "no opened fdDep: %s", tgts)
131 for _, tgt := range tgts {
132 tgtAbs, err := filepath.Abs(tgt)
136 tgtDir := path.Join(Cwd, DirPrefix)
137 tgtRel, err := filepath.Rel(tgtDir, tgtAbs)
141 if _, errStat := os.Stat(tgt); errStat == nil {
142 err = writeDep(fdDep, tgtDir, tgtRel)
144 trace(CDebug, "ifchange: %s <- %s (unexisting)", fdDep.Name(), tgtRel)
147 recfile.Field{Name: "Type", Value: DepTypeIfchange},
148 recfile.Field{Name: "Target", Value: tgtRel},
149 recfile.Field{Name: "Ctime", Value: "0.0"},
156 type DepInfo struct {
161 ifchanges []map[string]string
164 func depRead(fdDep *os.File) (*DepInfo, error) {
165 r := recfile.NewReader(fdDep)
166 m, err := r.NextMap()
171 if b := m["Build"]; b == "" {
172 return nil, errors.New(".dep missing Build:")
177 m, err := r.NextMap()
186 depInfo.always = true
187 case DepTypeIfcreate:
190 return nil, errors.New("invalid format of .dep")
192 depInfo.ifcreates = append(depInfo.ifcreates, dep)
193 case DepTypeIfchange:
195 depInfo.ifchanges = append(depInfo.ifchanges, m)
199 return nil, errors.New("invalid format of .dep")
203 return nil, errors.New("invalid format of .dep")