Module: check_mk
Branch: master
Commit: c2ad7ce9df2ffb46156b3b588081f705ff99d346
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c2ad7ce9df2ffb…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Jun 14 11:55:29 2017 +0200
Store save_file: Cleanup temporary files when files can not be saved
Change-Id: I9de07f5916cd990d5e5c82a5ae4301e9c6ea5057
---
lib/store.py | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/lib/store.py b/lib/store.py
index b6c7037..6a5b18c 100644
--- a/lib/store.py
+++ b/lib/store.py
@@ -155,7 +155,9 @@ def save_data_to_file(path, data, pretty=True):
def save_file(path, content, mode=0660):
try:
# Normally the file is already locked (when data has been loaded before with
lock=True),
- # but lock it just to be sure we have the lock on the file
+ # but lock it just to be sure we have the lock on the file.
+ #
+ # Please note that this already creates the file with 0 bytes (in case it is
missing).
aquire_lock(path)
tmp_path = None
@@ -194,6 +196,16 @@ def save_file(path, content, mode=0660):
os.rename(tmp_path, path)
except Exception, e:
+ # In case an exception happens during saving cleanup the tempfile created for
writing
+ try:
+ if tmp_path:
+ os.unlink(tmp_path)
+ except IOError, e:
+ if e.errno == 2: # No such file or directory
+ pass
+ else:
+ raise
+
# TODO: How to handle debug mode or logging?
raise MKGeneralException(_("Cannot write configuration file
\"%s\": %s") % (path, e))