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