diff options
author | Andrew M. Kuchling <amk@amk.ca> | 2006-11-09 21:16:46 +0000 |
---|---|---|
committer | Andrew M. Kuchling <amk@amk.ca> | 2006-11-09 21:16:46 +0000 |
commit | 411a970ba2af63478252105b18b89f274287813f (patch) | |
tree | f20a2c63c5919a459cda3e8d0cc994c6813c9024 /Lib/mailbox.py | |
parent | c38f36a5ede901c6d89b4c1224dc9b1b7a25c433 (diff) | |
download | cpython-411a970ba2af63478252105b18b89f274287813f.tar.gz |
[Patch #1514543] mailbox (Maildir): avoid losing messages on name clash
Two changes:
Where possible, use link()/remove() to move files into a directory; this
makes it easier to avoid overwriting an existing file.
Use _create_carefully() to create files in tmp/, which uses O_EXCL.
Backport candidate.
Diffstat (limited to 'Lib/mailbox.py')
-rwxr-xr-x | Lib/mailbox.py | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 51c2ccc80f..c6b0fa00e3 100755 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -255,7 +255,19 @@ class Maildir(Mailbox): suffix = '' uniq = os.path.basename(tmp_file.name).split(self.colon)[0] dest = os.path.join(self._path, subdir, uniq + suffix) - os.rename(tmp_file.name, dest) + try: + if hasattr(os, 'link'): + os.link(tmp_file.name, dest) + os.remove(tmp_file.name) + else: + os.rename(tmp_file.name, dest) + except OSError, e: + os.remove(tmp_file.name) + if e.errno == errno.EEXIST: + raise ExternalClashError('Name clash with existing message: %s' + % dest) + else: + raise if isinstance(message, MaildirMessage): os.utime(dest, (os.path.getatime(dest), message.get_date())) return uniq @@ -431,12 +443,17 @@ class Maildir(Mailbox): except OSError, e: if e.errno == errno.ENOENT: Maildir._count += 1 - return open(path, 'wb+') + try: + return _create_carefully(path) + except OSError, e: + if e.errno != errno.EEXIST: + raise else: raise - else: - raise ExternalClashError('Name clash prevented file creation: %s' % - path) + + # Fall through to here if stat succeeded or open raised EEXIST. + raise ExternalClashError('Name clash prevented file creation: %s' % + path) def _refresh(self): """Update table of contents mapping.""" |