"""Nullfs mount handling."""

from __future__ import annotations

import os

from .config import Mount
from .runner import run


def mount_all(root: str, mounts: list[Mount], *, log_file: str | None = None) -> None:
    for mnt in mounts:
        target = os.path.join(root, mnt.jail.lstrip("/"))
        os.makedirs(target, exist_ok=True)
        opts = "ro" if mnt.readonly else "rw"
        run(["mount", "-t", "nullfs", "-o", opts, mnt.host, target], log_file=log_file)


def unmount_all(root: str, mounts: list[Mount], *, log_file: str | None = None) -> None:
    for mnt in reversed(mounts):
        target = os.path.join(root, mnt.jail.lstrip("/"))
        run(["umount", target], allow_fail=True, log_file=log_file)
