move.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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-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 radicale import item as radicale_item
  20. from radicale import pathutils, storage
  21. from radicale.storage import multifilesystem
  22. from radicale.storage.multifilesystem.base import StorageBase
  23. class StoragePartMove(StorageBase):
  24. def move(self, item: radicale_item.Item,
  25. to_collection: storage.BaseCollection, to_href: str) -> None:
  26. if not pathutils.is_safe_filesystem_path_component(to_href):
  27. raise pathutils.UnsafePathError(to_href)
  28. assert isinstance(to_collection, multifilesystem.Collection)
  29. assert isinstance(item.collection, multifilesystem.Collection)
  30. assert item.href
  31. os.replace(pathutils.path_to_filesystem(
  32. item.collection._filesystem_path, item.href),
  33. pathutils.path_to_filesystem(
  34. to_collection._filesystem_path, to_href))
  35. self._sync_directory(to_collection._filesystem_path)
  36. if item.collection._filesystem_path != to_collection._filesystem_path:
  37. self._sync_directory(item.collection._filesystem_path)
  38. # Move the item cache entry
  39. cache_folder = os.path.join(item.collection._filesystem_path,
  40. ".Radicale.cache", "item")
  41. to_cache_folder = os.path.join(to_collection._filesystem_path,
  42. ".Radicale.cache", "item")
  43. self._makedirs_synced(to_cache_folder)
  44. try:
  45. os.replace(os.path.join(cache_folder, item.href),
  46. os.path.join(to_cache_folder, to_href))
  47. except FileNotFoundError:
  48. pass
  49. else:
  50. self._makedirs_synced(to_cache_folder)
  51. if cache_folder != to_cache_folder:
  52. self._makedirs_synced(cache_folder)
  53. # Track the change
  54. to_collection._update_history_etag(to_href, item)
  55. item.collection._update_history_etag(item.href, None)
  56. to_collection._clean_history()
  57. if item.collection._filesystem_path != to_collection._filesystem_path:
  58. item.collection._clean_history()