<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
# -*- coding: utf-8 -*-
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENCE.TXT
#
import typing
from dataclasses import dataclass, field

if typing.TYPE_CHECKING:
    from .mount_config import IsolatedRootConfig
from .mount_types import MountEntry, MountType


@dataclass
class MountConfig:
    """
    Builds mount configuration for jail.c implementation.

    Accumulates MountEntry objects, then renders everything at the end.
    """

    uid: int
    gid: int

    _mounts: list[MountEntry] = field(default_factory=list, repr=False)

    @property
    def _default_opts(self) -> tuple[str, ...]:
        return f"uid={self.uid}", f"gid={self.gid}", "mode=0750"

    @property
    def mounts(self) -> tuple[MountEntry, ...]:
        """Read-only access to accumulated mount entries."""
        return tuple(self._mounts)

    def add(self, type_: MountType, source: str, target: str = "", options: tuple[str] = tuple()):
        """Add a single mount entry."""
        self._mounts.append(
            MountEntry(
                type=type_,
                source=source,
                target=target,
                options=options,
            )
        )

    def comment(self, text: str):
        """Add a comment line for organization."""
        self._mounts.append(MountEntry(MountType.COMMENT, text))

    def add_overlay(self, overlay: "IsolatedRootConfig"):
        """
        Add mount operations for a directory overlay.

        Args:
            overlay: The overlay configuration
        """
        if not overlay.persistent:
            # Create temporary in-memory storage
            self._mounts.append(
                MountEntry(
                    MountType.BIND, "tmpfs", overlay.root_path, ("mkdir",) + self._default_opts
                )
            )
        else:
            # Persistent real directory storage (bind to self with mkdir)
            self._mounts.append(
                MountEntry(
                    MountType.BIND,
                    overlay.root_path,
                    overlay.root_path,
                    ("mkdir",) + self._default_opts,
                )
            )

    def close_overlay(self, overlay: "IsolatedRootConfig"):
        """Add the final mount that closes an overlay."""
        self._mounts.extend(overlay.mounts)

        self._mounts.append(
            MountEntry(MountType.BIND, overlay.root_path, overlay.target, ("recursive",))
        )

    def render(self, docroot: str) -> str:
        """Render complete configuration to jail.c syntax."""
        lines = [f"[{docroot}]"]
        lines.extend(m.render() for m in self._mounts)
        return "\n".join(lines)
