Open a verified regular file and return an owned binary read descriptor.
(scan_dir: Path, relative_path: str, context: str)
| 432 | |
| 433 | |
| 434 | def open_read_fd(scan_dir: Path, relative_path: str, context: str) -> int: |
| 435 | """Open a verified regular file and return an owned binary read descriptor.""" |
| 436 | |
| 437 | try: |
| 438 | with _locked_parent(scan_dir, relative_path, create=False) as (parent_path, leaf_name): |
| 439 | path = parent_path / leaf_name |
| 440 | handle = _create_file( |
| 441 | path, |
| 442 | access=_GENERIC_READ | _FILE_READ_ATTRIBUTES, |
| 443 | # Denying write/delete sharing keeps the opened contents stable. |
| 444 | share=_FILE_SHARE_READ, |
| 445 | disposition=_OPEN_EXISTING, |
| 446 | flags=_FILE_FLAG_OPEN_REPARSE_POINT, |
| 447 | ) |
| 448 | assert handle is not None and handle.value is not None |
| 449 | try: |
| 450 | _verify_regular_file(handle.value, path) |
| 451 | raw_handle = handle.detach() |
| 452 | try: |
| 453 | assert _msvcrt is not None |
| 454 | return _msvcrt.open_osfhandle(raw_handle, os.O_RDONLY | os.O_BINARY) |
| 455 | except BaseException: |
| 456 | _close_handle(raw_handle) |
| 457 | raise |
| 458 | finally: |
| 459 | handle.close() |
| 460 | except WindowsScanLocalFileError as exc: |
| 461 | raise WindowsScanLocalFileError( |
| 462 | exc.errno, |
| 463 | f"{context}: {exc.strerror}", |
| 464 | exc.filename, |
| 465 | ) from exc |
| 466 | |
| 467 | |
| 468 | def _write_all(handle: int, payload: bytes) -> None: |
nothing calls this directly
no test coverage detected