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