fix(mover): Stabilität mit frischen Attributen (open + fstat)

CIFS lieferte per stat() sekundenlang eine veraltete Größe einer noch
wachsenden Datei; erst open() erzwingt frische Attribute. Gesperrte
Dateien gelten als in Benutzung.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Florian Wilde
2026-10-10 21:50:06 +02:00
co-authored by Claude Opus 5.5
parent 48a52c5290
commit 8276ce2afb
3 changed files with 86 additions and 9 deletions
+15 -8
View File
@@ -13,6 +13,7 @@ Reihenfolge pro Lauf:
from __future__ import annotations
import dataclasses
import errno
import os
import stat
@@ -39,7 +40,7 @@ from netmover.mover.destinations import (
)
from netmover.mover.ledger import FailureInfo, Ledger
from netmover.mover.naming import collision_candidates
from netmover.mover.scan import Candidate, FileState, Matcher, current_state, scan
from netmover.mover.scan import BUSY, Candidate, FileState, Matcher, current_state, scan
from netmover.timeouts import Cancelled, CancelToken
log = get_logger(__name__)
@@ -222,26 +223,32 @@ class Engine:
else:
pending.append(c)
# Stabilität (Fallstrick 1): zweite Messung nach dem Stabilitätsfenster.
# Stabilität (Fallstrick 1): zwei frische Messungen (öffnen + fstat, nicht
# der Verzeichnis-Cache) im Abstand des Stabilitätsfensters.
first: dict[str, FileState | None] = {c.relpath: current_state(c.path) for c in pending}
if pending and rule.stability_seconds > 0:
self._sleep(rule.stability_seconds)
token.check()
stable: list[Candidate] = []
for c in pending:
st = current_state(c.path)
if st is None:
before, st = first[c.relpath], current_state(c.path)
if before is None or st is None:
continue # inzwischen verschwunden – nichts zu tun
if st != c.state:
if st != before or st == BUSY:
result.add(
ItemResult(
c.relpath,
ItemStatus.SKIPPED,
size=st.size,
message="wird noch geschrieben (Größe/Zeit geändert)",
size=max(st.size, 0),
message="wird noch geschrieben (Größe/Zeit geändert)"
if st != BUSY
else "Datei ist gerade in Benutzung",
)
)
continue
stable.append(c)
# ab hier gilt der frisch gemessene Zustand (für Prüfung vor dem Umbenennen
# und für die Kopier-Buchführung)
stable.append(dataclasses.replace(c, state=st))
failures = self.ledger.failures(rule.id)
for c in stable:
+22 -1
View File
@@ -10,6 +10,7 @@
from __future__ import annotations
import errno
import fnmatch
import os
import re
@@ -100,11 +101,31 @@ def scan(
yield from walk(root, "")
# Datei ist geöffnet, aber gerade nicht lesbar (z. B. exklusiv vom Schreiber
# gesperrt): gilt als „noch nicht fertig“ – gleicht nie einem echten Zustand.
BUSY = FileState(-1, -1)
def current_state(path: Path) -> FileState | None:
"""Aktuelle Größe und mtime – **frisch vom Server**.
``stat()`` beantworten CIFS- und NFS-Clients aus ihrem Attribut-Cache; im
Praxistest lieferte CIFS so sekundenlang eine veraltete Größe, während die
Datei noch wuchs. Erst ``open()`` erzwingt frische Attribute (NFS:
Close-to-open-Konsistenz, SMB: Antwort auf CREATE). Darum öffnen + ``fstat``.
"""
try:
st = os.stat(path, follow_symlinks=False)
fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
except FileNotFoundError:
return None
except OSError as exc:
if exc.errno in (errno.ELOOP, errno.ENOTDIR):
return None # Symlink oder kein Pfad mehr
return BUSY
try:
st = os.fstat(fd)
finally:
os.close(fd)
if not stat.S_ISREG(st.st_mode):
return None
return FileState(st.st_size, st.st_mtime_ns)