[PATCH 2/2 v2] Don't use temporary file and move when writing out an ifcfg file
Vratislav Podzimek
vpodzime at redhat.com
Wed Sep 11 07:45:36 UTC 2013
NetworkManager uses IN_CLOSE_WRITE inotify event which is not triggered if a new
file is moved to the place of the watched file. Also the SELinux context is
wrong when a file from /tmp is used to the /etc tree.
Related: rhbz#972362
Based on the patch from Hans de Goede <hdegoede at redhat.com>.
Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
pyanaconda/simpleconfig.py | 36 ++++++++++++++++++++----------------
1 file changed, 20 insertions(+), 16 deletions(-)
diff --git a/pyanaconda/simpleconfig.py b/pyanaconda/simpleconfig.py
index d0757cf..ae2e3dc 100644
--- a/pyanaconda/simpleconfig.py
+++ b/pyanaconda/simpleconfig.py
@@ -70,26 +70,31 @@ class SimpleConfigFile(object):
if key:
self.info[key] = value
- def write(self, filename=None):
+ def write(self, filename=None, use_tmp=True):
""" passing filename will override the filename passed to init.
"""
filename = filename or self.filename
if not filename:
return None
- tmpf = tempfile.NamedTemporaryFile(mode="w", delete=False)
- tmpf.write(str(self))
- tmpf.close()
+ if use_tmp:
+ tmpf = tempfile.NamedTemporaryFile(mode="w", delete=False)
+ tmpf.write(str(self))
+ tmpf.close()
- # Move the temporary file (with 0600 permissions) over the top of the
- # original and preserve the original's permissions
- filename = os.path.realpath(filename)
- if os.path.exists(filename):
- m = os.stat(filename).st_mode
+ # Move the temporary file (with 0600 permissions) over the top of the
+ # original and preserve the original's permissions
+ filename = os.path.realpath(filename)
+ if os.path.exists(filename):
+ m = os.stat(filename).st_mode
+ else:
+ m = int('0100644', 8)
+ shutil.move(tmpf.name, filename)
+ os.chmod(filename, m)
else:
- m = int('0100644', 8)
- shutil.move(tmpf.name, filename)
- os.chmod(filename, m)
+ # write directly to the file
+ with open(filename, "w") as fobj:
+ fobj.write(str(self))
def set(self, *args):
for key, value in args:
@@ -176,8 +181,6 @@ class IfcfgFile(SimpleConfigFile):
SimpleConfigFile.read(self, self.path)
return len(self.info)
- # ifcfg-rh is using inotify IN_CLOSE_WRITE event
- # so we don't use temporary file for new configuration.
def write(self, directory=None):
""" Writes values into ifcfg file.
"""
@@ -187,5 +190,6 @@ class IfcfgFile(SimpleConfigFile):
else:
path = os.path.join(directory, os.path.basename(self.path))
- SimpleConfigFile.write(self, path)
-
+ # ifcfg-rh is using inotify IN_CLOSE_WRITE event so we don't use
+ # temporary file for new configuration
+ SimpleConfigFile.write(self, path, use_tmp=False)
--
1.7.11.7
More information about the anaconda-patches
mailing list