Skip to content

Commit 581c090

Browse files
committed
Merge pull request bnb-chain#1856 from karalabe/andorid-path-fix
common: fix bnb-chain#1818, secondary datadir paths to fall back to
2 parents e3ac56d + 74578ab commit 581c090

3 files changed

Lines changed: 41 additions & 21 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func makeDefaultExtra() []byte {
390390
}
391391

392392
func run(ctx *cli.Context) {
393-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
393+
utils.CheckLegalese(utils.MustDataDir(ctx))
394394
if ctx.GlobalBool(utils.OlympicFlag.Name) {
395395
utils.InitOlympic()
396396
}
@@ -409,7 +409,7 @@ func run(ctx *cli.Context) {
409409
}
410410

411411
func attach(ctx *cli.Context) {
412-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
412+
utils.CheckLegalese(utils.MustDataDir(ctx))
413413

414414
var client comms.EthereumClient
415415
var err error
@@ -441,7 +441,7 @@ func attach(ctx *cli.Context) {
441441
}
442442

443443
func console(ctx *cli.Context) {
444-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
444+
utils.CheckLegalese(utils.MustDataDir(ctx))
445445

446446
cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
447447
cfg.ExtraData = makeExtra(ctx)
@@ -475,7 +475,7 @@ func console(ctx *cli.Context) {
475475
}
476476

477477
func execJSFiles(ctx *cli.Context) {
478-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
478+
utils.CheckLegalese(utils.MustDataDir(ctx))
479479

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

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

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

529529
func blockRecovery(ctx *cli.Context) {
530-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
530+
utils.CheckLegalese(utils.MustDataDir(ctx))
531531

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

595595
func accountList(ctx *cli.Context) {
596-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
596+
utils.CheckLegalese(utils.MustDataDir(ctx))
597597

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

645645
func accountCreate(ctx *cli.Context) {
646-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
646+
utils.CheckLegalese(utils.MustDataDir(ctx))
647647

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

657657
func accountUpdate(ctx *cli.Context) {
658-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
658+
utils.CheckLegalese(utils.MustDataDir(ctx))
659659

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

674674
func importWallet(ctx *cli.Context) {
675-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
675+
utils.CheckLegalese(utils.MustDataDir(ctx))
676676

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

696696
func accountImport(ctx *cli.Context) {
697-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
697+
utils.CheckLegalese(utils.MustDataDir(ctx))
698698

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

712712
func makedag(ctx *cli.Context) {
713-
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
713+
utils.CheckLegalese(utils.MustDataDir(ctx))
714714

715715
args := ctx.Args()
716716
wrongArgs := func() {

‎cmd/utils/flags.go‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
416416

417417
cfg := &eth.Config{
418418
Name: common.MakeName(clientID, version),
419-
DataDir: ctx.GlobalString(DataDirFlag.Name),
419+
DataDir: MustDataDir(ctx),
420420
GenesisNonce: ctx.GlobalInt(GenesisNonceFlag.Name),
421421
GenesisFile: ctx.GlobalString(GenesisFileFlag.Name),
422422
BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
@@ -509,7 +509,7 @@ func SetupEth(ctx *cli.Context) {
509509

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

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

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

547+
// MustDataDir retrieves the currently requested data directory, terminating if
548+
// none (or the empty string) is specified.
549+
func MustDataDir(ctx *cli.Context) string {
550+
if path := ctx.GlobalString(DataDirFlag.Name); path != "" {
551+
return path
552+
}
553+
Fatalf("Cannot determine default data directory, please set manually (--datadir)")
554+
return ""
555+
}
556+
547557
func IpcSocketPath(ctx *cli.Context) (ipcpath string) {
548558
if runtime.GOOS == "windows" {
549559
ipcpath = common.DefaultIpcPath()

‎common/path.go‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,24 @@ func DefaultAssetPath() string {
100100
}
101101

102102
func DefaultDataDir() string {
103-
usr, _ := user.Current()
104-
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")
103+
// Try to place the data folder in the user's home dir
104+
var home string
105+
if usr, err := user.Current(); err == nil {
106+
home = usr.HomeDir
108107
} else {
109-
return filepath.Join(usr.HomeDir, ".ethereum")
108+
home = os.Getenv("HOME")
110109
}
110+
if home != "" {
111+
if runtime.GOOS == "darwin" {
112+
return filepath.Join(home, "Library", "Ethereum")
113+
} else if runtime.GOOS == "windows" {
114+
return filepath.Join(home, "AppData", "Roaming", "Ethereum")
115+
} else {
116+
return filepath.Join(home, ".ethereum")
117+
}
118+
}
119+
// As we cannot guess a stable location, return empty and handle later
120+
return ""
111121
}
112122

113123
func DefaultIpcPath() string {

0 commit comments

Comments
 (0)