]> Cypherpunks.ru repositories - goredo.git/blobdiff - run.go
Download link for 2.6.2 release
[goredo.git] / run.go
diff --git a/run.go b/run.go
index 0b935c55ac228f1129c13dca4515614d513c072f..8ea91d1eb9ca10d5789cd5cd9159c6c9aeb715f4 100644 (file)
--- a/run.go
+++ b/run.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/>.
 
 // Targets runner
 
@@ -57,7 +55,7 @@ const (
 
        RedoDir      = ".redo"
        LockSuffix   = ".lock"
-       DepSuffix    = ".rec"
+       DepSuffix    = ".dep"
        TmpPrefix    = ".redo."
        LogSuffix    = ".log"
        LogRecSuffix = ".log-rec"
@@ -151,8 +149,8 @@ func isModified(dep *Dep, tgt *Tgt) (
                        err = ErrLine(err)
                        return
                }
-               hshPrev = dep.hash
-               modified = !ourInode.Equals(dep.inode)
+               hshPrev = ifchange.Hash()
+               modified = !ourInode.Equals(ifchange.Inode())
                break
        }
        return
@@ -169,12 +167,13 @@ func syncDir(dir string) error {
 }
 
 func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
-       redoDir := path.Join(tgt.h, RedoDir)
+       tgtH, tgtT := pathSplit(tgt.a)
+       redoDir := path.Join(tgtH, RedoDir)
        if err := mkdirs(redoDir); err != nil {
                return TgtError{tgt, ErrLine(err)}
        }
 
-       shCtx := fmt.Sprintf("sh: %s: cwd:%s", tgt, tgt.h)
+       shCtx := fmt.Sprintf("sh: %s: cwd:%s", tgt, tgtH)
        jsToken := jsAcquire(shCtx)
        jsNeedsRelease := true
        defer func() {
@@ -185,7 +184,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
 
        // Acquire lock
        fdLock, err := os.OpenFile(
-               path.Join(redoDir, tgt.t+LockSuffix),
+               path.Join(redoDir, tgtT+LockSuffix),
                os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
                os.FileMode(0666),
        )
@@ -240,14 +239,14 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                                        log.Fatal(err)
                                }
                        }
-                       build, err := depReadBuild(tgt.Dep())
+                       build, err := depBuildRead(tgt.dep)
                        if err == nil {
                                if build != BuildUUID {
                                        err = errors.New("was not built: build differs")
                                }
                        } else {
                                if errors.Is(err, fs.ErrNotExist) {
-                                       err = errors.New("was not built: no .rec")
+                                       err = errors.New("was not built: no .dep")
                                }
                        }
                        if err != nil {
@@ -258,7 +257,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                return nil
        }
 
-       // It scans the whole .rec file while searching for the single target,
+       // It scans the whole .dep file while searching for the single target,
        // but that one is always located at the very end
        dep, err := depRead(tgt)
        if err != nil {
@@ -288,22 +287,25 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                if StopIfMod {
                        return fmt.Errorf("%s externally modified", tgt)
                }
+               Jobs.Add(1)
                tracef(CWarn, "%s externally modified: not redoing", tgt)
                go func() {
                        errs <- nil
+                       Jobs.Done()
                }()
                return nil
        }
        dep = nil
 
-       // Start preparing .rec
-       fdDep, err := tempfile(redoDir, tgt.t+DepSuffix)
+       // Start preparing .dep
+       fdDep, err := tempfile(redoDir, tgtT+DepSuffix)
        if err != nil {
                lockRelease()
                return TgtError{tgt, ErrLine(err)}
        }
        fdDepOpened := true
        fdDepExists := true
+       fdDepW := bufio.NewWriter(fdDep)
        cleanup := func() {
                lockRelease()
                if fdDepOpened {
@@ -313,9 +315,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                        os.Remove(fdDep.Name())
                }
        }
