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