
import os

class ShareDirs:
    """
    需要共享的目录
    """

    def __init__(self, shadow=False, ostree=False):
        # brwap docker
        self.shadow = shadow
        self.ostree = ostree
        self.driver_dirs = ["/sys"]
        self.process_dirs = ["/run", "/proc"]
        self.storage_dirs = ["/home", "/media", "/mnt", "/tmp", "/data"]
        # self.time_dirs = ["/etc/localtime", "/etc/timezone"]
        self.time_dirs = ["/etc/timezone"]
        self.shadow_dirs = ["/etc/apt/sources.list", "/etc/dpkg/plugins", "/opt/certaide.kylin"]
        self.other = []
        self.crun_dirs = [
            # tmp
            ["/tmp", ["rbind","rw","rprivate"], "/tmp"],
            ["/sys", ["nodev","nosuid","noexec"], "tmpfs", "tmpfs"],
            ["/dev",["nodev","strictatime","mode=755","size=65536k"], "tmpfs", "tmpfs"],
            # cgroup
            ["/sys/fs/cgroup", ["nodev","nosuid","noexec"], "cgroup", "cgroup"],
            # ["/run/udev", ["rbind","rprivate"], "/run/udev", "cgroup"],
            # bind
            ["/sys", ["rbind","rprivate"], "/sys"],
            # ["/etc/passwd", ["rbind","ro","rprivate"], "/etc/passwd"],
            # ["/etc/group", ["rbind","ro","rprivate"], "/etc/group"],
            ["/etc/x-proj", ["rbind","ro","rprivate"], "/etc/x-proj"],
            ["/etc/machine-id", ["rbind","ro","rprivate"], "/etc/machine-id"],
            ["/etc/resolv.conf", ["rbind","ro","rprivate"], "/etc/resolv.conf"],
            ["/etc/host.conf", ["rbind","ro","rprivate"], "/etc/host.conf"],
            ["/etc/hosts", ["rbind","ro","rprivate"], "/etc/hosts"],
            ["/etc/gai.conf", ["rbind","ro","rprivate"], "/etc/gai.conf"],
            ["/usr/share/zoneinfo", ["rbind","ro","rprivate"], "/usr/share/zoneinfo"],
            ["/etc/localtime", ["rbind","ro","rprivate"], "/etc/localtime"],
            # ["/etc/localtime", ["rbind","ro","rprivate"], "/usr/share/zoneinfo/Asia/Shanghai"],
            ["/etc/timezone", ["rbind","ro","rprivate"], "/etc/timezone"],
            ["/tmp/.X11-unix", ["rbind","rprivate"], "/tmp/.X11-unix"],
            ["/proc", ["rbind","rw","rprivate"], "/proc"],
            # run 直接挂载与间接挂载是否可共存
            ["/run", ["rbind","rw","rprivate"], "/run"],
            # ["/run/systemd/resolve/io.systemd.Resolve", ["rbind","ro","rprivate"], "/run/systemd/resolve/io.systemd.Resolve"],
            # ["/run/systemd/journal/socket", ["rbind","ro","rprivate"], "/run/systemd/journal/socket"],
            # ["/run/systemd/journal/stdout", ["rbind","ro","rprivate"], "/run/systemd/journal/stdout"],
            # ["/run/dbus/system_bus_socket", ["rbind","ro","rprivate"], "/var/run/dbus/system_bus_socket"],
            # ["/run/host/share", ["rbind","rprivate"], "/usr/share"],
            ["/data", ["rbind","rprivate"], "/data"],
            ["/mnt", ["rbind","rw","rprivate"], "/mnt"],
            ["/dev", ["rbind","rprivate"], "/dev"],
            ["/home", ["rbind","rw","rprivate"], "/home"],
            ["/var/log", ["rbind","rw","rprivate"], "/var/log"],
            ["/cdrom", ["rbind","rw","rprivate"], "/cdrom"],
            # host-spwan
            # ["/run/host/usr", ["rbind","ro","rprivate"], "/usr"],
            # ["/run/host/etc", ["rbind","ro","rprivate"], "/etc"],
            # DKMS路径
            ["/opt/system/lib/modules", ["rbind","rw","rprivate"], "/opt/system/lib/modules"],
            ["/etc/dkms", ["rbind","rw","rprivate"], "/etc/dkms"],
            ["/var/lib/dkms", ["rbind","rw","rprivate"], "/var/lib/dkms"],
            ["/etc/cups/ppd", ["rbind","rw","rprivate"], "/etc/cups/ppd"],
            ["/var/spool/cups", ["rbind","rw","rprivate"], "/var/spool/cups"],
            ["/var/tmp", ["rbind","rw","rprivate"], "/var/tmp"],
            # 生物认证
            # ["/usr/lib/biometric-authentication/drivers", ["rbind","rw","rprivate"], "/usr/lib/biometric-authentication/drivers"],
            # ["/etc/biometric-auth", ["rbind","rw","rprivate"], "/etc/biometric-auth"],
            # kare 配置文件目录
            ["/etc/kare", ["rbind","rw","rprivate"], "/etc/kare"],
            ["/opt/kaiming/share/driver", ["rbind","rw","rprivate"], "/opt/kaiming/share/driver"],
            ["/etc/udev/rules.d/", ["rbind","rw","rprivate"], "/opt/system/conf/udev/rules.d/"],

        ]
        # 解决数科安装失败问题
        if os.getuid() != 0:
            self.crun_dirs.append(["/run/host/usr", ["rbind","ro","rprivate"], "/usr"])
            self.crun_dirs.append(["/run/host/etc", ["rbind","ro","rprivate"], "/etc"])
            self.crun_dirs.append(["/run/host/share", ["rbind","rprivate"], "/usr/share"])
        self.crun_dirs.append(["/run/media", ["rbind","rw","rshared"], "/run/media"])
        self.crun_shadow_dirs = [
            ["/etc/apt/sources.list", ["rbind","ro","rprivate"], "/etc/apt/sources.list"],
            # ["/etc/dpkg/plugins", ["rbind","ro","rprivate"], "/dev/null"],
            ["/etc/dpkg/dpkg.cfg.d/ostree-guard", ["rbind","ro","rprivate"], "/dev/null"],
            ["/opt/certaide.kylin", ["rbind","ro","rprivate"], "/opt/certaide.kylin"]
        ]

    @property
    def share_dirs(self):
        if self.shadow:
            return self.driver_dirs + self.process_dirs + self.storage_dirs + self.time_dirs + self.shadow_dirs
        else:
            return self.driver_dirs + self.process_dirs + self.storage_dirs + self.time_dirs

    def create_bind_entry(self, destination, options, source, type="bind"):
        if os.path.exists(source) or source.startswith("/") is False:
            return {"destination": destination, "options": options, "source": source, "type": type}

    @property    
    def get_bind_dirs(self):
        bind_dirs = []
        for bind_dir in self.crun_dirs:
            if self.create_bind_entry(*bind_dir) is not None:
                bind_dirs.append(self.create_bind_entry(*bind_dir))
        if self.shadow:
            for bind_dir in self.crun_shadow_dirs:
                if self.create_bind_entry(*bind_dir) is not None:
                    bind_dirs.append(self.create_bind_entry(*bind_dir))
        return bind_dirs

    @property 
    def get_chroot_share_dir(self):
        chroot_dir=["/sys","/run","/proc","/tmp","/dev","/media", "/mnt","/etc/apt/sources.list", "/etc/dpkg/plugins"]
        return chroot_dir
    @property
    def get_chroot_dkms_dir(self):
        dkms_dir = ["/opt/system/lib/modules", "/etc/dkms", "/var/lib/dkms"]
        return dkms_dir
class BaseDirs:

    def __init__(self, base_path):
        self.base_path = "/" if os.path.basename(base_path) == "kare-shadow" else base_path
        self._dirs = ["bin", "usr", "var", "etc", "lib", "opt", "root", "sbin", "lib64"]

    def __getattr__(self, name):
        dir_path = os.path.join(self.base_path, name)
        if os.path.exists(dir_path):
            return dir_path

    @property
    def base_dirs(self):
        return [getattr(self, dir_name) for dir_name in self._dirs if getattr(self, dir_name)]

