X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=run.go;h=8ea91d1eb9ca10d5789cd5cd9159c6c9aeb715f4;hb=HEAD;hp=7a6ac37ed124474f787ccf7b1ef267e909529d46;hpb=0db0e21921706ba645c229e809fd3668542dcaae;p=goredo.git diff --git a/run.go b/run.go index 7a6ac37..8ea91d1 100644 --- a/run.go +++ b/run.go @@ -1,19 +1,17 @@ -/* -goredo -- djb's redo implementation on pure Go -Copyright (C) 2020-2023 Sergey Matveev - -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 . -*/ +// goredo -- djb's redo implementation on pure Go +// Copyright (C) 2020-2024 Sergey Matveev +// +// 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 . // Targets runner @@ -57,7 +55,7 @@ const ( RedoDir = ".redo" LockSuffix = ".lock" - DepSuffix = ".rec" + DepSuffix = ".dep" TmpPrefix = ".redo." LogSuffix = ".log" LogRecSuffix = ".log-rec" @@ -111,7 +109,7 @@ type RunError struct { func (e *RunError) Name() string { var name string if e.DoFile == "" { - name = e.Tgt.String() + name = e.Tgt.rel } else { name = fmt.Sprintf("%s (%s)", e.Tgt, e.DoFile) } @@ -132,14 +130,14 @@ func mkdirs(pth string) error { return os.MkdirAll(pth, os.FileMode(0777)) } -func isModified(depInfo *DepInfo, tgt *Tgt) ( - modified bool, ourInode *Inode, hshPrev string, err error, +func isModified(dep *Dep, tgt *Tgt) ( + modified bool, ourInode *Inode, hshPrev Hash, err error, ) { - if depInfo == nil { + if dep == nil { return } - for _, dep := range depInfo.ifchanges { - if dep.tgt.a != tgt.a { + for _, ifchange := range dep.ifchanges { + if ifchange.tgt.a != tgt.a { continue } ourInode, err = inodeFromFileByPath(tgt.a) @@ -151,8 +149,8 @@ func isModified(depInfo *DepInfo, 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,9 +257,9 @@ 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 - depInfo, err := depRead(tgt) + dep, err := depRead(tgt) if err != nil { if errors.Is(err, fs.ErrNotExist) { err = nil @@ -271,14 +270,14 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error { } // Check if it was already built in parallel - if !forced && depInfo != nil && depInfo.build == BuildUUID { + if !forced && dep != nil && dep.build == BuildUUID { lockRelease() errs <- nil return nil } // Check if target is not modified externally - modified, inodePrev, hshPrev, err := isModified(depInfo, tgt) + modified, inodePrev, hshPrev, err := isModified(dep, tgt) if err != nil { lockRelease() return TgtError{tgt, ErrLine(err)} @@ -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 } - depInfo = 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] - runErr.DoFile = doFile.String() + 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)} } @@ -365,8 +370,8 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error { // Prepare command line var cmdName string var args []string - if err = unix.Access(doFile.String(), unix.X_OK); err == nil { - cmdName = doFile.t + if err = unix.Access(doFile.rel, unix.X_OK); err == nil { + 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,17 +557,17 @@ 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 } - for _, dep := range ifchanges { + for _, ifchange := range ifchanges { fields = append(fields, recfile.Field{ Name: "Ifchange", - Value: dep, + Value: ifchange, }) } } @@ -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) @@ -665,11 +676,6 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error { errs <- runErr return } - if err != nil { - runErr.Err = err - errs <- runErr - return - } if !inode.Equals(inodePrev) { runErr.Err = Err1WasTouched errs <- runErr @@ -701,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 { @@ -708,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 } @@ -715,13 +728,13 @@ 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 } } else { - var hsh string + var hsh Hash if hshPrev != "" { _, err = fd.Seek(0, io.SeekStart) if err != nil { @@ -739,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 } @@ -767,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 } @@ -800,14 +823,15 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error { fdDep.Close() fdDepOpened = false - // Post-commit .rec sanitizing - if depInfo, err := depRead(tgt); err == nil { - ifchangeSeen := make(map[string]struct{}, len(depInfo.ifchanges)) - for _, dep := range depInfo.ifchanges { - ifchangeSeen[dep.tgt.a] = struct{}{} + // Post-commit .dep sanitizing + dep, err = depRead(tgt) + if err == nil { + ifchangeSeen := make(map[string]struct{}, len(dep.ifchanges)) + for _, ifchange := range dep.ifchanges { + ifchangeSeen[ifchange.tgt.a] = struct{}{} } - for _, dep := range depInfo.ifcreates { - if _, exists := ifchangeSeen[dep.a]; exists { + for _, ifcreate := range dep.ifcreates { + if _, exists := ifchangeSeen[ifcreate.a]; exists { tracef(CWarn, "simultaneous ifcreate and ifchange records: %s", tgt) } }