OSDN Git Service

Add the implementation for dppos
[bytom/vapor.git] / vendor / github.com / tendermint / tmlibs / common / os.go
1 package common
2
3 import (
4         "bufio"
5         "fmt"
6         "io"
7         "io/ioutil"
8         "os"
9         "os/exec"
10         "os/signal"
11         "path/filepath"
12         "strings"
13         "syscall"
14 )
15
16 var gopath string
17
18 // GoPath returns GOPATH env variable value. If it is not set, this function
19 // will try to call `go env GOPATH` subcommand.
20 func GoPath() string {
21         if gopath != "" {
22                 return gopath
23         }
24
25         path := os.Getenv("GOPATH")
26         if len(path) == 0 {
27                 goCmd := exec.Command("go", "env", "GOPATH")
28                 out, err := goCmd.Output()
29                 if err != nil {
30                         panic(fmt.Sprintf("failed to determine gopath: %v", err))
31                 }
32                 path = string(out)
33         }
34         gopath = path
35         return path
36 }
37
38 // TrapSignal catches the SIGTERM and executes cb function. After that it exits
39 // with code 1.
40 func TrapSignal(cb func()) {
41         c := make(chan os.Signal, 1)
42         signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGKILL)
43         go func() {
44                 for sig := range c {
45                         fmt.Printf("captured %v, exiting...\n", sig)
46                         if cb != nil {
47                                 cb()
48                         }
49                         os.Exit(1)
50                 }
51         }()
52         select {}
53 }
54
55 // Kill the running process by sending itself SIGTERM.
56 func Kill() error {
57         p, err := os.FindProcess(os.Getpid())
58         if err != nil {
59                 return err
60         }
61         return p.Signal(syscall.SIGTERM)
62 }
63
64 func Exit(s string) {
65         fmt.Printf(s + "\n")
66         os.Exit(1)
67 }
68
69 func EnsureDir(dir string, mode os.FileMode) error {
70         if _, err := os.Stat(dir); os.IsNotExist(err) {
71                 err := os.MkdirAll(dir, mode)
72                 if err != nil {
73                         return fmt.Errorf("Could not create directory %v. %v", dir, err)
74                 }
75         }
76         return nil
77 }
78
79 func IsDirEmpty(name string) (bool, error) {
80         f, err := os.Open(name)
81         if err != nil {
82                 if os.IsNotExist(err) {
83                         return true, err
84                 }
85                 // Otherwise perhaps a permission
86                 // error or some other error.
87                 return false, err
88         }
89         defer f.Close()
90
91         _, err = f.Readdirnames(1) // Or f.Readdir(1)
92         if err == io.EOF {
93                 return true, nil
94         }
95         return false, err // Either not empty or error, suits both cases
96 }
97
98 func FileExists(filePath string) bool {
99         _, err := os.Stat(filePath)
100         return !os.IsNotExist(err)
101 }
102
103 func ReadFile(filePath string) ([]byte, error) {
104         return ioutil.ReadFile(filePath)
105 }
106
107 func MustReadFile(filePath string) []byte {
108         fileBytes, err := ioutil.ReadFile(filePath)
109         if err != nil {
110                 Exit(Fmt("MustReadFile failed: %v", err))
111                 return nil
112         }
113         return fileBytes
114 }
115
116 func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
117         return ioutil.WriteFile(filePath, contents, mode)
118 }
119
120 func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
121         err := WriteFile(filePath, contents, mode)
122         if err != nil {
123                 Exit(Fmt("MustWriteFile failed: %v", err))
124         }
125 }
126
127 // WriteFileAtomic writes newBytes to temp and atomically moves to filePath
128 // when everything else succeeds.
129 func WriteFileAtomic(filePath string, newBytes []byte, mode os.FileMode) error {
130         dir := filepath.Dir(filePath)
131         f, err := ioutil.TempFile(dir, "")
132         if err != nil {
133                 return err
134         }
135         _, err = f.Write(newBytes)
136         if err == nil {
137                 err = f.Sync()
138         }
139         if closeErr := f.Close(); err == nil {
140                 err = closeErr
141         }
142         if permErr := os.Chmod(f.Name(), mode); err == nil {
143                 err = permErr
144         }
145         if err == nil {
146                 err = os.Rename(f.Name(), filePath)
147         }
148         // any err should result in full cleanup
149         if err != nil {
150                 os.Remove(f.Name())
151         }
152         return err
153 }
154
155 //--------------------------------------------------------------------------------
156
157 func Tempfile(prefix string) (*os.File, string) {
158         file, err := ioutil.TempFile("", prefix)
159         if err != nil {
160                 PanicCrisis(err)
161         }
162         return file, file.Name()
163 }
164
165 func Tempdir(prefix string) (*os.File, string) {
166         tempDir := os.TempDir() + "/" + prefix + RandStr(12)
167         err := EnsureDir(tempDir, 0700)
168         if err != nil {
169                 panic(Fmt("Error creating temp dir: %v", err))
170         }
171         dir, err := os.Open(tempDir)
172         if err != nil {
173                 panic(Fmt("Error opening temp dir: %v", err))
174         }
175         return dir, tempDir
176 }
177
178 //--------------------------------------------------------------------------------
179
180 func Prompt(prompt string, defaultValue string) (string, error) {
181         fmt.Print(prompt)
182         reader := bufio.NewReader(os.Stdin)
183         line, err := reader.ReadString('\n')
184         if err != nil {
185                 return defaultValue, err
186         } else {
187                 line = strings.TrimSpace(line)
188                 if line == "" {
189                         return defaultValue, nil
190                 }
191                 return line, nil
192         }
193 }