"""Inspect or delete only the active WSL user's Texrra project store.""" from contextlib import ExitStack import fcntl import json import os from pathlib import Path import re import shutil import stat import sys import unicodedata def write_display(name, value, max_units): encoded = value.encode("utf-16-le") if not encoded or len(encoded) > max_units * 2 or any( unicodedata.category(char) in ("Cc", "Cf") for char in value ): raise ValueError("The project location cannot be displayed safely") with open(name, "xb") as display: display.write(encoded) def mounted_project_tree(projects): for line in Path("/proc/self/mountinfo").read_bytes().splitlines(): mount = re.sub(rb"\\([0-7]{3})", lambda match: bytes([int(match[1], 8)]), line.split()[4]) mount = Path(os.fsdecode(mount)) if mount == projects or projects in mount.parents: return True return False def run(action): uid = os.geteuid() distro = os.environ.get("WSL_DISTRO_NAME", "") if not uid or not distro or '"' in distro or "\\" in distro: raise ValueError("The WSL user or distribution is unavailable") data = Path(os.environ.get("XDG_DATA_HOME", "")) if not data.is_absolute(): data = Path.home() / ".local/share" if data.anchor != "/" or ".." in data.parts: raise ValueError("The WSL data location is unsafe") home = data / "texrra-desktop" projects = home / "projects" if not shutil.rmtree.avoids_symlink_attacks: raise ValueError("Safe recursive deletion is unavailable") directory_flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW with ExitStack() as held: parent = None try: for part in home.parts: parent = os.open(part, directory_flags, dir_fd=parent) held.callback(os.close, parent) info = os.stat("projects", dir_fd=parent, follow_symlinks=False) except FileNotFoundError: return 2 if ( os.fstat(parent).st_uid != uid or info.st_uid != uid or not stat.S_ISDIR(info.st_mode) or mounted_project_tree(projects) ): raise ValueError("The project location is unsafe") # Use the same lock as the desktop launcher. An active editor keeps its data. lock = os.open("backend.lock", os.O_RDONLY | os.O_NONBLOCK | os.O_NOFOLLOW, dir_fd=parent) held.callback(os.close, lock) lock_info = os.fstat(lock) if not stat.S_ISREG(lock_info.st_mode) or lock_info.st_uid != uid or lock_info.st_nlink != 1: raise ValueError("The project store has no safe editor lock") fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) if os.stat("projects", dir_fd=parent, follow_symlinks=False) != info: raise ValueError("The project location changed while acquiring the editor lock") identity = [distro, uid, str(projects), info.st_dev, info.st_ino, info.st_ctime_ns] if action == "inspect": write_display("projects-distro.txt", distro, 160) write_display("projects-path.txt", str(projects), 620) with open("projects-identity.json", "x", encoding="utf-8") as ticket: json.dump(identity, ticket) elif action == "delete": with open("projects-identity.json", encoding="utf-8") as ticket: if json.load(ticket) != identity: raise ValueError("The project location changed after confirmation") shutil.rmtree("projects", dir_fd=parent) else: raise ValueError("Unknown cleanup action") return 0 if __name__ == "__main__": try: sys.exit(run(sys.argv[1])) except (OSError, ValueError, IndexError): sys.exit(1)