Explorar el Código

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 <eclipse7@gmx.net>
Heikki Hokkanen hace 13 años
padre
commit
19e687b96e
Se han modificado 1 ficheros con 7 adiciones y 1 borrados
  1. 7
    1
      gitstats

+ 7
- 1
gitstats Ver fichero

@@ -241,11 +241,17 @@ class DataCollector:
241 241
 	# Save cacheable data
242 242
 	def saveCache(self, cachefile):
243 243
 		print 'Saving cache...'
244
-		f = open(cachefile, 'wb')
244
+		tempfile = cachefile + '.tmp'
245
+		f = open(tempfile, 'wb')
245 246
 		#pickle.dump(self.cache, f)
246 247
 		data = zlib.compress(pickle.dumps(self.cache))
247 248
 		f.write(data)
248 249
 		f.close()
250
+		try:
251
+			os.remove(cachefile)
252
+		except OSError:
253
+			pass
254
+		os.rename(tempfile, cachefile)
249 255
 
250 256
 class GitDataCollector(DataCollector):
251 257
 	def collect(self, dir):