1
|
|
<?php
|
2
|
|
/**
|
3
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
4
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
5
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
6
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
7
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
8
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
9
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
10
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
11
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
12
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
13
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
14
|
|
*
|
15
|
|
* This software consists of voluntary contributions made by many individuals
|
16
|
|
* and is licensed under the LGPL. For more information please see
|
17
|
|
* <http://phing.info>.
|
18
|
|
*/
|
19
|
|
|
20
|
|
/**
|
21
|
|
* Task that changes the permissions on a file/directory.
|
22
|
|
*
|
23
|
|
* @author Mehmet Emre Yilmaz <mehmety@gmail.com>
|
24
|
|
* @package phing.tasks.system
|
25
|
|
*/
|
26
|
|
class ChownTask extends Task
|
27
|
|
{
|
28
|
|
use DirSetAware;
|
29
|
|
use FileSetAware;
|
30
|
|
|
31
|
|
private $file;
|
32
|
|
|
33
|
|
private $user;
|
34
|
|
private $group;
|
35
|
|
|
36
|
|
private $quiet = false;
|
37
|
|
private $failonerror = true;
|
38
|
|
private $verbose = true;
|
39
|
|
|
40
|
|
/**
|
41
|
|
* This flag means 'note errors to the output, but keep going'
|
42
|
|
*
|
43
|
|
* @see setQuiet()
|
44
|
|
* @param $bool
|
45
|
|
*/
|
46
|
0
|
public function setFailonerror($bool)
|
47
|
|
{
|
48
|
0
|
$this->failonerror = $bool;
|
49
|
|
}
|
50
|
|
|
51
|
|
/**
|
52
|
|
* Set quiet mode, which suppresses warnings if chown() fails.
|
53
|
|
*
|
54
|
|
* @see setFailonerror()
|
55
|
|
* @param $bool
|
56
|
|
*/
|
57
|
0
|
public function setQuiet($bool)
|
58
|
|
{
|
59
|
0
|
$this->quiet = $bool;
|
60
|
0
|
if ($this->quiet) {
|
61
|
0
|
$this->failonerror = false;
|
62
|
|
}
|
63
|
|
}
|
64
|
|
|
65
|
|
/**
|
66
|
|
* Set verbosity, which if set to false surpresses all but an overview
|
67
|
|
* of what happened.
|
68
|
|
*
|
69
|
|
* @param $bool
|
70
|
|
*/
|
71
|
0
|
public function setVerbose(bool $bool)
|
72
|
|
{
|
73
|
0
|
$this->verbose = $bool;
|
74
|
|
}
|
75
|
|
|
76
|
|
/**
|
77
|
|
* Sets a single source file to touch. If the file does not exist
|
78
|
|
* an empty file will be created.
|
79
|
|
*
|
80
|
|
* @param PhingFile $file
|
81
|
|
*/
|
82
|
0
|
public function setFile(PhingFile $file)
|
83
|
|
{
|
84
|
0
|
$this->file = $file;
|
85
|
|
}
|
86
|
|
|
87
|
|
/**
|
88
|
|
* Sets the user
|
89
|
|
*
|
90
|
|
* @param $user
|
91
|
|
*/
|
92
|
0
|
public function setUser($user)
|
93
|
|
{
|
94
|
0
|
$this->user = $user;
|
95
|
|
}
|
96
|
|
|
97
|
|
/**
|
98
|
|
* Sets the group
|
99
|
|
*
|
100
|
|
* @param $group
|
101
|
|
*/
|
102
|
0
|
public function setGroup($group)
|
103
|
|
{
|
104
|
0
|
$this->group = $group;
|
105
|
|
}
|
106
|
|
|
107
|
|
/**
|
108
|
|
* Execute the touch operation.
|
109
|
|
*
|
110
|
|
* @return void
|
111
|
|
*/
|
112
|
0
|
public function main()
|
113
|
|
{
|
114
|
|
// Check Parameters
|
115
|
0
|
$this->checkParams();
|
116
|
0
|
$this->chown();
|
117
|
|
}
|
118
|
|
|
119
|
|
/**
|
120
|
|
* Ensure that correct parameters were passed in.
|
121
|
|
*
|
122
|
|
* @throws BuildException
|
123
|
|
* @return void
|
124
|
|
*/
|
125
|
0
|
private function checkParams()
|
126
|
|
{
|
127
|
0
|
if ($this->file === null && empty($this->filesets) && empty($this->dirsets)) {
|
128
|
0
|
throw new BuildException("Specify at least one source - a file or a fileset.");
|
129
|
|
}
|
130
|
|
|
131
|
0
|
if ($this->user === null && $this->group === null) {
|
132
|
0
|
throw new BuildException("You have to specify either an owner or a group for chown.");
|
133
|
|
}
|
134
|
|
}
|
135
|
|
|
136
|
|
/**
|
137
|
|
* Does the actual work.
|
138
|
|
*
|
139
|
|
* @return void
|
140
|
|
*/
|
141
|
0
|
private function chown()
|
142
|
|
{
|
143
|
0
|
$userElements = explode('.', $this->user);
|
144
|
|
|
145
|
0
|
$user = $userElements[0];
|
146
|
|
|
147
|
0
|
if (count($userElements) > 1) {
|
148
|
0
|
$group = $userElements[1];
|
149
|
|
} else {
|
150
|
0
|
$group = $this->group;
|
151
|
|
}
|
152
|
|
|
153
|
|
// counters for non-verbose output
|
154
|
0
|
$total_files = 0;
|
155
|
0
|
$total_dirs = 0;
|
156
|
|
|
157
|
|
// one file
|
158
|
0
|
if ($this->file !== null) {
|
159
|
0
|
$total_files = 1;
|
160
|
0
|
$this->chownFile($this->file, $user, $group);
|
161
|
|
}
|
162
|
|
|
163
|
0
|
$this->filesets = array_merge($this->filesets, $this->dirsets);
|
164
|
|
|
165
|
|
// filesets
|
166
|
0
|
foreach ($this->filesets as $fs) {
|
167
|
0
|
$ds = $fs->getDirectoryScanner($this->project);
|
168
|
0
|
$fromDir = $fs->getDir($this->project);
|
169
|
|
|
170
|
0
|
$srcFiles = $ds->getIncludedFiles();
|
171
|
0
|
$srcDirs = $ds->getIncludedDirectories();
|
172
|
|
|
173
|
0
|
$filecount = count($srcFiles);
|
174
|
0
|
$total_files += $filecount;
|
175
|
0
|
for ($j = 0; $j < $filecount; $j++) {
|
176
|
0
|
$this->chownFile(new PhingFile($fromDir, $srcFiles[$j]), $user, $group);
|
177
|
|
}
|
178
|
|
|
179
|
0
|
$dircount = count($srcDirs);
|
180
|
0
|
$total_dirs += $dircount;
|
181
|
0
|
for ($j = 0; $j < $dircount; $j++) {
|
182
|
0
|
$this->chownFile(new PhingFile($fromDir, $srcDirs[$j]), $user, $group);
|
183
|
|
}
|
184
|
|
}
|
185
|
|
|
186
|
0
|
if (!$this->verbose) {
|
187
|
0
|
$this->log('Total files changed to ' . $user . ($group ? "." . $group : "") . ': ' . $total_files);
|
188
|
0
|
$this->log('Total directories changed to ' . $user . ($group ? "." . $group : "") . ': ' . $total_dirs);
|
189
|
|
}
|
190
|
|
}
|
191
|
|
|
192
|
|
/**
|
193
|
|
* Actually change the mode for the file.
|
194
|
|
*
|
195
|
|
* @param PhingFile $file
|
196
|
|
* @param string $user
|
197
|
|
* @param string $group
|
198
|
|
* @throws BuildException
|
199
|
|
* @throws Exception
|
200
|
|
*/
|
201
|
0
|
private function chownFile(PhingFile $file, $user, $group = "")
|
202
|
|
{
|
203
|
0
|
if (!$file->exists()) {
|
204
|
0
|
throw new BuildException("The file " . $file->__toString() . " does not exist");
|
205
|
|
}
|
206
|
|
|
207
|
|
try {
|
208
|
0
|
if (!empty($user)) {
|
209
|
0
|
$file->setUser($user);
|
210
|
|
}
|
211
|
|
|
212
|
0
|
if (!empty($group)) {
|
213
|
0
|
$file->setGroup($group);
|
214
|
|
}
|
215
|
|
|
216
|
0
|
if ($this->verbose) {
|
217
|
0
|
$this->log(
|
218
|
0
|
"Changed file owner on '" . $file->__toString() . "' to " . $user . ($group ? "." . $group : "")
|
219
|
|
);
|
220
|
|
}
|
221
|
0
|
} catch (Exception $e) {
|
222
|
0
|
if ($this->failonerror) {
|
223
|
0
|
throw $e;
|
224
|
|
}
|
225
|
|
|
226
|
0
|
$this->log($e->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
|
227
|
|
}
|
228
|
|
}
|
229
|
|
}
|