summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathieu Pasquet <mathieui@mathieui.net>2013-12-04 01:14:28 +0100
committerMathieu Pasquet <mathieui@mathieui.net>2013-12-04 01:14:28 +0100
commitc0e010e2cdb0419f2a44d822d13219469032e38b (patch)
tree9e45b02ea08f4f8fc12c1649506e1fb7b3d394ea
parent4a7e18cd03e18b825292f942d4a3c051a3e11615 (diff)
downloadpoezio-c0e010e2cdb0419f2a44d822d13219469032e38b.tar.gz
poezio-c0e010e2cdb0419f2a44d822d13219469032e38b.tar.bz2
poezio-c0e010e2cdb0419f2a44d822d13219469032e38b.tar.xz
poezio-c0e010e2cdb0419f2a44d822d13219469032e38b.zip
Write the config to a tmp file before a final copy
(should prevent some conditions leading to config corruption happenning when poezio cannot write anymore)
-rw-r--r--src/config.py23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/config.py b/src/config.py
index 5eed4c09..5f07cc80 100644
--- a/src/config.py
+++ b/src/config.py
@@ -14,12 +14,13 @@ DEFSECTION = "Poezio"
from gettext import gettext as _
import sys
+import tempfile
+import os
import logging
-log = logging.getLogger(__name__)
from configparser import RawConfigParser, NoOptionError, NoSectionError
-from os import environ, makedirs, path
+from os import environ, makedirs, path, remove
from shutil import copy2
from args import parse_args
@@ -178,12 +179,22 @@ class Config(RawConfigParser):
elif not written:
result_lines.append('%s = %s' % (option, value))
try:
- df = open(self.file_name, 'w', encoding='utf-8')
+ prefix, file = path.split(self.file_name)
+ filename = path.join(prefix, '.%s.tmp' % file)
+ fd = os.fdopen(
+ os.open(
+ filename,
+ os.O_WRONLY | os.O_CREAT,
+ 0o600),
+ 'w')
for line in result_lines:
- df.write('%s\n' % line)
- df.close()
+ fd.write('%s\n' % line)
+ fd.close()
+ copy2(filename, self.file_name)
+ remove(filename)
except:
success = False
+ log.error('Unable to save the config file.', exc_info=True)
else:
success = True
return success
@@ -333,3 +344,5 @@ else:
# it needs to be after logger configuration
from common import safeJID
+log = logging.getLogger(__name__)
+