-
Notifications
You must be signed in to change notification settings - Fork 2
/
FsChownCommand.php
173 lines (154 loc) · 4.69 KB
/
FsChownCommand.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace Droid\Plugin\Fs\Command;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessBuilder;
use Droid\Lib\Plugin\Command\CheckableTrait;
use Droid\Plugin\Fs\Utils;
use Droid\Plugin\Fs\Service\AclObjectLookupInterface;
class FsChownCommand extends Command
{
use CheckableTrait;
protected $processBuilder;
protected $userLookup;
public function __construct(
AclObjectLookupInterface $userLookup,
ProcessBuilder $processBuilder,
$name = null
) {
$this->userLookup = $userLookup;
$this->processBuilder = $processBuilder;
return parent::__construct($name);
}
public function configure()
{
$this
->setName('fs:chown')
->setDescription('Change ownership of a file')
->addArgument(
'file',
InputArgument::REQUIRED,
'Path to file'
)
->addArgument(
'user',
InputArgument::REQUIRED,
'User name'
)
->addArgument(
'group',
InputArgument::REQUIRED,
'Group name'
)
;
$this->configureCheckMode();
}
public function execute(InputInterface $input, OutputInterface $output)
{
$this->activateCheckMode($input);
$path = Utils::normalizePath($input->getArgument('file'));
if (! file_exists($path)) {
throw new RuntimeException(
sprintf('The file "%s" does not exist.', $path)
);
}
$user = $input->getArgument('user');
$uid = $this->userLookup->userId($user);
if ($uid === null) {
throw new RuntimeException(
sprintf(
'Failed to determine user id for a user named "%s". Do they exist?',
$user
)
);
}
$group = $input->getArgument('group');
$gid = $this->userLookup->groupId($group);
if ($gid === null) {
throw new RuntimeException(
sprintf(
'Failed to determine group id for a group named "%s". Does it exist?',
$group
)
);
}
$stat = stat($path);
if ($stat === false) {
throw new RuntimeException(
sprintf('Cannot stat file "%s".', $path)
);
}
if ($uid === $stat['uid'] && $gid === $stat['gid']) {
$output->writeln(
sprintf(
'The file "%s" already has the supplied ownership. Nothing to do.',
$path
)
);
$this->reportChange($output);
return 0;
}
$this->markChange();
if ($uid === $stat['uid']) {
$output->writeln(
sprintf(
'The file "%s" is already owned by user "%s". I will not change user ownership.',
$path,
$user
)
);
}
if ($gid === $stat['gid']) {
$output->writeln(
sprintf(
'The file "%s" is already owned by group "%s". I will not change group ownership.',
$path,
$group
)
);
}
if ($this->checkMode()) {
$output->writeln(sprintf('I would chown file "%s".', $path));
} else {
$this->chown($user, $group, $path);
$output->writeln(sprintf('Chowned file "%s".', $path));
}
$this->reportChange($output);
}
private function chown($user, $group, $path)
{
if ($this->checkMode()) {
return;
}
$p = $this->getProcess(
array(
'sudo', #?
'chown',
sprintf('%s:%s', $user, $group),
$path
)
);
if ($p->run()) {
throw new RuntimeException(
sprintf(
'Failed to change ownership of file "%s": %s',
$path,
trim($p->getErrorOutput())
),
$p->getExitCode()
);
}
}
private function getProcess($arguments)
{
return $this
->processBuilder
->setArguments($arguments)
->setTimeout(0.0)
->getProcess()
;
}
}