Merge pull request #560 from Unrud/detclose

Always close files when creating collection
This commit is contained in:
Guillaume Ayoub 2017-03-08 15:55:47 +01:00 committed by GitHub
commit 96248c299e

View File

@ -25,6 +25,7 @@ entry.
""" """
import contextlib
import errno import errno
import json import json
import os import os
@ -583,17 +584,18 @@ class Collection(BaseCollection):
uploads them nonatomic and without existence checks. uploads them nonatomic and without existence checks.
""" """
fs = [] with contextlib.ExitStack() as stack:
for href, item in vobject_items.items(): fs = []
if not is_safe_filesystem_path_component(href): for href, item in vobject_items.items():
raise UnsafePathError(href) if not is_safe_filesystem_path_component(href):
path = path_to_filesystem(self._filesystem_path, href) raise UnsafePathError(href)
fs.append(open(path, "w", encoding=self.encoding, newline="")) path = path_to_filesystem(self._filesystem_path, href)
fs[-1].write(item.serialize()) fs.append(stack.enter_context(
# sync everything at once because it's slightly faster. open(path, "w", encoding=self.encoding, newline="")))
for f in fs: fs[-1].write(item.serialize())
self._fsync(f.fileno()) # sync everything at once because it's slightly faster.
f.close() for f in fs:
self._fsync(f.fileno())
self._sync_directory(self._filesystem_path) self._sync_directory(self._filesystem_path)
@classmethod @classmethod