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