Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog/+android-device-protected.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Recognize Android device-protected app storage in the ``sys.path`` fallback and preserve its user ID when resolving shared storage paths.
4 changes: 2 additions & 2 deletions src/platformdirs/_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def android_folder() -> str | None:
_ANDROID_FOLDER_PATTERN: Final[re.Pattern[str]] = re.compile(
r"""
(?P<folder>
/data/(?:data|user/\d+) # internal storage of the primary user or of user N
/data/(?:data|user(?:_de)?/\d+) # credential- or device-protected internal storage
/[^/]+ # package name, one path segment
)
/files(?=/|\Z) # the files directory the interpreter runs from
Expand All @@ -55,7 +55,7 @@ def android_folder() -> str | None:
r"""
(?P<folder>
/mnt/expand/[a-fA-F0-9-]{36} # adopted storage volume, named by its UUID
/(?:data|user/\d+) # the primary user or user N
/(?:data|user(?:_de)?/\d+) # credential- or device-protected storage
/[^/]+ # package name, one path segment
)
/files(?=/|\Z) # the files directory the interpreter runs from
Expand Down
2 changes: 1 addition & 1 deletion src/platformdirs/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def _shared_storage() -> str:

_USER_ID: Final[re.Pattern[str]] = re.compile(
r"""
/user/ # the per-user data root, under /data or an adopted /mnt/expand volume
/user(?:_de)?/ # the credential- or device-protected per-user data root
(?P<user>\d+) # the Android user id
/ # followed by the package folder
""",
Expand Down
12 changes: 12 additions & 0 deletions tests/platformdirs/test_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,18 @@ def test_android(params: AppParams, func: str) -> None:
("path", "expected"),
[
pytest.param("/data/user/1/a/files", "/data/user/1/a", id="user"),
pytest.param("/data/user_de/10/a/files/app", "/data/user_de/10/a", id="device-protected"),
pytest.param("/data/data/a/files", "/data/data/a", id="data"),
pytest.param(
"/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user/1/a/files",
"/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user/1/a",
id="adopted",
),
pytest.param(
"/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user_de/11/a/files/app",
"/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user_de/11/a",
id="adopted-device-protected",
),
pytest.param("/data/data/filesync.app/files/app", "/data/data/filesync.app", id="data-files-prefix"),
pytest.param(
"/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user/0/filesync.app/files/app",
Expand Down Expand Up @@ -213,11 +219,17 @@ def _clear_android_caches() -> None:
[
pytest.param("/data/data/org.example.app", "/storage/emulated/0", id="owner"),
pytest.param("/data/user/10/org.example.app", "/storage/emulated/10", id="secondary-user"),
pytest.param("/data/user_de/10/org.example.app", "/storage/emulated/10", id="device-protected"),
pytest.param(
"/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user/11/org.example.app",
"/storage/emulated/11",
id="adopted",
),
pytest.param(
"/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user_de/11/org.example.app",
"/storage/emulated/11",
id="adopted-device-protected",
),
pytest.param(None, "/storage/emulated/0", id="unknown"),
],
)
Expand Down