"""Geteilte Helfer für Scanner-Module."""
from __future__ import annotations

import os
import time
from pathlib import Path


def dir_size(path: Path) -> int:
    """Rekursive Grösse in Bytes. Symlinks werden nicht gefolgt."""
    if path.is_symlink():
        return 0
    if path.is_file():
        try:
            return path.stat(follow_symlinks=False).st_size
        except OSError:
            return 0
    if not path.is_dir():
        return 0
    total = 0
    for root, _dirs, files in os.walk(path, followlinks=False, onerror=lambda _e: None):
        for f in files:
            try:
                total += os.stat(os.path.join(root, f), follow_symlinks=False).st_size
            except OSError:
                continue
    return total


def last_access_days(path: Path) -> int | None:
    """Tage seit letzter Modifikation (mtime). None wenn nicht lesbar."""
    try:
        mtime = path.stat().st_mtime
        return int((time.time() - mtime) / 86400)
    except OSError:
        return None
