"""
This program 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 3 of the License,
or (at your option) any later version.


This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.

Copyright © 2019 Cloud Linux Software Inc.

This software is also available under ImunifyAV commercial license,
see <https://www.imunify360.com/legal/eula>
"""
import asyncio
import errno
import json
import logging
import os
import pwd
import re
import signal as signal_module
import subprocess
import tempfile
import time
from collections import defaultdict
from contextlib import AsyncExitStack, asynccontextmanager, suppress
from functools import partial
from pathlib import Path
from typing import Callable, Dict, Generator, List, Optional, Set, Tuple, Union

from defence360agent.contracts.config import (
    Malware,
    MalwareSignatures,
    MyImunifyConfig,
)
from defence360agent.contracts.license import LicenseCLN
from defence360agent.contracts.messages import MessageType
from defence360agent.contracts.permissions import (
    ms_clean_requires_myimunify_protection,
)
from defence360agent.utils import (
    RecurringCheckStop,
    Singleton,
    base64_encode_filename,
    recurring_check,
    resource_limits,
)
from defence360agent.internals import feature_flags
from imav.malwarelib.cleanup import curator_version
from imav.contracts.config import MalwareTune
from imav.malwarelib.cleanup import blacklist_hashes as hash_input
from imav.malwarelib.cleanup import RUSTCURATOR_BINARY, report_file
from imav.malwarelib.cleanup import sa_verdicts as sa_input
from imav.malwarelib.cleanup.curator_version import package_version
from imav.malwarelib.cleanup.leftovers import RunJournal
from imav.malwarelib.cleanup.reasons import (
    RUN_FAILURE,
    fatal_reason,
    parse_reason,
    run_patterns,
)
from imav.malwarelib.cleanup.run_as import identity as run_as_identity
from imav.malwarelib.engine import curator_path
from imav.malwarelib.model import MalwareHit
from imav.malwarelib.stop_process.report import _complete_owned
from imav.malwarelib.utils import unchanged_since_detection
from imav.malwarelib.utils.revisium import (
    DeletionType,
    ErrorType,
    RescanResultType,
    RevisiumCSVFile,
    RevisiumJsonFile,
    RevisiumTempFile,
)

logger = logging.getLogger(__name__)


def cleaner_result_instance(tempdir=None, mode=None):
    if MalwareTune.USE_JSON_REPORT:
        return RevisiumJsonFile(tempdir, mode)
    return RevisiumCSVFile(tempdir, mode)


def account_uid(user) -> Optional[int]:
    try:
        return pwd.getpwnam(user).pw_uid
    except (KeyError, TypeError, ValueError):
        return None


def flagged_uid(user) -> tuple:
    """Whether the run is under the report flag (see report_file) and, if
    so, the account's uid: with the flag off a run is today's."""
    if not report_file.enabled():
        return False, None
    return True, account_uid(user)


def usable_tempdir() -> str:
    """The temp dir both the agent and the privilege-dropped cleaner can use."""
    tempdir = Path(tempfile.gettempdir())
    # gettempdir() falls back to the cwd when TMPDIR, /tmp, /var/tmp and
    # /usr/tmp are all unusable, and the agent unit has no WorkingDirectory,
    # so that fallback is "/" — where nothing can be created.
    if tempdir == Path(os.sep) or not os.access(tempdir, os.W_OK | os.X_OK):
        raise PermissionError(
            errno.EACCES, "no usable temp directory", str(tempdir)
        )
    return str(tempdir)


class CleanerMemory:
    SAMPLE_INTERVAL = 2

    def __init__(self, pid):
        self._pid = pid
        self._peak_rss_kb = None
        self._bound = self._read(resource_limits.memory_bound_kb)
        self.sample()

    def _read(self, probe):
        try:
            return probe(self._pid)
        except OSError:
            return None

    def sample(self):
        peak = self._read(resource_limits.peak_rss_kb)
        if peak:
            self._peak_rss_kb = max(peak, self._peak_rss_kb or 0)

    async def watch(self):
        while True:
            await asyncio.sleep(self.SAMPLE_INTERVAL)
            self.sample()

    def note(self) -> Optional[str]:
        if not self._peak_rss_kb:
            return None
        peak_mb = self._peak_rss_kb // 1024
        if self._bound:
            bound_kb, source = self._bound
            return f"peak RSS {peak_mb} MB of {bound_kb // 1024} MB, {source}"
        # Cleanup is spawned outside run-with-intensity, so on CloudLinux the
        # ceiling that killed it is the user's LVE, which we cannot read here.
        lve = ""
        with suppress(OSError):
            if resource_limits.is_lve_active():
                lve = ", LVE active"
        return f"peak RSS {peak_mb} MB, no cgroup limit{lve}"


