Skip to content

Commit

Permalink
Fix home directory handling in filesystem prompt
Browse files Browse the repository at this point in the history
Paths with tilde slashes weren't handled correctly, causing Protontricks
to incorrectly detect the paths as inaccessible.

In addition, shorten home directories in the generated `flatpak
override` command by using a tilde slash in the path.
  • Loading branch information
Matoking committed Mar 20, 2022
1 parent f1d9944 commit d66bd9d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/protontricks/flatpak.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,20 @@ def _map_path(path):
return None

if path.startswith("xdg-data/"):
return \
Path("~/.local/share").resolve() / path.split("xdg-data/")[1]
return (
Path("~/.local/share").expanduser()
/ path.split("xdg-data/")[1]
)

if path == "home":
return Path.home()

if path.startswith("/"):
return Path(path).resolve()

if path.startswith("~"):
return Path(path).expanduser()

logger.warning(
"Unknown Flatpak file system permission '%s', ignoring.",
path
Expand Down
16 changes: 15 additions & 1 deletion src/protontricks/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,20 @@ def prompt_filesystem_access(paths, show_dialog=False):
:param show_dialog: Show a dialog. If disabled, just print the message
instead.
"""
def _map_path(path):
"""
Map path to a path to be added into the `flatpak override` command.
This means adding a tilde slash if the path is inside the home
directory.
"""
home_dir = str(Path.home())
path = str(path)

if path.startswith(home_dir):
path = f"~/{path[len(home_dir)+1:]}"

return path

config = get_config()

inaccessible_paths = get_inaccessible_paths(paths)
Expand All @@ -301,7 +315,7 @@ def prompt_filesystem_access(paths, show_dialog=False):
return None

cmd_filesystem = " ".join([
"--filesystem={}".format(shlex.quote(path))
"--filesystem={}".format(shlex.quote(_map_path(path)))
for path in remaining_paths
])

Expand Down
38 changes: 34 additions & 4 deletions tests/test_flatpak.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_flatpak_disabled(self):
"""
assert get_inaccessible_paths(["/fake", "/fake_2"]) == []

def test_flatpak_active(self, monkeypatch, tmp_path):
def test_flatpak_active(self, monkeypatch, home_dir, tmp_path):
"""
Test that inaccessible paths are correctly detected when
Flatpak is active
Expand All @@ -62,13 +62,14 @@ def test_flatpak_active(self, monkeypatch, tmp_path):
)

inaccessible_paths = get_inaccessible_paths([
"/mnt/SSD_A", "/mnt/SSD_C", "~/.local/share/SteamOld",
"~/.local/share/Steam"
"/mnt/SSD_A", "/mnt/SSD_C",
str(home_dir / ".local/share/SteamOld"),
str(home_dir / ".local/share/Steam")
])
assert len(inaccessible_paths) == 2
assert str(inaccessible_paths[0]) == "/mnt/SSD_C"
assert str(inaccessible_paths[1]) == \
str(Path("~/.local/share/SteamOld").resolve())
str(Path("~/.local/share/SteamOld").expanduser())

def test_flatpak_home(self, monkeypatch, tmp_path, home_dir):
"""
Expand Down Expand Up @@ -101,6 +102,35 @@ def test_flatpak_home(self, monkeypatch, tmp_path, home_dir):
assert str(inaccessible_paths[0]) == "/mnt/SSD_A"
assert str(inaccessible_paths[1]) == "/var/fake_path"

def test_flatpak_home_tilde(self, monkeypatch, tmp_path, home_dir):
"""
Test that tilde slash is expanded if included in the list of
file systems
"""
flatpak_info_path = tmp_path / "flatpak-info"

flatpak_info_path.write_text(
"[Application]\n"
"name=fake.flatpak.Protontricks\n"
"\n"
"[Instance]\n"
"flatpak-version=1.12.1\n"
"\n"
"[Context]\n"
"filesystems=~/fake_path"
)
monkeypatch.setattr(
"protontricks.flatpak.FLATPAK_INFO_PATH", str(flatpak_info_path)
)

inaccessible_paths = get_inaccessible_paths([
str(home_dir / "fake_path"),
str(home_dir / "fake_path_2")
])

assert len(inaccessible_paths) == 1
assert str(inaccessible_paths[0]) == str(home_dir / "fake_path_2")

def test_flatpak_host(self, monkeypatch, tmp_path, home_dir):
"""
Test that 'host' filesystem permission grants permission to the
Expand Down
33 changes: 33 additions & 0 deletions tests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,39 @@ def test_prompt_without_desktop(self, home_dir, caplog):
assert "--filesystem=/mnt/fake_SSD_2" in record.message
assert str(home_dir / "fake_path") not in record.message

def test_prompt_home_dir(self, home_dir, tmp_path, caplog):
"""
Test that calling 'prompt_filesystem_access' with a path
in the home directory will result in the command using a tilde slash
as the shorthand instead
"""
flatpak_info_path = tmp_path / "flatpak-info"

flatpak_info_path.write_text(
"[Application]\n"
"name=fake.flatpak.Protontricks\n"
"\n"
"[Instance]\n"
"flatpak-version=1.12.1\n"
"\n"
"[Context]\n"
"filesystems=/mnt/SSD_A"
)
prompt_filesystem_access(
[home_dir / "fake_path", "/mnt/SSD_A"],
show_dialog=False
)

assert len(caplog.records) == 1

record = caplog.records[0]

assert record.levelname == "WARNING"
assert "Protontricks does not appear to have access" in record.message

assert "--filesystem='~/fake_path'" in record.message
assert "/mnt/SSD_A" not in record.message

def test_prompt_with_desktop_no_dialog(self, home_dir, gui_provider):
"""
Test that calling 'prompt_filesystem_access' with 'show_dialog'
Expand Down

0 comments on commit d66bd9d

Please sign in to comment.