path.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. package common
  17. import (
  18. "fmt"
  19. "os"
  20. "os/user"
  21. "path/filepath"
  22. "runtime"
  23. "strings"
  24. )
  25. // MakeName creates a node name that follows the ethereum convention
  26. // for such names. It adds the operation system name and Go runtime version
  27. // the name.
  28. func MakeName(name, version string) string {
  29. return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
  30. }
  31. func ExpandHomePath(p string) (path string) {
  32. path = p
  33. sep := fmt.Sprintf("%s", os.PathSeparator)
  34. // Check in case of paths like "/something/~/something/"
  35. if len(p) > 1 && p[:1+len(sep)] == "~"+sep {
  36. usr, _ := user.Current()
  37. dir := usr.HomeDir
  38. path = strings.Replace(p, "~", dir, 1)
  39. }
  40. return
  41. }
  42. func FileExist(filePath string) bool {
  43. _, err := os.Stat(filePath)
  44. if err != nil && os.IsNotExist(err) {
  45. return false
  46. }
  47. return true
  48. }
  49. func AbsolutePath(Datadir string, filename string) string {
  50. if filepath.IsAbs(filename) {
  51. return filename
  52. }
  53. return filepath.Join(Datadir, filename)
  54. }
  55. func HomeDir() (home string) {
  56. if usr, err := user.Current(); err == nil {
  57. home = usr.HomeDir
  58. } else {
  59. home = os.Getenv("HOME")
  60. }
  61. return
  62. }
  63. func DefaultDataDir() string {
  64. // Try to place the data folder in the user's home dir
  65. home := HomeDir()
  66. if home != "" {
  67. if runtime.GOOS == "darwin" {
  68. return filepath.Join(home, "Library", "Ethereum")
  69. } else if runtime.GOOS == "windows" {
  70. return filepath.Join(home, "AppData", "Roaming", "Ethereum")
  71. } else {
  72. return filepath.Join(home, ".ethereum")
  73. }
  74. }
  75. // As we cannot guess a stable location, return empty and handle later
  76. return ""
  77. }
  78. func DefaultIpcPath() string {
  79. if runtime.GOOS == "windows" {
  80. return `\\.\pipe\geth.ipc`
  81. }
  82. return filepath.Join(DefaultDataDir(), "geth.ipc")
  83. }