[2.10] find module - stop dir traversal when depth is exceeded (#73808)
* find module - stop traversing directories with os.walk when depth is already exceeded (#73718)
(cherry picked from commit 8628c12f30
)
* Update tests since there are fewer prior tasks creating files/directories
This commit is contained in:
parent
b1d278a595
commit
ece31e1b51
3 changed files with 48 additions and 0 deletions
2
changelogs/fragments/73718-find-dir-depth-traversal.yml
Normal file
2
changelogs/fragments/73718-find-dir-depth-traversal.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- find module - Stop traversing directories past the requested depth. (https://github.com/ansible/ansible/issues/73627)
|
|
@ -413,6 +413,8 @@ def main():
|
||||||
wpath = npath.rstrip(os.path.sep) + os.path.sep
|
wpath = npath.rstrip(os.path.sep) + os.path.sep
|
||||||
depth = int(fsname.count(os.path.sep)) - int(wpath.count(os.path.sep)) + 1
|
depth = int(fsname.count(os.path.sep)) - int(wpath.count(os.path.sep)) + 1
|
||||||
if depth > params['depth']:
|
if depth > params['depth']:
|
||||||
|
# Empty the list used by os.walk to avoid traversing deeper unnecessarily
|
||||||
|
del(dirs[:])
|
||||||
continue
|
continue
|
||||||
if os.path.basename(fsname).startswith('.') and not params['hidden']:
|
if os.path.basename(fsname).startswith('.') and not params['hidden']:
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -67,6 +67,7 @@
|
||||||
- 'find_test0.msg is defined'
|
- 'find_test0.msg is defined'
|
||||||
- 'find_test0.matched == 8'
|
- 'find_test0.matched == 8'
|
||||||
- 'find_test0.files | length == 8'
|
- 'find_test0.files | length == 8'
|
||||||
|
- 'find_test0.examined == 16'
|
||||||
|
|
||||||
- name: find the xml and img files
|
- name: find the xml and img files
|
||||||
find:
|
find:
|
||||||
|
@ -95,3 +96,46 @@
|
||||||
- 'find_test2.matched == 1'
|
- 'find_test2.matched == 1'
|
||||||
- 'find_test2.files[0].pw_name is defined'
|
- 'find_test2.files[0].pw_name is defined'
|
||||||
- 'find_test2.files[0].gr_name is defined'
|
- 'find_test2.files[0].gr_name is defined'
|
||||||
|
|
||||||
|
- 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 == 16
|
||||||
|
- total_contents.examined == 16
|
||||||
|
|
||||||
|
- 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 == 6
|
||||||
|
# dir contents are considered until the depth exceeds the requested depth
|
||||||
|
# there are 6 files/directories in the requested depth and 4 that exceed it by 1
|
||||||
|
- contents_with_depth.examined == 10
|
||||||
|
|
||||||
|
- name: Find files with depth
|
||||||
|
find:
|
||||||
|
paths: "{{ output_dir_test }}"
|
||||||
|
depth: 2
|
||||||
|
recurse: yes
|
||||||
|
register: files_with_depth
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- files_with_depth.matched == 2
|
||||||
|
# dir contents are considered until the depth exceeds the requested depth
|
||||||
|
# there are 6 files/directories in the requested depth and 4 that exceed it by 1
|
||||||
|
- files_with_depth.examined == 10
|
||||||
|
|
Loading…
Reference in a new issue