Document _makedirs_synced and remove exist_ok

This commit is contained in:
Guillaume Ayoub 2016-08-06 14:08:21 +02:00
parent 4549d1b2db
commit 4236077b04

View File

@ -428,16 +428,21 @@ class Collection(BaseCollection):
raise FileExistsError(errno.EEXIST, "No usable file name found")
@classmethod
def _makedirs_synced(cls, filesystem_path, exist_ok=False):
if os.path.isdir(filesystem_path) and exist_ok:
def _makedirs_synced(cls, filesystem_path):
"""Recursively create a directory and its parents in a sync'ed way.
This method acts silently when the folder already exists.
"""
if os.path.isdir(filesystem_path):
return
parent_filesystem_path = os.path.dirname(filesystem_path)
# Prevent infinite loop
if filesystem_path != parent_filesystem_path:
# Create parent dirs recursively
cls._makedirs_synced(parent_filesystem_path, exist_ok=True)
cls._makedirs_synced(parent_filesystem_path)
# Possible race!
os.makedirs(filesystem_path, exist_ok=exist_ok)
os.makedirs(filesystem_path, exist_ok=True)
sync_directory(parent_filesystem_path)
@classmethod
@ -454,7 +459,7 @@ class Collection(BaseCollection):
folder = cls._get_collection_root_folder()
# Create the root collection
cls._makedirs_synced(folder, exist_ok=True)
cls._makedirs_synced(folder)
try:
filesystem_path = path_to_filesystem(folder, sane_path)
except ValueError:
@ -512,11 +517,11 @@ class Collection(BaseCollection):
if not props.get("tag") and collection:
props["tag"] = collection[0].name
if not props:
cls._makedirs_synced(filesystem_path, exist_ok=True)
cls._makedirs_synced(filesystem_path)
return cls(sane_path, principal=principal)
parent_dir = os.path.dirname(filesystem_path)
cls._makedirs_synced(parent_dir, exist_ok=True)
cls._makedirs_synced(parent_dir)
# Create a temporary directory with an unsafe name
with TemporaryDirectory(
@ -735,7 +740,7 @@ class Collection(BaseCollection):
if not cls._lock_file:
folder = os.path.expanduser(
cls.configuration.get("storage", "filesystem_folder"))
cls._makedirs_synced(folder, exist_ok=True)
cls._makedirs_synced(folder)
lock_path = os.path.join(folder, ".Radicale.lock")
cls._lock_file = open(lock_path, "w+")
# Set access rights to a necessary minimum to prevent locking