coreutils
coreutils copied to clipboard
mkdir -p fails on existing directories that the current user doesn't have permission to access
I stumbled upon this from a weird ansible error.
$ ansible -m ping <redacted-hostname> --become-user unprivileged-user
<redacted-hostname> | UNREACHABLE! => {
"changed": false,
"msg": "Failed to create temporary directory. In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in \"/tmp\", for more error information use -vvv. Failed command was: ( umask 77 && mkdir -p \"` echo /var/tmp `\"&& mkdir \"` echo /var/tmp/ansible-tmp-1720284467.1994517-229738-239118036920172 `\" && echo ansible-tmp-1720284467.1994517-229738-239118036920172=\"` echo /var/tmp/ansible-tmp-1720284467.1994517-229738-239118036920172 `\" ), exited with result 1",
"unreachable": true
}
Turns out that this error went away when replacing mkdir from uutils with the original GNU mkdir.
This can be reproduced very easily:
- Run
mkdir -p /rootas a non-root user
With the GNU Coreutils mkdir (version 9.4), this works without any issues.
With the uutils mkdir, it prints mkdir: cannot set permissions '/root': Permission denied with exit status 1.
Reproducable on both 8cc129cff89523ea0f26ca23e3ca3de660041968 as well as 0.0.27.
interesting, it might be consider as a bug/weakness of GNU coreutils
The problem here is that mkdir -p is trying to change the mode of the latest created component even if it exists already. This causes this bug, when the directory does not exist but is not modifiable, but it also causes mkdir -p /a/b to set the mode of b even if it existed prior to the command.
I'll propose a PR to fix this.