changeset 34295:3bb2a9f25fe9

util: add an mmapread method This is useful for large files that are only partly touched. Test Plan: Will be used and tested in a later patch. Differential Revision: https://phab.mercurial-scm.org/D476
author Mark Thomas <mbthomas@fb.com>
date Thu, 21 Sep 2017 05:54:34 -0700
parents 05131c963767
children 3c9691728237
files mercurial/util.py
diffstat 1 files changed, 12 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Wed Sep 20 09:35:45 2017 -0700
+++ b/mercurial/util.py	Thu Sep 21 05:54:34 2017 -0700
@@ -26,6 +26,7 @@
 import gc
 import hashlib
 import imp
+import mmap
 import os
 import platform as pyplatform
 import re as remod
@@ -407,6 +408,17 @@
             self._lenbuf += len(data)
             self._buffer.append(data)
 
+def mmapread(fp):
+    try:
+        fd = getattr(fp, 'fileno', lambda: fp)()
+        return mmap.mmap(fd, 0, access=mmap.ACCESS_READ)
+    except ValueError:
+        # Empty files cannot be mmapped, but mmapread should still work.  Check
+        # if the file is empty, and if so, return an empty buffer.
+        if os.fstat(fd).st_size == 0:
+            return ''
+        raise
+
 def popen2(cmd, env=None, newlines=False):
     # Setting bufsize to -1 lets the system decide the buffer size.
     # The default for bufsize is 0, meaning unbuffered. This leads to