From 46ea7243757027aa317dffa01b25910298b548be Mon Sep 17 00:00:00 2001 From: Ishu Patel <258577714+ishu7w@users.noreply.github.com> Date: Fri, 27 Nov 2026 12:52:33 +0530 Subject: [PATCH] Filter local storage objects before reading metadata --- libcloud/storage/drivers/local.py | 19 ++++++----- libcloud/test/storage/test_local.py | 49 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/libcloud/storage/drivers/local.py b/libcloud/storage/drivers/local.py index b1c5ecfe49..f0dd30c841 100644 --- a/libcloud/storage/drivers/local.py +++ b/libcloud/storage/drivers/local.py @@ -280,9 +280,14 @@ def iterate_containers(self): continue yield self._make_container(container_name) - def _get_objects(self, container): + def _get_objects(self, container, prefix=None): """ - Recursively iterate through the file-system and return the object names + Recursively iterate through the file-system and return matching objects. + + :param container: Container instance. + :type container: :class:`Container` + :param prefix: Filter objects starting with this object-name prefix. + :type prefix: ``str`` """ cpath = self.get_container_cdn_url(container, check=True) @@ -296,7 +301,9 @@ def _get_objects(self, container): for name in files: full_path = os.path.join(folder, name) object_name = relpath(full_path, start=cpath) - yield self._make_object(container, object_name) + # Filter names before reading metadata and constructing objects. + if prefix is None or object_name.startswith(prefix): + yield self._make_object(container, object_name) def iterate_container_objects(self, container, prefix=None, ex_prefix=None): """ @@ -316,10 +323,8 @@ def iterate_container_objects(self, container, prefix=None, ex_prefix=None): """ prefix = self._normalize_prefix_argument(prefix, ex_prefix) - objects = self._get_objects(container) - objects = sorted(objects, key=lambda o: o.name) - - return self._filter_listed_container_objects(objects, prefix) + objects = self._get_objects(container, prefix=prefix) + return iter(sorted(objects, key=lambda o: o.name)) def get_container(self, container_name): """ diff --git a/libcloud/test/storage/test_local.py b/libcloud/test/storage/test_local.py index 1c9b61d489..da15cef75c 100644 --- a/libcloud/test/storage/test_local.py +++ b/libcloud/test/storage/test_local.py @@ -181,6 +181,55 @@ def test_containers_success(self): for container in containers: self.driver.delete_container(container) + def test_prefix_listing_constructs_only_matching_objects(self): + container = self.driver.create_container("prefix-filter") + names = [ + "foo1", + "foo-bar", + "foobar/child", + "foo/child", + "foo/deep/file", + "other/child", + "Afoo", + ".lock/ignored", + ".hash/ignored", + ] + for name in names: + path = os.path.join(self.key, container.name, *name.split("/")) + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, "wb") as output: + output.write(b"data") + + valid_names = [ + os.path.join(*name.split("/")) + for name in names + if not name.startswith((".lock/", ".hash/")) + ] + prefixes = [ + None, + "", + "foo", + "foo1", + "foob", + "foo-", + "missing", + os.path.join("foo", ""), + os.path.join("foo", "de"), + ] + for prefix in prefixes: + with self.subTest(prefix=prefix): + expected = sorted( + name for name in valid_names if prefix is None or name.startswith(prefix) + ) + with mock.patch.object( + self.driver, "_make_object", wraps=self.driver._make_object + ) as make_object: + result = list(self.driver.iterate_container_objects(container, prefix=prefix)) + self.assertEqual([obj.name for obj in result], expected) + self.assertEqual( + sorted(call.args[1] for call in make_object.call_args_list), expected + ) + def test_objects_success(self): tmppath = self.make_tmp_file()