]> Cypherpunks.ru repositories - goredo.git/blobdiff - dep.go
Download link for 2.6.2 release
[goredo.git] / dep.go
diff --git a/dep.go b/dep.go
index 3736bc2419e8ba78d1c7f8c288d0bc4552ef3aee..77c248ec9026c6ca1e325c11468e643c030654ed 100644 (file)
--- a/dep.go
+++ b/dep.go
@@ -1,19 +1,17 @@
-/*
-goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, version 3 of the License.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
+// goredo -- djb's redo implementation on pure Go
+// Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 // Dependencies saver
 
@@ -41,7 +39,7 @@ const (
        DepTypeIfchange      = 0x02
        DepTypeAlways        = 0x03
        DepTypeStamp         = 0x04
-       DepTypeIfchangeDummy = 0x05
+       DepTypeIfchangeNonex = 0x05
 )
 
 var (
@@ -67,11 +65,12 @@ func chunkWrite(in []byte) (out []byte) {
 
 type Ifchange struct {
        tgt  *Tgt
-       meta string
+       meta [InodeLen + HashLen]byte
 }
 
-func (ifchange *Ifchange) Inode() Inode {
-       return Inode(ifchange.meta[:InodeLen])
+func (ifchange *Ifchange) Inode() *Inode {
+       inode := Inode(ifchange.meta[:InodeLen])
+       return &inode
 }
 
 func (ifchange *Ifchange) Hash() Hash {
@@ -140,17 +139,17 @@ func depWrite(w io.Writer, fdDepName, cwd string, tgt *Tgt, hsh Hash) (err error
        }
        _, err = io.Copy(w, bytes.NewBuffer(chunkWrite(bytes.Join([][]byte{
                {DepTypeIfchange},
-               []byte(inode),
+               inode[:],
                []byte(hsh),
                []byte(tgt.RelTo(cwd)),
        }, nil))))
        return
 }
 
-func depWriteDummy(w io.Writer, fdDepName, tgtRel string) (err error) {
+func depWriteNonex(w io.Writer, fdDepName, tgtRel string) (err error) {
        tracef(CDebug, "ifchange: %s <- %s (non-existing)", fdDepName, tgtRel)
        _, err = io.Copy(w, bytes.NewBuffer(chunkWrite(bytes.Join([][]byte{
-               {DepTypeIfchangeDummy},
+               {DepTypeIfchangeNonex},
                []byte(tgtRel),
        }, nil))))
        return
@@ -174,7 +173,7 @@ func depsWrite(fdDep *os.File, tgts []*Tgt) error {
                        err = ErrLine(depWrite(fdDepW, fdDep.Name(), tgtDir, tgt, ""))
                } else {
                        tgtRel := tgt.RelTo(tgtDir)
-                       err = ErrLine(depWriteDummy(fdDepW, fdDep.Name(), tgtRel))
+                       err = ErrLine(depWriteNonex(fdDepW, fdDep.Name(), tgtRel))
                }
                if err != nil {
                        return err
@@ -226,20 +225,19 @@ func depBinIfchangeParse(tgt *Tgt, chunk []byte) (*Ifchange, string, error) {
        if len(chunk) < InodeLen+HashLen+1 {
                return nil, "", errors.New("too short \"ifchange\" format")
        }
-       name := string(chunk[InodeLen+HashLen:])
-       meta := string(chunk[:InodeLen+HashLen])
 
        tgtH, _ := pathSplit(tgt.a)
-       ifchange := &Ifchange{tgt: NewTgt(path.Join(tgtH, name)), meta: meta}
-       cachedFound := false
+       name := string(chunk[InodeLen+HashLen:])
+       ifchange := &Ifchange{
+               tgt:  NewTgt(path.Join(tgtH, name)),
+               meta: ([InodeLen + HashLen]byte)(chunk),
+       }
        for _, cached := range IfchangeCache[ifchange.tgt.rel] {
                if ifchange.meta == cached.meta {
-                       ifchange = cached
-                       cachedFound = true
-                       break
+                       return cached, name, nil
                }
        }
-       if IfchangeCache != nil && !cachedFound {
+       if IfchangeCache != nil {
                IfchangeCache[ifchange.tgt.rel] = append(IfchangeCache[ifchange.tgt.rel], ifchange)
        }
        return ifchange, name, nil
@@ -281,7 +279,7 @@ func depParse(tgt *Tgt, data []byte) (*Dep, error) {
                                return nil, ErrLine(err)
                        }
                        dep.ifchanges = append(dep.ifchanges, ifchange)
-               case DepTypeIfchangeDummy:
+               case DepTypeIfchangeNonex:
                        if len(chunk) < 1 {
                                return nil, ErrLine(errors.New("too short \"ifchange\" format"))
                        }
@@ -326,7 +324,7 @@ func depReadOnlyIfchanges(pth string) (ifchanges []string, err error) {
                                return nil, ErrLine(err)
                        }
                        ifchanges = append(ifchanges, tgt)
-               case DepTypeIfchangeDummy:
+               case DepTypeIfchangeNonex:
                        ifchanges = append(ifchanges, string(chunk))
                }
        }