Skip to content

Commit ad776f1

Browse files
authored
Merge pull request #2 from ms-slasher13/undefined_index_mergeMeta
Fixed undefined index when merging file metadata
2 parents 7b3778e + 3f90145 commit ad776f1

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/Flysystem/Plugin/Stat.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,38 @@ protected function getWithMetadata($path, array $ignore)
139139
return $metadata;
140140
}
141141

142+
/**
143+
* Normalize a permissions string.
144+
*
145+
* @param string $permissions
146+
*
147+
* @return int
148+
*/
149+
protected function normalizePermissions($permissions)
150+
{
151+
if (is_numeric($permissions)) {
152+
return $permissions & 0777;
153+
}
154+
155+
// remove the type identifier
156+
$permissions = substr($permissions, 1);
157+
158+
// map the string rights to the numeric counterparts
159+
$map = ['-' => '0', 'r' => '4', 'w' => '2', 'x' => '1'];
160+
$permissions = strtr($permissions, $map);
161+
162+
// split up the permission groups
163+
$parts = str_split($permissions, 3);
164+
165+
// convert the groups
166+
$mapper = function ($part) {
167+
return array_sum(str_split($part));
168+
};
169+
170+
// converts to decimal number
171+
return octdec(implode('', array_map($mapper, $parts)));
172+
}
173+
142174
/**
143175
* Merges the available metadata from Filesystem::getMetadata().
144176
*
@@ -154,7 +186,11 @@ protected function mergeMeta(array $metadata)
154186
$ret['gid'] = $this->uid->getGid();
155187

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

159195
if (isset($metadata['size'])) {
160196
$ret['size'] = (int) $metadata['size'];

0 commit comments

Comments
 (0)