From 30d287ce00a4312731084183cbf10319ba903c3a Mon Sep 17 00:00:00 2001 From: Unrud Date: Thu, 25 Aug 2016 06:00:15 +0200 Subject: [PATCH] Write files nonatomic in upload_all It's only used in temporary collections. --- radicale/storage.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/radicale/storage.py b/radicale/storage.py index ef4fcfc..fc81507 100644 --- a/radicale/storage.py +++ b/radicale/storage.py @@ -582,14 +582,25 @@ class Collection(BaseCollection): """Upload a new set of items. This takes a mapping of href and vobject items and - returns a list of uploaded items. - Might bring optimizations on some storages. + uploads them nonatomic and without existence checks. """ - return [ - self.upload(href, vobject_item) - for href, vobject_item in vobject_items.items() - ] + fs = [] + for href, item in vobject_items.items(): + path = path_to_filesystem(self._filesystem_path, href) + fs.append(open(path, "w", encoding=self.encoding, newline="")) + fs[-1].write(item.serialize()) + fsync_fn = lambda fd: None + if self.configuration.getboolean("storage", "fsync"): + if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"): + fsync_fn = lambda fd: fcntl.fcntl(fd, fcntl.F_FULLFSYNC) + else: + fsync_fn = os.fsync + # sync everything at once because it's slightly faster. + for f in fs: + fsync_fn(f.fileno()) + f.close() + self._sync_directory(self._filesystem_path) @classmethod def move(cls, item, to_collection, to_href):