Directory permssions comparison check will always report different

Created on 26 December 2022, over 1 year ago
Updated 30 January 2023, over 1 year ago

Problem/Motivation

In the file FileStorage.php, class FileStorage there is this check in the function createDirectory:
if (fileperms($directory) !== $mode) {
The problem is that fileperms() adds 040000 octal onto the permissions for a directory so with a string to compare like 0775 it will always show as different and therefore will always hit the file system.

Steps to reproduce

Perform any action in Drupal that causes this to be called (probably most things that create a directory) and watch it with a debugger or just checkout the PHP documentation on fileperms(), as it's obvious just from the documentation that this isn't going to work.

Proposed resolution

Here is the blurb from the function I created that cleans up the permissions before doing a comparison which I think nicely sums up what I propose.

Ensures we have sensible permission strings to compare.

Decimal numbers are converted to octal. It is assumed that any non-zero
number in the 5th position should not be there in octal form and is
removed. This will make a result from fileperms() compatible with normal
permissions because it adds 040000 (seen once converted to octal) to
specify a directory which we don't want when doing comparisons of
permissions strings.

We allow the option to ignore special permissions bits which will all be in
the 4th octal place because most of the time this is something an admin
should be able to change without Drupal being aware.

Once in octal form we make sure there are enough leading zeros for a
string of 4 characters minimum but no more than one leading zero if it goes
past 4 characters. A leading zero is necessary for PHP to know that it is
an octal string.

The final string should be either 4 or 5 characters long depending on
whether special bits are set namely: sticky, SUID, SGID; unless we are
ignoring them.

This formatting is so that we can do a useful string comparison of
permissions between default or user defined which will typically be like:
0775, 0755 and what fileperms() returns which will typically be 040775
(in octal) for a directory but would be fine for a normal file.

Note: We could have done a conversion to a number instead of string however
this format is what we are used to seeing when it comes to
permissions and so makes it a little easier to debug when necessary.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
File systemΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States Reg

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

Production build 0.69.0 2024