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

[3006.x] fix: file.directory state children_only kwarg did not work #64497

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/64497.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed an issue in the ``file.directory`` state where the ``children_only`` keyword
argument was not being respected.
12 changes: 8 additions & 4 deletions salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ def _check_directory(
exclude_pat=None,
max_depth=None,
follow_symlinks=False,
children_only=False,
):
"""
Check what changes need to be made on a directory
Expand Down Expand Up @@ -792,10 +793,12 @@ def _check_directory(
)
if fchange:
changes[path] = fchange
# Recurse skips root (we always do dirs, not root), so always check root:
fchange = _check_dir_meta(name, user, group, dir_mode, follow_symlinks)
if fchange:
changes[name] = fchange
# Recurse skips root (we always do dirs, not root), so check root unless
# children_only is specified:
if not children_only:
fchange = _check_dir_meta(name, user, group, dir_mode, follow_symlinks)
if fchange:
changes[name] = fchange
if clean:
keep = _gen_keep_files(name, require, walk_d)

Expand Down Expand Up @@ -3954,6 +3957,7 @@ def directory(
exclude_pat,
max_depth,
follow_symlinks,
children_only,
)

if tchanges:
Expand Down
42 changes: 42 additions & 0 deletions tests/pytests/functional/states/file/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,48 @@ def _get_oct_mode(name):
assert _mode == _get_oct_mode(untouched_dir)


@pytest.mark.skip_on_windows
def test_directory_children_only(file, tmp_path):
"""
file.directory with children_only=True
"""

name = tmp_path / "directory_children_only_dir"
name.mkdir(0o0700)

strayfile = name / "strayfile"
strayfile.touch()
os.chmod(strayfile, 0o700)

straydir = name / "straydir"
straydir.mkdir(0o0700)

# none of the children nor parent are currently set to the correct mode
ret = file.directory(
name=str(name),
file_mode="0644",
dir_mode="0755",
recurse=["mode"],
children_only=True,
)
assert ret.result is True

# Assert parent directory's mode remains unchanged
assert (
oct(name.stat().st_mode)[-3:] == "700"
), f"Expected mode 700 for {name}, got {oct(name.stat().st_mode)[-3:]}"

# Assert child file's mode is changed
assert (
oct(strayfile.stat().st_mode)[-3:] == "644"
), f"Expected mode 644 for {strayfile}, got {oct(strayfile.stat().st_mode)[-3:]}"

# Assert child directory's mode is changed
assert (
oct(straydir.stat().st_mode)[-3:] == "755"
), f"Expected mode 755 for {straydir}, got {oct(straydir.stat().st_mode)[-3:]}"


def test_directory_clean(file, tmp_path):
"""
file.directory with clean=True
Expand Down
Loading