From 40f57c87816f6e3e99f495b4e6f8b5abe8cce7a9 Mon Sep 17 00:00:00 2001 From: beierlm Date: Thu, 14 Apr 2022 14:45:07 -0400 Subject: [PATCH] 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 --- osm_common/fsmongo.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) 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 @@ class FsMongo(FsBase): :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 @@ class FsMongo(FsBase): :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 @@ class FsMongo(FsBase): :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 @@ class FsMongo(FsBase): "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 @@ class FsMongo(FsBase): :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 @@ class FsMongo(FsBase): :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 @@ class FsMongo(FsBase): 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 @@ class FsMongo(FsBase): 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 @@ class FsMongo(FsBase): """ 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 @@ class FsMongo(FsBase): """ 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 @@ class FsMongo(FsBase): """ try: f = storage if isinstance(storage, str) else "/".join(storage) + f = f.rstrip("/") file_cursor = self.fs.find({"filename": f}) found = False -- 2.25.1