fix: protect transaction conflicts and correct editor navigation
This commit is contained in:
@@ -126,6 +126,8 @@ def publish_outputs(outputs: dict[Path, Path], expected: dict, backup: Path, can
|
||||
if destination.exists():
|
||||
copy = backup / f"previous-{index}"
|
||||
shutil.copyfile(destination, copy)
|
||||
if fingerprint(copy) != expected[destination]:
|
||||
raise OSError(f"导出期间目标文件发生变化,已停止替换:{destination}")
|
||||
originals[destination] = copy
|
||||
else:
|
||||
originals[destination] = None
|
||||
@@ -133,13 +135,20 @@ def publish_outputs(outputs: dict[Path, Path], expected: dict, backup: Path, can
|
||||
try:
|
||||
for destination, staged in outputs.items():
|
||||
check_cancelled(cancel)
|
||||
atomic_write(destination, staged.read_bytes())
|
||||
written.append(destination)
|
||||
data = staged.read_bytes()
|
||||
# Conversion and backup of other files can take time. Validate the
|
||||
# destination again immediately before publishing this particular file.
|
||||
if fingerprint(destination) != expected[destination]:
|
||||
raise OSError(f"导出期间目标文件发生变化,已停止替换:{destination}")
|
||||
atomic_write(destination, data)
|
||||
written.append((destination, hashlib.sha256(data).hexdigest()))
|
||||
check_cancelled(cancel)
|
||||
except Exception as error:
|
||||
failures = []
|
||||
for destination in reversed(written):
|
||||
for destination, published in reversed(written):
|
||||
try:
|
||||
if fingerprint(destination) != published:
|
||||
raise OSError(f"目标文件已被外部修改,保留当前内容:{destination}")
|
||||
if originals[destination] is None: destination.unlink(missing_ok=True)
|
||||
else: atomic_write(destination, originals[destination].read_bytes())
|
||||
except OSError as rollback:
|
||||
|
||||
Reference in New Issue
Block a user