summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2013-09-18 12:35:45 (GMT)
committerR David Murray <rdmurray@bitdance.com>2013-09-18 12:35:45 (GMT)
commitc64566e30fd4051d62b124a13f65fdd1910a7481 (patch)
tree627015113e616903bffd3c74ff8bd9bdef6ed878
parentba58e1d827445c3132a8b1a2d587c7e5eb9d5531 (diff)
downloadcpython-c64566e30fd4051d62b124a13f65fdd1910a7481.zip
cpython-c64566e30fd4051d62b124a13f65fdd1910a7481.tar.gz
cpython-c64566e30fd4051d62b124a13f65fdd1910a7481.tar.bz2
#19037: adjust file times *before* moving maildir files into place.
This avoids race conditions when other programs are monitoring the maildir directory. Patch by janzert.
-rw-r--r--Lib/mailbox.py18
-rw-r--r--Misc/NEWS4
2 files changed, 17 insertions, 5 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py
index 530d3c5..4cdd7d4 100644
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -286,6 +286,12 @@ class Maildir(Mailbox):
suffix = ''
uniq = os.path.basename(tmp_file.name).split(self.colon)[0]
dest = os.path.join(self._path, subdir, uniq + suffix)
+ if isinstance(message, MaildirMessage):
+ os.utime(tmp_file.name,
+ (os.path.getatime(tmp_file.name), message.get_date()))
+ # No file modification should be done after the file is moved to its
+ # final position in order to prevent race conditions with changes
+ # from other programs
try:
if hasattr(os, 'link'):
os.link(tmp_file.name, dest)
@@ -299,8 +305,6 @@ class Maildir(Mailbox):
% dest)
else:
raise
- if isinstance(message, MaildirMessage):
- os.utime(dest, (os.path.getatime(dest), message.get_date()))
return uniq
def remove(self, key):
@@ -335,11 +339,15 @@ class Maildir(Mailbox):
else:
suffix = ''
self.discard(key)
+ tmp_path = os.path.join(self._path, temp_subpath)
new_path = os.path.join(self._path, subdir, key + suffix)
- os.rename(os.path.join(self._path, temp_subpath), new_path)
if isinstance(message, MaildirMessage):
- os.utime(new_path, (os.path.getatime(new_path),
- message.get_date()))
+ os.utime(tmp_path,
+ (os.path.getatime(tmp_path), message.get_date()))
+ # No file modification should be done after the file is moved to its
+ # final position in order to prevent race conditions with changes
+ # from other programs
+ os.rename(tmp_path, new_path)
def get_message(self, key):
"""Return a Message representation or raise a KeyError."""
diff --git a/Misc/NEWS b/Misc/NEWS
index 7f4ef60..3c44f00 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,6 +32,10 @@ Core and Builtins
Library
-------
+- Issue #19037: The mailbox module now makes all changes to maildir files
+ before moving them into place, to avoid race conditions with other programs
+ that may be accessing the maildir directory.
+
- Issue #14984: On POSIX systems, when netrc is called without a filename
argument (and therefore is reading the user's $HOME/.netrc file), it now
enforces the same security rules as typical ftp clients: the .netrc file must