Write files nonatomic in upload_all

It's only used in temporary collections.
This commit is contained in:
Unrud 2016-08-25 06:00:15 +02:00
parent e31ea57883
commit 30d287ce00

View File

@ -582,14 +582,25 @@ class Collection(BaseCollection):
"""Upload a new set of items. """Upload a new set of items.
This takes a mapping of href and vobject items and This takes a mapping of href and vobject items and
returns a list of uploaded items. uploads them nonatomic and without existence checks.
Might bring optimizations on some storages.
""" """
return [ fs = []
self.upload(href, vobject_item) for href, item in vobject_items.items():
for href, vobject_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 @classmethod
def move(cls, item, to_collection, to_href): def move(cls, item, to_collection, to_href):