From 15a20095063459b391084bc24532b50608f2caeb Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 7 Oct 2009 08:31:54 -0600 Subject: [PATCH] Add a little disaster recovery --- ctf/points.py | 14 +++++++++++++- ctf/pointsd.py | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ctf/points.py b/ctf/points.py index 3879a02..5099550 100755 --- a/ctf/points.py +++ b/ctf/points.py @@ -90,7 +90,7 @@ def incdict(dict, key, amt=1): dict[key] = dict.get(key, 0) + amt class Storage: - def __init__(self, fn=None): + def __init__(self, fn=None, fix=False): if not fn: fn = config.datafile('scores.dat') self.teams = set() @@ -100,6 +100,8 @@ class Storage: self.f = io.BytesIO() # Read stored scores + truncate = False + lastgood = 0 try: f = open(fn, 'rb') while True: @@ -113,7 +115,13 @@ class Storage: team = b[catlen:].decode('utf-8') req = (when, cat, team, score) self.add(req, False) + lastgood = f.tell() f.close() + except struct.error: + if fix: + truncate = True + else: + raise except IOError: pass @@ -122,6 +130,10 @@ class Storage: except IOError: self.f = None + if truncate: + self.f.seek(lastgood) + self.f.truncate() + def __len__(self): return len(self.log) diff --git a/ctf/pointsd.py b/ctf/pointsd.py index 85328e7..f557ab5 100755 --- a/ctf/pointsd.py +++ b/ctf/pointsd.py @@ -14,7 +14,7 @@ class MyHandler(asyncore.dispatcher): asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_DGRAM) self.bind(('', port)) - self.store = points.Storage() + self.store = points.Storage(fix=True) self.acked = set() self.outq = []