From 6c33bb6e1c77a43d6fd93e59b961b17adc9a2bbf Mon Sep 17 00:00:00 2001 From: pictuga Date: Wed, 29 Jan 2014 20:36:45 +0100 Subject: [PATCH] Safer Cache saving Create tmp file and then move it to destination. Avoids corrupt files during write --- morss/morss.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/morss/morss.py b/morss/morss.py index 13f82ce..b02c7cb 100644 --- a/morss/morss.py +++ b/morss/morss.py @@ -138,10 +138,11 @@ class Cache: self._dir = folder self._dic = dic - maxsize = os.statvfs('./').f_namemax - len(self._dir) - 1 + maxsize = os.statvfs('./').f_namemax - len(self._dir) - 1 - 4 # ".tmp" self._hash = urllib.quote_plus(self._key)[:maxsize] self._file = self._dir + '/' + self._hash + self._file_tmp = self._file + '.tmp' self._cached = {} # what *was* cached self._cache = {} # new things to put in cache @@ -188,8 +189,13 @@ class Cache: out = json.dumps(self._cache, indent=4) - with open(self._file, 'w+') as file: - file.write(out) + try: + open(self._file_tmp, 'w+').write(out) + os.rename(self._file_tmp, self._file) + except IOError: + log('failed to write cache to tmp file') + except OSError: + log('failed to move cache to file') def isYoungerThan(self, sec): if not os.path.exists(self._file):