2 goredo -- redo implementation on pure Go
3 Copyright (C) 2020 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/crypto/blake2b"
36 const EnvNoHash = "REDO_NO_HASH"
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 fields := []recfile.Field{
72 recfile.Field{Name: "Type", Value: DepTypeStamp},
73 recfile.Field{Name: "Hash", Value: hsh},
75 same := StampPrev == hsh
77 fields = append(fields, recfile.Field{Name: "Same", Value: "true"})
79 trace(CDebug, "stamp: %s <- %s (%v)", fdDep.Name(), hsh, same)
80 return recfileWrite(fdDep, fields...)
83 func fileCtime(fd *os.File) (string, error) {
88 stat := fi.Sys().(*syscall.Stat_t)
89 sec, nsec := stat.Ctimespec.Unix()
90 return fmt.Sprintf("%d.%d", sec, nsec), nil
93 func fileHash(fd *os.File) (string, error) {
94 h, err := blake2b.New256(nil)
98 if _, err = io.Copy(h, bufio.NewReader(fd)); err != nil {
101 return hex.EncodeToString(h.Sum(nil)), nil
104 func writeDep(fdDep *os.File, cwd, tgt string) error {
105 trace(CDebug, "ifchange: %s <- %s", fdDep.Name(), tgt)
106 fd, err := os.Open(path.Join(cwd, tgt))
111 ts, err := fileCtime(fd)
115 fields := []recfile.Field{
116 recfile.Field{Name: "Type", Value: DepTypeIfchange},
117 recfile.Field{Name: "Target", Value: tgt},
118 recfile.Field{Name: "Ctime", Value: ts},
122 hsh, err = fileHash(fd)
126 fields = append(fields, recfile.Field{Name: "Hash", Value: hsh})
128 return recfileWrite(fdDep, fields...)
131 func writeDeps(fdDep *os.File, tgts []string) error {
133 trace(CDebug, "no opened fdDep: %s", tgts)
137 upLevels := strings.Count(os.Getenv(EnvDirPrefix), "/")
138 for i := 0; i < upLevels; i++ {
139 ups = append(ups, "..")
141 up := path.Join(ups...)
142 for _, tgt := range tgts {
143 if _, err := os.Stat(tgt); err == nil {
144 if err = writeDep(fdDep, Cwd, path.Join(up, tgt)); err != nil {