X Tutup
Skip to content

Commit 36178df

Browse files
committed
sql: test read-write permissions for given path and raise early
maybe fix spesmilo#6485
1 parent 6802bcb commit 36178df

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

electrum/sql_db.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sqlite3
77

88
from .logging import Logger
9+
from .util import test_read_write_permissions
910

1011

1112
def sql(func):
@@ -17,12 +18,14 @@ def wrapper(self, *args, **kwargs):
1718
return f
1819
return wrapper
1920

21+
2022
class SqlDB(Logger):
2123

2224
def __init__(self, asyncio_loop, path, commit_interval=None):
2325
Logger.__init__(self)
2426
self.asyncio_loop = asyncio_loop
2527
self.path = path
28+
test_read_write_permissions(path)
2629
self.commit_interval = commit_interval
2730
self.db_requests = queue.Queue()
2831
self.sql_thread = threading.Thread(target=self.run_sql)

electrum/storage.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from enum import IntEnum
3232

3333
from . import ecc
34-
from .util import profiler, InvalidPassword, WalletFileException, bfh, standardize_path
34+
from .util import (profiler, InvalidPassword, WalletFileException, bfh, standardize_path,
35+
test_read_write_permissions)
3536

3637
from .wallet_db import WalletDB
3738
from .logging import Logger
@@ -62,7 +63,10 @@ def __init__(self, path):
6263
self.logger.info(f"wallet path {self.path}")
6364
self.pubkey = None
6465
self.decrypted = ''
65-
self._test_read_write_permissions(self.path)
66+
try:
67+
test_read_write_permissions(self.path)
68+
except IOError as e:
69+
raise StorageReadWriteError(e) from e
6670
if self.file_exists():
6771
with open(self.path, "r", encoding='utf-8') as f:
6872
self.raw = f.read()
@@ -74,28 +78,6 @@ def __init__(self, path):
7478
def read(self):
7579
return self.decrypted if self.is_encrypted() else self.raw
7680

77-
@classmethod
78-
def _test_read_write_permissions(cls, path):
79-
# note: There might already be a file at 'path'.
80-
# Make sure we do NOT overwrite/corrupt that!
81-
temp_path = "%s.tmptest.%s" % (path, os.getpid())
82-
echo = "fs r/w test"
83-
try:
84-
# test READ permissions for actual path
85-
if os.path.exists(path):
86-
with open(path, "r", encoding='utf-8') as f:
87-
f.read(1) # read 1 byte
88-
# test R/W sanity for "similar" path
89-
with open(temp_path, "w", encoding='utf-8') as f:
90-
f.write(echo)
91-
with open(temp_path, "r", encoding='utf-8') as f:
92-
echo2 = f.read()
93-
os.remove(temp_path)
94-
except Exception as e:
95-
raise StorageReadWriteError(e) from e
96-
if echo != echo2:
97-
raise StorageReadWriteError('echo sanity-check failed')
98-
9981
@profiler
10082
def write(self, data):
10183
s = self.encrypt_before_writing(data)

electrum/util.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,3 +1465,25 @@ def random_shuffled_copy(x: Iterable[T]) -> List[T]:
14651465
x_copy = list(x) # copy
14661466
random.shuffle(x_copy) # shuffle in-place
14671467
return x_copy
1468+
1469+
1470+
def test_read_write_permissions(path) -> None:
1471+
# note: There might already be a file at 'path'.
1472+
# Make sure we do NOT overwrite/corrupt that!
1473+
temp_path = "%s.tmptest.%s" % (path, os.getpid())
1474+
echo = "fs r/w test"
1475+
try:
1476+
# test READ permissions for actual path
1477+
if os.path.exists(path):
1478+
with open(path, "rb") as f:
1479+
f.read(1) # read 1 byte
1480+
# test R/W sanity for "similar" path
1481+
with open(temp_path, "w", encoding='utf-8') as f:
1482+
f.write(echo)
1483+
with open(temp_path, "r", encoding='utf-8') as f:
1484+
echo2 = f.read()
1485+
os.remove(temp_path)
1486+
except Exception as e:
1487+
raise IOError(e) from e
1488+
if echo != echo2:
1489+
raise IOError('echo sanity-check failed')

0 commit comments

Comments
 (0)
X Tutup