reuse some code between Maildir and GmailMaildir

In particular, this makes changing labels for an existing message
safer.

Previously, we did not use a unique filename in tmp/ (used the existing
name) and that could lead to filename collision and data loss if
multiple offlineimap processes accessed the same file.