1
  6   ˆl—Ým_JJi?u8 Z¸Û7uLZ7ÿ�þB g…R�­…¯ç¿ƒh¹ÄÂÁ      1
  7   ˆl–Ým_JJi?u8 [¸®Å1hß‘äÇò 8à$)ë®º?Àƒh¹ÅÃÂ      00000040
  5   	ˆl–Ým_JJi?u8 Z¸Ø.2Ê˜´o�þB g…}÷
I/óÁƒh¹ÆÄÃ      	1
  5   ˆl–Ým_JJi?u8 Z¸Ó—Ú˜´o�þB g…xcÄÐ¿ŸÂƒh¹ÇÅÄ      Normal
     ˆÉÈÇ525ÆÅÄ     ‹      Õ•ßoÚ0ÇßûWx~ØÖÔñÔ¤¡Q@”iš¦irbC¬&6²ŒýõsH(´*[§mZ÷äóåë»û\y�ºÓ�ãz;½ ñû7ƒ~áÀ‡V šFåƒv½ ô†áÔ¹E`½^××­º6s˜N uyÖëŒL\�;Žé	)|ô‘T0îWDœt™ }ÅÅW¤gôÒYÉ…?·±ÀÅJ&Â¶9ä"×fS-gPš‹Fµ=?»ŒK!�2ªÏU"k¾)ò<—¹añ6eaJ\ŠV,“sb§˜™Ï‘5IˆA&ZYˆ3¦nës9Ãˆe.ÄŸúÝÑg¯—§)a(5bâ×Ýpx9
#L‡,Øãšk¯¹ÂtÀ¬C¹ær&?*¾)Å7òÛñ€Q©‰„MŒ\8©Õ^
ñ5Ñ™]0ÏúÊ¤æPUJøÏºÁ’{Í_MzÃiÔŸ”-áEˆ}u¿óæ03#”C‘4"qÞYa•9^ªØ..w[Tmä<u˜"TC�*�t¦3.Ì!ë1Ê¦;ë°Ò¥5ÍÓZÔ‚œu§=#WÂl*ë¹0,Õ­Òkuá?èA|aKÊèXÜÑì]OÀj6QÐî´.ŽbùõÝ?Àz€ô¿â,ÅÊÿÖ˜–ëßxâõÕ` °AÊøü9$&e    A   ˆl–ß=¿J¥2ÛB‚ „ �pM\ŠbÑ¿�þBË�¬ÙY#ã•àÆ@ùÃƒi÷È_ŠI|¥�+Wé3ž¯ÇÆ e    # This file is part of Radicale - CalDAV and CardDAV server
# Copyright Â© 2014 Jean-Marc Martins
# Copyright Â© 2012-2017 Guillaume Ayoub
# Copyright Â© 2017-2018 Unrud <unrud@outlook.com>
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale.  If not, see <http://www.gnu.org/licenses/>.

import errno
import os
import pickle
import sys
from typing import Iterable, Iterator, TextIO, cast

import radicale.item as radicale_item
from radicale import pathutils
from radicale.storage.multifilesystem.base import CollectionBase
from radicale.storage.multifilesystem.cache import CollectionPartCache
from radicale.storage.multifilesystem.get import CollectionPartGet
from radicale.storage.multifilesystem.history import CollectionPartHistory


class CollectionPartUpload(CollectionPartGet, CollectionPartCache,
                           CollectionPartHistory, CollectionBase):

    def upload(self, href: str, item: radicale_item.Item
               ) -> radicale_item.Item:
        if not pathutils.is_safe_filesystem_path_component(href):
            raise pathutils.UnsafePathError(href)
        try:
            self._store_item_cache(href, item)
        except Exception as e:
            raise ValueError("Failed to store item %r in collection %r: %s" %
                             (href, self.path, e)) from e
        path = pathutils.path_to_filesystem(self._filesystem_path, href)
        with self._atomic_write(path, newline="") as fo:
            f = cast(TextIO, fo)
            f.write(item.serialize())
        # Clean the cache after the actual item is stored, or the cache entry
        # will be removed again.
        self._clean_item_cache()
        # Track the change
        self._update_history_etag(href, item)
        self._clean_history()
        uploaded_item = self._get(href, verify_href=False)
        if uploaded_item is None:
            raise RuntimeError("Storage modified externally")
        return uploaded_item

    def _upload_all_nonatomic(self, items: Iterable[radicale_item.Item],
                              suffix: str = "") -> None:
        """Upload a new set of items non-atomic"""
        def is_safe_free_href(href: str) -> bool:
            return (pathutils.is_safe_filesystem_path_component(href) and
                    not os.path.lexists(
                        os.path.join(self._filesystem_path, href)))

        def get_safe_free_hrefs(uid: str) -> Iterator[str]:
            for href in [uid if uid.lower().endswith(suffix.lower())
                         else uid + suffix,
                         radicale_item.get_etag(uid).strip('"') + suffix]:
    