# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; coding:utf-8 -*-
#
# Copyright 2002 Ben Escoto
# Copyright 2007 Kenneth Loafman
#
# This file is part of duplicity.
#
# Duplicity is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# Duplicity is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with duplicity; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


"""
Functions for patching of directories.
"""

import os
import re
import tempfile

from duplicity import diffdir
from duplicity import dup_tarfile
from duplicity import errors
from duplicity import librsync
from duplicity import log
from duplicity import selection
from duplicity import tempdir
from duplicity import util
from duplicity.lazy import ITRBranch
from duplicity.lazy import IterTreeReducer
from duplicity.path import ROPath


class PatchDirException(Exception):
    """
    Base exception for directory patching errors.
    """

    pass


def filter_path_iter(path_iter, index):
    """
    Rewrite path elements of path_iter so they start with index.

    Discard any that doesn't start with index, and remove the index
    prefix from the rest.

    :param iterator path_iter: Iterator of ROPaths.
    :param tuple index: The path prefix to filter and strip.
    :returns: An iterator of rewritten ROPaths.
    :rtype: iterator
    """
    assert isinstance(index, tuple) and index, "Index must be a non-empty tuple specifying the path prefix"
    l = len(index)
    for path in path_iter:
        if path.index[:l] == index:
            path.index = path.index[l:]
            yield path


def difftar2path_iter(diff_tarfile):
    """
    Turn a file-like difftar object into an iterator of ROPaths.

    :param TarFile diff_tarfile: The tarfile object containing the diff.
    :returns: An iterator of ROPaths.
    :rtype: iterator
    """
    tar_iter = iter(diff_tarfile)
    multivol_fileobj = None

    # The next tar_info is stored in this one element list so
    # Multivol_Filelike below can update it.  Any StopIterations will
    # be passed upwards.
    try:
        tarinfo_list = [next(tar_iter)]
    except StopIteration:
        return

    while True:
        # This section relevant when a multivol diff is last in tar
        if not tarinfo_list[0]:
            return
        if multivol_fileobj and not multivol_fileobj.at_end:
            multivol_fileobj.close()  # aborting in middle of multivol
            continue

        index, difftype, multivol = get_index_from_tarinfo(tarinfo_list[0])
        ropath = ROPath(index)
        ropath.init_from_tarinfo(tarinfo_list[0])
        ropath.difftype = difftype
        if difftype == "deleted":
            ropath.type = None
        elif ropath.isreg():
            if multivol:
                multivol_fileobj = Multivol_Filelike(diff_tarfile, tar_iter, tarinfo_list, index)
                ropath.setfileobj(multivol_fileobj)
                yield ropath
                continue  # Multivol_Filelike will reset tarinfo_list
            else:
                ropath.setfileobj(diff_tarfile.extractfile(tarinfo_list[0]))
        yield ropath
        try:
            tarinfo_list[0] = next(tar_iter)
        except StopIteration:
            return


def get_index_from_tarinfo(tarinfo):
    """
    Return (index, difftype, multivol) pair from tarinfo object.

    :param TarInfo tarinfo: The tarinfo object to parse.
    :returns: A tuple of (index, difftype, multivol).
    :rtype: tuple
    :raises PatchDirException: If the diff entry is unrecognized or contains '..'.
    """
    for prefix in [
        "snapshot/",
        "diff/",
        "deleted/",
        "multivol_diff/",
        "multivol_snapshot/",
    ]:
        tiname = util.get_tarinfo_name(tarinfo)
        if tiname.startswith(prefix):
            name = tiname[len(prefix) :]  # strip prefix
            if prefix.startswith("multivol"):
                if prefix == "multivol_diff/":
                    difftype = "diff"
                else:
                    difftype = "snapshot"
                multivol = 1
                name, num_subs = re.subn("(?s)^multivol_(diff|snapshot)/?(.*)/[0-9]+$", "\\2", tiname)
                if num_subs != 1:
                    raise PatchDirException(f"Unrecognized diff entry {tiname}")
            else:
                difftype = prefix[:-1]  # strip trailing /
                name = tiname[len(prefix) :]
                if name.endswith(r"/"):
                    name = name[:-1]  # strip trailing /'s
                multivol = 0
            break
    else:
        raise PatchDirException(f"Unrecognized diff entry {tiname}")
    if name == r"." or name == r"":
        index = ()
    else:
        index = tuple(os.fsencode(name).split(b"/"))
        if b".." in index:
            raise PatchDirException(f"Tar entry {os.fsdecode(tiname)} contains '..'.  Security violation")
    return index, difftype, multivol


