Skip to content

Commit

Permalink
common: fix #1818, secondary datadir paths to fall back to
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Oct 1, 2015
1 parent 9b94076 commit 74578ab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
24 changes: 12 additions & 12 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func makeDefaultExtra() []byte {
}

func run(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))
if ctx.GlobalBool(utils.OlympicFlag.Name) {
utils.InitOlympic()
}
Expand All @@ -409,7 +409,7 @@ func run(ctx *cli.Context) {
}

func attach(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

var client comms.EthereumClient
var err error
Expand Down Expand Up @@ -441,7 +441,7 @@ func attach(ctx *cli.Context) {
}

func console(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
cfg.ExtraData = makeExtra(ctx)
Expand Down Expand Up @@ -475,7 +475,7 @@ func console(ctx *cli.Context) {
}

func execJSFiles(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
ethereum, err := eth.New(cfg)
Expand All @@ -502,7 +502,7 @@ func execJSFiles(ctx *cli.Context) {
}

func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int) (addrHex, auth string) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

var err error
addrHex, err = utils.ParamToAddress(addr, am)
Expand All @@ -527,7 +527,7 @@ func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int) (
}

func blockRecovery(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

arg := ctx.Args().First()
if len(ctx.Args()) < 1 && len(arg) > 0 {
Expand Down Expand Up @@ -593,7 +593,7 @@ func startEth(ctx *cli.Context, eth *eth.Ethereum) {
}

func accountList(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

am := utils.MakeAccountManager(ctx)
accts, err := am.Accounts()
Expand Down Expand Up @@ -643,7 +643,7 @@ func getPassPhrase(ctx *cli.Context, desc string, confirmation bool, i int) (pas
}

func accountCreate(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

am := utils.MakeAccountManager(ctx)
passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0)
Expand All @@ -655,7 +655,7 @@ func accountCreate(ctx *cli.Context) {
}

func accountUpdate(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

am := utils.MakeAccountManager(ctx)
arg := ctx.Args().First()
Expand All @@ -672,7 +672,7 @@ func accountUpdate(ctx *cli.Context) {
}

func importWallet(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

keyfile := ctx.Args().First()
if len(keyfile) == 0 {
Expand All @@ -694,7 +694,7 @@ func importWallet(ctx *cli.Context) {
}

func accountImport(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

keyfile := ctx.Args().First()
if len(keyfile) == 0 {
Expand All @@ -710,7 +710,7 @@ func accountImport(ctx *cli.Context) {
}

func makedag(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
utils.CheckLegalese(utils.MustDataDir(ctx))

args := ctx.Args()
wrongArgs := func() {
Expand Down
16 changes: 13 additions & 3 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {

cfg := &eth.Config{
Name: common.MakeName(clientID, version),
DataDir: ctx.GlobalString(DataDirFlag.Name),
DataDir: MustDataDir(ctx),
GenesisNonce: ctx.GlobalInt(GenesisNonceFlag.Name),
GenesisFile: ctx.GlobalString(GenesisFileFlag.Name),
BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
Expand Down Expand Up @@ -509,7 +509,7 @@ func SetupEth(ctx *cli.Context) {

// MakeChain creates a chain manager from set command line flags.
func MakeChain(ctx *cli.Context) (chain *core.ChainManager, chainDb ethdb.Database) {
datadir := ctx.GlobalString(DataDirFlag.Name)
datadir := MustDataDir(ctx)
cache := ctx.GlobalInt(CacheFlag.Name)

var err error
Expand Down Expand Up @@ -539,11 +539,21 @@ func MakeChain(ctx *cli.Context) (chain *core.ChainManager, chainDb ethdb.Databa

// MakeChain creates an account manager from set command line flags.
func MakeAccountManager(ctx *cli.Context) *accounts.Manager {
dataDir := ctx.GlobalString(DataDirFlag.Name)
dataDir := MustDataDir(ctx)
ks := crypto.NewKeyStorePassphrase(filepath.Join(dataDir, "keystore"))
return accounts.NewManager(ks)
}

// MustDataDir retrieves the currently requested data directory, terminating if
// none (or the empty string) is specified.
func MustDataDir(ctx *cli.Context) string {
if path := ctx.GlobalString(DataDirFlag.Name); path != "" {
return path
}
Fatalf("Cannot determine default data directory, please set manually (--datadir)")
return ""
}

func IpcSocketPath(ctx *cli.Context) (ipcpath string) {
if runtime.GOOS == "windows" {
ipcpath = common.DefaultIpcPath()
Expand Down
22 changes: 16 additions & 6 deletions common/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,24 @@ func DefaultAssetPath() string {
}

func DefaultDataDir() string {
usr, _ := user.Current()
if runtime.GOOS == "darwin" {
return filepath.Join(usr.HomeDir, "Library", "Ethereum")
} else if runtime.GOOS == "windows" {
return filepath.Join(usr.HomeDir, "AppData", "Roaming", "Ethereum")
// Try to place the data folder in the user's home dir
var home string
if usr, err := user.Current(); err == nil {
home = usr.HomeDir
} else {
return filepath.Join(usr.HomeDir, ".ethereum")
home = os.Getenv("HOME")
}
if home != "" {
if runtime.GOOS == "darwin" {
return filepath.Join(home, "Library", "Ethereum")
} else if runtime.GOOS == "windows" {
return filepath.Join(home, "AppData", "Roaming", "Ethereum")
} else {
return filepath.Join(home, ".ethereum")
}
}
// As we cannot guess a stable location, return empty and handle later
return ""
}

func DefaultIpcPath() string {
Expand Down

0 comments on commit 74578ab

Please sign in to comment.