Bug 1977: Strip trailing /
Ensure no files or directories are stored with a trailing /
Ensure file/directory lookups happen without trailing /
Change-Id: Id34438495170756883a4adeac3b6970e4f91b6b6
Signed-off-by: beierlm <mark.beierl@canonical.com>
diff --git a/osm_common/fsmongo.py b/osm_common/fsmongo.py
index 487eaf8..a057e37 100644
--- a/osm_common/fsmongo.py
+++ b/osm_common/fsmongo.py
@@ -294,6 +294,7 @@
:param folder:
:return: None or raises an exception
"""
+ folder = folder.rstrip("/")
try:
self.fs.upload_from_stream(folder, BytesIO(), metadata={"type": "dir"})
except errors.FileExists: # make it idempotent
@@ -308,6 +309,9 @@
:param dst: destination directory
:return: None or raises and exception
"""
+ dst = dst.rstrip("/")
+ src = src.rstrip("/")
+
try:
dst_cursor = self.fs.find(
{"filename": {"$regex": "^{}(/|$)".format(dst)}}, no_cursor_timeout=True
@@ -333,6 +337,7 @@
:return: True, False
"""
f = storage if isinstance(storage, str) else "/".join(storage)
+ f = f.rstrip("/")
cursor = self.fs.find({"filename": f})
@@ -344,7 +349,7 @@
"Multiple files found", http_code=HTTPStatus.INTERNAL_SERVER_ERROR
)
- print(requested_file.metadata)
+ self.logger.debug("Entry {} metadata {}".format(f, requested_file.metadata))
# if no special mode is required just check it does exists
if not mode:
@@ -365,6 +370,7 @@
:return: file size
"""
f = storage if isinstance(storage, str) else "/".join(storage)
+ f = f.rstrip("/")
cursor = self.fs.find({"filename": f})
@@ -386,6 +392,7 @@
:return: None
"""
f = path if isinstance(path, str) else "/".join(path)
+ f = f.rstrip("/")
if type(compressed_object) is tarfile.TarFile:
for member in compressed_object.getmembers():
@@ -404,7 +411,9 @@
file_type = "dir"
metadata = {"type": file_type, "permissions": member.mode}
+ member.name = member.name.rstrip("/")
+ self.logger.debug("Uploading {}".format(member.name))
self.fs.upload_from_stream(
f + "/" + member.name, stream, metadata=metadata
)
@@ -423,9 +432,9 @@
file_type = "file"
metadata = {"type": file_type}
+ member.filename = member.filename.rstrip("/")
- print("Now uploading...")
- print(f + "/" + member.filename)
+ self.logger.debug("Uploading {}".format(member.filename))
self.fs.upload_from_stream(
f + "/" + member.filename, stream, metadata=metadata
)
@@ -442,6 +451,7 @@
"""
try:
f = storage if isinstance(storage, str) else "/".join(storage)
+ f = f.rstrip("/")
if "b" in mode:
return GridByteStream(f, self.fs, mode)
@@ -464,6 +474,7 @@
"""
try:
f = storage if isinstance(storage, str) else "/".join(storage)
+ f = f.rstrip("/")
files = []
dir_cursor = self.fs.find({"filename": f})
@@ -506,6 +517,7 @@
"""
try:
f = storage if isinstance(storage, str) else "/".join(storage)
+ f = f.rstrip("/")
file_cursor = self.fs.find({"filename": f})
found = False