فهرست منبع

use tempfile to delete collection

Unrud 9 سال پیش
والد
کامیت
f4eb143ba8
1فایلهای تغییر یافته به همراه7 افزوده شده و 11 حذف شده
  1. 7 11
      radicale/storage.py

+ 7 - 11
radicale/storage.py

@@ -580,21 +580,17 @@ class Collection(BaseCollection):
         if href is None:
             # Delete the collection
             if os.path.isdir(self._filesystem_path):
+                parent_dir = os.path.dirname(self._filesystem_path)
                 try:
                     os.rmdir(self._filesystem_path)
                 except OSError:
-                    while True:
-                        tmp_filesystem_path = os.path.join(
-                            os.path.dirname(self._filesystem_path),
-                            ".Radicale.tmp-" + hex(getrandbits(32))[2:])
-                        if not os.path.exists(tmp_filesystem_path):
-                            break
-                    os.rename(self._filesystem_path, tmp_filesystem_path)
-                    sync_directory(os.path.dirname(self._filesystem_path))
-                    # Deferred because it might take a long time
-                    shutil.rmtree(tmp_filesystem_path)
+                    with TemporaryDirectory(prefix=".Radicale.tmp-",
+                                            dir=parent_dir) as tmp_dir:
+                        os.rename(self._filesystem_path, os.path.join(
+                            tmp_dir, os.path.basename(self._filesystem_path)))
+                        sync_directory(parent_dir)
                 else:
-                    sync_directory(os.path.dirname(self._filesystem_path))
+                    sync_directory(parent_dir)
         else:
             # Delete an item
             if not is_safe_filesystem_path_component(href):