delete.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. from tempfile import TemporaryDirectory
  20. from radicale import pathutils, storage
  21. class CollectionDeleteMixin:
  22. def delete(self, href=None):
  23. if href is None:
  24. # Delete the collection
  25. parent_dir = os.path.dirname(self._filesystem_path)
  26. try:
  27. os.rmdir(self._filesystem_path)
  28. except OSError:
  29. with TemporaryDirectory(
  30. prefix=".Radicale.tmp-", dir=parent_dir) as tmp:
  31. os.rename(self._filesystem_path, os.path.join(
  32. tmp, os.path.basename(self._filesystem_path)))
  33. self._storage._sync_directory(parent_dir)
  34. else:
  35. self._storage._sync_directory(parent_dir)
  36. else:
  37. # Delete an item
  38. if not pathutils.is_safe_filesystem_path_component(href):
  39. raise pathutils.UnsafePathError(href)
  40. path = pathutils.path_to_filesystem(self._filesystem_path, href)
  41. if not os.path.isfile(path):
  42. raise storage.ComponentNotFoundError(href)
  43. os.remove(path)
  44. self._storage._sync_directory(os.path.dirname(path))
  45. # Track the change
  46. self._update_history_etag(href, None)
  47. self._clean_history()