36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
"""Bounded temporary work files on a private per-run tmpfs mount."""
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
from .catalog import safe_path
|
|
|
|
|
|
def mounts(settings):
|
|
result = []
|
|
for line in Path("/proc/mounts").read_text().splitlines():
|
|
device, target, kind, *_ = line.split()
|
|
if not device.startswith("xas-run-") or kind != "tmpfs":
|
|
continue
|
|
identity = device.removeprefix("xas-run-")
|
|
if not re.fullmatch(r"[a-f0-9]{32}", identity):
|
|
continue
|
|
expected = settings.runs / identity / "work"
|
|
if target != str(expected):
|
|
continue
|
|
result.append(safe_path(expected, settings.runs))
|
|
return result
|
|
|
|
|
|
def create(settings, identity, uid):
|
|
target = safe_path(settings.runs / identity / "work", settings.runs)
|
|
if target in mounts(settings):
|
|
raise ValueError("本次临时目录已经挂载,拒绝覆盖")
|
|
subprocess.run(["mount", "-t", "tmpfs", "-o", f"size=134217728,nodev,nosuid,noexec,mode=0770,uid={uid},gid={uid}",
|
|
"xas-run-" + identity, str(target)], check=True, capture_output=True, timeout=10)
|
|
|
|
|
|
def remove(settings, target):
|
|
if target not in mounts(settings):
|
|
raise ValueError("临时挂载归属核验失败")
|
|
subprocess.run(["umount", "--", str(target)], check=True, capture_output=True, timeout=10)
|