class MalwareCleanerLog(RevisiumTempFile):
    pass


class MalwareCleanerProgress(RevisiumJsonFile):
    """
    Get progress from external source
    """

    _progress = 0

    @recurring_check(2)
    async def watch(self, callback):
        try:
            data = self.read()
        except FileNotFoundError:
            raise RecurringCheckStop()
        except json.JSONDecodeError:
            return

        progress = data["current"]

        increment, self._progress = progress - self._progress, progress

        callback(increment)


class MalwareCleanupFileList(RevisiumTempFile):
    def write(self, filelist):
        with self.open_for_write() as w:
            w.writelines(base64_encode_filename(f) + b"\n" for f in filelist)


def _normalize_path(path: str) -> str:
    """Round-trip through fsencode/fsdecode to match FilenameField behavior.

    Ensures CleanupResult keys are consistent with MalwareHit.orig_file
    which goes through FilenameField's os.fsencode/os.fsdecode cycle.
    """
    return os.fsdecode(os.fsencode(path))


def _parse_int(value: Union[str, int]) -> int:
    """Convert str|int to int, in case errors return -2
    -1 used as default value when storing CH
    """
    try:
        return int(value)
    except (TypeError, ValueError):
        return -2


class CleanupResultEntry(dict):
    unchanged = False

    def __init__(self, data: Dict[str, Union[str, int]]):
        # fields:
        # d - cleanup result
        # e - error description
        # s - signature that was triggered for the file during scan
        # f - file path (it's unexpected that f is absent)
        # r - the result of aibolit rescan after cleanup
        #
        # We shouldn't fail on parsing one record (to do not stop processing
        #  report), so we consider default values for all fields.
        super().__init__(
            d=_parse_int(data.get("d", -1)),
            e=_parse_int(data.get("e", -1)),
            s=data["s"],
            f=data["f"],
            r=_parse_int(data.get("r", -1)),
            mtime_before=_parse_int(data.get("mb", -1)),
            mtime_after=_parse_int(data.get("ma", -1)),
            hash_before=data.get("hb", ""),
            hash_after=data.get("ha", ""),
        )

        self.update(parse_reason(data.get("reason_details", data)))

    def is_cleaned(self):
        if self.is_failed() or self.requires_myimunify_protection():
            return False

        if self["e"] == ErrorType.NOT_CLEANEDUP:
            return True

        return (
            self["e"] == ErrorType.NO_ERROR
            and self["d"] == DeletionType.INJECTION_REMOVED
        )

    def is_removed(self):
        return (
            not self.is_failed()
            and self["e"] == ErrorType.NO_ERROR
            and self["d"] > DeletionType.INJECTION_REMOVED
        )

    def is_failed(self):
        # a system-owner refusal carries a synthetic r=1 for older agents;
        # it is not a failed cleanup attempt and must not go to MRS
        return self.unchanged or (
            self["r"] == RescanResultType.DETECTED
            and self["e"] != ErrorType.SKIPPED_SYSTEM_OWNER
        )

    def requires_myimunify_protection(self):
        return self["r"] == RescanResultType.REQUIRED_ADVANCED_SIGNATURES

    def not_exist(self):
        return not self.is_failed() and self["e"] == ErrorType.FILE_NOT_EXISTS


