upload.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # This file is part of Radicale Server - Calendar 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 os
  19. import pickle
  20. import sys
  21. import radicale.item as radicale_item
  22. from radicale import pathutils
  23. class CollectionUploadMixin:
  24. def upload(self, href, item):
  25. if not pathutils.is_safe_filesystem_path_component(href):
  26. raise pathutils.UnsafePathError(href)
  27. try:
  28. self._store_item_cache(href, item)
  29. except Exception as e:
  30. raise ValueError("Failed to store item %r in collection %r: %s" %
  31. (href, self.path, e)) from e
  32. path = pathutils.path_to_filesystem(self._filesystem_path, href)
  33. with self._atomic_write(path, newline="") as fd:
  34. fd.write(item.serialize())
  35. # Clean the cache after the actual item is stored, or the cache entry
  36. # will be removed again.
  37. self._clean_item_cache()
  38. # Track the change
  39. self._update_history_etag(href, item)
  40. self._clean_history()
  41. return self._get(href, verify_href=False)
  42. def _upload_all_nonatomic(self, items, suffix=""):
  43. """Upload a new set of items.
  44. This takes a list of vobject items and
  45. uploads them nonatomic and without existence checks.
  46. """
  47. cache_folder = os.path.join(self._filesystem_path,
  48. ".Radicale.cache", "item")
  49. self._storage._makedirs_synced(cache_folder)
  50. hrefs = set()
  51. for item in items:
  52. uid = item.uid
  53. try:
  54. cache_content = self._item_cache_content(item)
  55. except Exception as e:
  56. raise ValueError(
  57. "Failed to store item %r in temporary collection %r: %s" %
  58. (uid, self.path, e)) from e
  59. href_candidate_funtions = []
  60. if os.name == "posix" or sys.platform == "win32":
  61. href_candidate_funtions.append(
  62. lambda: uid if uid.lower().endswith(suffix.lower())
  63. else uid + suffix)
  64. href_candidate_funtions.extend((
  65. lambda: radicale_item.get_etag(uid).strip('"') + suffix,
  66. lambda: radicale_item.find_available_uid(hrefs.__contains__,
  67. suffix)))
  68. href = f = None
  69. while href_candidate_funtions:
  70. href = href_candidate_funtions.pop(0)()
  71. if href in hrefs:
  72. continue
  73. if not pathutils.is_safe_filesystem_path_component(href):
  74. if not href_candidate_funtions:
  75. raise pathutils.UnsafePathError(href)
  76. continue
  77. try:
  78. f = open(pathutils.path_to_filesystem(
  79. self._filesystem_path, href),
  80. "w", newline="", encoding=self._encoding)
  81. break
  82. except OSError as e:
  83. if href_candidate_funtions and (
  84. os.name == "posix" and e.errno == 22 or
  85. sys.platform == "win32" and e.errno == 123):
  86. continue
  87. raise
  88. with f:
  89. f.write(item.serialize())
  90. f.flush()
  91. self._storage._fsync(f)
  92. hrefs.add(href)
  93. with open(os.path.join(cache_folder, href), "wb") as f:
  94. pickle.dump(cache_content, f)
  95. f.flush()
  96. self._storage._fsync(f)
  97. self._storage._sync_directory(cache_folder)
  98. self._storage._sync_directory(self._filesystem_path)