From 39d38f36a556b257573a335483b9b91dfdcafcb6 Mon Sep 17 00:00:00 2001 From: Unrud Date: Mon, 1 Aug 2016 13:55:08 +0200 Subject: [PATCH] Check if file name already exists The chances are pretty low but maybe the RNG is bad or something --- radicale/storage.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/radicale/storage.py b/radicale/storage.py index 446d7bb..9e771a6 100644 --- a/radicale/storage.py +++ b/radicale/storage.py @@ -25,6 +25,7 @@ entry. """ +import errno import json import os import posixpath @@ -394,6 +395,14 @@ class Collection(BaseCollection): @classmethod def create_collection(cls, href, collection=None, tag=None): + def find_available_file_name(exists_fn): + # Prevent infinite loop + for _ in range(10000): + file_name = hex(getrandbits(32))[2:] + if not exists_fn(file_name): + return file_name + raise FileExistsError(errno.EEXIST, "No usable file name found") + folder = os.path.expanduser( cls.configuration.get("storage", "filesystem_folder")) path = path_to_filesystem(folder, href) @@ -421,15 +430,14 @@ class Collection(BaseCollection): new_collection = vobject.iCalendar() for item in items: new_collection.add(item) - file_name = hex(getrandbits(32))[2:] - self.upload(file_name, new_collection) + self.upload(find_available_file_name(self.has), + new_collection) elif tag == "VCARD": self.set_meta("tag", "VADDRESSBOOK") if collection: for card in collection: - file_name = hex(getrandbits(32))[2:] - self.upload(file_name, card) + self.upload(find_available_file_name(self.has), card) return self