1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-06-26 16:45:52 +00:00
Radicale/radicale/storage/multifilesystem/move.py

63 lines
2.9 KiB
Python
Raw Normal View History

2021-12-08 21:45:42 +01:00
# This file is part of Radicale - CalDAV and CardDAV server
2018-09-04 03:33:50 +02:00
# Copyright © 2014 Jean-Marc Martins
# Copyright © 2012-2017 Guillaume Ayoub
2024-12-03 21:34:00 +01:00
# Copyright © 2017-2021 Unrud <unrud@outlook.com>
2025-03-02 07:53:07 +01:00
# Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
2018-09-04 03:33:50 +02:00
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
import os
2021-07-26 20:56:47 +02:00
from radicale import item as radicale_item
from radicale import pathutils, storage
from radicale.storage import multifilesystem
from radicale.storage.multifilesystem.base import StorageBase
2018-09-04 03:33:50 +02:00
2021-07-26 20:56:47 +02:00
class StoragePartMove(StorageBase):
2021-07-26 20:56:47 +02:00
def move(self, item: radicale_item.Item,
to_collection: storage.BaseCollection, to_href: str) -> None:
2018-09-04 03:33:50 +02:00
if not pathutils.is_safe_filesystem_path_component(to_href):
raise pathutils.UnsafePathError(to_href)
2021-07-26 20:56:47 +02:00
assert isinstance(to_collection, multifilesystem.Collection)
assert isinstance(item.collection, multifilesystem.Collection)
assert item.href
os.replace(pathutils.path_to_filesystem(
item.collection._filesystem_path, item.href),
pathutils.path_to_filesystem(
to_collection._filesystem_path, to_href))
self._sync_directory(to_collection._filesystem_path)
2018-09-04 03:33:50 +02:00
if item.collection._filesystem_path != to_collection._filesystem_path:
self._sync_directory(item.collection._filesystem_path)
2018-09-04 03:33:50 +02:00
# Move the item cache entry
2024-12-10 08:25:14 +01:00
cache_folder = self._get_collection_cache_subfolder(item.collection._filesystem_path, ".Radicale.cache", "item")
to_cache_folder = self._get_collection_cache_subfolder(to_collection._filesystem_path, ".Radicale.cache", "item")
self._makedirs_synced(to_cache_folder)
2018-09-04 03:33:50 +02:00
try:
os.replace(os.path.join(cache_folder, item.href),
os.path.join(to_cache_folder, to_href))
except FileNotFoundError:
pass
else:
self._makedirs_synced(to_cache_folder)
2018-09-04 03:33:50 +02:00
if cache_folder != to_cache_folder:
self._makedirs_synced(cache_folder)
2018-09-04 03:33:50 +02:00
# Track the change
to_collection._update_history_etag(to_href, item)
item.collection._update_history_etag(item.href, None)
to_collection._clean_history()
if item.collection._filesystem_path != to_collection._filesystem_path:
item.collection._clean_history()