2015-10-21 12:15:19 -07:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2017-01-27 20:37:04 -08:00
|
|
|
"io"
|
2015-10-21 12:15:19 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
2017-09-22 11:42:29 -04:00
|
|
|
"syscall"
|
2015-10-21 12:15:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
GoPath = os.Getenv("GOPATH")
|
|
|
|
)
|
|
|
|
|
|
|
|
func TrapSignal(cb func()) {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
2017-09-22 11:42:29 -04:00
|
|
|
signal.Notify(c, syscall.SIGTERM)
|
2015-10-21 12:15:19 -07:00
|
|
|
go func() {
|
|
|
|
for sig := range c {
|
|
|
|
fmt.Printf("captured %v, exiting...\n", sig)
|
|
|
|
if cb != nil {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Exit(s string) {
|
|
|
|
fmt.Printf(s + "\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-12-03 23:44:24 -08:00
|
|
|
func EnsureDir(dir string, mode os.FileMode) error {
|
2015-10-21 12:15:19 -07:00
|
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
2015-12-03 23:44:24 -08:00
|
|
|
err := os.MkdirAll(dir, mode)
|
2015-10-21 12:15:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not create directory %v. %v", dir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-27 20:37:04 -08:00
|
|
|
func IsDirEmpty(name string) (bool, error) {
|
|
|
|
f, err := os.Open(name)
|
|
|
|
if err != nil {
|
2017-08-04 02:03:46 -06:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
// Otherwise perhaps a permission
|
|
|
|
// error or some other error.
|
|
|
|
return false, err
|
2017-01-27 20:37:04 -08:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Readdirnames(1) // Or f.Readdir(1)
|
|
|
|
if err == io.EOF {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, err // Either not empty or error, suits both cases
|
|
|
|
}
|
|
|
|
|
2015-10-21 12:15:19 -07:00
|
|
|
func FileExists(filePath string) bool {
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadFile(filePath string) ([]byte, error) {
|
|
|
|
return ioutil.ReadFile(filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MustReadFile(filePath string) []byte {
|
|
|
|
fileBytes, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
Exit(Fmt("MustReadFile failed: %v", err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fileBytes
|
|
|
|
}
|
|
|
|
|
2015-12-03 23:56:50 -08:00
|
|
|
func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
|
2017-09-22 11:42:29 -04:00
|
|
|
return ioutil.WriteFile(filePath, contents, mode)
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
|
|
|
|
2015-12-03 23:56:50 -08:00
|
|
|
func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
|
|
|
|
err := WriteFile(filePath, contents, mode)
|
2015-10-21 12:15:19 -07:00
|
|
|
if err != nil {
|
|
|
|
Exit(Fmt("MustWriteFile failed: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[common] use temp intead of {filePath}.new
The problem with {filePath}.new is that it is not safe for concurrent
use! Calling this function with the same params results in the following
error:
```
panic: Panicked on a Crisis: rename /root/.tendermint_test/consensus_replay_test/priv_validator.json.new /root/.tendermint_test/consensus_replay_test/priv_validator.json: no such file or directory
goroutine 47860 [running]:
github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.PanicCrisis(0xcba800, 0xc42152d640)
/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/errors.go:33 +0x10f
github.com/tendermint/tendermint/types.(*PrivValidator).save(0xc42235f2c0)
/go/src/github.com/tendermint/tendermint/types/priv_validator.go:165 +0x159
github.com/tendermint/tendermint/types.(*PrivValidator).signBytesHRS(0xc42235f2c0, 0x6, 0x0, 0xc424e88f03, 0xc429908580, 0xca, 0x155, 0x80, 0xc424e88f00, 0x7f4ecafc88d0, ...)
/go/src/github.com/tendermint/tendermint/types/priv_validator.go:249 +0x2bb
github.com/tendermint/tendermint/types.(*PrivValidator).SignVote(0xc42235f2c0, 0xc4228c7460, 0xf, 0xc424e88f00, 0x0, 0x0)
/go/src/github.com/tendermint/tendermint/types/priv_validator.go:186 +0x1a2
github.com/tendermint/tendermint/consensus.(*ConsensusState).signVote(0xc424efd520, 0xc400000002, 0xc422d5e3c0, 0x14, 0x20, 0x1, 0xc4247b6560, 0x14, 0x20, 0x0, ...)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1556 +0x35e
github.com/tendermint/tendermint/consensus.(*ConsensusState).signAddVote(0xc424efd520, 0x2, 0xc422d5e3c0, 0x14, 0x20, 0x1, 0xc4247b6560, 0x14, 0x20, 0xc42001b300)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1568 +0x200
github.com/tendermint/tendermint/consensus.(*ConsensusState).enterPrecommit(0xc424efd520, 0x6, 0x0)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1082 +0x13a4
github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc424efd520, 0xc424e88780, 0x0, 0x0, 0x39, 0x1dc, 0x28c)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1477 +0x1be5
github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc424efd520, 0xc424e88780, 0x0, 0x0, 0xd7fb00, 0xc42152ce00)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1382 +0x93
github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc424efd520, 0xcb58e0, 0xc42547feb8, 0x0, 0x0, 0x6, 0x0, 0x4, 0xed10ca07e, 0x3077bfea, ...)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:660 +0x9fc
github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc424efd520, 0x0)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:615 +0x5f5
created by github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:332 +0x4a7
exit status 2
FAIL github.com/tendermint/tendermint/consensus 76.644s
make: *** [test_integrations] Error 1
```
See https://github.com/tendermint/tendermint/pull/568
2017-07-28 11:22:48 -04:00
|
|
|
// WriteFileAtomic writes newBytes to temp and atomically moves to filePath
|
|
|
|
// when everything else succeeds.
|
2015-12-03 23:56:50 -08:00
|
|
|
func WriteFileAtomic(filePath string, newBytes []byte, mode os.FileMode) error {
|
[common] use temp intead of {filePath}.new
The problem with {filePath}.new is that it is not safe for concurrent
use! Calling this function with the same params results in the following
error:
```
panic: Panicked on a Crisis: rename /root/.tendermint_test/consensus_replay_test/priv_validator.json.new /root/.tendermint_test/consensus_replay_test/priv_validator.json: no such file or directory
goroutine 47860 [running]:
github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.PanicCrisis(0xcba800, 0xc42152d640)
/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/errors.go:33 +0x10f
github.com/tendermint/tendermint/types.(*PrivValidator).save(0xc42235f2c0)
/go/src/github.com/tendermint/tendermint/types/priv_validator.go:165 +0x159
github.com/tendermint/tendermint/types.(*PrivValidator).signBytesHRS(0xc42235f2c0, 0x6, 0x0, 0xc424e88f03, 0xc429908580, 0xca, 0x155, 0x80, 0xc424e88f00, 0x7f4ecafc88d0, ...)
/go/src/github.com/tendermint/tendermint/types/priv_validator.go:249 +0x2bb
github.com/tendermint/tendermint/types.(*PrivValidator).SignVote(0xc42235f2c0, 0xc4228c7460, 0xf, 0xc424e88f00, 0x0, 0x0)
/go/src/github.com/tendermint/tendermint/types/priv_validator.go:186 +0x1a2
github.com/tendermint/tendermint/consensus.(*ConsensusState).signVote(0xc424efd520, 0xc400000002, 0xc422d5e3c0, 0x14, 0x20, 0x1, 0xc4247b6560, 0x14, 0x20, 0x0, ...)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1556 +0x35e
github.com/tendermint/tendermint/consensus.(*ConsensusState).signAddVote(0xc424efd520, 0x2, 0xc422d5e3c0, 0x14, 0x20, 0x1, 0xc4247b6560, 0x14, 0x20, 0xc42001b300)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1568 +0x200
github.com/tendermint/tendermint/consensus.(*ConsensusState).enterPrecommit(0xc424efd520, 0x6, 0x0)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1082 +0x13a4
github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc424efd520, 0xc424e88780, 0x0, 0x0, 0x39, 0x1dc, 0x28c)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1477 +0x1be5
github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc424efd520, 0xc424e88780, 0x0, 0x0, 0xd7fb00, 0xc42152ce00)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1382 +0x93
github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc424efd520, 0xcb58e0, 0xc42547feb8, 0x0, 0x0, 0x6, 0x0, 0x4, 0xed10ca07e, 0x3077bfea, ...)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:660 +0x9fc
github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc424efd520, 0x0)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:615 +0x5f5
created by github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:332 +0x4a7
exit status 2
FAIL github.com/tendermint/tendermint/consensus 76.644s
make: *** [test_integrations] Error 1
```
See https://github.com/tendermint/tendermint/pull/568
2017-07-28 11:22:48 -04:00
|
|
|
f, err := ioutil.TempFile("", "")
|
2015-10-21 12:15:19 -07:00
|
|
|
if err != nil {
|
[common] use temp intead of {filePath}.new
The problem with {filePath}.new is that it is not safe for concurrent
use! Calling this function with the same params results in the following
error:
```
panic: Panicked on a Crisis: rename /root/.tendermint_test/consensus_replay_test/priv_validator.json.new /root/.tendermint_test/consensus_replay_test/priv_validator.json: no such file or directory
goroutine 47860 [running]:
github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.PanicCrisis(0xcba800, 0xc42152d640)
/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/errors.go:33 +0x10f
github.com/tendermint/tendermint/types.(*PrivValidator).save(0xc42235f2c0)
/go/src/github.com/tendermint/tendermint/types/priv_validator.go:165 +0x159
github.com/tendermint/tendermint/types.(*PrivValidator).signBytesHRS(0xc42235f2c0, 0x6, 0x0, 0xc424e88f03, 0xc429908580, 0xca, 0x155, 0x80, 0xc424e88f00, 0x7f4ecafc88d0, ...)
/go/src/github.com/tendermint/tendermint/types/priv_validator.go:249 +0x2bb
github.com/tendermint/tendermint/types.(*PrivValidator).SignVote(0xc42235f2c0, 0xc4228c7460, 0xf, 0xc424e88f00, 0x0, 0x0)
/go/src/github.com/tendermint/tendermint/types/priv_validator.go:186 +0x1a2
github.com/tendermint/tendermint/consensus.(*ConsensusState).signVote(0xc424efd520, 0xc400000002, 0xc422d5e3c0, 0x14, 0x20, 0x1, 0xc4247b6560, 0x14, 0x20, 0x0, ...)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1556 +0x35e
github.com/tendermint/tendermint/consensus.(*ConsensusState).signAddVote(0xc424efd520, 0x2, 0xc422d5e3c0, 0x14, 0x20, 0x1, 0xc4247b6560, 0x14, 0x20, 0xc42001b300)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1568 +0x200
github.com/tendermint/tendermint/consensus.(*ConsensusState).enterPrecommit(0xc424efd520, 0x6, 0x0)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1082 +0x13a4
github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc424efd520, 0xc424e88780, 0x0, 0x0, 0x39, 0x1dc, 0x28c)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1477 +0x1be5
github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc424efd520, 0xc424e88780, 0x0, 0x0, 0xd7fb00, 0xc42152ce00)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1382 +0x93
github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc424efd520, 0xcb58e0, 0xc42547feb8, 0x0, 0x0, 0x6, 0x0, 0x4, 0xed10ca07e, 0x3077bfea, ...)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:660 +0x9fc
github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc424efd520, 0x0)
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:615 +0x5f5
created by github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart
github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:332 +0x4a7
exit status 2
FAIL github.com/tendermint/tendermint/consensus 76.644s
make: *** [test_integrations] Error 1
```
See https://github.com/tendermint/tendermint/pull/568
2017-07-28 11:22:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = f.Write(newBytes)
|
|
|
|
if err == nil {
|
|
|
|
err = f.Sync()
|
|
|
|
}
|
|
|
|
if closeErr := f.Close(); err == nil {
|
|
|
|
err = closeErr
|
|
|
|
}
|
|
|
|
if permErr := os.Chmod(f.Name(), mode); err == nil {
|
|
|
|
err = permErr
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = os.Rename(f.Name(), filePath)
|
|
|
|
}
|
|
|
|
// any err should result in full cleanup
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(f.Name())
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func Tempfile(prefix string) (*os.File, string) {
|
|
|
|
file, err := ioutil.TempFile("", prefix)
|
|
|
|
if err != nil {
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
|
|
|
return file, file.Name()
|
|
|
|
}
|
|
|
|
|
2016-11-21 20:01:11 -08:00
|
|
|
func Tempdir(prefix string) (*os.File, string) {
|
|
|
|
tempDir := os.TempDir() + "/" + prefix + RandStr(12)
|
|
|
|
err := EnsureDir(tempDir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
panic(Fmt("Error creating temp dir: %v", err))
|
|
|
|
}
|
|
|
|
dir, err := os.Open(tempDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(Fmt("Error opening temp dir: %v", err))
|
|
|
|
}
|
|
|
|
return dir, tempDir
|
|
|
|
}
|
|
|
|
|
2016-10-17 16:15:57 -07:00
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
2015-10-21 12:15:19 -07:00
|
|
|
func Prompt(prompt string, defaultValue string) (string, error) {
|
|
|
|
fmt.Print(prompt)
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return defaultValue, err
|
|
|
|
} else {
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if line == "" {
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
return line, nil
|
|
|
|
}
|
|
|
|
}
|