class Multivol_Filelike(object):
    """Emulate a file like object from multivols

    Maintains a buffer about the size of a volume.  When it is read()
    to the end, pull in more volumes as desired.

    """

    def __init__(self, tf, tar_iter, tarinfo_list, index):
        """
        Initializer.  tf is TarFile obj, tarinfo is first tarinfo
        """
        self.tf, self.tar_iter = tf, tar_iter
        self.tarinfo_list = tarinfo_list  # must store as list for write access
        self.index = index
        self.buffer = b""
        self.at_end = False

    def read(self, length=-1):
        """
        Read length bytes from file
        """
        if length < 0:
            while self.addtobuffer():
                pass
            real_len = len(self.buffer)
        else:
            while len(self.buffer) < length:
                if not self.addtobuffer():
                    break
            real_len = min(len(self.buffer), length)

        result = self.buffer[:real_len]
        self.buffer = self.buffer[real_len:]
        return result

    def addtobuffer(self):
        """
        Add next chunk to buffer
        """
        if self.at_end:
            return False
        index, difftype, multivol = get_index_from_tarinfo(self.tarinfo_list[0])
        if not multivol or index != self.index:
            # we've moved on
            # the following communicates next tarinfo to difftar2path_iter
            self.at_end = True
            return False

        fp = self.tf.extractfile(self.tarinfo_list[0])
        self.buffer += fp.read()
        fp.close()

        try:
            self.tarinfo_list[0] = next(self.tar_iter)
        except StopIteration:
            self.tarinfo_list[0] = None
            self.at_end = True
            return False
        return True

    def close(self):
        """
        If not at end, read remaining data
        """
        if not self.at_end:
            while True:
                self.buffer = b""
                if not self.addtobuffer():
                    break
        self.at_end = True


class TarFile_FromFileobjs(object):
    """
    Like a dup_tarfile.TarFile iterator, but read from multiple fileobjs
    """

    def __init__(self, fileobj_iter):
        """Make new tarinfo iterator

        fileobj_iter should be an iterator of file objects opened for
        reading.  They will be closed at end of reading.

        """
        self.fileobj_iter = fileobj_iter
        self.dup_tarfile, self.tar_iter = None, None
        self.current_fp = None

    def __iter__(self):  # pylint: disable=non-iterator-returned
        """
        Return self as an iterator.
        """
        return self

    def set_tarfile(self):
        """
        Set dup_tarfile from next file object, or raise StopIteration
        """
        if self.current_fp:
            assert (
                not self.current_fp.close()
            ), "Closing current file pointer returned a truthy value indicating an error"

        self.current_fp = next(self.fileobj_iter)

        self.dup_tarfile = util.make_tarfile("r", self.current_fp)
        self.tar_iter = iter(self.dup_tarfile)

    def __next__(self):
        """
        Return the next tarinfo object.
        """
        if not self.dup_tarfile:
            try:
                self.set_tarfile()
            except StopIteration:
                return
        try:
            return next(self.tar_iter)
        except StopIteration:
            assert not self.dup_tarfile.close(), "self.dup_tarfile failed to close"
            self.set_tarfile()
            return next(self.tar_iter)

    def extractfile(self, tarinfo):
        """
        Return data associated with given tarinfo
        """
        return self.dup_tarfile.extractfile(tarinfo)


def collate_iters(iter_list):
    """Collate iterators by index

    Input is a list of n iterators each of which must iterate elements
    with an index attribute.  The elements must come out in increasing
    order, and the index should be a tuple itself.

    The output is an iterator which yields tuples where all elements
    in the tuple have the same index, and the tuple has n elements in
    it.  If any iterator lacks an element with that index, the tuple
    will have None in that spot.

    """
    # overflow[i] means that iter_list[i] has been exhausted
    # elems[i] is None means that it is time to replenish it.
    iter_num = len(iter_list)
    if iter_num == 2:
        return diffdir.collate2iters(iter_list[0], iter_list[1])
    overflow = [None] * iter_num
    elems = overflow[:]

    def setrorps(overflow, elems):
        """
        Set the overflow and rorps list
        """
        for i in range(iter_num):
            if not overflow[i] and elems[i] is None:
                try:
                    elems[i] = next(iter_list[i])
                except StopIteration:
                    overflow[i] = 1
                    elems[i] = None

    def getleastindex(elems):
        """
        Return the first index in elems, assuming elems isn't empty
        """
        return min([elem.index for elem in [x for x in elems if x]])

    def yield_tuples(iter_num, overflow, elems):
        while True:
            setrorps(overflow, elems)
            if None not in overflow:
                break

            index = getleastindex(elems)
            yieldval = []
            for i in range(iter_num):
                if elems[i] and elems[i].index == index:
                    yieldval.append(elems[i])
                    elems[i] = None
                else:
                    yieldval.append(None)
            yield tuple(yieldval)

    return yield_tuples(iter_num, overflow, elems)


def normalize_ps(patch_sequence):
    """Given an sequence of ROPath deltas, remove blank and unnecessary

    The sequence is assumed to be in patch order (later patches apply
    to earlier ones).  A patch is unnecessary if a later one doesn't
    require it (for instance, any patches before a "delete" are
    unnecessary).

    """
    result_list = []
    i = len(patch_sequence) - 1
    while i >= 0:
        delta = patch_sequence[i]
        if delta is not None:
            # skip blank entries
            result_list.insert(0, delta)
            if delta.difftype != "diff":
                break
        i -= 1
    return result_list


