From 51ccec7a333611463876c514a5d07d7a0fd3129d Mon Sep 17 00:00:00 2001 From: David Stainton Date: Tue, 19 Jan 2016 21:04:17 +0100 Subject: [PATCH] Use chmod instead of changing umask Conflicts: src/allmydata/frontends/magic_folder.py --- src/allmydata/frontends/magic_folder.py | 21 +++++++++++++-------- src/allmydata/test/test_magic_folder.py | 4 ++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/allmydata/frontends/magic_folder.py b/src/allmydata/frontends/magic_folder.py index bf4b43123..afbaa38a6 100644 --- a/src/allmydata/frontends/magic_folder.py +++ b/src/allmydata/frontends/magic_folder.py @@ -573,7 +573,7 @@ class WriteFileMixin(object): def _get_conflicted_filename(self, abspath_u): return abspath_u + u".conflict" - def _write_downloaded_file(self, abspath_u, file_contents, is_conflict=False, now=None): + def _write_downloaded_file(self, local_path_u, abspath_u, file_contents, is_conflict=False, now=None): self._log("_write_downloaded_file(%r, <%d bytes>, is_conflict=%r, now=%r)" % (abspath_u, len(file_contents), is_conflict, now)) @@ -596,12 +596,17 @@ class WriteFileMixin(object): # ensure parent directory exists head, tail = os.path.split(abspath_u) - old_mask = os.umask(self._umask) - try: - fileutil.make_dirs(head, (~ self._umask) & 0777) - fileutil.write(replacement_path_u, file_contents) - finally: - os.umask(old_mask) + fileutil.make_dirs(head) + os.chmod(head, (~ self._umask) & 0777) + while head and head != os.path.abspath(local_path_u): + head, tail = os.path.split(head) + if head == os.path.abspath(local_path_u): + break + else: + os.chmod(head, (~ self._umask) & 0777) + + fileutil.write(replacement_path_u, file_contents) + os.chmod(replacement_path_u, (~ self._umask) & 0777) # FUDGE_SECONDS is used to determine if another process # has written to the same file concurrently. This is described @@ -888,7 +893,7 @@ class Downloader(QueueMixin, WriteFileMixin): d.addCallback(lambda ign: self._rename_deleted_file(abspath_u)) else: d.addCallback(lambda ign: item.file_node.download_best_version(progress=item.progress)) - d.addCallback(lambda contents: self._write_downloaded_file(abspath_u, contents, + d.addCallback(lambda contents: self._write_downloaded_file(self._local_path_u, abspath_u, contents, is_conflict=is_conflict)) d.addCallbacks(do_update_db, failed) diff --git a/src/allmydata/test/test_magic_folder.py b/src/allmydata/test/test_magic_folder.py index 00a872f8f..c2034e5ee 100644 --- a/src/allmydata/test/test_magic_folder.py +++ b/src/allmydata/test/test_magic_folder.py @@ -1254,7 +1254,7 @@ class MockTest(MagicFolderTestMixin, unittest.TestCase): fileutil.write(local_file, "foo") # if is_conflict is False, then the .conflict file shouldn't exist. - writefile._write_downloaded_file(local_file, "bar", False, None) + writefile._write_downloaded_file(workdir, local_file, "bar", False, None) conflicted_path = local_file + u".conflict" self.failIf(os.path.exists(conflicted_path)) @@ -1270,7 +1270,7 @@ class MockTest(MagicFolderTestMixin, unittest.TestCase): self.failUnlessEqual(fileutil.read(local_file), "bar") # now a test for conflicted case - writefile._write_downloaded_file(local_file, "bar", True, None) + writefile._write_downloaded_file(workdir, local_file, "bar", True, None) self.failUnless(os.path.exists(conflicted_path)) # .tmp file shouldn't exist