ChownTask.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*
  3. * $Id: ChownTask.php 905 2010-10-05 16:28:03Z mrook $
  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. require_once 'phing/Task.php';
  22. include_once 'phing/types/FileSet.php';
  23. /**
  24. * Task that changes the permissions on a file/directory.
  25. *
  26. * @author Mehmet Emre Yilmaz <mehmety@gmail.com>
  27. * @version $Revision: 905 $
  28. * @package phing.tasks.system
  29. */
  30. class ChownTask extends Task {
  31. private $file;
  32. private $user;
  33. private $filesets = array();
  34. private $filesystem;
  35. private $quiet = false;
  36. private $failonerror = true;
  37. private $verbose = true;
  38. /**
  39. * This flag means 'note errors to the output, but keep going'
  40. * @see setQuiet()
  41. */
  42. function setFailonerror($bool) {
  43. $this->failonerror = $bool;
  44. }
  45. /**
  46. * Set quiet mode, which suppresses warnings if chown() fails.
  47. * @see setFailonerror()
  48. */
  49. function setQuiet($bool) {
  50. $this->quiet = $bool;
  51. if ($this->quiet) {
  52. $this->failonerror = false;
  53. }
  54. }
  55. /**
  56. * Set verbosity, which if set to false surpresses all but an overview
  57. * of what happened.
  58. */
  59. function setVerbose($bool) {
  60. $this->verbose = (bool)$bool;
  61. }
  62. /**
  63. * Sets a single source file to touch. If the file does not exist
  64. * an empty file will be created.
  65. */
  66. function setFile(PhingFile $file) {
  67. $this->file = $file;
  68. }
  69. /**
  70. * Sets the user
  71. */
  72. function setUser($user) {
  73. $this->user = $user;
  74. }
  75. /**
  76. * Nested creator, adds a set of files (nested fileset attribute).
  77. */
  78. function createFileSet() {
  79. $num = array_push($this->filesets, new FileSet());
  80. return $this->filesets[$num-1];
  81. }
  82. /**
  83. * Execute the touch operation.
  84. * @return void
  85. */
  86. function main() {
  87. // Check Parameters
  88. $this->checkParams();
  89. $this->chown();
  90. }
  91. /**
  92. * Ensure that correct parameters were passed in.
  93. * @return void
  94. */
  95. private function checkParams() {
  96. if ($this->file === null && empty($this->filesets)) {
  97. throw new BuildException("Specify at least one source - a file or a fileset.");
  98. }
  99. if ($this->user === null) {
  100. throw new BuildException("You have to specify an owner for chown.");
  101. }
  102. }
  103. /**
  104. * Does the actual work.
  105. * @return void
  106. */
  107. private function chown() {
  108. $userElements = explode('.', $this->user);
  109. $user = $userElements[0];
  110. if (count($userElements) > 1) {
  111. $group = $userElements[1];
  112. } else {
  113. $group = null;
  114. }
  115. // counters for non-verbose output
  116. $total_files = 0;
  117. $total_dirs = 0;
  118. // one file
  119. if ($this->file !== null) {
  120. $total_files = 1;
  121. $this->chownFile($this->file, $user, $group);
  122. }
  123. // filesets
  124. foreach($this->filesets as $fs) {
  125. $ds = $fs->getDirectoryScanner($this->project);
  126. $fromDir = $fs->getDir($this->project);
  127. $srcFiles = $ds->getIncludedFiles();
  128. $srcDirs = $ds->getIncludedDirectories();
  129. $filecount = count($srcFiles);
  130. $total_files = $total_files + $filecount;
  131. for ($j = 0; $j < $filecount; $j++) {
  132. $this->chownFile(new PhingFile($fromDir, $srcFiles[$j]), $user, $group);
  133. }
  134. $dircount = count($srcDirs);
  135. $total_dirs = $total_dirs + $dircount;
  136. for ($j = 0; $j < $dircount; $j++) {
  137. $this->chownFile(new PhingFile($fromDir, $srcDirs[$j]), $user, $group);
  138. }
  139. }
  140. if (!$this->verbose) {
  141. $this->log('Total files changed to ' . $user . ($group ? "." . $group : "") . ': ' . $total_files);
  142. $this->log('Total directories changed to ' . $user . ($group ? "." . $group : "") . ': ' . $total_dirs);
  143. }
  144. }
  145. /**
  146. * Actually change the mode for the file.
  147. * @param PhingFile $file
  148. * @param string $user
  149. * @param string $group
  150. */
  151. private function chownFile(PhingFile $file, $user, $group = "") {
  152. if ( !$file->exists() ) {
  153. throw new BuildException("The file " . $file->__toString() . " does not exist");
  154. }
  155. try {
  156. $file->setUser($user);
  157. if (!empty($group)) {
  158. $file->setGroup($group);
  159. }
  160. if ($this->verbose) {
  161. $this->log("Changed file owner on '" . $file->__toString() ."' to " . $user . ($group ? "." . $group : ""));
  162. }
  163. } catch (Exception $e) {
  164. if($this->failonerror) {
  165. throw $e;
  166. } else {
  167. $this->log($e->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
  168. }
  169. }
  170. }
  171. }