Skip to content

fix(operations.files): don't re-apply a symbolic directory mode that already matches - #2118

Open
knjoroge wants to merge 2 commits into
pyinfra-dev:3.xfrom
knjoroge:fix/directory-symbolic-mode
Open

knjoroge wants to merge 2 commits into
pyinfra-dev:3.xfrom
knjoroge:fix/directory-symbolic-mode

Conversation

@knjoroge

@knjoroge knjoroge commented Nov 26, 2026 •

Copy link
Copy Markdown

Fixes #2084

What

files.directory(mode="u+rwx,g+rwx,o-rwx") ran chmod on every deploy, even when the directory already had that mode.

Why

The directory's current mode is a number (770), but a symbolic mode stays as text, so 770 != "u+rwx,g+rwx,o-rwx" was always true.

Changes

  • New apply_symbolic_mode() in operations/util/files.py. It works out the number a symbolic mode would give the current directory, e.g. 770 + "o+rwx" → 777.
  • files.directory skips chmod when that number already matches. Otherwise it still sends the symbolic mode as-is, so nothing else changes.
  • With recursive=True it always runs chmod -R like before, because files inside could still need it.
  • Anything it can't work out for sure still runs chmod like before: no u/g/o/a (e.g. +x, which depends on the umask), X/s/t, or a directory with setuid/setgid/sticky bits.
  • Tests: unit tests for the helper, plus 4 fixtures (match → noop, match with recursive → chmod, no match → chmod, +x → chmod).

Rebased on #2117.

I also compared apply_symbolic_mode() against the real chmod (macOS) on 3000 random modes, and they all matched.

  • Pull request is based on the default branch (3.x at this time)
  • Pull request includes tests for any new/updated operations/facts
  • Pull request includes documentation for any new/updated operations/facts (no docs change needed)
  • Tests pass (see scripts/dev-test.sh)
  • Type checking & code style passes (see scripts/dev-lint.sh)
  • Pull request title follows the conventional commits format

AI disclosure: I used Claude Code to write the code and tests, and I reviewed and understand all of it.

@wowi42 wowi42 added bug Label for all kind of bugs. operations Issues with operations. labels Nov 26, 2026

@Fizzadar Fizzadar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One problem with recursive inline, otherwise this looks good!

Comment thread src/pyinfra/operations/files.py Outdated
Comment on lines +1970 to +1972
# Skip a symbolic mode (eg "g+w") that wouldn't change the directory
if isinstance(mode, str) and apply_symbolic_mode(info["mode"], mode) == info["mode"]:
mode = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With recursive=True this skips the chmod when the top directory already matches, but children could still be wrong. Should we only skip when recursive is false? Also this block overlaps with #2117, so this needs a rebase now 🙏

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@Fizzadar nice catch, thanks!
It now only skips when recursive is false and there is a test for the recursive case. I also rebased

@knjoroge
knjoroge force-pushed the fix/directory-symbolic-mode branch from 7ff5975 to 89ce7fc Compare November 26, 2026 20:48
@knjoroge
knjoroge requested a review from Fizzadar November 26, 2026 20:49

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Label for all kind of bugs. operations Issues with operations.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Operation files.directory() always applies mode recursively if mode is given in its symbolic form.

3 participants