class CleanupResult(Dict[str, CleanupResultEntry]):
    """
    Cleanup result container for result entries
    """

    def __init__(self, report=None):
        self.attempts = {}
        if not report:
            return
        named = [entry for entry in report if entry.get("f")]
        if len(named) != len(report):
            logger.warning(
                "Dropped %d cleanup report entries without a path out of %d",
                len(report) - len(named),
                len(report),
            )
        super().__init__(
            {
                _normalize_path(entry["f"]): CleanupResultEntry(entry)
                for entry in named
            }
        )

    @staticmethod
    def __key(hit: Union[str, MalwareHit]) -> str:
        return getattr(hit, "orig_file", hit)

    def __contains__(self, hit: Union[str, MalwareHit]):
        return super().__contains__(self.__key(hit))

    def __getitem__(self, hit: Union[str, MalwareHit]):
        return super().__getitem__(self.__key(hit))

    async def mark_unchanged(self, detected: Dict[str, Optional[str]]):
        """Fail the "not cleaned up" entries whose file still has the
        sha256 it was detected with: the curator found no way to clean it.

        *detected* maps a local path to the sha256 recorded at detection.
        """
        for path, detection_hash in detected.items():
            if path not in self or self[path]["e"] != ErrorType.NOT_CLEANEDUP:
                continue
            entry = self[path]
            before = entry.get("hash_before")
            if (
                isinstance(detection_hash, str)
                and re.fullmatch(r"[0-9a-f]{64}", detection_hash)
                and isinstance(before, str)
                and re.fullmatch(r"[0-9a-f]{64}", before)
                and before != detection_hash
            ):
                entry.setdefault(
                    "cleaner_reason",
                    entry.get("reason", "legacy_reason_unavailable"),
                )
                entry["reason_version"] = 1
                entry["reason"] = "file_changed_since_detection"
        not_cleaned_up = {
            path: sha256
            for path, sha256 in detected.items()
            if path in self
            and self[path]["e"] == ErrorType.NOT_CLEANEDUP
            and self[path].is_cleaned()
        }
        for path in await unchanged_since_detection(not_cleaned_up):
            logger.info(
                "Cleanup did not change %s, counting it as failed",
                path,
            )
            self[path].unchanged = True


