Skip to content

Commit 45780e6

Browse files
committed
Fix max_depth not respected in file.directory state
Fixes: #55306
1 parent 984ecb1 commit 45780e6

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

changelog/55306.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed max_depth not respected in file.directory state

salt/states/file.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3528,12 +3528,21 @@ def _depth_limited_walk(top, max_depth=None):
35283528
Walk the directory tree under root up till reaching max_depth.
35293529
With max_depth=None (default), do not limit depth.
35303530
"""
3531+
top_depth = top.rstrip(os.path.sep).count(os.path.sep)
3532+
35313533
for root, dirs, files in salt.utils.path.os_walk(top):
3534+
rel_depth = root.rstrip(os.path.sep).count(os.path.sep) - top_depth
3535+
35323536
if max_depth is not None:
3533-
rel_depth = root.count(os.path.sep) - top.count(os.path.sep)
35343537
if rel_depth >= max_depth:
3535-
del dirs[:]
3536-
yield (str(root), list(dirs), list(files))
3538+
# This clear does nothing because os_walk returns copies of data from os.walk,
3539+
# so modifying this list has no effect on recursion.
3540+
# If os_walk ever returns the "real" dirs list, this will speed up execution
3541+
# by preventing recursion into directories deeper than max_depth.
3542+
dirs.clear()
3543+
continue
3544+
3545+
yield (str(root), dirs.copy(), files.copy())
35373546

35383547

35393548
def directory(

tests/pytests/functional/states/file/test_directory.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,22 @@ def _get_oct_mode(name):
8686
return salt.utils.files.normalize_mode(oct(name.stat().st_mode & 0o777))
8787

8888
top = tmp_path / "top_dir"
89+
top_file = top / "top_file"
8990
sub = top / "sub_dir"
91+
sub_file = sub / "sub_file"
9092
subsub = sub / "sub_sub_dir"
93+
subsub_file = subsub / "subsub_file"
9194
dirs = [top, sub, subsub]
95+
files = [top_file, sub_file, subsub_file]
9296

93-
initial_mode = "0111"
97+
initial_mode = "0110"
9498
changed_mode = "0555"
9599

96-
# Check that we are not just running photon but the kernel matches. This
97-
# check should fail if we are in a photon container running on and os other
98-
# than photon.
99-
if salt.utils.platform.is_photonos() and _kernel_check("photon"):
100-
initial_modes = {
101-
0: {sub: "0750", subsub: "0110"},
102-
1: {sub: "0110", subsub: "0110"},
103-
2: {sub: "0110", subsub: "0110"},
104-
}
105-
else:
106-
initial_modes = {
107-
0: {sub: "0755", subsub: "0111"},
108-
1: {sub: "0111", subsub: "0111"},
109-
2: {sub: "0111", subsub: "0111"},
110-
}
100+
for folder in dirs:
101+
folder.mkdir(mode=int(initial_mode, 8))
111102

112-
subsub.mkdir(mode=int(initial_mode, 8), exist_ok=True, parents=True)
103+
for _file in files:
104+
_file.touch(mode=int(initial_mode, 8))
113105

114106
for depth in range(3):
115107
ret = file.directory(
@@ -121,9 +113,12 @@ def _get_oct_mode(name):
121113
assert ret.result is True
122114
for changed_dir in dirs[0 : depth + 1]:
123115
assert changed_mode == _get_oct_mode(changed_dir)
116+
for changed_file in files[0:depth]:
117+
assert changed_mode == _get_oct_mode(changed_file)
124118
for untouched_dir in dirs[depth + 1 :]:
125-
_mode = initial_modes[depth][untouched_dir]
126-
assert _mode == _get_oct_mode(untouched_dir)
119+
assert initial_mode == _get_oct_mode(untouched_dir)
120+
for untouched_file in files[depth:]:
121+
assert initial_mode == _get_oct_mode(untouched_file)
127122

128123

129124
@pytest.mark.skip_on_windows

0 commit comments

Comments
 (0)