def patch_seq2ropath(patch_seq):
    """
    Apply the patches in patch_seq, return single ropath
    """
    first = patch_seq[0]
    assert first.difftype != "diff", f"First patch in sequence {patch_seq} was a diff"
    if not first.isreg():
        # No need to bother with data if not regular file
        assert len(patch_seq) == 1, f"Patch sequence isn't regular, but has {len(patch_seq)} entries"
        return first.get_ropath()

    current_file = first.open("rb")

    for delta_ropath in patch_seq[1:]:
        assert (
            delta_ropath.difftype == "diff"
        ), f"Expected difftype 'diff' in patch sequence, got {delta_ropath.difftype}"
        try:
            cur_file.fileno()
        except Exception as e:
            """
            librsync insists on a real file object, which we create manually
            by using the duplicity.tempdir to tell us where.

            See https://bugs.launchpad.net/duplicity/+bug/670891 for discussion
            of os.tmpfile() vs tempfile.TemporaryFile() w.r.t. Windows / Posix,
            which is worked around in librsync.PatchedFile() now.
            """
            tempfp = tempfile.TemporaryFile(dir=tempdir.default().dir())
            util.copyfileobj(current_file, tempfp)
            assert (
                not current_file.close()
            ), "Closing temporary current_file returned a truthy value indicating an error"
            tempfp.seek(0)
            current_file = tempfp
        current_file = librsync.PatchedFile(current_file, delta_ropath.open("rb"))
    result = patch_seq[-1].get_ropath()
    result.setfileobj(current_file)
    return result


def integrate_patch_iters(iter_list):
    """
    Combine a list of iterators of ROPath patches.

    The ``iter_list`` should be sorted in patch order, and the elements in
    each iterator need to be ordered by index. The output will be an
    iterator of the final ROPaths in index order.

    :param list iter_list: A list of ROPath iterators.
    :returns: An iterator of integrated ROPaths.
    :rtype: iterator
    """
    collated = collate_iters(iter_list)
    for patch_seq in collated:
        normalized = normalize_ps(patch_seq)
        try:
            final_ropath = patch_seq2ropath(normalized)
            if final_ropath.exists():
                # otherwise final patch was delete
                yield final_ropath
        except Exception as e:
            filename = normalized[-1].get_ropath().get_relative_path()
            log.Warn(
                _("Error '%s' patching %s") % (util.uexc(e), os.fsdecode(filename)),
                log.WarningCode.cannot_process,
                util.escape(filename),
            )


def tarfiles2rop_iter(tarfile_list, restrict_index=()):
    """Integrate tarfiles of diffs into single ROPath iter

    Then filter out all the diffs in that index which don't start with
    the restrict_index.

    """
    diff_iters = [difftar2path_iter(x) for x in tarfile_list]
    if restrict_index:
        # Apply filter before integration
        diff_iters = [filter_path_iter(x, restrict_index) for x in diff_iters]
    return integrate_patch_iters(diff_iters)


def Write_ROPaths(base_path, rop_iter):
    """
    Write out ROPaths in ``rop_iter`` starting at ``base_path``.

    :param Path base_path: The base path where to write the ROPaths.
    :param iterator rop_iter: An iterator of ROPaths to write.
    :returns: 1 if something was actually written, 0 otherwise.
    :rtype: int
    """
    ITR = IterTreeReducer(ROPath_IterWriter, [base_path])
    return_val = 0
    for ropath in rop_iter:
        return_val = 1
        ITR(ropath.index, ropath)
    ITR.Finish()
    base_path.setdata()
    return return_val


class ROPath_IterWriter(ITRBranch):
    """Used in Write_ROPaths above

    We need to use an ITR because we have to update the
    permissions/times of directories after we write the files in them.

    """

    def __init__(self, base_path):
        """
        Set base_path, Path of root of tree
        """
        self.base_path = base_path
        self.dir_diff_ropath = None
        self.dir_new_path = None

    def start_process(self, index, ropath):
        """
        Write ropath.  Only handles the directory case.
        """
        if not ropath.isdir():
            # Base may not be a directory, but rest should
            new_path = self.base_path.new_index(index)
            if index != ():
                parent_path = new_path.get_parent_dir()
                if not parent_path.exists():
                    parent_path.mkdir()
            if ropath.exists():
                if new_path.exists():
                    new_path.deltree()
                ropath.copy(new_path)

        self.dir_new_path = self.base_path.new_index(index)
        if not self.dir_new_path.exists():
            self.dir_new_path.mkdir()
        self.dir_diff_ropath = ropath

    def end_process(self):
        """
        Update information of a directory when leaving it.
        """
        if self.dir_diff_ropath:
            self.dir_diff_ropath.copy_attribs(self.dir_new_path)

    def can_fast_process(self, index, ropath):  # pylint: disable=unused-argument
        """
        Can fast process (no recursion) if ropath isn't a directory.
        """
        log.Info(
            _("Writing %s of type %s") % (os.fsdecode(ropath.get_relative_path()), ropath.type),
            log.InfoCode.patch_file_writing,
            f"{util.escape(ropath.get_relative_path())} {ropath.type}",
        )
        return not ropath.isdir()

    def fast_process(self, index, ropath):
        """
        Write non-directory ropath to destination.
        """
        if ropath.exists():
            ropath.copy(self.base_path.new_index(index))
