X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=run.go;h=2e5dbf5f7ebd5fc8e9c4cfc6e8194333b0456fd9;hb=7a09ce628f351bd1ab22fb761a5cd14ec62c2724;hp=44d25d55045943eafcc873831444915ae458b404;hpb=906d2d0d72e16bbdb17653b834a980f134309a59;p=goredo.git diff --git a/run.go b/run.go index 44d25d5..2e5dbf5 100644 --- a/run.go +++ b/run.go @@ -1,6 +1,6 @@ /* goredo -- djb's redo implementation on pure Go -Copyright (C) 2020-2022 Sergey Matveev +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 @@ -21,6 +21,8 @@ package main import ( "bufio" + "crypto/rand" + "encoding/hex" "errors" "flag" "fmt" @@ -124,7 +126,7 @@ func (e RunError) Error() string { } func mkdirs(pth string) error { - if _, err := os.Stat(pth); err == nil { + if FileExists(pth) { return nil } return os.MkdirAll(pth, os.FileMode(0777)) @@ -154,18 +156,13 @@ func isModified(cwd, redoDir, tgt string) (bool, *Inode, string, error) { if m["Type"] != DepTypeIfchange || m["Target"] != tgt { continue } - fd, err := os.Open(path.Join(cwd, tgt)) + ourInode, err = inodeFromFileByPath(path.Join(cwd, tgt)) if err != nil { if os.IsNotExist(err) { return false, nil, "", nil } return false, nil, "", err } - ourInode, err = inodeFromFile(fd) - fd.Close() - if err != nil { - return false, nil, "", err - } theirInode, err := inodeFromRec(m) if err != nil { return false, nil, "", err @@ -220,7 +217,7 @@ func runScript(tgtOrig string, errs chan error, traced bool) error { tracef(CLock, "LOCK_UN: %s", fdLock.Name()) flock.Type = unix.F_UNLCK if err := unix.FcntlFlock(fdLock.Fd(), unix.F_SETLK, &flock); err != nil { - log.Fatalln(err) + log.Fatalln(err, fdLock.Name()) } fdLock.Close() } @@ -234,7 +231,7 @@ func runScript(tgtOrig string, errs chan error, traced bool) error { } Jobs.Add(1) if err = unix.FcntlFlock(fdLock.Fd(), unix.F_GETLK, &flock); err != nil { - log.Fatalln(err) + log.Fatalln(err, fdLock.Name()) } tracef(CDebug, "waiting: %s (pid=%d)", tgtOrig, flock.Pid) if FdStatus != nil { @@ -245,8 +242,11 @@ func runScript(tgtOrig string, errs chan error, traced bool) error { go func() { defer Jobs.Done() tracef(CLock, "LOCK_EX: %s", fdLock.Name()) + LockAgain: if err := unix.FcntlFlock(fdLock.Fd(), unix.F_SETLKW, &flock); err != nil { - log.Fatalln(err) + log.Println(err, fdLock.Name()) + time.Sleep(10 * time.Millisecond) + goto LockAgain } lockRelease() tracef(CDebug, "waiting done: %s", tgtOrig) @@ -402,7 +402,11 @@ func runScript(tgtOrig string, errs chan error, traced bool) error { cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", EnvDirPrefix, dirPrefix)) cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", EnvBuildUUID, BuildUUID)) - childStderrPrefix := tempsuffix() + childStderrPrefixRaw := make([]byte, 8) + if _, err = io.ReadFull(rand.Reader, childStderrPrefixRaw); err != nil { + panic(err) + } + childStderrPrefix := hex.EncodeToString(childStderrPrefixRaw) cmd.Env = append(cmd.Env, fmt.Sprintf( "%s=%s", EnvStderrPrefix, childStderrPrefix, )) @@ -576,7 +580,6 @@ func runScript(tgtOrig string, errs chan error, traced bool) error { os.Remove(fdDep.Name()) os.Remove(fdStdout.Name()) os.Remove(tmpPath) - os.Remove(fdLock.Name()) if FdStatus != nil { if _, err = FdStatus.Write([]byte{StatusDone}); err != nil { log.Fatalln(err) @@ -649,15 +652,12 @@ func runScript(tgtOrig string, errs chan error, traced bool) error { } // Was $1 touched? - if fd, err := os.Open(path.Join(cwdOrig, tgt)); err == nil { + if inode, err := inodeFromFileByPath(path.Join(cwdOrig, tgt)); err == nil { if inodePrev == nil { - fd.Close() runErr.Err = Err1WasTouched errs <- runErr return } - inode, err := inodeFromFile(fd) - fd.Close() if err != nil { runErr.Err = err errs <- runErr @@ -671,10 +671,8 @@ func runScript(tgtOrig string, errs chan error, traced bool) error { } if inodePrev != nil { - if fd, err := os.Open(path.Join(cwdOrig, tgt)); err == nil { - inode, err := inodeFromFile(fd) - fd.Close() - if err == nil && !inode.Equals(inodePrev) { + if inode, err := inodeFromFileByPath(path.Join(cwdOrig, tgt)); err == nil { + if !inode.Equals(inodePrev) { runErr.Err = Err1WasTouched errs <- runErr return