class MalwareCleaner:
    PROCU_DB = MalwareSignatures.PROCU_DB

    def __init__(self, loop=None, sink=None, watch_progress=True):
        self._loop = loop if loop else asyncio.get_event_loop()
        self._proxy = MalwareCleanupProxy()
        self._sink = sink
        self._watch_progress = watch_progress

    def _cmd(
        self,
        filename,
        progress_path,
        result_path,
        log_path,
        soft,
        *,
        username,
        blacklist=None,
        use_csv=True,
        standard_only=True,
        run_as=None,
        cleaner_binary=None,
    ):
        if cleaner_binary is None:
            cleaner_binary = curator_path()
        if run_as is None:
            identity = ["--username=%s" % username]
        else:
            identity = ["--uid=%d" % run_as[0], "--gid=%d" % run_as[1]]
        cmd = [
            "/opt/ai-bolit/wrapper",
            cleaner_binary,
            "--deobfuscate",
            "--nobackup",
            "--forcibly_cleanup",
            "--rescan",
            "--list=%s" % filename,
            "--input-fn-b64-encoded",
            *identity,
            "--report-hashes",
        ]
        if (
            username == "root"
            and cleaner_binary == RUSTCURATOR_BINARY
            and feature_flags.is_enabled("clean_system_owned_site_files")
            and curator_version.supports_system_owned_cleanup(cleaner_binary)
        ):
            cmd.append("--clean-system-owned-site-files")
        if blacklist:
            cmd.append("--black-list=%s" % blacklist)
        cmd.extend(
            [
                "--log=%s" % log_path,
                "--progress=%s" % progress_path,
            ]
        )
        if (
            Malware.CLEANUP_DISABLE_CLOUDAV
            or not LicenseCLN.is_cloud_assisted_cleanup_allowed()
        ):
            cmd.append("--disable-cloudav")

        if use_csv:
            cmd.extend(["--csv_result=%s" % result_path])
        else:
            cmd.extend(["--result=%s" % result_path])

        if standard_only:
            cmd.extend(["--standard-only"])

        if os.path.exists(self.PROCU_DB):
            cmd.append("--avdb")
            cmd.append(self.PROCU_DB)
        if soft:
            cmd.append("--soft")

        return cmd

    CLEANUP_ERROR_PREFIX = "Cleanup failed."

    # Procu exit codes (must match constants in procu2_src.php)
    _EXIT_ERROR_GENERAL = 1
    _EXIT_ERROR_INPUT_NOT_FOUND = 2
    _EXIT_ERROR_INVALID_USERNAME = 255
    _SIGNAL_EXIT_CODE_OFFSET = 128

    @staticmethod
    def _get_cleaner_error_info(
        exc: Exception,
        cmd: List[str],
        returncode: int,
        stdout: Optional[bytes],
        stderr: Optional[bytes],
    ):
        return dict(
            exception=exc.__class__.__name__,
            return_code=returncode,
            command=cmd,
            out=stdout.decode(errors="replace") if stdout is not None else "",
            err=stderr.decode(errors="replace") if stderr is not None else "",
        )

    @classmethod
    def _categorize_process_error(
        cls,
        returncode: int,
        stderr: bytes,
        memory_note: Optional[str] = None,
    ) -> Optional[str]:
        """Categorize procu process failures into explicit error types.

        Returns a human-readable error string if the process failed,
        or None on success (exit code 0).
        """
        if returncode == 0:
            return None

        prefix = cls.CLEANUP_ERROR_PREFIX
        stderr_text = stderr.decode(errors="replace") if stderr else ""

        # asyncio reports -N for a signalled child; the wrapper reports 128+N.
        if returncode < 0:
            sig_num = -returncode
        else:
            sig_num = returncode - cls._SIGNAL_EXIT_CODE_OFFSET

        sig_name = None
        if sig_num > 0:
            with suppress(ValueError):
                sig_name = signal_module.Signals(sig_num).name

        if sig_name:
            # Only the signal categories carry the note: the other strings are
            # matched exactly by the cleanup dashboards.
            memory = f" [{memory_note}]" if memory_note else ""
            if sig_num == signal_module.SIGSEGV:
                return (
                    f"{prefix} Segmentation fault (signal {sig_name}){memory}"
                )
            if sig_num == signal_module.SIGKILL:
                return (
                    f"{prefix} Process killed (signal {sig_name},"
                    f" likely OOM){memory}"
                )
            if sig_num == signal_module.SIGTERM:
                return (
                    f"{prefix} Process terminated (signal {sig_name}){memory}"
                )
            return f"{prefix} Process killed by signal {sig_name}{memory}"

        if (
            "Allowed memory size of" in stderr_text
            and "bytes exhausted" in stderr_text
        ):
            return f"{prefix} Out of memory"
        if "Fatal error" in stderr_text:
            return f"{prefix} PHP fatal error (exit code {returncode})"
        if "Parse error" in stderr_text:
            return f"{prefix} PHP parse error (exit code {returncode})"
        if "error while loading shared libraries" in stderr_text:
            return f"{prefix} Shared library error (exit code {returncode})"

        if returncode == cls._EXIT_ERROR_GENERAL:
            return f"{prefix} General error (exit code 1)"
        if returncode == cls._EXIT_ERROR_INPUT_NOT_FOUND:
            return f"{prefix} Input file not found (exit code 2)"
        if (
            returncode == cls._EXIT_ERROR_INVALID_USERNAME
            and "Invalid username" in stderr_text
        ):
            return f"{prefix} Invalid username (exit code 255)"

        return f"{prefix} Process exited with code {returncode}"

    @classmethod
    def _read_report(cls, result) -> Tuple[Optional[list], Optional[str]]:
        """Returns (report, error); the error is None when the read worked."""
        try:
            return result.read(), None
        except FileNotFoundError:
            return None, f"{cls.CLEANUP_ERROR_PREFIX} Report file is missing"
        except Exception as exc:
            logger.warning(
                "Cleanup report %s is unreadable: %s: %s",
                result.filename,
                exc.__class__.__name__,
                exc,
            )
            error = (
                f"{cls.CLEANUP_ERROR_PREFIX} Report is unreadable"
                f" ({exc.__class__.__name__})"
            )
            return None, error

    async def _fail_before_run(
        self, exc: OSError
    ) -> Tuple[CleanupResult, Optional[str], List[str]]:
        error = (
            f"{self.CLEANUP_ERROR_PREFIX} No usable temp directory"
            f" ({exc.__class__.__name__})"
        )
        logger.error("%s: %s", error, exc)
        info = self._get_cleaner_error_info(
            exc, [], -1, stdout=b"", stderr=b""
        )
        await self._send_cleanup_failed_message({**info, "message": error})
        return CleanupResult(), error, []

    @asynccontextmanager
    async def _watch_memory(self, pid):
        memory = CleanerMemory(pid)
        watcher = self._loop.create_task(memory.watch())
        try:
            yield memory
        finally:
            watcher.cancel()
            with suppress(asyncio.CancelledError):
                await watcher

    async def _send_cleanup_failed_message(self, info: dict):
        if self._sink:
            try:
                msg = MessageType.CleanupFailed(
                    {**info, **{"timestamp": int(time.time())}}
                )
                await self._sink.process_message(msg)
            except asyncio.CancelledError:
                raise
            except Exception:
                logger.exception(
                    "Exception while sending CleanupFailed message"
                )

    async def _write_root_input(self, stack, journal, tempdir, kind, records):
        """A root-only 0600 input beside the lists, removed with the run."""

        def write():
            handle = stack.enter_context(kind(tempdir=tempdir, mode=0o600))
            if journal is not None:
                journal.note_file(tempdir, handle.filename)
            handle.write(records)
            return handle.filename

        # Keep the ExitStack alive until its worker finishes, including
        # when this task is cancelled during creation.
        filename, cancelled = await _complete_owned(
            self._loop.run_in_executor(None, write)
        )
        if cancelled:
            raise asyncio.CancelledError
        return filename

    async def start(
        self,
        user,
        filelist,
        soft=True,
        blacklist=None,
        standard_only=None,
        run_as=None,
        blacklist_hashes=None,
        sa_verdicts=None,
    ) -> Tuple[CleanupResult, Optional[str], List[str]]:
        """Clean *filelist* as *user*, or as the (uid, gid) of *run_as*.

        *run_as* is for an owner UID without a user name; only a rustcurator
        that takes --uid gets it, any other cleaner is not started. Every
        run checks all the gates again (the flag may have been turned off
        since *run_as* was chosen), so a stale *run_as* is never used."""
        standard_only = self.is_standard_only(user, standard_only)
        cleaner_binary = curator_path()
        if run_as is not None and (
            cleaner_binary != RUSTCURATOR_BINARY
            or await run_as_identity(run_as[0]) != tuple(run_as)
        ):
            error = (
                f"{self.CLEANUP_ERROR_PREFIX} The cleaner cannot run as"
                " the file owner UID"
            )
            logger.warning("%s: %s", error, cleaner_binary)
            return CleanupResult(), error, []

        try:
            tempdir = usable_tempdir()
        except OSError as exc:
            return await self._fail_before_run(exc)

        result_file = cleaner_result_instance(tempdir=tempdir)
        use_csv = isinstance(result_file, RevisiumCSVFile)

        async with AsyncExitStack() as stack:
            try:
                flist = stack.enter_context(
                    MalwareCleanupFileList(tempdir=tempdir, mode=0o644)
                )
                blk = stack.enter_context(
                    MalwareCleanupFileList(tempdir=tempdir, mode=0o644)
                )
                progress = stack.enter_context(
                    MalwareCleanerProgress(tempdir=tempdir)
                )
                result = stack.enter_context(result_file)
                log = stack.enter_context(MalwareCleanerLog(tempdir=tempdir))
                flist.write(filelist)
                if blacklist:
                    blk.write(blacklist)
            except OSError as exc:
                return await self._fail_before_run(exc)
            handed_over = [
                f.filename for f in (flist, blk, progress, result, log)
            ]
            flagged, uid = await self._loop.run_in_executor(
                None, flagged_uid, user
            )
            journal = None
            if flagged:
                # the run journal and its sweep are part of the flag
                journal = await self._see_through(
                    RunJournal, tempdir, handed_over
                )
                # unwound after the report release, before the files' own
                # exits; registered before the journal is written, which a
                # cancel may interrupt
                stack.push_async_callback(self._see_through, journal.close)
                await self._see_through(journal.__enter__)
            if self._watch_progress:
                self._loop.create_task(progress.watch(self._proxy.progress_cb))

            cmd = await self._loop.run_in_executor(
                None,
                partial(
                    self._cmd,
                    flist.filename,
                    progress.filename,
                    result.filename,
                    log.filename,
                    soft,
                    username=user,
                    blacklist=blk.filename if blacklist else None,
                    use_csv=use_csv,
                    standard_only=standard_only,
                    run_as=run_as,
                    cleaner_binary=cleaner_binary,
                ),
            )
            black_paths = set(blacklist or ())
            hashes = {
                path: digest
                for path, digest in (blacklist_hashes or {}).items()
                if path in black_paths
            }
            if blacklist_hashes and await self._loop.run_in_executor(
                None, hash_input.supported, cmd[1]
            ):
                try:
                    hashes_filename = await self._write_root_input(
                        stack,
                        journal,
                        tempdir,
                        hash_input.BlacklistHashes,
                        hashes,
                    )
                except OSError as exc:
                    return await self._fail_before_run(exc)
                cmd.append("--black-list-hashes=%s" % hashes_filename)
                handed_over.append(hashes_filename)
            listed = set(filelist)
            verdicts = {
                path: verdict
                for path, verdict in (sa_verdicts or {}).items()
                if path in listed
            }
            if verdicts and await self._loop.run_in_executor(
                None, sa_input.supported, cmd[1]
            ):
                try:
                    verdicts_filename = await self._write_root_input(
                        stack, journal, tempdir, sa_input.SaVerdicts, verdicts
                    )
                except OSError as exc:
                    return await self._fail_before_run(exc)
                cmd.append("%s=%s" % (sa_input.OPTION, verdicts_filename))
                handed_over.append(verdicts_filename)
            curator = (
                "rustcurator"
                if Path(cmd[1]).name == "rustcurator"
                else "procu2"
            )
            curator_version = await self._loop.run_in_executor(
                None, package_version, cmd[1]
            )
            if (
                flagged
                and curator == "rustcurator"
                and await self._see_through(
                    self._prepare_report,
                    stack,
                    journal,
                    result,
                    user,
                    uid,
                    cmd[1],
                    run_as,
                )
            ):
                cmd.append(report_file.OPEN_AS_ROOT)
            logger.debug("Executing %s", " ".join(cmd))

            out, err = b"", b""
            proc = None
            memory = None
            try:
                proc = await asyncio.subprocess.create_subprocess_exec(
                    *cmd,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                )
                if journal is not None:
                    await self._see_through(journal.note_cleaner, proc.pid)
                async with self._watch_memory(proc.pid) as memory:
                    out, err = await proc.communicate()
                if journal is not None:
                    journal.note_exit(proc.returncode)
            except asyncio.CancelledError:
                if proc:
                    with suppress(ProcessLookupError):
                        proc.terminate()
                    if journal is not None and proc.returncode is None:
                        # it may still write: sweep again once it is gone
                        journal.finish_after(self._loop, proc.wait())
                raise
            except Exception as exc:
                info = self._get_cleaner_error_info(
                    exc,
                    cmd,
                    proc.returncode if proc else 126,  # 126 - permission error
                    stdout=out,
                    stderr=err,
                )
                # Exit code in the template, so Sentry groups by it
                logger.error(
                    f"Cleanup failed exit_code={info['return_code']}: %s",
                    f"{info['out']} {info['err']}",
                    extra={**info, "exception": exc},
                )
                await self._send_cleanup_failed_message(
                    {**info, "message": str(exc)}
                )
                return (
                    CleanupResult(),
                    (
                        f"{self.CLEANUP_ERROR_PREFIX} Failed to run the"
                        f" cleaner ({exc.__class__.__name__})"
                    ),
                    cmd,
                )

            # Categorize before reading, or the exit code is lost to the read.
            error = self._categorize_process_error(
                proc.returncode, err, memory.note() if memory else None
            )
            failure = (
                fatal_reason(
                    err,
                    run_patterns(handed_over)
                    | ({self.PROCU_DB} if self.PROCU_DB in cmd else set()),
                )
                if error
                else {}
            )
            report, read_error = self._read_report(result)
            error = error or read_error
            if not error and not report:
                error = f"{self.CLEANUP_ERROR_PREFIX} Report is empty"

            if error:
                logger.error(
                    "%s (exit_code=%d, input_files=%d, blacklisted=%d,"
                    " stderr=%s)",
                    error,
                    proc.returncode,
                    len(filelist),
                    len(blacklist or ()),
                    err,
                )
                info = self._get_cleaner_error_info(
                    RuntimeError(error),
                    cmd,
                    proc.returncode,
                    stdout=out,
                    stderr=err,
                )
                await self._send_cleanup_failed_message(
                    {**info, "message": error}
                )
            elif len(report) < len(filelist):
                logger.warning(
                    "Partial cleanup report: %d entries for %d input files",
                    len(report),
                    len(filelist),
                )

            parsed = CleanupResult(report)
            metadata = {"curator": curator}
            if isinstance(curator_version, str) and curator_version:
                metadata["curator_version"] = curator_version
            attempt = dict(metadata)
            if failure:
                # the run failed as a whole: the reason is every file's
                attempt[RUN_FAILURE] = failure
            parsed.attempts = {
                _normalize_path(path): dict(attempt)
                for path in list(filelist) + list(blacklist or ())
            }
            for entry in parsed.values():
                entry.update(metadata)
            return parsed, error, cmd

    async def _see_through(self, func, *args):
        """Run *func* in the executor and wait for it even when cancelled:
        what it adds to the run's exit stack (the journal, the report made
        for the account) must be there before the stack unwinds, and what it
        removes must be gone before the run is over."""
        future = self._loop.run_in_executor(None, func, *args)
        cancelled = False
        while not future.done():
            try:
                await asyncio.shield(future)
            except asyncio.CancelledError:
                cancelled = True
            except Exception:
                break  # future.result() below raises it
        if cancelled:
            raise asyncio.CancelledError
        return future.result()

    def _prepare_report(
        self, stack, journal, result, user, uid, binary, run_as=None
    ) -> bool:
        """Under the flag: whether rustcurator *binary* is to open its
        report files as root; else, when the flag says so, the report file
        is made for the account (see report_file) and otherwise the cleaner
        creates it. A run as a UID without a user name (*run_as*) never
        gets a report made for it: there is no account to make it for."""
        if report_file.opens_reports_as_root(binary):
            return True
        if run_as is not None:
            return False
        if uid is None or not (
            report_file.any_account() or report_file.account_isolated(user)
        ):
            return False
        made = report_file.precreate(result.filename, uid)
        if made:
            journal.note_report(made)
            # released off the event loop, as the journal is closed
            stack.push_async_callback(
                self._see_through, report_file.release, result.filename
            )
        return False

    @staticmethod
    def is_standard_only(user: str, standard_only: bool) -> bool:
        """Check if only standard signatures should be applied for the user"""

        # FIXME: DEF-20763 Remove this line to enable standard signatures
        return False

        if not MyImunifyConfig.ENABLED:
            # Ignore standard_only value if MyImunify is disabled
            return False
        elif standard_only is None:
            # When cleaned by default action
            return not ms_clean_requires_myimunify_protection(user)

        return standard_only


