Don't silently drop files

This commit is contained in:
Unrud 2016-06-10 00:05:09 +02:00 committed by Unrud
parent ef63865e31
commit e3d7d08eab

View File

@ -438,56 +438,56 @@ class Collection(BaseCollection):
def get(self, href): def get(self, href):
if not href: if not href:
return return None
href = href.strip("{}").replace("/", "_") href = href.strip("{}").replace("/", "_")
if is_safe_filesystem_path_component(href): if not is_safe_filesystem_path_component(href):
path = os.path.join(self._filesystem_path, href)
if os.path.isfile(path):
with open(path, encoding=self.storage_encoding) as fd:
text = fd.read()
last_modified = time.strftime(
"%a, %d %b %Y %H:%M:%S GMT",
time.gmtime(os.path.getmtime(path)))
return Item(self, vobject.readOne(text), href, last_modified)
else:
self.logger.debug( self.logger.debug(
"Can't tranlate name safely to filesystem, " "Can't tranlate name safely to filesystem: %s", href)
"skipping component: %s", href) return None
path = path_to_filesystem(self._filesystem_path, href)
if not os.path.isfile(path):
return None
with open(path, encoding=self.storage_encoding) as fd:
text = fd.read()
last_modified = time.strftime(
"%a, %d %b %Y %H:%M:%S GMT",
time.gmtime(os.path.getmtime(path)))
return Item(self, vobject.readOne(text), href, last_modified)
def has(self, href): def has(self, href):
return self.get(href) is not None return self.get(href) is not None
def upload(self, href, vobject_item): def upload(self, href, vobject_item):
# TODO: use returned object in code # TODO: use returned object in code
if is_safe_filesystem_path_component(href): if not is_safe_filesystem_path_component(href):
path = path_to_filesystem(self._filesystem_path, href) raise ValueError(
if not os.path.exists(path): "Can't tranlate name safely to filesystem: %s" % href)
item = Item(self, vobject_item, href) path = path_to_filesystem(self._filesystem_path, href)
with self._atomic_write(path) as fd: if os.path.exists(path):
fd.write(item.serialize()) raise ValueError("Component already exists: %s" % href)
return item item = Item(self, vobject_item, href)
else: with self._atomic_write(path) as fd:
self.logger.debug( fd.write(item.serialize())
"Can't tranlate name safely to filesystem, " return item
"skipping component: %s", href)
def update(self, href, vobject_item, etag=None): def update(self, href, vobject_item, etag=None):
# TODO: use etag in code and test it here # TODO: use etag in code and test it here
# TODO: use returned object in code # TODO: use returned object in code
if is_safe_filesystem_path_component(href): if not is_safe_filesystem_path_component(href):
path = path_to_filesystem(self._filesystem_path, href) raise ValueError(
if os.path.exists(path): "Can't tranlate name safely to filesystem: %s" % href)
with open(path, encoding=self.storage_encoding) as fd: path = path_to_filesystem(self._filesystem_path, href)
text = fd.read() if not os.path.isfile(path):
if not etag or etag == get_etag(text): raise ValueError("Component doesn't exist: %s" % href)
item = Item(self, vobject_item, href) with open(path, encoding=self.storage_encoding) as fd:
with self._atomic_write(path) as fd: text = fd.read()
fd.write(item.serialize()) if etag and etag != get_etag(text):
return item raise ValueError(
else: "ETag doesn't match: %s != %s" % (etag, get_etag(text)))
self.logger.debug( item = Item(self, vobject_item, href)
"Can't tranlate name safely to filesystem, " with self._atomic_write(path) as fd:
"skipping component: %s", href) fd.write(item.serialize())
return item
def delete(self, href=None, etag=None): def delete(self, href=None, etag=None):
# TODO: use etag in code and test it here # TODO: use etag in code and test it here
@ -499,20 +499,20 @@ class Collection(BaseCollection):
props_path = self._filesystem_path + ".props" props_path = self._filesystem_path + ".props"
if os.path.isfile(props_path): if os.path.isfile(props_path):
os.remove(props_path) os.remove(props_path)
return
elif is_safe_filesystem_path_component(href):
# Delete an item
path = path_to_filesystem(self._filesystem_path, href)
if os.path.isfile(path):
with open(path, encoding=self.storage_encoding) as fd:
text = fd.read()
if not etag or etag == get_etag(text):
os.remove(path)
return
else: else:
self.logger.debug( # Delete an item
"Can't tranlate name safely to filesystem, " if not is_safe_filesystem_path_component(href):
"skipping component: %s", href) raise ValueError(
"Can't tranlate name safely to filesystem: %s" % href)
path = path_to_filesystem(self._filesystem_path, href)
if not os.path.isfile(path):
raise ValueError("Component doesn't exist: %s" % href)
with open(path, encoding=self.storage_encoding) as fd:
text = fd.read()
if etag and etag != get_etag(text):
raise ValueError(
"ETag doesn't match: %s != %s" % (etag, get_etag(text)))
os.remove(path)
def get_meta(self, key): def get_meta(self, key):
props_path = self._filesystem_path + ".props" props_path = self._filesystem_path + ".props"