|
2 | 2 | # Copyright © 2014 Jean-Marc Martins |
3 | 3 | # Copyright © 2012-2017 Guillaume Ayoub |
4 | 4 | # Copyright © 2017-2021 Unrud <unrud@outlook.com> |
5 | | -# Copyright © 2024-2024 Peter Bieringer <pb@bieringer.de> |
| 5 | +# Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de> |
6 | 6 | # |
7 | 7 | # This library is free software: you can redistribute it and/or modify |
8 | 8 | # it under the terms of the GNU General Public License as published by |
@@ -50,27 +50,31 @@ def create_collection(self, href: str, |
50 | 50 | self._makedirs_synced(parent_dir) |
51 | 51 |
|
52 | 52 | # Create a temporary directory with an unsafe name |
53 | | - with TemporaryDirectory(prefix=".Radicale.tmp-", dir=parent_dir |
54 | | - ) as tmp_dir: |
55 | | - # The temporary directory itself can't be renamed |
56 | | - tmp_filesystem_path = os.path.join(tmp_dir, "collection") |
57 | | - os.makedirs(tmp_filesystem_path) |
58 | | - col = self._collection_class( |
59 | | - cast(multifilesystem.Storage, self), |
60 | | - pathutils.unstrip_path(sane_path, True), |
61 | | - filesystem_path=tmp_filesystem_path) |
62 | | - col.set_meta(props) |
63 | | - if items is not None: |
64 | | - if props.get("tag") == "VCALENDAR": |
65 | | - col._upload_all_nonatomic(items, suffix=".ics") |
66 | | - elif props.get("tag") == "VADDRESSBOOK": |
67 | | - col._upload_all_nonatomic(items, suffix=".vcf") |
| 53 | + try: |
| 54 | + with TemporaryDirectory(prefix=".Radicale.tmp-", dir=parent_dir |
| 55 | + ) as tmp_dir: |
| 56 | + # The temporary directory itself can't be renamed |
| 57 | + tmp_filesystem_path = os.path.join(tmp_dir, "collection") |
| 58 | + os.makedirs(tmp_filesystem_path) |
| 59 | + col = self._collection_class( |
| 60 | + cast(multifilesystem.Storage, self), |
| 61 | + pathutils.unstrip_path(sane_path, True), |
| 62 | + filesystem_path=tmp_filesystem_path) |
| 63 | + col.set_meta(props) |
| 64 | + if items is not None: |
| 65 | + if props.get("tag") == "VCALENDAR": |
| 66 | + col._upload_all_nonatomic(items, suffix=".ics") |
| 67 | + elif props.get("tag") == "VADDRESSBOOK": |
| 68 | + col._upload_all_nonatomic(items, suffix=".vcf") |
68 | 69 |
|
69 | | - if os.path.lexists(filesystem_path): |
70 | | - pathutils.rename_exchange(tmp_filesystem_path, filesystem_path) |
71 | | - else: |
72 | | - os.rename(tmp_filesystem_path, filesystem_path) |
73 | | - self._sync_directory(parent_dir) |
| 70 | + if os.path.lexists(filesystem_path): |
| 71 | + pathutils.rename_exchange(tmp_filesystem_path, filesystem_path) |
| 72 | + else: |
| 73 | + os.rename(tmp_filesystem_path, filesystem_path) |
| 74 | + self._sync_directory(parent_dir) |
| 75 | + except Exception as e: |
| 76 | + raise ValueError("Failed to create collection %r as %r %s" % |
| 77 | + (href, filesystem_path, e)) from e |
74 | 78 |
|
75 | 79 | return self._collection_class( |
76 | 80 | cast(multifilesystem.Storage, self), |
|
0 commit comments