comparison shasum.py @ 67:19893b4f42a5

Flag to disable the use of mmap
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 26 Feb 2022 14:08:24 +0100
parents c52e5f86b0ab
children 4c2da9c74d7c
comparison
equal deleted inserted replaced
66:c52e5f86b0ab 67:19893b4f42a5
451 '*' if binary else ' ', 451 '*' if binary else ' ',
452 '-' if filename is None else normalize_filename(filename)), 452 '-' if filename is None else normalize_filename(filename)),
453 file=dest) 453 file=dest)
454 454
455 455
456 def compute_digest_file(hashobj, filename): 456 def compute_digest_file(hashobj, filename, use_mmap=True):
457 """ 457 """
458 :param hashobj: a :mod:`hashlib` compatible hash algorithm type or factory 458 :param hashobj: a :mod:`hashlib` compatible hash algorithm type or factory
459 :param str filename: filename within the filesystem 459 :param str filename: filename within the filesystem
460 :param bool use_mmap: use the :mod:`mmap` module if available
460 :return: the digest in binary form 461 :return: the digest in binary form
461 :rtype: bytes 462 :rtype: bytes
462 463
463 """ 464 """
464 h = hashobj() 465 h = hashobj()
466 | getattr(os, "O_SEQUENTIAL", 0) | getattr(os, "O_NOCTTY", 0) 467 | getattr(os, "O_SEQUENTIAL", 0) | getattr(os, "O_NOCTTY", 0)
467 fd = os.open(filename, flags) 468 fd = os.open(filename, flags)
468 try: 469 try:
469 st = os.fstat(fd) 470 st = os.fstat(fd)
470 filesize = st[stat.ST_SIZE] 471 filesize = st[stat.ST_SIZE]
471 if mmap is None: 472 if mmap is None or not use_mmap:
472 # No mmmap available -> use traditional low-level file IO 473 # No mmmap available -> use traditional low-level file IO
473 while True: 474 while True:
474 try: 475 try:
475 buf = os.read(fd, CHUNK_SIZE) 476 buf = os.read(fd, CHUNK_SIZE)
476 except OSError as e: 477 except OSError as e: