upload.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. import errno
  19. import os
  20. import pickle
  21. import sys
  22. from typing import Iterable, Set, TextIO, cast
  23. import radicale.item as radicale_item
  24. from radicale import pathutils
  25. from radicale.storage.multifilesystem.base import CollectionBase
  26. from radicale.storage.multifilesystem.cache import CollectionPartCache
  27. from radicale.storage.multifilesystem.get import CollectionPartGet
  28. from radicale.storage.multifilesystem.history import CollectionPartHistory
  29. class CollectionPartUpload(CollectionPartGet, CollectionPartCache,
  30. CollectionPartHistory, CollectionBase):
  31. def upload(self, href: str, item: radicale_item.Item
  32. ) -> radicale_item.Item:
  33. if not pathutils.is_safe_filesystem_path_component(href):
  34. raise pathutils.UnsafePathError(href)
  35. try:
  36. self._store_item_cache(href, item)
  37. except Exception as e:
  38. raise ValueError("Failed to store item %r in collection %r: %s" %
  39. (href, self.path, e)) from e
  40. path = pathutils.path_to_filesystem(self._filesystem_path, href)
  41. with self._atomic_write(path, newline="") as fo:
  42. f = cast(TextIO, fo)
  43. f.write(item.serialize())
  44. # Clean the cache after the actual item is stored, or the cache entry
  45. # will be removed again.
  46. self._clean_item_cache()
  47. # Track the change
  48. self._update_history_etag(href, item)
  49. self._clean_history()
  50. uploaded_item = self._get(href, verify_href=False)
  51. if uploaded_item is None:
  52. raise RuntimeError("Storage modified externally")
  53. return uploaded_item
  54. def _upload_all_nonatomic(self, items: Iterable[radicale_item.Item],
  55. suffix: str = "") -> None:
  56. """Upload a new set of items.
  57. This takes a list of vobject items and
  58. uploads them nonatomic and without existence checks.
  59. """
  60. cache_folder = os.path.join(self._filesystem_path,
  61. ".Radicale.cache", "item")
  62. self._storage._makedirs_synced(cache_folder)
  63. hrefs: Set[str] = set()
  64. for item in items:
  65. uid = item.uid
  66. try:
  67. cache_content = self._item_cache_content(item)
  68. except Exception as e:
  69. raise ValueError(
  70. "Failed to store item %r in temporary collection %r: %s" %
  71. (uid, self.path, e)) from e
  72. href_candidate_funtions = [
  73. lambda: uid if uid.lower().endswith(suffix.lower())
  74. else uid + suffix,
  75. lambda: radicale_item.get_etag(uid).strip('"') + suffix,
  76. lambda: radicale_item.find_available_uid(
  77. hrefs.__contains__, suffix)]
  78. href = f = None
  79. while href_candidate_funtions:
  80. href = href_candidate_funtions.pop(0)()
  81. if href in hrefs:
  82. continue
  83. if not pathutils.is_safe_filesystem_path_component(href):
  84. if not href_candidate_funtions:
  85. raise pathutils.UnsafePathError(href)
  86. continue
  87. try:
  88. f = open(pathutils.path_to_filesystem(
  89. self._filesystem_path, href),
  90. "w", newline="", encoding=self._encoding)
  91. break
  92. except OSError as e:
  93. if href_candidate_funtions and (
  94. sys.platform != "win32" and
  95. e.errno == errno.EINVAL or
  96. sys.platform == "win32" and e.errno == 123):
  97. continue
  98. raise
  99. assert href is not None and f is not None
  100. with f:
  101. f.write(item.serialize())
  102. f.flush()
  103. self._storage._fsync(f)
  104. hrefs.add(href)
  105. with open(os.path.join(cache_folder, href), "wb") as fb:
  106. pickle.dump(cache_content, fb)
  107. fb.flush()
  108. self._storage._fsync(fb)
  109. self._storage._sync_directory(cache_folder)
  110. self._storage._sync_directory(self._filesystem_path)