diff options
| author | Petri Lehtinen <petri@digip.org> | 2012-06-28 10:48:17 (GMT) | 
|---|---|---|
| committer | Petri Lehtinen <petri@digip.org> | 2012-06-28 10:49:37 (GMT) | 
| commit | 45f0d9835ce9546429b5c94b7bdea9c20ec91b02 (patch) | |
| tree | 7c2c75fd2bb7d8af679cd10db698989cedf0bc59 /Lib/mailbox.py | |
| parent | 8c368efa5560f748587d68bb8bf8da34f8caaaa6 (diff) | |
| download | cpython-45f0d9835ce9546429b5c94b7bdea9c20ec91b02.zip cpython-45f0d9835ce9546429b5c94b7bdea9c20ec91b02.tar.gz cpython-45f0d9835ce9546429b5c94b7bdea9c20ec91b02.tar.bz2  | |
#9559: Append data to single-file mailbox files if messages are only added
If messages were only added, a new file is no longer created and
renamed over the old file when flush() is called on an mbox, MMDF or
Babyl mailbox.
Diffstat (limited to 'Lib/mailbox.py')
| -rw-r--r-- | Lib/mailbox.py | 18 | 
1 files changed, 15 insertions, 3 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 6a69819..ecd9f61 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -561,16 +561,19 @@ class _singlefileMailbox(Mailbox):          self._file = f          self._toc = None          self._next_key = 0 -        self._pending = False   # No changes require rewriting the file. +        self._pending = False       # No changes require rewriting the file. +        self._pending_sync = False  # No need to sync the file          self._locked = False -        self._file_length = None        # Used to record mailbox size +        self._file_length = None    # Used to record mailbox size      def add(self, message):          """Add message and return assigned key."""          self._lookup()          self._toc[self._next_key] = self._append_message(message)          self._next_key += 1 -        self._pending = True +        # _append_message appends the message to the mailbox file. We +        # don't need a full rewrite + rename, sync is enough. +        self._pending_sync = True          return self._next_key - 1      def remove(self, key): @@ -616,6 +619,11 @@ class _singlefileMailbox(Mailbox):      def flush(self):          """Write any pending changes to disk."""          if not self._pending: +            if self._pending_sync: +                # Messages have only been added, so syncing the file +                # is enough. +                _sync_flush(self._file) +                self._pending_sync = False              return          # In order to be writing anything out at all, self._toc must @@ -669,6 +677,7 @@ class _singlefileMailbox(Mailbox):          self._file = open(self._path, 'rb+')          self._toc = new_toc          self._pending = False +        self._pending_sync = False          if self._locked:              _lock_file(self._file, dotlock=False) @@ -705,6 +714,9 @@ class _singlefileMailbox(Mailbox):          """Append message to mailbox and return (start, stop) offsets."""          self._file.seek(0, 2)          before = self._file.tell() +        if len(self._toc) == 0: +            # This is the first message +            self._pre_mailbox_hook(self._file)          try:              self._pre_message_hook(self._file)              offsets = self._install_message(message)  | 
