123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- require_once 'phing/Task.php';
- include_once 'phing/types/FileSet.php';
- class ChmodTask extends Task {
- private $file;
- private $mode;
- private $filesets = array();
- private $filesystem;
-
- private $quiet = false;
- private $failonerror = true;
- private $verbose = true;
-
-
- function setFailonerror($bool) {
- $this->failonerror = $bool;
- }
-
- function setQuiet($bool) {
- $this->quiet = $bool;
- if ($this->quiet) {
- $this->failonerror = false;
- }
- }
-
-
- function setVerbose($bool) {
- $this->verbose = (bool)$bool;
- }
-
-
- function setFile(PhingFile $file) {
- $this->file = $file;
- }
- function setMode($str) {
- $this->mode = $str;
- }
-
- function createFileSet() {
- $num = array_push($this->filesets, new FileSet());
- return $this->filesets[$num-1];
- }
-
- function main() {
-
- $this->checkParams();
- $this->chmod();
- }
-
-
- private function checkParams() {
-
- if ($this->file === null && empty($this->filesets)) {
- throw new BuildException("Specify at least one source - a file or a fileset.");
- }
- if ($this->mode === null) {
- throw new BuildException("You have to specify an octal mode for chmod.");
- }
-
- if (!preg_match('/^([0-7]){3,4}$/', $this->mode)) {
- throw new BuildException("You have specified an invalid mode.");
- }
-
- }
-
- private function chmod() {
-
- if (strlen($this->mode) === 4) {
- $mode = octdec($this->mode);
- } else {
-
- $mode = octdec("0". $this->mode);
- }
-
-
- $total_files = 0;
- $total_dirs = 0;
-
-
- if ($this->file !== null) {
- $total_files = 1;
- $this->chmodFile($this->file, $mode);
- }
-
- foreach($this->filesets as $fs) {
-
- $ds = $fs->getDirectoryScanner($this->project);
- $fromDir = $fs->getDir($this->project);
- $srcFiles = $ds->getIncludedFiles();
- $srcDirs = $ds->getIncludedDirectories();
- $filecount = count($srcFiles);
- $total_files = $total_files + $filecount;
- for ($j = 0; $j < $filecount; $j++) {
- $this->chmodFile(new PhingFile($fromDir, $srcFiles[$j]), $mode);
- }
- $dircount = count($srcDirs);
- $total_dirs = $total_dirs + $dircount;
- for ($j = 0; $j < $dircount; $j++) {
- $this->chmodFile(new PhingFile($fromDir, $srcDirs[$j]), $mode);
- }
- }
- if (!$this->verbose) {
- $this->log('Total files changed to ' . vsprintf('%o', $mode) . ': ' . $total_files);
- $this->log('Total directories changed to ' . vsprintf('%o', $mode) . ': ' . $total_dirs);
- }
- }
-
- private function chmodFile(PhingFile $file, $mode) {
- if ( !$file->exists() ) {
- throw new BuildException("The file " . $file->__toString() . " does not exist");
- }
-
- try {
- $file->setMode($mode);
- if ($this->verbose) {
- $this->log("Changed file mode on '" . $file->__toString() ."' to " . vsprintf("%o", $mode));
- }
- } catch (Exception $e) {
- if($this->failonerror) {
- throw $e;
- } else {
- $this->log($e->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
- }
- }
- }
-
- }
|