Ticket #219: ChownTask.php

File ChownTask.php, 5.5 KB (added by hans, 4 years ago)

ChmodTask (trunk)

Line 
1<?php
2/*
3 *  $Id$
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
22namespace phing::tasks::system;
23use phing::BuildException;
24use phing::Task;
25use phing::Project;
26use phing::types::FileSet;
27use phing::system::io::File;
28
29/**
30 * Task that changes the permissions on a file/directory.
31 *
32 * @author      Mehmet Emre Yilmaz <mehmety@gmail.com>
33 * @author    Manuel Holtgrewe <grin@gmx.net>
34 * @author    Hans Lellelid <hans@xmpl.org>
35 * @version   $Revision: 1.12 $
36 * @package   phing.tasks.system
37 */
38class 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     * This flag means 'note errors to the output, but keep going'
54     * @see setQuiet()
55     */
56    function setFailonerror($bool) {
57        $this->failonerror = $bool;
58    }
59
60    /**
61     * Set quiet mode, which suppresses warnings if chmod() fails.
62     * @see setFailonerror()
63     */
64    function setQuiet($bool) {
65        $this->quiet = $bool;
66        if ($this->quiet) {
67            $this->failonerror = false;
68        }
69    }
70
71    /**
72     * Set verbosity, which if set to false surpresses all but an overview
73     * of what happened.
74     */
75    function setVerbose($bool) {
76        $this->verbose = (bool)$bool;
77    }
78
79    /**
80     * Sets a single source file to touch.  If the file does not exist
81     * an empty file will be created.
82     */
83    function setFile(File $file) {
84        $this->file = $file;
85    }
86
87    function setUser($str) {
88        $this->user = $str;
89    }
90
91    /**
92     * Nested creator, adds a set of files (nested fileset attribute).
93     */
94    function createFileSet() {
95        $num = array_push($this->filesets, new FileSet());
96        return $this->filesets[$num-1];
97    }
98
99    /**
100     * Execute the touch operation.
101     * @return void
102     */
103    function main() {
104        // Check Parameters
105        $this->checkParams();
106        $this->chmod();
107    }
108
109    /**
110     * Ensure that correct parameters were passed in.
111     * @return void
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        // check for mode to be in the correct format
124
125
126    }
127
128    /**
129     * Does the actual work.
130     * @return void
131     */
132    private function chown() {
133
134        $user= $this->user;
135
136        // counters for non-verbose output
137        $total_files = 0;
138        $total_dirs = 0;
139
140        // one file
141        if ($this->file !== null) {
142            $total_files = 1;
143            $this->chownFile($this->file, $user);
144        }
145
146        // filesets
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     * Actually change the mode for the file.
177     * @param File $file
178     * @param int $mode
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