# Copyright (c) TuxCare, Inc. All rights reserved.
# Licensed under TUXCARE LICENSE AGREEMENT
# https://tuxcare.com/legal/

import errno
import os
import shutil
import stat
import tempfile

from . import log_utils, process_utils

if False:  # pragma: no cover
    from typing import Any, Optional, Union  # noqa: F401


def atomic_write(fname, content, ensure_dir=False, mode='w', create_mode=0o644, preserve_attrs=False, force_mode=None):
    # type: (str, Union[str, bytes], bool, str, int, bool, Optional[int]) -> None
    """Replace fname with content so that after a reset it has either the old or the new content.

    The rename gives fname a new inode. preserve_attrs also keeps what an admin may have set
    on the old one (owner, ACLs, xattrs, SELinux label) and writes through a symlink that
    only root could have placed. Hard links can't survive a rename.

    The file keeps the mode of the one it replaces, or gets create_mode when there is none.
    force_mode sets the mode whatever the old file had: for a secret that an earlier version
    left readable (create_mode alone would never tighten it). The temp file is created 0600 and gets the final
    mode before the content is written; with preserve_attrs the old file's attributes are copied
    onto it first, so it carries them (the old mode, if wider) until that fchmod.

    A failure of the directory fsync after the rename is only logged: the new content is in place.
    """
    if preserve_attrs:
        fname = resolve_trusted_link(fname)
    dname = os.path.dirname(fname)
    if ensure_dir and not os.path.exists(dname):
        os.makedirs(dname)
    if force_mode is not None:
        st_mode = force_mode
    else:
        try:
            st_mode = os.stat(fname).st_mode
        except Exception:
            st_mode = create_mode
    tmp_file = tempfile.NamedTemporaryFile(mode=mode, dir=dname, prefix=os.path.basename(fname) + '.', delete=False)
    try:
        with tmp_file as f:
            # in a directory others can write, they could swap the temp file for a link that cp follows
            if preserve_attrs and os.path.isfile(fname) and is_trusted_dir(dname):
                copy_attrs(fname, tmp_file.name)
            os.fchmod(f.fileno(), st_mode)
            f.write(content)
            f.flush()
            os.fsync(f.fileno())
        os.rename(tmp_file.name, fname)
    except Exception:
        os.unlink(tmp_file.name)
        raise

    # ensure folder is also updated, so the rename itself is on disk too.
    # If this fails, the new content is in place but may not survive a reset, so it still raises
    # https://www.quora.com/When-should-you-fsync-the-containing-directory-in-addition-to-the-file-itself
    # https://www.reddit.com/r/kernel/comments/1du6ot8/comment/lbgu46i/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
    fsync_path(dname)


MAX_SYMLINKS = 40  # as the kernel, so a loop of links ends


def is_trusted_dir(path):
    # type: (str) -> bool
    """Is path owned by root, or the user running us, and writable by nobody else?

    Only path itself is checked: its ancestors are assumed to be root-controlled.
    """
    try:
        st = os.stat(path)
    except OSError:
        # can't tell, so don't trust it
        return False
    return st.st_uid in (0, os.geteuid()) and not st.st_mode & (stat.S_IWGRP | stat.S_IWOTH)


def resolve_trusted_link(path):
    # type: (str) -> str
    """Follow path through symlinks that only root, or the user running us, could have placed.

    Writing through a link as root lands wherever the link points, so a link that someone
    else could plant (they own it, or own or can write to its directory) is not followed:
    the rename then replaces the link itself.
    """
    for _ in range(MAX_SYMLINKS):
        if not os.path.islink(path):
            return path
        dname = os.path.dirname(path)
        if os.lstat(path).st_uid not in (0, os.geteuid()) or not is_trusted_dir(dname):
            return path
        path = os.path.join(dname, os.readlink(path))
    return path


