-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
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)
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))))
-import sys, os, pwd, subprocess, errno, socket, select
+import sys, os, pwd, subprocess, errno, socket, select, mmap
def log(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)
-import os, stat, time, struct, tempfile, mmap
+import os, stat, time, struct, tempfile
from helpers import *
EMPTY_SHA = '\0'*20
% (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):