From 5e1f198ef2275ca0bd2db0f264ed75ae8561b73d Mon Sep 17 00:00:00 2001 From: Konstantin Ryabitsev Date: Thu, 20 May 2021 15:00:31 -0400 Subject: Write maildir atomically It probably doesn't matter for b4 usage, but the maildir standard requires that files are written to tmp first and then moved into new (or hardlinked, really, then removed from tmp). Since nothing is reading the dir we're writing to, it's not as important to fully follow the standard when it comes to hardlinking, but let's at least move them into place once writing is completed. Signed-off-by: Konstantin Ryabitsev --- b4/mbox.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/b4/mbox.py b/b4/mbox.py index 910bac5..74e0f8e 100644 --- a/b4/mbox.py +++ b/b4/mbox.py @@ -126,9 +126,12 @@ def make_am(msgs, cmdargs, msgid): pathlib.Path(d_new).mkdir(parents=True) d_cur = os.path.join(am_filename, 'cur') pathlib.Path(d_cur).mkdir(parents=True) + d_tmp = os.path.join(am_filename, 'tmp') + pathlib.Path(d_tmp).mkdir(parents=True) for m_slug, msg in am_msgs: - with open(os.path.join(d_new, f'{m_slug}.eml'), 'wb') as mfh: + with open(os.path.join(d_tmp, f'{m_slug}.eml'), 'wb') as mfh: mfh.write(msg.as_bytes(policy=b4.emlpolicy)) + os.rename(os.path.join(d_tmp, f'{m_slug}.eml'), os.path.join(d_new, f'{m_slug}.eml')) else: with open(am_filename, 'wb') as fh: b4.save_git_am_mbox([x[1] for x in am_msgs], fh) -- cgit v1.2.3