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