From 19e687b96e0d1b98841368293b4d0e31e0408afb Mon Sep 17 00:00:00 2001 From: Heikki Hokkanen Date: Mon, 17 Dec 2012 21:44:20 +0200 Subject: [PATCH] Fix cachefile corruption when out of disk space. Avoid corrupt cache by writing it to a temporary file first, and then overwriting the original one. Should also fix other exceptional cases. Thanks-to: Alexander Strasser --- gitstats | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/gitstats b/gitstats index eac9d02..b2d3909 100755 --- a/gitstats +++ b/gitstats @@ -241,11 +241,17 @@ class DataCollector: # Save cacheable data def saveCache(self, cachefile): print 'Saving cache...' - f = open(cachefile, 'wb') + tempfile = cachefile + '.tmp' + f = open(tempfile, 'wb') #pickle.dump(self.cache, f) data = zlib.compress(pickle.dumps(self.cache)) f.write(data) f.close() + try: + os.remove(cachefile) + except OSError: + pass + os.rename(tempfile, cachefile) class GitDataCollector(DataCollector): def collect(self, dir): -- 2.11.4.GIT