]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[Patch #1514543] mailbox (Maildir): avoid losing messages on name clash
authorAndrew M. Kuchling <amk@amk.ca>
Thu, 9 Nov 2006 21:16:46 +0000 (21:16 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Thu, 9 Nov 2006 21:16:46 +0000 (21:16 +0000)
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.

Lib/mailbox.py
Misc/NEWS

index 51c2ccc80f5a530a62738a8d39a887e4c42ce958..c6b0fa00e3c13087f27b72c83c299e05878d200c 100755 (executable)
@@ -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."""
index 3aa5ff32ef004321eec03a2c902b20ea3ec0467b..354a7b0bc1dad77ab24c706c188c94feecc618f4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -100,6 +100,10 @@ Library
   weren't passing the message factory on to newly created Maildir/MH
   objects.
 
+- Patch #1514543: In the Maildir clash, report errors if there's 
+  a name clash instead of possibly losing a message.  (Patch by David
+  Watson.)
+
 - Patch #1514544: Try to ensure that messages/indexes have been physically
   written to disk after calling .flush() or .close(). (Patch by David
   Watson.)