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/>.
33 "go.cypherpunks.ru/recfile"
34 "golang.org/x/crypto/blake2b"
37 func recfileWrite(fdDep *os.File, fields ...recfile.Field) error {
38 w := recfile.NewWriter(fdDep)
39 if _, err := w.RecordStart(); err != nil {
42 if _, err := w.WriteFields(fields...); err != nil {
48 func ifcreate(fdDep *os.File, tgt string) error {
49 trace(CDebug, "ifcreate: %s <- %s", fdDep.Name(), tgt)
52 recfile.Field{Name: "Type", Value: DepTypeIfcreate},
53 recfile.Field{Name: "Target", Value: tgt},
57 func always(fdDep *os.File) error {
58 trace(CDebug, "always: %s", fdDep.Name())
59 return recfileWrite(fdDep, recfile.Field{Name: "Type", Value: DepTypeAlways})
62 func stamp(fdDep, src *os.File) error {
64 hsh, err := fileHash(os.Stdin)
68 trace(CDebug, "stamp: %s <- %s", fdDep.Name(), hsh)
71 recfile.Field{Name: "Type", Value: DepTypeStamp},
72 recfile.Field{Name: "Hash", Value: hsh},
76 func fileCtime(fd *os.File) (string, error) {
81 stat := fi.Sys().(*syscall.Stat_t)
82 sec, nsec := stat.Ctimespec.Unix()
83 return fmt.Sprintf("%d.%d", sec, nsec), nil
86 func fileHash(fd *os.File) (string, error) {
87 h, err := blake2b.New256(nil)
91 if _, err = io.Copy(h, bufio.NewReader(fd)); err != nil {
94 return hex.EncodeToString(h.Sum(nil)), nil
97 func writeDep(fdDep *os.File, cwd, tgt string) error {
98 trace(CDebug, "ifchange: %s <- %s", fdDep.Name(), tgt)
99 fd, err := os.Open(path.Join(cwd, tgt))
104 ts, err := fileCtime(fd)
108 hsh, err := fileHash(fd)
114 recfile.Field{Name: "Type", Value: DepTypeIfchange},
115 recfile.Field{Name: "Target", Value: tgt},
116 recfile.Field{Name: "Ctime", Value: ts},
117 recfile.Field{Name: "Hash", Value: hsh},
121 func writeDeps(fdDep *os.File, tgts []string) error {
123 trace(CDebug, "no opened fdDep: %s", tgts)
127 upLevels := strings.Count(os.Getenv(EnvDirPrefix), "/")
128 for i := 0; i < upLevels; i++ {
129 ups = append(ups, "..")
131 up := path.Join(ups...)
132 for _, tgt := range tgts {
133 if _, err := os.Stat(tgt); err == nil {
134 if err = writeDep(fdDep, Cwd, path.Join(up, tgt)); err != nil {
142 type DepInfo struct {
147 ifchanges []map[string]string
150 func depRead(fdDep *os.File) (*DepInfo, error) {
151 r := recfile.NewReader(fdDep)
152 m, err := r.NextMap()
157 if b := m["Build"]; b == "" {
158 return nil, errors.New(".dep missing Build:")
163 m, err := r.NextMap()
172 depInfo.always = true
173 case DepTypeIfcreate:
176 return nil, errors.New("invalid format of .dep")
178 depInfo.ifcreates = append(depInfo.ifcreates, dep)
179 case DepTypeIfchange:
181 depInfo.ifchanges = append(depInfo.ifchanges, m)
185 return nil, errors.New("invalid format of .dep")
189 return nil, errors.New("invalid format of .dep")