Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find module - stop dir traversal when depth is exceeded #73718

Merged
merged 1 commit into from Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/73718-find-dir-depth-traversal.yml
@@ -0,0 +1,2 @@
bugfixes:
- find module - Stop traversing directories past the requested depth. (https://github.com/ansible/ansible/issues/73627)
2 changes: 2 additions & 0 deletions lib/ansible/modules/find.py
Expand Up @@ -436,6 +436,8 @@ def main():
wpath = npath.rstrip(os.path.sep) + os.path.sep
depth = int(fsname.count(os.path.sep)) - int(wpath.count(os.path.sep)) + 1
if depth > params['depth']:
# Empty the list used by os.walk to avoid traversing deeper unnecessarily
del(dirs[:])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python documentation says that del dirs[:] is equivalent to dirs.clear(). Just out of curiosity is there a reason to use the former? Is it faster maybe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear() does not seem to exist in py2.7

continue
if os.path.basename(fsname).startswith('.') and not params['hidden']:
continue
Expand Down
44 changes: 44 additions & 0 deletions test/integration/targets/find/tasks/main.yml
Expand Up @@ -71,6 +71,7 @@
- 'find_test0.msg is defined'
- 'find_test0.matched == 8'
- 'find_test0.files | length == 8'
- 'find_test0.examined == 16'

- name: find the xml and img files
find:
Expand Down Expand Up @@ -207,3 +208,46 @@
that:
- failed_path.files == []
- failed_path.msg.startswith("Skipped '{{mypath}}' path due to this access issue")

- name: test number of examined directories/files
block:
- name: Get all files/directories in the path
find:
paths: "{{ output_dir_test }}"
recurse: yes
file_type: any
register: total_contents

- assert:
that:
- total_contents.matched == 18
- total_contents.examined == 18

- name: Get files and directories with depth
find:
paths: "{{ output_dir_test }}"
recurse: yes
file_type: any
depth: 2
register: contents_with_depth

- assert:
that:
- contents_with_depth.matched == 8
# dir contents are considered until the depth exceeds the requested depth
# there are 8 files/directories in the requested depth and 4 that exceed it by 1
- contents_with_depth.examined == 12

- name: Find files with depth
find:
paths: "{{ output_dir_test }}"
depth: 2
recurse: yes
register: files_with_depth

- assert:
that:
- files_with_depth.matched == 4
# dir contents are considered until the depth exceeds the requested depth
# there are 8 files/directories in the requested depth and 4 that exceed it by 1
- files_with_depth.examined == 12