move.py 3.2 KB

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