]> granicus.if.org Git - python/commitdiff
#5346: Preserve permissions of mbox, MMDF and Babyl mailbox files on flush()
authorPetri Lehtinen <petri@digip.org>
Fri, 29 Jun 2012 12:09:12 +0000 (15:09 +0300)
committerPetri Lehtinen <petri@digip.org>
Fri, 29 Jun 2012 12:10:41 +0000 (15:10 +0300)
Lib/mailbox.py
Lib/test/test_mailbox.py
Misc/NEWS

index 73c43e22f7ada3a6f398848a68b9b8e48d339984..8b00460ec6db8b0945606e36b18a85913f93c653 100644 (file)
@@ -691,6 +691,9 @@ class _singlefileMailbox(Mailbox):
         _sync_close(new_file)
         # self._file is about to get replaced, so no need to sync.
         self._file.close()
+        # Make sure the new file's mode is the same as the old file's
+        mode = os.stat(self._path).st_mode
+        os.chmod(new_file.name, mode)
         try:
             os.rename(new_file.name, self._path)
         except OSError as e:
index d8dca1d86cb87336921adff9537d487bfda9a7c0..9f1fdad383c90148c6294efe9d7b42743e25aefd 100644 (file)
@@ -966,6 +966,23 @@ class _TestSingleFile(TestMailbox):
         self._box = self._factory(self._path)
         self.assertEqual(len(self._box), 1)
 
+    def test_permissions_after_flush(self):
+        # See issue #5346
+
+        # Make the mailbox world writable. It's unlikely that the new
+        # mailbox file would have these permissions after flush(),
+        # because umask usually prevents it.
+        mode = os.stat(self._path).st_mode | 0o666
+        os.chmod(self._path, mode)
+
+        self._box.add(self._template % 0)
+        i = self._box.add(self._template % 1)
+        # Need to remove one message to make flush() create a new file
+        self._box.remove(i)
+        self._box.flush()
+
+        self.assertEqual(os.stat(self._path).st_mode, mode)
+
 
 class _TestMboxMMDF(_TestSingleFile):
 
index 242b8f76d1a54d9ab540c51e10db5893123184e0..99515c0639227e32d2c07f77fd50909c6aa3bfec 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #5346: Preserve permissions of mbox, MMDF and Babyl mailbox
+  files on flush().
+
 - Issue #10571: Fix the "--sign" option of distutils' upload command.
   Patch by Jakub Wilk.