]> arthur.barton.de Git - bup.git/commitdiff
mmap: Make closing source file optional
authorBrandon Low <lostlogic@lostlogicx.com>
Mon, 7 Feb 2011 06:06:05 +0000 (22:06 -0800)
committerAvery Pennarun <apenwarr@gmail.com>
Mon, 7 Feb 2011 09:31:49 +0000 (01:31 -0800)
New index file formats require this behavior (bloom, midx3, etc.)

Signed-off-by: Brandon Low <lostlogic@lostlogicx.com>
lib/bup/helpers.py

index 931d43d63ea40405294176368f97b4dc5bf2e0e1..7b5eeadafaa78e051083d9119ab687e6882d0f65 100644 (file)
@@ -450,7 +450,7 @@ def slashappend(s):
         return s
 
 
-def _mmap_do(f, sz, flags, prot):
+def _mmap_do(f, sz, flags, prot, close):
     if not sz:
         st = os.fstat(f.fileno())
         sz = st.st_size
@@ -460,24 +460,26 @@ def _mmap_do(f, sz, flags, prot):
         # no elements :)
         return ''
     map = mmap.mmap(f.fileno(), sz, flags, prot)
-    f.close()  # map will persist beyond file close
+    if close:
+        f.close()  # map will persist beyond file close
     return map
 
 
-def mmap_read(f, sz = 0):
+def mmap_read(f, sz = 0, close=True):
     """Create a read-only memory mapped region on file 'f'.
 
     If sz is 0, the region will cover the entire file.
     """
-    return _mmap_do(f, sz, mmap.MAP_PRIVATE, mmap.PROT_READ)
+    return _mmap_do(f, sz, mmap.MAP_PRIVATE, mmap.PROT_READ, close)
 
 
-def mmap_readwrite(f, sz = 0):
+def mmap_readwrite(f, sz = 0, close=True):
     """Create a read-write memory mapped region on file 'f'.
 
     If sz is 0, the region will cover the entire file.
     """
-    return _mmap_do(f, sz, mmap.MAP_SHARED, mmap.PROT_READ|mmap.PROT_WRITE)
+    return _mmap_do(f, sz, mmap.MAP_SHARED, mmap.PROT_READ|mmap.PROT_WRITE,
+                    close)
 
 
 def parse_num(s):