diff options
| author | Managilvosh <152973789+Managilvosh@users.noreply.github.com> | 2026-11-24 14:03:59 +0530 |
|---|---|---|
| committer | Managilvosh <152973789+Managilvosh@users.noreply.github.com> | 2026-11-24 14:03:59 +0530 |
| commit | f8650dc5aa9fb45ca7d360cdc6f22aecfbf7fdfd (patch) | |
| tree | 4cacd6cd565e9bc2427a9ce1d7068b22b429e7d1 /tools/calibrated_texture.py | |
| parent | 1c93c8697078c3ccbd6f374caf5bb3ab0b6bc8e4 (diff) | |
calibrated_texture: atomic no-clobber export with MoveFileExW and cleanup (CALIBRATED-TEXTURE-NO-CLOBBER)
Diffstat (limited to 'tools/calibrated_texture.py')
| -rw-r--r-- | tools/calibrated_texture.py | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/tools/calibrated_texture.py b/tools/calibrated_texture.py index 1168fae03..7845e5d53 100644 --- a/tools/calibrated_texture.py +++ b/tools/calibrated_texture.py | |||
| @@ -8,6 +8,7 @@ import os | |||
| 8 | from pathlib import Path | 8 | from pathlib import Path |
| 9 | import sys | 9 | import sys |
| 10 | import tempfile | 10 | import tempfile |
| 11 | import time | ||
| 11 | import zlib | 12 | import zlib |
| 12 | 13 | ||
| 13 | # `python -I` implies `-P`, so the script's directory is not on sys.path; the sibling modules are next to it. | 14 | # `python -I` implies `-P`, so the script's directory is not on sys.path; the sibling modules are next to it. |
| @@ -16,6 +17,7 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |||
| 16 | import numpy as np | 17 | import numpy as np |
| 17 | import bcpack | 18 | import bcpack |
| 18 | import bcpack_tables | 19 | import bcpack_tables |
| 20 | import replace_retry | ||
| 19 | import ttpf_mips as ttpf | 21 | import ttpf_mips as ttpf |
| 20 | 22 | ||
| 21 | 23 | ||
| @@ -420,6 +422,34 @@ def local_bc3(symbols, profile): | |||
| 420 | return np.column_stack((low, high)).astype("<u8").tobytes() | 422 | return np.column_stack((low, high)).astype("<u8").tobytes() |
| 421 | 423 | ||
| 422 | 424 | ||
| 425 | def _rename_no_clobber(src, dst, *, seconds=None): | ||
| 426 | """Atomically move src to dst on Windows without overwriting, retrying transient sharing locks.""" | ||
| 427 | deadline = time.monotonic() + (replace_retry.RETRY_SECONDS if seconds is None else seconds) | ||
| 428 | delay = replace_retry.FIRST_DELAY | ||
| 429 | while True: | ||
| 430 | try: | ||
| 431 | return os.rename(src, dst) | ||
| 432 | except PermissionError as e: | ||
| 433 | if not replace_retry._transient(e) or time.monotonic() >= deadline: | ||
| 434 | raise | ||
| 435 | time.sleep(min(delay, max(0.0, deadline - time.monotonic()))) | ||
| 436 | delay = min(delay * 2, replace_retry.MAX_DELAY) | ||
| 437 | |||
| 438 | |||
| 439 | def _move_no_clobber(src, dst, *, is_nt=None): | ||
| 440 | """Move src to dst without overwriting dst, raising FileExistsError if dst exists. | ||
| 441 | |||
| 442 | On Windows, os.rename wraps MoveFileExW without MOVEFILE_REPLACE_EXISTING, which is an atomic | ||
| 443 | kernel operation that raises FileExistsError ([WinError 183]) if dst exists. | ||
| 444 | On POSIX, os.link atomically creates a hard link and raises FileExistsError (EEXIST) if dst exists. | ||
| 445 | If os.link fails for reasons other than destination existence, fail safely by re-raising the | ||
| 446 | original exception without non-atomic fallbacks, strictly preventing TOCTOU clobbering. | ||
| 447 | """ | ||
| 448 | if os.name == "nt" if is_nt is None else is_nt: | ||
| 449 | return _rename_no_clobber(src, dst) | ||
| 450 | return os.link(src, dst) | ||
| 451 | |||
| 452 | |||
| 423 | def export_file(pool, index, target, mips, force, profile): | 453 | def export_file(pool, index, target, mips, force, profile): |
| 424 | target = Path(target) | 454 | target = Path(target) |
| 425 | with open(profile, encoding="utf-8") as source: | 455 | with open(profile, encoding="utf-8") as source: |
| @@ -431,13 +461,12 @@ def export_file(pool, index, target, mips, force, profile): | |||
| 431 | with tempfile.NamedTemporaryFile(dir=target.parent, delete=False) as f: | 461 | with tempfile.NamedTemporaryFile(dir=target.parent, delete=False) as f: |
| 432 | tmp = Path(f.name) | 462 | tmp = Path(f.name) |
| 433 | try: | 463 | try: |
| 434 | f.write(data) | 464 | with f: |
| 435 | f.close() | 465 | f.write(data) |
| 436 | if force: | 466 | if force: |
| 437 | os.replace(tmp, target) | 467 | replace_retry.replace(tmp, target) |
| 438 | else: | 468 | else: |
| 439 | os.link(tmp, target) | 469 | _move_no_clobber(tmp, target) |
| 440 | finally: | 470 | finally: |
| 441 | f.close() | ||
| 442 | tmp.unlink(missing_ok=True) | 471 | tmp.unlink(missing_ok=True) |
| 443 | return 0 | 472 | return 0 |