move.py 2.5 KB

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