fix(indexer): resolve Maildir keys by scanning filesystem directly
All checks were successful
perso/mcp-maildir/pipeline/head This commit looks good

This commit is contained in:
Julien Cabillot
2026-03-22 16:35:07 -04:00
parent 88694264de
commit 1b1ba0ef1a

View File

@@ -214,19 +214,24 @@ def get_recent_keys(mbox: mailbox.Maildir, days: int) -> set:
recent = set()
maildir_root = mbox._path # type: ignore[attr-defined]
for key in mbox.keys():
# Maildir keys are stored under cur/ or new/ — try both
for subdir in ("cur", "new"):
file_path = os.path.join(maildir_root, subdir, key)
for subdir in ("cur", "new"):
subdir_path = os.path.join(maildir_root, subdir)
try:
filenames = os.listdir(subdir_path)
except OSError:
continue
for filename in filenames:
file_path = os.path.join(subdir_path, filename)
try:
# Single stat() call instead of isfile() + getmtime()
if os.stat(file_path).st_mtime >= cutoff_ts:
# Maildir keys are the filename, but the actual file on disk
# often has a suffix (like ":2,S"). mailbox.Maildir treats
# the part BEFORE the first colon as the key.
key = filename.split(":")[0] if ":" in filename else filename
recent.add(key)
except FileNotFoundError:
continue # try next subdir
except OSError:
pass
break # found the file (stat succeeded or non-ENOENT error), stop trying subdirs
continue
return recent