[master 5/5] Fix the use of a temporary file in SimpleConfig.write

dashea installerbot-noreply at redhat.com
Tue Jul 14 21:56:37 UTC 2015


From: David Shea <dshea at redhat.com>

shutil.move is only an atomic rename if the files are on the same
filesystem, and since /tmp is usually a tmpfs, the temporary file and
the config file were usually not on the same filesystem. Create the
temporary file as a hidden file in the target directory and use
os.rename to move it into place.
---
 pyanaconda/simpleconfig.py | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/pyanaconda/simpleconfig.py b/pyanaconda/simpleconfig.py
index 4becb09..f30258f 100644
--- a/pyanaconda/simpleconfig.py
+++ b/pyanaconda/simpleconfig.py
@@ -23,7 +23,6 @@
 #            Brian C. Lane <bcl at redhat.com>
 #
 import os
-import shutil
 import shlex
 import string # pylint: disable=deprecated-module
 import tempfile
@@ -112,19 +111,24 @@ def write(self, filename=None, use_tmp=True):
             return None
 
         if use_tmp:
-            tmpf = tempfile.NamedTemporaryFile(mode="w", delete=False)
+            filename = os.path.realpath(filename)
+
+            # Create a temporary in the same directory as the target file to ensure
+            # the new file is on the same filesystem
+            tmpf = tempfile.NamedTemporaryFile(mode="w", delete=False,
+                    dir=os.path.dirname(filename), prefix="." + os.path.basename(filename))
             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)
+            # Change the permissions (currently 0600) to match the original file
             if os.path.exists(filename):
                 m = os.stat(filename).st_mode
             else:
-                m = int('0100644', 8)
-            shutil.move(tmpf.name, filename)
+                m = 0o0644
             eintr_retry_call(os.chmod, filename, m)
+
+            # Move the temporary file  over the top of the original
+            os.rename(tmpf.name, filename)
         else:
             # write directly to the file
             with open(filename, "w") as fobj:


-- 
To view this commit on github, visit https://github.com/rhinstaller/anaconda/commit/6fe5170c5b7f5490b6d2b4db6145d34cd99412db


More information about the anaconda-patches mailing list