path.go 3.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2014 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with go-ethereum.  If not, see <http://www.gnu.org/licenses/>.

obscuren's avatar
obscuren committed
17
package common
obscuren's avatar
obscuren committed
18 19

import (
Taylor Gerring's avatar
Taylor Gerring committed
20
	"fmt"
obscuren's avatar
obscuren committed
21
	"os"
obscuren's avatar
obscuren committed
22
	"os/user"
23 24
	"path/filepath"
	"runtime"
obscuren's avatar
obscuren committed
25
	"strings"
26 27

	"github.com/kardianos/osext"
obscuren's avatar
obscuren committed
28 29
)

Taylor Gerring's avatar
Taylor Gerring committed
30 31 32 33 34 35 36
// MakeName creates a node name that follows the ethereum convention
// for such names. It adds the operation system name and Go runtime version
// the name.
func MakeName(name, version string) string {
	return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
}

obscuren's avatar
obscuren committed
37 38
func ExpandHomePath(p string) (path string) {
	path = p
39
	sep := fmt.Sprintf("%s", os.PathSeparator)
obscuren's avatar
obscuren committed
40 41

	// Check in case of paths like "/something/~/something/"
42
	if len(p) > 1 && p[:1+len(sep)] == "~"+sep {
obscuren's avatar
obscuren committed
43 44 45 46 47 48 49 50
		usr, _ := user.Current()
		dir := usr.HomeDir

		path = strings.Replace(p, "~", dir, 1)
	}

	return
}
obscuren's avatar
obscuren committed
51 52 53 54 55 56 57 58 59 60

func FileExist(filePath string) bool {
	_, err := os.Stat(filePath)
	if err != nil && os.IsNotExist(err) {
		return false
	}

	return true
}

obscuren's avatar
obscuren committed
61
func AbsolutePath(Datadir string, filename string) string {
62
	if filepath.IsAbs(filename) {
obscuren's avatar
obscuren committed
63 64
		return filename
	}
65
	return filepath.Join(Datadir, filename)
obscuren's avatar
obscuren committed
66
}
67 68 69 70

func DefaultAssetPath() string {
	var assetPath string
	pwd, _ := os.Getwd()
71
	srcdir := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist")
72 73 74 75 76

	// If the current working directory is the go-ethereum dir
	// assume a debug build and use the source directory as
	// asset directory.
	if pwd == srcdir {
77
		assetPath = filepath.Join(pwd, "assets")
78 79 80 81 82
	} else {
		switch runtime.GOOS {
		case "darwin":
			// Get Binary Directory
			exedir, _ := osext.ExecutableFolder()
83
			assetPath = filepath.Join(exedir, "..", "Resources")
84
		case "linux":
85
			assetPath = filepath.Join("usr", "share", "mist")
86
		case "windows":
87
			assetPath = filepath.Join(".", "assets")
88 89 90 91 92 93 94 95
		default:
			assetPath = "."
		}
	}

	// Check if the assetPath exists. If not, try the source directory
	// This happens when binary is run from outside cmd/mist directory
	if _, err := os.Stat(assetPath); os.IsNotExist(err) {
96
		assetPath = filepath.Join(srcdir, "assets")
97 98 99 100 101 102 103 104
	}

	return assetPath
}

func DefaultDataDir() string {
	usr, _ := user.Current()
	if runtime.GOOS == "darwin" {
105
		return filepath.Join(usr.HomeDir, "Library", "Ethereum")
106
	} else if runtime.GOOS == "windows" {
107
		return filepath.Join(usr.HomeDir, "AppData", "Roaming", "Ethereum")
108
	} else {
109
		return filepath.Join(usr.HomeDir, ".ethereum")
110 111 112
	}
}

113
func DefaultIpcPath() string {
114 115 116
	if runtime.GOOS == "windows" {
		return `\\.\pipe\geth.ipc`
	}
117 118 119
	return filepath.Join(DefaultDataDir(), "geth.ipc")
}

120 121 122 123 124 125 126 127 128 129
func IsWindows() bool {
	return runtime.GOOS == "windows"
}

func WindonizePath(path string) string {
	if string(path[0]) == "/" && IsWindows() {
		path = path[1:]
	}
	return path
}