def copy_attrs(src, dst):
    # type: (str, str) -> None
    """Copy src's owner, mode, ACLs, xattrs and SELinux label to dst, where cp can.

    cp is the one way to copy all of them on every supported Python. With --preserve=all
    it doesn't fail on an xattr or a label the filesystem can't hold. If cp itself fails,
    dst keeps the attributes it has and the write goes on.
    """
    # system directories only, never PATH, so root doesn't run a planted cp;
    # a split-/usr host (debian up to 11, ubuntu up to 18.04) keeps cp in /bin
    cp = process_utils.find_cmd('cp', ('/usr/bin', '/bin'), raise_exc=False)
    code, stderr = None, 'cp not found'  # type: Optional[int], str
    if cp:
        cmd = [cp, '--attributes-only', '--preserve=all', '--', src, dst]
        try:
            code, _, stderr = process_utils.run_command(cmd, catch_stdout=True, catch_stderr=True)  # type: ignore[no-untyped-call]
        except OSError as e:
            stderr = str(e)
    if code != 0:
        log_utils.logwarn('Failed to keep the attributes of {0}: {1}'.format(src, stderr), print_msg=False)


def fsync_path(path, raise_exc=True):
    # type: (str, bool) -> None
    """fsync path. With raise_exc=False a failure is only logged, as a warning.

    Only for a sync whose failure must not undo or misreport what already happened: the manifest
    and signature swap of install_manifest (both renames are done), and the depmod index synced
    after the module was loaded. Everywhere else a failure (EIO on a failing disk) raises.
    """
    try:
        fd = os.open(path, os.O_RDONLY)
        try:
            os.fsync(fd)
        finally:
            os.close(fd)
    except OSError as e:
        if e.errno in (errno.EINVAL, errno.ENOTSUP, errno.EOPNOTSUPP):
            # the filesystem has no fsync for it (a directory on CIFS, some FUSE): name it, so it can be moved
            log_utils.logwarn(
                'Failed to sync {0}: the filesystem mounted at {1} does not support fsync, so changes there '
                'may not survive a power loss'.format(path, mount_of(path))
            )
        elif not raise_exc:
            log_utils.logwarn('Failed to sync {0}: {1}'.format(path, e))
        if raise_exc:
            raise


def mount_of(path):
    # type: (str) -> str
    """The mount point of the filesystem that holds path.

    For a bind mount of a directory, it is the mount point of the filesystem the directory comes from.
    """
    mount = os.path.realpath(path)
    while not os.path.ismount(mount):
        mount = os.path.dirname(mount)
    return mount


def sync_rename(src, dst):
    # type: (str, str) -> None
    """Move a fully written src over dst, so after a reset dst has either its old or its new content.

    The data fsync, the rename and the directory fsync after it raise on failure.
    """
    # a symlink holds no data to sync, and opening one would sync its target instead
    if not os.path.islink(src):
        fsync_path(src)
    os.rename(src, dst)
    fsync_path(os.path.dirname(dst) or os.curdir)


def fsync_tree(path):
    # type: (str) -> None
    """fsync path and every file and directory under it, e.g. after an external tool unpacked files there.

    A regular file is a tree of one. Anything else at path, such as a FIFO or a device, is skipped, as
    it is under a directory. It raises on the first failure; the caller decides what that means.
    fetch_userspace_patch only logs it: the patches work now, and the next update unpacks them again.
    """

    def _raise(err):  # type: (OSError) -> None
        # by default os.walk skips a directory it can't list, which would pass a partial sync as complete
        raise err

    mode = os.stat(path).st_mode
    if not stat.S_ISDIR(mode):
        if stat.S_ISREG(mode):
            fsync_path(path)
        return
    for root, _, names in os.walk(path, onerror=_raise):
        for name in names:
            name_path = os.path.join(root, name)
            # regular files only: a symlink's target may be missing, and opening a FIFO blocks
            if stat.S_ISREG(os.lstat(name_path).st_mode):
                fsync_path(name_path)
        fsync_path(root)


def save_to_file(response, dst):
    # type: (Any, str) -> None
    parent_dir = os.path.dirname(dst)
    if not os.path.exists(parent_dir):
        os.makedirs(parent_dir)
    with open(dst, 'wb') as f:
        shutil.copyfileobj(response, f)
        f.flush()
        os.fsync(f.fileno())
