Skip to content

Commit

Permalink
use path.touch() to ensure permission to access file
Browse files Browse the repository at this point in the history
  • Loading branch information
munnsmunns committed Jul 1, 2024
1 parent cd0cb8d commit be4203f
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions bmds_ui/desktop/components/database_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ def path_exists(value: str):
def file_valid(value: str):
if not value.endswith(".sqlite3") and not value.endswith(".sqlite"):
return False
stemmed = Path(value).stem
if len(stemmed) == 0:
return True


def check_permission(path: str, db: str):
db_path = (Path(path).expanduser().resolve() / db).absolute()
try:
db_path.touch()
except PermissionError:
return False
return True

Expand Down Expand Up @@ -154,7 +160,9 @@ async def create_db(self) -> None:
path = self.query_one("#path").value
db = self.query_one("#filename").value
description = self.query_one("#description").value
if not all((str_exists(name), path_exists(path), file_valid(db))):
if not all(
(str_exists(name), path_exists(path), file_valid(db), check_permission(path, db))
):
self.query_one(FormError).message = "An error occurred."
return
db_path = (Path(path).expanduser().resolve() / db).absolute()
Expand All @@ -176,7 +184,9 @@ async def on_db_update(self) -> None:
path = self.query_one("#path").value
db = self.query_one("#filename").value
description = self.query_one("#description").value
if not all((str_exists(name), path_exists(path), file_valid(db))):
if not all(
(str_exists(name), path_exists(path), file_valid(db), check_permission(path, db))
):
self.query_one(FormError).message = "An error occurred."
return

Expand Down

0 comments on commit be4203f

Please sign in to comment.