Skip to content

files.directory doesn't ensure recursive ownership (add op arg) #1435

@missytake

Description

@missytake

Describe the bug

In https://github.com/pyinfra-dev/pyinfra/blob/3.x/pyinfra/operations/files.py#L1609 only ownership of the top-level directory is checked, not of the subdirectories and files, also if you specify recursive=True.

To Reproduce

Steps to reproduce the behavior, please include where possible:

Create this directory structure:

$ ls -la test/
total 8
drwxrwxr-x  2 user     user 4096 Aug 16 09:38 .
drwxrwxr-x 15 user     user 4096 Aug 16 09:38 ..
-rw-rw-r--  1 www-data root    0 Aug 16 09:38 file

And apply the following operation to it:

files.directory(
  path="test",
  user="user",
  group="user",
  recursive=True,
)

Expected behavior

If you call files.directory with recursive=True and the user or group argument, you expect that it ensures that the directory and its subdirectories and all files in them are ensured to be owned by the specified user/group.

Recursive ownership could be checked with a simple find $path ! -user $user -group $group; for the largest directory we have (500k maildir directories), this check took ~13 minutes, compared to just running a chown, which took ~48 minutes.
This can be a performance nightmare of course; maybe instead of fixing this, the current behavior should simply be documented.
Maybe there are also faster options of checking recursive ownership I don't know about.

Thanks @cliffmccarthy for finding this: chatmail/relay#609 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    operationsIssues with operations.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions