diff --git a/src/allmydata/frontends/magic_folder.py b/src/allmydata/frontends/magic_folder.py index afbaa38a6..43b1fc50e 100644 --- a/src/allmydata/frontends/magic_folder.py +++ b/src/allmydata/frontends/magic_folder.py @@ -593,18 +593,8 @@ class WriteFileMixin(object): if now is None: now = time.time() - # ensure parent directory exists - head, tail = os.path.split(abspath_u) - - 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) - + initial, last = os.path.split(abspath_u) + fileutil.make_dirs_with_absolute_mode(local_path_u, initial, (~ self._umask) & 0777) fileutil.write(replacement_path_u, file_contents) os.chmod(replacement_path_u, (~ self._umask) & 0777) diff --git a/src/allmydata/util/fileutil.py b/src/allmydata/util/fileutil.py index f24f32d28..f4e897d15 100644 --- a/src/allmydata/util/fileutil.py +++ b/src/allmydata/util/fileutil.py @@ -170,6 +170,27 @@ def is_ancestor_path(parent, dirname): return False return True +def make_dirs_with_absolute_mode(parent, dirname, mode,): + """ + Make directory `dirname` and chmod it to `mode` afterwards. + We chmod all parent directories of `dirname` until we reach + `parent`. + """ + make_dirs(dirname) + os.chmod(dirname, mode) + initial, last = os.path.split(dirname) + if initial == os.path.abspath(parent): + os.chmod(initial, mode) + else: + return + while initial and initial != os.path.abspath(parent): + initial, last = os.path.split(initial) + if initial == os.path.abspath(parent): + break + else: + os.chmod(initial, mode) + + def make_dirs(dirname, mode=0777): """ An idempotent version of os.makedirs(). If the dir already exists, do