From f5f52582a1a9935b0f5f50c2c5a1797103d377d0 Mon Sep 17 00:00:00 2001 From: Unrud Date: Mon, 8 Aug 2016 05:17:41 +0200 Subject: [PATCH] Add option to disable syncing to disk Disabling syncing increases the risk of data loss when the system crashes or power fails. On the positive it can increase the performance to a great extent. --- config | 5 +++++ radicale/config.py | 1 + radicale/storage.py | 15 +++++++++------ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/config b/config index a0922b36..1cdfb75a 100644 --- a/config +++ b/config @@ -103,6 +103,11 @@ # Folder for storing local collections, created if not present #filesystem_folder = ~/.config/radicale/collections +# Sync all changes to disk during requests. (This can impair performance.) +# Disabling it increases the risk of data loss, when the system crashes or +# power fails! +#fsync = True + # Command that is run after changes to storage #hook = # Example: git add -A && (git diff --cached --quiet || git commit -m "Changes by "%(user)s) diff --git a/radicale/config.py b/radicale/config.py index 980cf78c..1e02656d 100644 --- a/radicale/config.py +++ b/radicale/config.py @@ -58,6 +58,7 @@ INITIAL_CONFIG = { "type": "multifilesystem", "filesystem_folder": os.path.expanduser( "~/.config/radicale/collections"), + "fsync": "True", "hook": ""}, "logging": { "config": "/etc/radicale/logging", diff --git a/radicale/storage.py b/radicale/storage.py index 06b2905c..9542ad6b 100644 --- a/radicale/storage.py +++ b/radicale/storage.py @@ -393,10 +393,11 @@ class Collection(BaseCollection): delete=False, prefix=".Radicale.tmp-") try: yield tmp - if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"): - fcntl.fcntl(tmp.fileno(), fcntl.F_FULLFSYNC) - else: - os.fsync(tmp.fileno()) + if self.configuration.getboolean("storage", "fsync"): + if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"): + fcntl.fcntl(tmp.fileno(), fcntl.F_FULLFSYNC) + else: + os.fsync(tmp.fileno()) tmp.close() os.rename(tmp.name, path) except: @@ -413,13 +414,15 @@ class Collection(BaseCollection): return file_name raise FileExistsError(errno.EEXIST, "No usable file name found") - @staticmethod - def _sync_directory(path): + @classmethod + def _sync_directory(cls, path): """Sync directory to disk. This only works on POSIX and does nothing on other systems. """ + if not cls.configuration.getboolean("storage", "fsync"): + return if os.name == "posix": fd = os.open(path, 0) try: