Skip to content
Open
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
38 changes: 37 additions & 1 deletion src/Flysystem/Plugin/Stat.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,38 @@ protected function getWithMetadata($path, array $ignore)
return $metadata;
}

/**
* Normalize a permissions string.
*
* @param string $permissions

Choose a reason for hiding this comment

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

Suggested change
* @param string $permissions
* @param string $permissions A permissions string, such as '644' or
* 'drw-rw-r--'

*
* @return int

Choose a reason for hiding this comment

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

Suggested change
* @return int
* @return int The numeric version of the permissions.

*/
protected function normalizePermissions($permissions)
{
if (is_numeric($permissions)) {
return $permissions & 0777;
}

// remove the type identifier

Choose a reason for hiding this comment

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

Suggested change
// remove the type identifier
// Remove the type identifier.

$permissions = substr($permissions, 1);

Choose a reason for hiding this comment

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

Suggested change
$permissions = substr($permissions, 1);
$permissions = substr($permissions, -9);

This will work with or without the initial d or - for regular file vs. directory. I think this also makes it clear that we expect 9 characters.

I considered padding with - characters to ensure 9 characters, but I am not sure when that would be likely to make a difference. Pad on the left or the right? Maybe it is a bad idea to fix problems that no one has.

Copy link

@hexus hexus Feb 11, 2021

Choose a reason for hiding this comment

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

I feel like the original code is closer to what the comment says it's trying to do, but indeed -9 is clearer for the actual intent here: extracting the permission groups in isolation.

Padding would make it mildly safer to use with the code below it, and if you're using substr from the right then I'd say it makes sense to pad the left, if at all.

Suggested change
$permissions = substr($permissions, 1);
$permissions = substr(str_pad($permissions, 9, '-', STR_PAD_LEFT), -9);

The comment could be updated to:

// Extract the permission groups.

The trickiest thing is how vague the file metadata is with Flysystem, but I suppose it has to be (for v1.x at least 😉).


// map the string rights to the numeric counterparts

Choose a reason for hiding this comment

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

Suggested change
// map the string rights to the numeric counterparts
// Map the string rights to the numeric counterparts.

$map = ['-' => '0', 'r' => '4', 'w' => '2', 'x' => '1'];
$permissions = strtr($permissions, $map);

// split up the permission groups

Choose a reason for hiding this comment

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

Suggested change
// split up the permission groups
// Split up the permission groups.

$parts = str_split($permissions, 3);

// convert the groups

Choose a reason for hiding this comment

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

Suggested change
// convert the groups
// Convert the groups.

$mapper = function ($part) {
return array_sum(str_split($part));
};

// converts to decimal number

Choose a reason for hiding this comment

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

Suggested change
// converts to decimal number
// Converts to decimal number.

return octdec(implode('', array_map($mapper, $parts)));
}

/**
* Merges the available metadata from Filesystem::getMetadata().
*
Expand All @@ -154,7 +186,11 @@ protected function mergeMeta(array $metadata)
$ret['gid'] = $this->uid->getGid();

$ret['mode'] = $metadata['type'] === 'dir' ? 040000 : 0100000;
$ret['mode'] += $this->permissions[$metadata['type']][$metadata['visibility']];
$visibility = $metadata['visibility'];
if ($visibility != AdapterInterface::VISIBILITY_PUBLIC && $visibility != AdapterInterface::VISIBILITY_PRIVATE) {
$visibility = $this->normalizePermissions($visibility) & 0044 ? AdapterInterface::VISIBILITY_PUBLIC : AdapterInterface::VISIBILITY_PRIVATE;
}
$ret['mode'] += $this->permissions[$metadata['type']][$visibility];

if (isset($metadata['size'])) {
$ret['size'] = (int) $metadata['size'];
Expand Down