| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | namespace phing::tasks::system; |
|---|
| 23 | use phing::BuildException; |
|---|
| 24 | use phing::Task; |
|---|
| 25 | use phing::Project; |
|---|
| 26 | use phing::types::FileSet; |
|---|
| 27 | use phing::system::io::File; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | class ChownTask extends Task { |
|---|
| 39 | |
|---|
| 40 | private $file; |
|---|
| 41 | |
|---|
| 42 | private $user; |
|---|
| 43 | |
|---|
| 44 | private $filesets = array(); |
|---|
| 45 | |
|---|
| 46 | private $filesystem; |
|---|
| 47 | |
|---|
| 48 | private $quiet = false; |
|---|
| 49 | private $failonerror = true; |
|---|
| 50 | private $verbose = true; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | function setFailonerror($bool) { |
|---|
| 57 | $this->failonerror = $bool; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | function setQuiet($bool) { |
|---|
| 65 | $this->quiet = $bool; |
|---|
| 66 | if ($this->quiet) { |
|---|
| 67 | $this->failonerror = false; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | function setVerbose($bool) { |
|---|
| 76 | $this->verbose = (bool)$bool; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | function setFile(File $file) { |
|---|
| 84 | $this->file = $file; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | function setUser($str) { |
|---|
| 88 | $this->user = $str; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | function createFileSet() { |
|---|
| 95 | $num = array_push($this->filesets, new FileSet()); |
|---|
| 96 | return $this->filesets[$num-1]; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | function main() { |
|---|
| 104 | |
|---|
| 105 | $this->checkParams(); |
|---|
| 106 | $this->chmod(); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | private function checkParams() { |
|---|
| 114 | |
|---|
| 115 | if ($this->file === null && empty($this->filesets)) { |
|---|
| 116 | throw new BuildException("Specify at least one source - a file or a fileset."); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | if ($this->user === null) { |
|---|
| 120 | throw new BuildException("You have to specify a user for chown."); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | private function chown() { |
|---|
| 133 | |
|---|
| 134 | $user= $this->user; |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | $total_files = 0; |
|---|
| 138 | $total_dirs = 0; |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | if ($this->file !== null) { |
|---|
| 142 | $total_files = 1; |
|---|
| 143 | $this->chownFile($this->file, $user); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | foreach($this->filesets as $fs) { |
|---|
| 148 | |
|---|
| 149 | $ds = $fs->getDirectoryScanner($this->project); |
|---|
| 150 | $fromDir = $fs->getDir($this->project); |
|---|
| 151 | |
|---|
| 152 | $srcFiles = $ds->getIncludedFiles(); |
|---|
| 153 | $srcDirs = $ds->getIncludedDirectories(); |
|---|
| 154 | |
|---|
| 155 | $filecount = count($srcFiles); |
|---|
| 156 | $total_files = $total_files + $filecount; |
|---|
| 157 | for ($j = 0; $j < $filecount; $j++) { |
|---|
| 158 | $this->chmodFile(new File($fromDir, $srcFiles[$j]), $user); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | $dircount = count($srcDirs); |
|---|
| 162 | $total_dirs = $total_dirs + $dircount; |
|---|
| 163 | for ($j = 0; $j < $dircount; $j++) { |
|---|
| 164 | $this->chmodFile(new File($fromDir, $srcDirs[$j]), $user); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | if (!$this->verbose) { |
|---|
| 169 | $this->log('Total files changed to ' . vsprintf('%o', $mode) . ': ' . $total_files); |
|---|
| 170 | $this->log('Total directories changed to ' . vsprintf('%o', $mode) . ': ' . $total_dirs); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | private function chmodFile(File $file, $user) { |
|---|
| 181 | if ( !$file->exists() ) { |
|---|
| 182 | throw new BuildException("The file " . $file->__toString() . " does not exist"); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | try { |
|---|
| 186 | $file->setUser($user); |
|---|
| 187 | if ($this->verbose) { |
|---|
| 188 | $this->log("Changed file mode on '" . $file->__toString() ."' to " . vsprintf("%o", $mode)); |
|---|
| 189 | } |
|---|
| 190 | } catch (Exception $e) { |
|---|
| 191 | if($this->failonerror) { |
|---|
| 192 | throw $e; |
|---|
| 193 | } else { |
|---|
| 194 | $this->log($e->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN); |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|