]> arthur.barton.de Git - bup.git/commitdiff
Wrap mmap calls to help with portability.
authorAvery Pennarun <apenwarr@gmail.com>
Sun, 24 Jan 2010 22:18:25 +0000 (17:18 -0500)
committerAvery Pennarun <apenwarr@gmail.com>
Sun, 24 Jan 2010 22:59:28 +0000 (17:59 -0500)
python2.4 in 'fink' on MacOS X seems to not like it when you pass a file
length of 0, even though that's supposed to mean "determine map size
automatically."

git.py
helpers.py
index.py

diff --git a/git.py b/git.py
index 4941e358fcc0d2bb90ff76d683d7ae1cf0384900..684bd5e6b645290ff01a7be8d1401da5d1ee0670 100644 (file)
--- a/git.py
+++ b/git.py
@@ -1,4 +1,4 @@
-import os, errno, zlib, time, sha, subprocess, struct, mmap, stat, re
+import os, errno, zlib, time, sha, subprocess, struct, stat, re
 from helpers import *
 
 verbose = 0
@@ -90,10 +90,7 @@ class PackBitmap:
         if not os.path.exists(self.mapname):
             self.gen_map()
         self.num = 1 << (MAP_BITS-3)
-        f = open(self.mapname)
-        self.map = mmap.mmap(f.fileno(), self.num,
-                             mmap.MAP_PRIVATE, mmap.PROT_READ)
-        f.close()
+        self.map = mmap_read(open(self.mapname), self.num)
 
     def gen_map(self):
         (dir,fn) = os.path.split(self.idxname)
@@ -128,10 +125,7 @@ class PackBitmap:
 class PackIndex:
     def __init__(self, filename):
         self.name = filename
-        f = open(filename)
-        self.map = mmap.mmap(f.fileno(), 0,
-                             mmap.MAP_SHARED, mmap.PROT_READ)
-        f.close()  # map will persist beyond file close
+        self.map = mmap_read(open(filename))
         assert(str(self.map[0:8]) == '\377tOc\0\0\0\2')
         self.fanout = list(struct.unpack('!256I',
                                          str(buffer(self.map, 8, 256*4))))
index b5b1d7dc8291b09f266d60772ab7ffc95c99f692..e6f0772f57749bfdca5553eb66597fd85e8a48d9 100644 (file)
@@ -1,4 +1,4 @@
-import sys, os, pwd, subprocess, errno, socket, select
+import sys, os, pwd, subprocess, errno, socket, select, mmap
 
 
 def log(s):
@@ -138,3 +138,19 @@ def slashappend(s):
     else:
         return s
 
+
+def _mmap_do(f, len, flags, prot):
+    if not len:
+        st = os.fstat(f.fileno())
+        len = st.st_size
+    map = mmap.mmap(f.fileno(), len, flags, prot)
+    f.close()  # map will persist beyond file close
+    return map
+
+
+def mmap_read(f, len = 0):
+    return _mmap_do(f, len, mmap.MAP_PRIVATE, mmap.PROT_READ)
+
+
+def mmap_readwrite(f, len = 0):
+    return _mmap_do(f, len, mmap.MAP_SHARED, mmap.PROT_READ|mmap.PROT_WRITE)
index 93841ed5b6a47bc73384241b3f3ac6a13f25b956..0c5f3a0ca567010d38997b7324de9d03713b866c 100644 (file)
--- a/index.py
+++ b/index.py
@@ -1,4 +1,4 @@
-import os, stat, time, struct, tempfile, mmap
+import os, stat, time, struct, tempfile
 from helpers import *
 
 EMPTY_SHA = '\0'*20
@@ -87,10 +87,7 @@ class Reader:
                                  % (filename, INDEX_HDR, b))
             st = os.fstat(f.fileno())
             if st.st_size:
-                self.m = mmap.mmap(f.fileno(), 0,
-                                   mmap.MAP_SHARED,
-                                   mmap.PROT_READ|mmap.PROT_WRITE)
-                f.close()  # map will persist beyond file close
+                self.m = mmap_readwrite(f)
                 self.writable = True
 
     def __del__(self):