Skip to content

Commit

Permalink
Support symlink of current unix username to steamuser
Browse files Browse the repository at this point in the history
  • Loading branch information
R1kaB3rN committed Feb 22, 2024
1 parent b97c9af commit 9d02075
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ulwgl_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from re import match
import subprocess
from ulwgl_dl_util import get_ulwgl_proton
from ulwgl_util import UnixUser

verbs: Set[str] = {
"waitforexitandrun",
Expand Down Expand Up @@ -62,6 +63,11 @@ def parse_args() -> Union[Namespace, Tuple[str, List[str]]]: # noqa: D103
def setup_pfx(path: str) -> None:
"""Create a symlink to the WINE prefix and tracked_files file."""
pfx: Path = Path(path).joinpath("pfx").expanduser()
steam: Path = Path(path).expanduser().joinpath("drive_c/users/steamuser")
user: UnixUser = UnixUser()
wineuser: Path = (
Path(path).expanduser().joinpath(f"drive_c/users/{user.get_user()}")
)

if pfx.is_symlink():
pfx.unlink()
Expand All @@ -71,6 +77,29 @@ def setup_pfx(path: str) -> None:

Path(path).joinpath("tracked_files").expanduser().touch()

# Create a symlink of the current user to the steamuser dir or vice versa
# Always recreate the symlink in case they move the dir
# Default for new prefixes is: unixuser -> steamuser
if (
not wineuser.is_dir()
and not steam.is_dir()
and not (wineuser.is_symlink() or steam.is_symlink())
):
# For new prefixes with our Proton: user -> steamuser
steam.mkdir(parents=True)
wineuser.unlink(missing_ok=True)
wineuser.symlink_to(steam)
elif wineuser.is_dir() and not steam.is_dir() and not steam.is_symlink():
# When there's a user dir: steamuser -> user
steam.unlink(missing_ok=True)
steam.symlink_to(wineuser)
elif not wineuser.exists() and not wineuser.is_symlink() and steam.is_dir():
wineuser.unlink(missing_ok=True)
wineuser.symlink_to(steam)
else:
# TODO Warn the user to consider merging the steamuser and user folder
pass


def check_env(
env: Dict[str, str], toml: Dict[str, Any] = None
Expand Down
33 changes: 33 additions & 0 deletions ulwgl_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from os import getuid
from pathlib import Path
from pwd import struct_passwd, getpwuid


class UnixUser:
"""Represents the User of the system as determined by the password database rather than environment variables or file system paths."""

def __init__(self):
"""Immutable properties of the user determined by the password database that's derived from the real user id."""
uid: int = getuid()
entry: struct_passwd = getpwuid(uid)
# Immutable properties, hence no setters
self.name: str = entry.pw_name
self.puid: str = entry.pw_uid # Should be equivalent to the value from getuid
self.dir: str = entry.pw_dir
self.is_user: bool = self.puid == uid

def get_home_dir(self) -> Path:
"""User home directory as determined by the password database that's derived from the current process's real user id."""
return Path(self.dir).as_posix()

def get_user(self) -> str:
"""User (login name) as determined by the password database that's derived from the current process's real user id."""
return self.name

def get_puid(self) -> int:
"""Numerical user ID as determined by the password database that's derived from the current process's real user id."""
return self.puid

def is_user(self, uid: int) -> bool:
"""Compare the UID passed in to this instance."""
return uid == self.puid

0 comments on commit 9d02075

Please sign in to comment.