OSDN Git Service

Merge branch 'dev' into dev-verify
[bytom/bytom.git] / common / path.go
1 // Copyright 2014 The go-ethereum Authors
2 // This file is part of the go-ethereum library.
3 //
4 // The go-ethereum library is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // The go-ethereum library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16
17 package common
18
19 import (
20         "fmt"
21         "os"
22         "os/user"
23         "path/filepath"
24         "runtime"
25         "strings"
26 )
27
28 // MakeName creates a node name that follows the ethereum convention
29 // for such names. It adds the operation system name and Go runtime version
30 // the name.
31 func MakeName(name, version string) string {
32         return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
33 }
34
35 func ExpandHomePath(p string) (path string) {
36         path = p
37         sep := string(os.PathSeparator)
38
39         // Check in case of paths like "/something/~/something/"
40         if len(p) > 1 && p[:1+len(sep)] == "~"+sep {
41                 usr, _ := user.Current()
42                 dir := usr.HomeDir
43
44                 path = strings.Replace(p, "~", dir, 1)
45         }
46
47         return
48 }
49
50 func FileExist(filePath string) bool {
51         _, err := os.Stat(filePath)
52         if err != nil && os.IsNotExist(err) {
53                 return false
54         }
55
56         return true
57 }
58
59 func AbsolutePath(Datadir string, filename string) string {
60         if filepath.IsAbs(filename) {
61                 return filename
62         }
63         return filepath.Join(Datadir, filename)
64 }
65
66 func HomeDir() string {
67         if home := os.Getenv("HOME"); home != "" {
68                 return home
69         }
70         if usr, err := user.Current(); err == nil {
71                 return usr.HomeDir
72         }
73         return ""
74 }