Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions libcloud/storage/drivers/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand Down
49 changes: 49 additions & 0 deletions libcloud/test/storage/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down