path.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package common
  17. import (
  18. "fmt"
  19. "os"
  20. "os/user"
  21. "path/filepath"
  22. "runtime"
  23. "strings"
  24. "github.com/kardianos/osext"
  25. )
  26. // MakeName creates a node name that follows the ethereum convention
  27. // for such names. It adds the operation system name and Go runtime version
  28. // the name.
  29. func MakeName(name, version string) string {
  30. return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
  31. }
  32. func ExpandHomePath(p string) (path string) {
  33. path = p
  34. sep := fmt.Sprintf("%s", os.PathSeparator)
  35. // Check in case of paths like "/something/~/something/"
  36. if len(p) > 1 && p[:1+len(sep)] == "~"+sep {
  37. usr, _ := user.Current()
  38. dir := usr.HomeDir
  39. path = strings.Replace(p, "~", dir, 1)
  40. }
  41. return
  42. }
  43. func FileExist(filePath string) bool {
  44. _, err := os.Stat(filePath)
  45. if err != nil && os.IsNotExist(err) {
  46. return false
  47. }
  48. return true
  49. }
  50. func AbsolutePath(Datadir string, filename string) string {
  51. if filepath.IsAbs(filename) {
  52. return filename
  53. }
  54. return filepath.Join(Datadir, filename)
  55. }
  56. func DefaultAssetPath() string {
  57. var assetPath string
  58. pwd, _ := os.Getwd()
  59. srcdir := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist")
  60. // If the current working directory is the go-ethereum dir
  61. // assume a debug build and use the source directory as
  62. // asset directory.
  63. if pwd == srcdir {
  64. assetPath = filepath.Join(pwd, "assets")
  65. } else {
  66. switch runtime.GOOS {
  67. case "darwin":
  68. // Get Binary Directory
  69. exedir, _ := osext.ExecutableFolder()
  70. assetPath = filepath.Join(exedir, "..", "Resources")
  71. case "linux":
  72. assetPath = filepath.Join("usr", "share", "mist")
  73. case "windows":
  74. assetPath = filepath.Join(".", "assets")
  75. default:
  76. assetPath = "."
  77. }
  78. }
  79. // Check if the assetPath exists. If not, try the source directory
  80. // This happens when binary is run from outside cmd/mist directory
  81. if _, err := os.Stat(assetPath); os.IsNotExist(err) {
  82. assetPath = filepath.Join(srcdir, "assets")
  83. }
  84. return assetPath
  85. }
  86. func DefaultDataDir() string {
  87. usr, _ := user.Current()
  88. if runtime.GOOS == "darwin" {
  89. return filepath.Join(usr.HomeDir, "Library", "Ethereum")
  90. } else if runtime.GOOS == "windows" {
  91. return filepath.Join(usr.HomeDir, "AppData", "Roaming", "Ethereum")
  92. } else {
  93. return filepath.Join(usr.HomeDir, ".ethereum")
  94. }
  95. }
  96. func DefaultIpcPath() string {
  97. if runtime.GOOS == "windows" {
  98. return `\\.\pipe\geth.ipc`
  99. }
  100. return filepath.Join(DefaultDataDir(), "geth.ipc")
  101. }
  102. func IsWindows() bool {
  103. return runtime.GOOS == "windows"
  104. }
  105. func WindonizePath(path string) string {
  106. if string(path[0]) == "/" && IsWindows() {
  107. path = path[1:]
  108. }
  109. return path
  110. }