Check if file name already exists

The chances are pretty low but maybe the RNG is bad or something
This commit is contained in:
Unrud 2016-08-01 13:55:08 +02:00
parent 6bfdcbafec
commit 39d38f36a5

View File

@ -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