history.py 3.7 KB

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