Skip to content

[Filesystem] chown and chgrp should also accept int as owner and group #35356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,11 @@ public function chmod($files, int $mode, int $umask = 0000, bool $recursive = fa
* Change the owner of an array of files or directories.
*
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change owner
* @param string|int $user A user name or number
*
* @throws IOException When the change fails
*/
public function chown($files, string $user, bool $recursive = false)
public function chown($files, $user, bool $recursive = false)
{
foreach ($this->toIterable($files) as $file) {
if ($recursive && is_dir($file) && !is_link($file)) {
Expand All @@ -232,10 +233,11 @@ public function chown($files, string $user, bool $recursive = false)
* Change the group of an array of files or directories.
*
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group
* @param string|int $group A group name or number
*
* @throws IOException When the change fails
*/
public function chgrp($files, string $group, bool $recursive = false)
public function chgrp($files, $group, bool $recursive = false)
{
foreach ($this->toIterable($files) as $file) {
if ($recursive && is_dir($file) && !is_link($file)) {
Expand Down
66 changes: 62 additions & 4 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public function testChmodChangesZeroModeOnSubdirectoriesOnRecursive()
$this->assertFilePermissions(753, $subdirectory);
}

public function testChown()
public function testChownByName()
{
$this->markAsSkippedIfPosixIsMissing();

Expand All @@ -534,7 +534,20 @@ public function testChown()
$this->assertSame($owner, $this->getFileOwner($dir));
}

public function testChownRecursive()
public function testChownById()
{
$this->markAsSkippedIfPosixIsMissing();

$dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
mkdir($dir);

$ownerId = $this->getFileOwnerId($dir);
$this->filesystem->chown($dir, $ownerId);

$this->assertSame($ownerId, $this->getFileOwnerId($dir));
}

public function testChownRecursiveByName()
{
$this->markAsSkippedIfPosixIsMissing();

Expand All @@ -549,6 +562,21 @@ public function testChownRecursive()
$this->assertSame($owner, $this->getFileOwner($file));
}

public function testChownRecursiveById()
{
$this->markAsSkippedIfPosixIsMissing();

$dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
mkdir($dir);
$file = $dir.\DIRECTORY_SEPARATOR.'file';
touch($file);

$ownerId = $this->getFileOwnerId($dir);
$this->filesystem->chown($dir, $ownerId, true);

$this->assertSame($ownerId, $this->getFileOwnerId($file));
}

public function testChownSymlink()
{
$this->markAsSkippedIfSymlinkIsMissing();
Expand Down Expand Up @@ -624,7 +652,7 @@ public function testChownFail()
$this->filesystem->chown($dir, 'user'.time().mt_rand(1000, 9999));
}

public function testChgrp()
public function testChgrpByName()
{
$this->markAsSkippedIfPosixIsMissing();

Expand All @@ -637,6 +665,19 @@ public function testChgrp()
$this->assertSame($group, $this->getFileGroup($dir));
}

public function testChgrpById()
{
$this->markAsSkippedIfPosixIsMissing();

$dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
mkdir($dir);

$groupId = $this->getFileGroupId($dir);
$this->filesystem->chgrp($dir, $groupId);

$this->assertSame($groupId, $this->getFileGroupId($dir));
}

public function testChgrpRecursive()
{
$this->markAsSkippedIfPosixIsMissing();
Expand All @@ -652,7 +693,7 @@ public function testChgrpRecursive()
$this->assertSame($group, $this->getFileGroup($file));
}

public function testChgrpSymlink()
public function testChgrpSymlinkByName()
{
$this->markAsSkippedIfSymlinkIsMissing();

Expand All @@ -669,6 +710,23 @@ public function testChgrpSymlink()
$this->assertSame($group, $this->getFileGroup($link));
}

public function testChgrpSymlinkById()
{
$this->markAsSkippedIfSymlinkIsMissing();
Copy link
Member

Choose a reason for hiding this comment

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

symlink is not missing on appveyor, so this doesn't skip Windows.
use if ('\\' === \DIRECTORY_SEPARATOR) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm unable to understand the bug as is: I'll create a separate playground PR to debug it

Copy link
Member

Choose a reason for hiding this comment

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

What about calling markAsSkippedIfPosixIsMissing() instead? I think chgrp() doesn't work otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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


$file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
$link = $this->workspace.\DIRECTORY_SEPARATOR.'link';

touch($file);

$this->filesystem->symlink($file, $link);

$groupId = $this->getFileGroupId($link);
$this->filesystem->chgrp($link, $groupId);

$this->assertSame($groupId, $this->getFileGroupId($link));
}

public function testChgrpLink()
{
$this->markAsSkippedIfLinkIsMissing();
Expand Down
21 changes: 18 additions & 3 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,36 @@ protected function assertFilePermissions($expectedFilePerms, $filePath)
);
}

protected function getFileOwnerId($filepath)
{
$this->markAsSkippedIfPosixIsMissing();

$infos = stat($filepath);

return $infos['uid'];
}

protected function getFileOwner($filepath)
{
$this->markAsSkippedIfPosixIsMissing();

return ($datas = posix_getpwuid($this->getFileOwnerId($filepath))) ? $datas['name'] : null;
}

protected function getFileGroupId($filepath)
{
$this->markAsSkippedIfPosixIsMissing();

$infos = stat($filepath);

return ($datas = posix_getpwuid($infos['uid'])) ? $datas['name'] : null;
return $infos['gid'];
}

protected function getFileGroup($filepath)
{
$this->markAsSkippedIfPosixIsMissing();

$infos = stat($filepath);
if ($datas = posix_getgrgid($infos['gid'])) {
if ($datas = posix_getgrgid($this->getFileGroupId($filepath))) {
return $datas['name'];
}

Expand Down