From: Avery Pennarun Date: Sat, 9 Jan 2010 22:12:36 +0000 (-0500) Subject: cmd-index: indexfiles should start with a well-known header. X-Git-Tag: bup-0.04~1^2~10 X-Git-Url: https://arthur.barton.de/gitweb/?a=commitdiff_plain;h=9e02c1f7d2be9dc2cd1b2567f65b261aa05209fa;p=bup.git cmd-index: indexfiles should start with a well-known header. --- diff --git a/cmd-index.py b/cmd-index.py index 9aa2757..3846d63 100755 --- a/cmd-index.py +++ b/cmd-index.py @@ -3,6 +3,7 @@ import sys, re, errno, stat, tempfile, struct, mmap import options from helpers import * +INDEX_HDR = 'BUPI\0\0\0\1' INDEX_SIG = '!IIIIIQ20sH' ENTLEN = struct.calcsize(INDEX_SIG) @@ -10,6 +11,10 @@ IX_EXISTS = 0x8000 IX_HASHVALID = 0x4000 +class IndexError(Exception): + pass + + class OsFile: def __init__(self, path): self.fd = None @@ -82,15 +87,20 @@ class IndexReader: else: raise if f: + b = f.read(len(INDEX_HDR)) + if b != INDEX_HDR: + raise IndexError('%s: header: expected %r, got %r' + % (filename, INDEX_HDR, b)) st = os.fstat(f.fileno()) - if f and 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.writable = True + 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.writable = True def __iter__(self): - ofs = 0 + ofs = len(INDEX_HDR) while ofs < len(self.m): eon = self.m.find('\0', ofs) assert(eon >= 0) @@ -118,6 +128,7 @@ class IndexWriter: (dir,name) = os.path.split(filename) (ffd,self.tmpname) = tempfile.mkstemp('.tmp', filename, dir) self.f = os.fdopen(ffd, 'wb', 65536) + self.f.write(INDEX_HDR) def __del__(self): self.abort()