aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEwan Mellor <ewan@xensource.com>2006-12-15 17:19:00 +0000
committerEwan Mellor <ewan@xensource.com>2006-12-15 17:19:00 +0000
commitcd48fe7de04f4b661d8ef3dd81f9de3b37575ebb (patch)
treeadef90ec786f9856f7975dd507d6263ee1991f32
parent539d271eff540d736d83aa66659518b872bd96c5 (diff)
downloadxen-cd48fe7de04f4b661d8ef3dd81f9de3b37575ebb.tar.gz
xen-cd48fe7de04f4b661d8ef3dd81f9de3b37575ebb.tar.bz2
xen-cd48fe7de04f4b661d8ef3dd81f9de3b37575ebb.zip
Write the new version of the persisted config to a tempfile and then rename it,
to avoid corrupting the file on failure. Signed-off-by: Ewan Mellor <ewan@xensource.com>
-rw-r--r--tools/python/xen/xend/XendDomain.py19
1 files changed, 12 insertions, 7 deletions
diff --git a/tools/python/xen/xend/XendDomain.py b/tools/python/xen/xend/XendDomain.py
index 4649525198..b0a8de6a0a 100644
--- a/tools/python/xen/xend/XendDomain.py
+++ b/tools/python/xen/xend/XendDomain.py
@@ -26,6 +26,7 @@ import os
import stat
import shutil
import socket
+import tempfile
import threading
import xen.lowlevel.xc
@@ -280,16 +281,20 @@ class XendDomain:
make_or_raise(domain_config_dir)
try:
- sxp_cache_file = open(self._managed_config_path(dom_uuid),'w')
- prettyprint(dominfo.sxpr(), sxp_cache_file, width = 78)
- sxp_cache_file.close()
+ fd, fn = tempfile.mkstemp()
+ f = os.fdopen(fd, 'w+b')
+ try:
+ prettyprint(dominfo.sxpr(), f, width = 78)
+ finally:
+ f.close()
+ try:
+ os.rename(fn, self._managed_config_path(dom_uuid))
+ except:
+ log.exception("Renaming %s" % fn)
+ os.remove(fn)
except:
log.exception("Error occurred saving configuration file " +
"to %s" % domain_config_dir)
- try:
- self._managed_domain_remove(dom_uuid)
- except:
- pass
raise XendError("Failed to save configuration file to: %s" %
domain_config_dir)
else: