mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-01-10 15:54:05 +00:00
wallet: allow to open in read-only mode, fix #2182
If wallet was opened via `NewWalletFromFile`, open it as read-only first and re-open for write if needed. Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
b4e24bef14
commit
7b6203d0d4
1 changed files with 10 additions and 2 deletions
|
@ -56,12 +56,13 @@ func NewWallet(location string) (*Wallet, error) {
|
||||||
|
|
||||||
// NewWalletFromFile creates a Wallet from the given wallet file path.
|
// NewWalletFromFile creates a Wallet from the given wallet file path.
|
||||||
func NewWalletFromFile(path string) (*Wallet, error) {
|
func NewWalletFromFile(path string) (*Wallet, error) {
|
||||||
file, err := os.OpenFile(path, os.O_RDWR, os.ModeAppend)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
wall := &Wallet{
|
wall := &Wallet{
|
||||||
rw: file,
|
|
||||||
path: file.Name(),
|
path: file.Name(),
|
||||||
}
|
}
|
||||||
if err := json.NewDecoder(file).Decode(wall); err != nil {
|
if err := json.NewDecoder(file).Decode(wall); err != nil {
|
||||||
|
@ -162,6 +163,13 @@ func (w *Wallet) savePretty() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Wallet) writeRaw(data []byte) error {
|
func (w *Wallet) writeRaw(data []byte) error {
|
||||||
|
if w.rw == nil {
|
||||||
|
f, err := os.OpenFile(w.path, os.O_RDWR, os.ModeAppend)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.rw = f
|
||||||
|
}
|
||||||
if err := w.rewind(); err != nil {
|
if err := w.rewind(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue