-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathchange_owner.php
43 lines (36 loc) · 993 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
38
39
40
41
42
43
<?php
declare(strict_types=1);
namespace Psl\Filesystem;
use Psl\Internal;
use Psl\Str;
use function chown;
use function lchown;
/**
* Change the owner of $node.
*
* @param non-empty-string $node
*
* @throws Exception\RuntimeException If unable to change the ownership for $node.
* @throws Exception\NotFoundException If $node does not exist.
*
* @mago-expect best-practices/no-else-clause
*/
function change_owner(string $node, int $user): void
{
if (!namespace\exists($node)) {
throw Exception\NotFoundException::forNode($node);
}
if (is_symbolic_link($node)) {
$fun = static fn(): bool => lchown($node, $user);
} else {
$fun = static fn(): bool => chown($node, $user);
}
[$success, $error] = Internal\box($fun);
if (!$success) {
throw new Exception\RuntimeException(Str\format(
'Failed to change owner for node "%s": %s',
$node,
$error ?? 'internal error.',
));
}
}