-       if _, err = recfile.NewWriter(fdDep).WriteFields(
-               recfile.Field{Name: "Build", Value: BuildUUID},
-       ); err != nil {
+       if err = depBuildWrite(fdDepW, BuildUUID); err != nil {
                cleanup()
                return TgtError{tgt, ErrLine(err)}
        }
@@ -323,11 +323,12 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
        var cwd string
        var dirPrefix string
        var doFile *Tgt
-       basename := tgt.t
+       var doFileT string
+       basename := tgtT
        runErr := RunError{Tgt: tgt}
        // Determine basename and DIRPREFIX
        {
-               doFileRelPath, upLevels, err := findDo(fdDep, tgt.h, tgt.t)
+               doFileRelPath, upLevels, err := findDo(fdDepW, fdDep.Name(), tgtH, tgtT)
                if err != nil {
                        cleanup()
                        return TgtError{tgt, ErrLine(err)}
@@ -336,25 +337,29 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                        cleanup()
                        return TgtError{tgt, errors.New("no .do found")}
                }
-               // ents := strings.Split(strings.TrimSuffix(tgt.h, "/"), "/")
-               ents := strings.Split(tgt.h, "/")
+               ents := strings.Split(tgtH, "/")
                ents = ents[len(ents)-upLevels:]
                dirPrefix = path.Join(ents...)
                ups := make([]string, 0, upLevels+2)
-               ups = append(ups, tgt.h)
+               ups = append(ups, tgtH)
                for i := 0; i < upLevels; i++ {
                        ups = append(ups, "..")
                }
                ups = append(ups, doFileRelPath)
                cwd = path.Clean(path.Join(ups[:len(ups)-1]...))
                doFile = NewTgt(path.Join(ups...))
-               if strings.HasPrefix(doFile.t, "default.") {
-                       basename = basename[:len(basename)-(len(doFile.t)-len("default.")-len(".do"))-1]
+               doFileT = path.Base(doFile.a)
+               if strings.HasPrefix(doFileT, "default.") {
+                       basename = basename[:len(basename)-(len(doFileT)-len("default.")-len(".do"))-1]
                        runErr.DoFile = doFile.rel
                }
        }
 
-       if err = depWrite(fdDep, tgt.h, doFile, ""); err != nil {
+       if err = depWrite(fdDepW, fdDep.Name(), tgtH, doFile, ""); err != nil {
+               cleanup()
+               return TgtError{tgt, ErrLine(err)}
+       }
+       if err = fdDepW.Flush(); err != nil {
                cleanup()
                return TgtError{tgt, ErrLine(err)}
        }
@@ -366,7 +371,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
        var cmdName string
        var args []string
        if err = unix.Access(doFile.rel, unix.X_OK); err == nil {
-               cmdName = doFile.t
+               cmdName = doFile.a
                args = make([]string, 0, 3)
        } else {
                cmdName = "/bin/sh"
@@ -375,11 +380,11 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                } else {
                        args = append(args, "-e")
                }
-               args = append(args, doFile.t)
+               args = append(args, doFileT)
        }
 
        // Temporary file for stdout
-       fdStdout, err := tempfile(tgt.h, tgt.t)
+       fdStdout, err := tempfile(tgtH, tgtT)
        if err != nil {
                cleanup()
                return TgtError{tgt, ErrLine(err)}
@@ -389,7 +394,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
        tmpPathRel := mustRel(cwd, tmpPath)
        args = append(
                args,
-               path.Join(dirPrefix, tgt.t),
+               path.Join(dirPrefix, tgtT),
                path.Join(dirPrefix, basename),
                tmpPathRel,
        )
@@ -434,7 +439,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
        var fdStderr *os.File
        if StderrKeep {
                fdStderr, err = os.OpenFile(
-                       path.Join(redoDir, tgt.t+LogSuffix),
+                       path.Join(redoDir, tgtT+LogSuffix),
                        os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
                        os.FileMode(0666),
                )
@@ -496,6 +501,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                        return
                }
                fdDepOpened = true
+               fdDepW.Reset(fdDep)
                cmd.ExtraFiles = append(cmd.ExtraFiles, fdDep)
                cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%d", EnvDepFd, 3+fdNum))
                fdNum++
@@ -508,14 +514,14 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                        fdStdout.Close()
                        if fdStderr != nil {
                                fdStderr.Close()
-                               logRecPath := path.Join(redoDir, tgt.t+LogRecSuffix)
+                               logRecPath := path.Join(redoDir, tgtT+LogRecSuffix)
                                if fdStderr, err = os.OpenFile(
                                        logRecPath,
                                        os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
                                        os.FileMode(0666),
                                ); err == nil {
                                        fields := []recfile.Field{
-                                               {Name: "Build", Value: BuildUUID},
+                                               {Name: "Build", Value: BuildUUID.String()},
                                                {Name: "PPID", Value: strconv.Itoa(os.Getpid())},
                                                {Name: "Cwd", Value: cwd},
                                        }
@@ -551,9 +557,9 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                                        }
 
                                        w := bufio.NewWriter(fdStderr)
-                                       {
+                                       if !fdDepExists {
                                                var ifchanges []string
-                                               ifchanges, err = depReadOnlyIfchanges(tgt.Dep())
+                                               ifchanges, err = depReadOnlyIfchanges(tgt.dep)
                                                if err != nil {
                                                        err = ErrLine(err)
                                                        goto Err
@@ -622,22 +628,27 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                                line = scanner.Text()
                                if strings.HasPrefix(line, childStderrPrefix) {
                                        line = line[len(childStderrPrefix):]
-                                       os.Stderr.WriteString(StderrPrefix + line + "\n")
+                                       if StderrPrefix == "" {
+                                               withPrependedTgt(line)
+                                       } else {
+                                               stderrWrite(StderrPrefix + line + "\n")
+                                       }
                                        continue
                                }
                                if fdStderr != nil {
                                        ts.FromTime(time.Now())
-                                       LogMutex.Lock()
                                        fmt.Fprintln(fdStderr, tai64n.Encode(ts[:]), line)
-                                       LogMutex.Unlock()
                                }
                                if StderrSilent {
                                        continue
                                }
-                               if MyPid == 0 {
-                                       tracef(CNone, "%s", line)
+                               if MyPID != 0 {
+                                       line = pid + " " + line
+                               }
+                               if StderrPrefix == "" {
+                                       withPrependedTgt("[" + tgt.rel + "]" + line)
                                } else {
-                                       tracef(CNone, "%s %s", pid, line)
+                                       stderrWrite(StderrPrefix + "[" + tgt.rel + "]" + line + "\n")
                                }
                        }
                        close(stderrTerm)
@@ -696,6 +707,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
 
                // Determine what file we must process at last
                var fd *os.File
+               var chmod fs.FileMode
                if tmpExists {
                        fd, err = os.Open(tmpPath)
                        if err != nil {
@@ -703,6 +715,12 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                                goto Finish
                        }
                        defer fd.Close()
+                       if fi, rerr := fd.Stat(); rerr == nil {
+                               chmod = fi.Mode()
+                       } else {
+                               err = ErrLine(rerr)
+                               goto Finish
+                       }
                } else if fiStdout.Size() > 0 {
                        fd = fdStdout
                }
@@ -710,7 +728,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                // Do we need to ifcreate it, or ifchange with renaming?
                if fd == nil {
                        os.Remove(tgt.a)
-                       err = ifcreate(fdDep, tgt.t)
+                       err = ifcreate(fdDepW, fdDep.Name(), tgtT)
                        if err != nil {
                                err = ErrLine(err)
                                goto Finish
@@ -734,17 +752,23 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                                        if err != nil {
                                                goto Finish
                                        }
+                                       if chmod != 0 {
+                                               err = ErrLine(os.Chmod(tgt.a, chmod))
+                                               if err != nil {
+                                                       goto Finish
+                                               }
+                                       }
                                        err = ErrLine(os.Chtimes(tgt.a, finished, finished))
                                        if err != nil {
                                                goto Finish
                                        }
                                        if !NoSync {
-                                               err = ErrLine(syncDir(tgt.h))
+                                               err = ErrLine(syncDir(tgtH))
                                                if err != nil {
                                                        goto Finish
                                                }
                                        }
-                                       err = ErrLine(depWrite(fdDep, tgt.h, tgt, hsh))
+                                       err = ErrLine(depWrite(fdDepW, fdDep.Name(), tgtH, tgt, hsh))
                                        if err != nil {
                                                goto Finish
                                        }
@@ -762,26 +786,30 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                                goto Finish
                        }
                        if !NoSync {
-                               err = ErrLine(syncDir(tgt.h))
+                               err = ErrLine(syncDir(tgtH))
                                if err != nil {
                                        goto Finish
                                }
                        }
-                       err = ErrLine(depWrite(fdDep, tgt.h, tgt, hsh))
+                       err = ErrLine(depWrite(fdDepW, fdDep.Name(), tgtH, tgt, hsh))
                        if err != nil {
                                goto Finish
                        }
                }
 
        RecCommit:
-               // Commit .rec
+               // Commit .dep
+               err = ErrLine(fdDepW.Flush())
+               if err != nil {
+                       goto Finish
+               }
                if !NoSync {
                        err = ErrLine(fdDep.Sync())
                        if err != nil {
                                goto Finish
                        }
                }
-               err = ErrLine(os.Rename(fdDep.Name(), tgt.Dep()))
+               err = ErrLine(os.Rename(fdDep.Name(), tgt.dep))
                if err != nil {
                        goto Finish
                }
@@ -795,7 +823,7 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error {
                fdDep.Close()
                fdDepOpened = false
 
-               // Post-commit .rec sanitizing
+               // Post-commit .dep sanitizing
                dep, err = depRead(tgt)
                if err == nil {
                        ifchangeSeen := make(map[string]struct{}, len(dep.ifchanges))