move.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2021 Unrud <unrud@outlook.com>
  5. # Copyright © 2024-2024 Peter Bieringer <pb@bieringer.de>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. import os
  20. from radicale import item as radicale_item
  21. from radicale import pathutils, storage
  22. from radicale.storage import multifilesystem
  23. from radicale.storage.multifilesystem.base import StorageBase
  24. class StoragePartMove(StorageBase):
  25. def move(self, item: radicale_item.Item,
  26. to_collection: storage.BaseCollection, to_href: str) -> None:
  27. if not pathutils.is_safe_filesystem_path_component(to_href):
  28. raise pathutils.UnsafePathError(to_href)
  29. assert isinstance(to_collection, multifilesystem.Collection)
  30. assert isinstance(item.collection, multifilesystem.Collection)
  31. assert item.href
  32. os.replace(pathutils.path_to_filesystem(
  33. item.collection._filesystem_path, item.href),
  34. pathutils.path_to_filesystem(
  35. to_collection._filesystem_path, to_href))
  36. self._sync_directory(to_collection._filesystem_path)
  37. if item.collection._filesystem_path != to_collection._filesystem_path:
  38. self._sync_directory(item.collection._filesystem_path)
  39. # Move the item cache entry
  40. cache_folder = self._get_collection_cache_folder(item.collection._filesystem_path,
  41. ".Radicale.cache", "item")
  42. to_cache_folder = self._get_collection_cache_folder(to_collection._filesystem_path,
  43. ".Radicale.cache", "item")
  44. self._makedirs_synced(to_cache_folder)
  45. try:
  46. os.replace(os.path.join(cache_folder, item.href),
  47. os.path.join(to_cache_folder, to_href))
  48. except FileNotFoundError:
  49. pass
  50. else:
  51. self._makedirs_synced(to_cache_folder)
  52. if cache_folder != to_cache_folder:
  53. self._makedirs_synced(cache_folder)
  54. # Track the change
  55. to_collection._update_history_etag(to_href, item)
  56. item.collection._update_history_etag(item.href, None)
  57. to_collection._clean_history()
  58. if item.collection._filesystem_path != to_collection._filesystem_path:
  59. item.collection._clean_history()