-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathchange_owner.php
37 lines (31 loc) · 928 Bytes
/
change_owner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
declare(strict_types=1);
namespace Psl\Filesystem;
use Psl;
use Psl\Internal;
use Psl\Str;
use function chown;
use function lchown;
/**
* Change the owner of $filename.
*
* @throws Exception\RuntimeException If unable to change the ownership for $filename.
* @throws Psl\Exception\InvariantViolationException If $filename does not exist.
*/
function change_owner(string $filename, int $user): void
{
Psl\invariant(exists($filename), '$filename does not exist.');
if (is_symbolic_link($filename)) {
$fun = static fn(): bool => lchown($filename, $user);
} else {
$fun = static fn(): bool => chown($filename, $user);
}
[$success, $error] = Internal\box($fun);
if (!$success) {
throw new Exception\RuntimeException(Str\format(
'Failed to change owner for file "%s": %s',
$filename,
$error ?? 'internal error.',
));
}
}