Mercurial > hgrepos > Python > apps > py-cutils
comparison shasum.py @ 66:c52e5f86b0ab
Handle EAGAIN and EWOULDBLOCK when reading files
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 26 Feb 2022 13:56:13 +0100 |
| parents | c9f9401abc0c |
| children | 19893b4f42a5 |
comparison
equal
deleted
inserted
replaced
| 65:a0cc15de8ae9 | 66:c52e5f86b0ab |
|---|---|
| 23 | 23 |
| 24 | 24 |
| 25 import argparse | 25 import argparse |
| 26 import base64 | 26 import base64 |
| 27 import binascii | 27 import binascii |
| 28 import errno | |
| 28 import hashlib | 29 import hashlib |
| 29 import io | 30 import io |
| 30 try: | 31 try: |
| 31 import mmap | 32 import mmap |
| 32 except ImportError: | 33 except ImportError: |
| 468 st = os.fstat(fd) | 469 st = os.fstat(fd) |
| 469 filesize = st[stat.ST_SIZE] | 470 filesize = st[stat.ST_SIZE] |
| 470 if mmap is None: | 471 if mmap is None: |
| 471 # No mmmap available -> use traditional low-level file IO | 472 # No mmmap available -> use traditional low-level file IO |
| 472 while True: | 473 while True: |
| 473 buf = os.read(fd, CHUNK_SIZE) | 474 try: |
| 474 if len(buf) == 0: | 475 buf = os.read(fd, CHUNK_SIZE) |
| 475 break | 476 except OSError as e: |
| 476 h.update(buf) | 477 if e.errno not in (errno.EAGAIN, errno.EWOULDBLOCK): |
| 478 raise | |
| 479 else: | |
| 480 if len(buf) == 0: | |
| 481 break | |
| 482 h.update(buf) | |
| 477 else: | 483 else: |
| 478 # | 484 # |
| 479 # Use mmap | 485 # Use mmap |
| 480 # | 486 # |
| 481 # NOTE: On Windows mmapped files with length 0 are not supported. | 487 # NOTE: On Windows mmapped files with length 0 are not supported. |
| 517 :rtype: bytes | 523 :rtype: bytes |
| 518 | 524 |
| 519 """ | 525 """ |
| 520 h = hashobj() | 526 h = hashobj() |
| 521 while True: | 527 while True: |
| 522 buf = instream.read(CHUNK_SIZE) | 528 try: |
| 523 if buf is not None: | 529 buf = instream.read(CHUNK_SIZE) |
| 524 if len(buf) == 0: | 530 except OSError as e: |
| 525 break | 531 if e.errno not in (errno.EAGAIN, errno.EWOULDBLOCK): |
| 526 h.update(buf) | 532 raise |
| 533 else: | |
| 534 if buf is not None: | |
| 535 if len(buf) == 0: | |
| 536 break | |
| 537 h.update(buf) | |
| 527 return h.digest() | 538 return h.digest() |
| 528 | 539 |
| 529 | 540 |
| 530 def normalize_filename(filename, strip_leading_dot_slash=False): | 541 def normalize_filename(filename, strip_leading_dot_slash=False): |
| 531 filename = filename.replace("\\", "/") | 542 filename = filename.replace("\\", "/") |