class MalwareCleanupProxy(metaclass=Singleton):
    _CHUNK_SIZE = 10000
    """
    Class to interconnect Cleanup status endpoint and Cleanup plugin
    """

    def __init__(self):
        self.current = self.total = 0
        self.hits = defaultdict(set)

    def add(self, cause, initiator, post_action, scan_id, standard_only, hits):
        self.hits[
            (cause, initiator, post_action, scan_id, standard_only)
        ].update(hits)

    def flush(
        self,
    ) -> Generator[Tuple[str, str, Callable, str, Set], None, None]:
        while self.hits:
            scan_info, hits = self.hits.popitem()

            # the rows of one file stay in one batch: the cleaner gets the
            # file once and its result goes to all of them
            def path(hit):
                return str(getattr(hit, "orig_file", hit))

            ordered = sorted(hits, key=path)
            end = self._CHUNK_SIZE
            while end < len(ordered) and path(ordered[end]) == path(
                ordered[end - 1]
            ):
                end += 1
            hits = set(ordered[:end])
            if ordered[end:]:
                self.hits[scan_info].update(ordered[end:])

            self.total += len(hits)
            yield *scan_info, hits

    def progress_cb(self, increment=1):
        self.current += increment

    def reset(self):
        self.current = self.total = 0

    def get_progress(self):
        try:
            return int(self.current / (self.total + len(self.hits)) * 100)
        except ZeroDivisionError:
            return None
