mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-09-01 01:35:15 +00:00
parent
3143ec328e
commit
5ad35a4460
2 changed files with 39 additions and 25 deletions
52
cmd.go
52
cmd.go
|
@ -59,9 +59,9 @@ var (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// walletdir returns the directory path which holds the wallet, utxo,
|
// accountdir returns the directory path which holds an account's wallet, utxo,
|
||||||
// and tx files.
|
// and tx files.
|
||||||
func walletdir(cfg *config, account string) string {
|
func accountdir(cfg *config, account string) string {
|
||||||
var wname string
|
var wname string
|
||||||
if account == "" {
|
if account == "" {
|
||||||
wname = "btcwallet"
|
wname = "btcwallet"
|
||||||
|
@ -72,6 +72,27 @@ func walletdir(cfg *config, account string) string {
|
||||||
return filepath.Join(cfg.DataDir, wname)
|
return filepath.Join(cfg.DataDir, wname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkCreateAccountDir checks that path exists and is a directory.
|
||||||
|
// If path does not exist, it is created.
|
||||||
|
func checkCreateAccountDir(path string) error {
|
||||||
|
fi, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
// Attempt data directory creation
|
||||||
|
if err = os.MkdirAll(path, 0700); err != nil {
|
||||||
|
return fmt.Errorf("cannot create account directory: %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("error checking account directory: %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !fi.IsDir() {
|
||||||
|
return fmt.Errorf("path '%s' is not a directory", cfg.DataDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// OpenAccount opens an account described by account in the data
|
// OpenAccount opens an account described by account in the data
|
||||||
// directory specified by cfg. If the wallet does not exist, ErrNoWallet
|
// directory specified by cfg. If the wallet does not exist, ErrNoWallet
|
||||||
// is returned as an error.
|
// is returned as an error.
|
||||||
|
@ -79,30 +100,19 @@ func walletdir(cfg *config, account string) string {
|
||||||
// Wallets opened from this function are not set to track against a
|
// Wallets opened from this function are not set to track against a
|
||||||
// btcd connection.
|
// btcd connection.
|
||||||
func OpenAccount(cfg *config, account string) (*Account, error) {
|
func OpenAccount(cfg *config, account string) (*Account, error) {
|
||||||
wdir := walletdir(cfg, account)
|
adir := accountdir(cfg, account)
|
||||||
fi, err := os.Stat(wdir)
|
if err := checkCreateAccountDir(adir); err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
if os.IsNotExist(err) {
|
|
||||||
// Attempt data directory creation
|
|
||||||
if err = os.MkdirAll(wdir, 0700); err != nil {
|
|
||||||
return nil, fmt.Errorf("cannot create data directory: %s", err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("error checking data directory: %s", err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if !fi.IsDir() {
|
|
||||||
return nil, fmt.Errorf("data directory '%s' is not a directory", cfg.DataDir)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wfilepath := filepath.Join(wdir, "wallet.bin")
|
wfilepath := filepath.Join(adir, "wallet.bin")
|
||||||
utxofilepath := filepath.Join(wdir, "utxo.bin")
|
utxofilepath := filepath.Join(adir, "utxo.bin")
|
||||||
txfilepath := filepath.Join(wdir, "tx.bin")
|
txfilepath := filepath.Join(adir, "tx.bin")
|
||||||
var wfile, utxofile, txfile *os.File
|
var wfile, utxofile, txfile *os.File
|
||||||
|
|
||||||
// Read wallet file.
|
// Read wallet file.
|
||||||
if wfile, err = os.Open(wfilepath); err != nil {
|
wfile, err := os.Open(wfilepath)
|
||||||
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
// Must create and save wallet first.
|
// Must create and save wallet first.
|
||||||
return nil, ErrNoWallet
|
return nil, ErrNoWallet
|
||||||
|
|
12
disksync.go
12
disksync.go
|
@ -69,10 +69,14 @@ func (w *Account) writeDirtyToDisk() error {
|
||||||
// for validity, and moved to replace the main file.
|
// for validity, and moved to replace the main file.
|
||||||
timeStr := fmt.Sprintf("%v", time.Now().Unix())
|
timeStr := fmt.Sprintf("%v", time.Now().Unix())
|
||||||
|
|
||||||
wdir := walletdir(cfg, w.name)
|
adir := accountdir(cfg, w.name)
|
||||||
wfilepath := filepath.Join(wdir, "wallet.bin")
|
if err := checkCreateAccountDir(adir); err != nil {
|
||||||
txfilepath := filepath.Join(wdir, "tx.bin")
|
return err
|
||||||
utxofilepath := filepath.Join(wdir, "utxo.bin")
|
}
|
||||||
|
|
||||||
|
wfilepath := filepath.Join(adir, "wallet.bin")
|
||||||
|
txfilepath := filepath.Join(adir, "tx.bin")
|
||||||
|
utxofilepath := filepath.Join(adir, "utxo.bin")
|
||||||
|
|
||||||
// UTXOs and transactions are synced to disk first. This prevents
|
// UTXOs and transactions are synced to disk first. This prevents
|
||||||
// any races from saving a wallet marked to be synced with block N
|
// any races from saving a wallet marked to be synced with block N
|
||||||
|
|
Loading…
Add table
Reference in a new issue