# Copyright 2013-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import os

from portage.util import apply_stat_permissions
from portage.util._async.AsyncTaskFuture import AsyncTaskFuture
from portage.util.file_copy import copyfile
from portage.util.futures import asyncio
from portage.util.futures.executor.fork import ForkExecutor


class FileCopier(AsyncTaskFuture):
    """
    Asynchronously copy a file.
    """

    __slots__ = ("dest_path", "src_path")

    def _start(self):
        self.future = asyncio.ensure_future(
            self.scheduler.run_in_executor(
                ForkExecutor(loop=self.scheduler),
                self._target,
                self.src_path,
                self.dest_path,
            )
        )
        super()._start()

    @staticmethod
    def _target(src_path, dest_path):
        copyfile(src_path, dest_path)
        apply_stat_permissions(dest_path, os.stat(src_path))
