Fix file permissions in storage folder

Closes #1024
This commit is contained in:
Unrud 2020-05-15 21:54:10 +02:00
parent 63e00ca677
commit 630d49b7cf

View File

@ -27,7 +27,7 @@ import contextlib
import os import os
import time import time
from itertools import chain from itertools import chain
from tempfile import NamedTemporaryFile from tempfile import TemporaryDirectory
from radicale import pathutils, storage from radicale import pathutils, storage
from radicale.storage.multifilesystem.cache import CollectionCacheMixin from radicale.storage.multifilesystem.cache import CollectionCacheMixin
@ -69,21 +69,17 @@ class Collection(
@contextlib.contextmanager @contextlib.contextmanager
def _atomic_write(self, path, mode="w", newline=None): def _atomic_write(self, path, mode="w", newline=None):
directory = os.path.dirname(path) parent_dir, name = os.path.split(path)
tmp = NamedTemporaryFile( # Do not use mkstemp because it creates with permissions 0o600
mode=mode, dir=directory, delete=False, prefix=".Radicale.tmp-", with TemporaryDirectory(
newline=newline, encoding=None if "b" in mode else self._encoding) prefix=".Radicale.tmp-", dir=parent_dir) as tmp_dir:
try: with open(os.path.join(tmp_dir, name), mode, newline=newline,
yield tmp encoding=None if "b" in mode else self._encoding) as tmp:
tmp.flush() yield tmp
self._storage._fsync(tmp) tmp.flush()
tmp.close() self._storage._fsync(tmp)
os.replace(tmp.name, path) os.replace(os.path.join(tmp_dir, name), path)
except BaseException: self._storage._sync_directory(parent_dir)
tmp.close()
os.remove(tmp.name)
raise
self._storage._sync_directory(directory)
@property @property
def last_modified(self): def last_modified(self):