Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2565288

Browse files
committed
#5346: Preserve permissions of mbox, MMDF and Babyl mailbox files on flush()
2 parents 31a78c3 + 5b5619f commit 2565288

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

Lib/mailbox.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,9 @@ def flush(self):
691691
_sync_close(new_file)
692692
# self._file is about to get replaced, so no need to sync.
693693
self._file.close()
694+
# Make sure the new file's mode is the same as the old file's
695+
mode = os.stat(self._path).st_mode
696+
os.chmod(new_file.name, mode)
694697
try:
695698
os.rename(new_file.name, self._path)
696699
except OSError as e:

Lib/test/test_mailbox.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,23 @@ def test_add_doesnt_rewrite(self):
969969
self._box = self._factory(self._path)
970970
self.assertEqual(len(self._box), 1)
971971

972+
def test_permissions_after_flush(self):
973+
# See issue #5346
974+
975+
# Make the mailbox world writable. It's unlikely that the new
976+
# mailbox file would have these permissions after flush(),
977+
# because umask usually prevents it.
978+
mode = os.stat(self._path).st_mode | 0o666
979+
os.chmod(self._path, mode)
980+
981+
self._box.add(self._template % 0)
982+
i = self._box.add(self._template % 1)
983+
# Need to remove one message to make flush() create a new file
984+
self._box.remove(i)
985+
self._box.flush()
986+
987+
self.assertEqual(os.stat(self._path).st_mode, mode)
988+
972989

973990
class _TestMboxMMDF(_TestSingleFile):
974991

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Core and Builtins
1414
Library
1515
-------
1616

17+
- Issue #5346: Preserve permissions of mbox, MMDF and Babyl mailbox
18+
files on flush().
19+
1720
- Issue #10571: Fix the "--sign" option of distutils' upload command.
1821
Patch by Jakub Wilk.
1922

0 commit comments

Comments
 (0)