Files
drobboxordinacebackup/indexer/scanner.py
T
2026-05-18 07:04:08 +02:00

35 lines
1.3 KiB
Python

import os
import unicodedata
from datetime import datetime
def scan_files(root_path: str) -> dict:
"""
Projde celý adresářový strom a vrátí dict všech souborů.
Nehasuje obsah — to se dělá až pro změněné soubory.
Returns:
{relative_path: {full_path, file_name, directory, size, mtime}}
"""
result = {}
for root, _, files in os.walk(root_path):
for name in files:
full_path = os.path.join(root, name)
try:
stat = os.stat(full_path)
except (FileNotFoundError, PermissionError):
continue
rel_path = unicodedata.normalize("NFC", os.path.relpath(full_path, root_path).replace("\\", "/"))
rel_dir = unicodedata.normalize("NFC", os.path.relpath(root, root_path).replace("\\", "/"))
# Truncate microseconds — MySQL DATETIME rounds to whole seconds,
# which causes false "modified" detections on every run.
mtime = datetime.fromtimestamp(stat.st_mtime).replace(microsecond=0)
result[rel_path] = {
"full_path": full_path,
"file_name": name,
"directory": rel_dir,
"size": stat.st_size,
"mtime": mtime,
}
return result