history.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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-2019 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 binascii
  19. import os
  20. import pickle
  21. from radicale import item as radicale_item
  22. from radicale import pathutils
  23. from radicale.log import logger
  24. class CollectionHistoryMixin:
  25. def _update_history_etag(self, href, item):
  26. """Updates and retrieves the history etag from the history cache.
  27. The history cache contains a file for each current and deleted item
  28. of the collection. These files contain the etag of the item (empty
  29. string for deleted items) and a history etag, which is a hash over
  30. the previous history etag and the etag separated by "/".
  31. """
  32. history_folder = os.path.join(self._filesystem_path,
  33. ".Radicale.cache", "history")
  34. try:
  35. with open(os.path.join(history_folder, href), "rb") as f:
  36. cache_etag, history_etag = pickle.load(f)
  37. except (FileNotFoundError, pickle.UnpicklingError, ValueError) as e:
  38. if isinstance(e, (pickle.UnpicklingError, ValueError)):
  39. logger.warning(
  40. "Failed to load history cache entry %r in %r: %s",
  41. href, self.path, e, exc_info=True)
  42. cache_etag = ""
  43. # Initialize with random data to prevent collisions with cleaned
  44. # expired items.
  45. history_etag = binascii.hexlify(os.urandom(16)).decode("ascii")
  46. etag = item.etag if item else ""
  47. if etag != cache_etag:
  48. self._storage._makedirs_synced(history_folder)
  49. history_etag = radicale_item.get_etag(
  50. history_etag + "/" + etag).strip("\"")
  51. try:
  52. # Race: Other processes might have created and locked the file.
  53. with self._atomic_write(os.path.join(history_folder, href),
  54. "wb") as f:
  55. pickle.dump([etag, history_etag], f)
  56. except PermissionError:
  57. pass
  58. return history_etag
  59. def _get_deleted_history_hrefs(self):
  60. """Returns the hrefs of all deleted items that are still in the
  61. history cache."""
  62. history_folder = os.path.join(self._filesystem_path,
  63. ".Radicale.cache", "history")
  64. try:
  65. for entry in os.scandir(history_folder):
  66. href = entry.name
  67. if not pathutils.is_safe_filesystem_path_component(href):
  68. continue
  69. if os.path.isfile(os.path.join(self._filesystem_path, href)):
  70. continue
  71. yield href
  72. except FileNotFoundError:
  73. pass
  74. def _clean_history(self):
  75. # Delete all expired history entries of deleted items.
  76. history_folder = os.path.join(self._filesystem_path,
  77. ".Radicale.cache", "history")
  78. self._clean_cache(history_folder, self._get_deleted_history_hrefs(),
  79. max_age=self._storage.configuration.get(
  80. "storage", "max_sync_token_age"))