From e4a26774b26985b9f52b2b4607b091e4ce059bfd Mon Sep 17 00:00:00 2001
From: David Stainton <dstainton415@gmail.com>
Date: Tue, 8 Dec 2015 17:44:59 +0200
Subject: [PATCH] Fix umask usage bit fiddling and use try finally blocks

---
 src/allmydata/frontends/magic_folder.py | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/allmydata/frontends/magic_folder.py b/src/allmydata/frontends/magic_folder.py
index a40c8c56..15ebf518 100644
--- a/src/allmydata/frontends/magic_folder.py
+++ b/src/allmydata/frontends/magic_folder.py
@@ -485,10 +485,12 @@ class WriteFileMixin(object):
         # ensure parent directory exists
         head, tail = os.path.split(abspath_u)
 
-        old_mask = os.umask(self._umask)
-        fileutil.make_dirs(head, ~ self._umask)
-        fileutil.write(replacement_path_u, file_contents)
-        os.umask(old_mask)
+        try:
+            old_mask = os.umask(self._umask)
+            fileutil.make_dirs(head, (~ self._umask) & 0777)
+            fileutil.write(replacement_path_u, file_contents)
+        finally:
+            os.umask(old_mask)
 
         os.utime(replacement_path_u, (now, now - self.FUDGE_SECONDS))
         if is_conflict:
-- 
2.45.2