1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Task that changes the permissions on a file/directory.
22
 *
23
 * @author  Manuel Holtgrewe <grin@gmx.net>
24
 * @author  Hans Lellelid <hans@xmpl.org>
25
 * @package phing.tasks.system
26
 */
27
class ChmodTask extends Task
28
{
29
    use DirSetAware;
30
    use FileSetAware;
31

32
    private $file;
33

34
    private $mode;
35

36
    private $quiet = false;
37
    private $failonerror = true;
38
    private $verbose = true;
39

40
    /**
41
     * This flag means 'note errors to the output, but keep going'
42
     *
43
     * @see   setQuiet()
44
     * @param $bool
45
     */
46 1
    public function setFailonerror($bool)
47
    {
48 1
        $this->failonerror = $bool;
49
    }
50

51
    /**
52
     * Set quiet mode, which suppresses warnings if chmod() fails.
53
     *
54
     * @see   setFailonerror()
55
     * @param $bool
56
     */
57 0
    public function setQuiet($bool)
58
    {
59 0
        $this->quiet = $bool;
60 0
        if ($this->quiet) {
61 0
            $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
     * @param $bool
70
     */
71 0
    public function setVerbose(bool $bool)
72
    {
73 0
        $this->verbose = $bool;
74
    }
75

76
    /**
77
     * Sets a single source file to touch.  If the file does not exist
78
     * an empty file will be created.
79
     *
80
     * @param PhingFile $file
81
     */
82 1
    public function setFile(PhingFile $file)
83
    {
84 1
        $this->file = $file;
85
    }
86

87
    /**
88
     * @param $str
89
     */
90 1
    public function setMode($str)
91
    {
92 1
        $this->mode = $str;
93
    }
94

95
    /**
96
     * Execute the touch operation.
97
     *
98
     * @return void
99
     */
100 1
    public function main()
101
    {
102
        // Check Parameters
103 1
        $this->checkParams();
104 1
        $this->chmod();
105
    }
106

107
    /**
108
     * Ensure that correct parameters were passed in.
109
     *
110
     * @throws BuildException
111
     * @return void
112
     */
113 1
    private function checkParams()
114
    {
115 1
        if ($this->file === null && empty($this->filesets) && empty($this->dirsets)) {
116 0
            throw new BuildException(
117 0
                "Specify at least one source - a file, dirset or a fileset."
118
            );
119
        }
120

121 1
        if ($this->mode === null) {
122 0
            throw new BuildException("You have to specify an octal mode for chmod.");
123
        }
124

125
        // check for mode to be in the correct format
126 1
        if (!preg_match('/^([0-7]){3,4}$/', $this->mode)) {
127 0
            throw new BuildException("You have specified an invalid mode.");
128
        }
129
    }
130

131
    /**
132
     * Does the actual work.
133
     *
134
     * @return void
135
     */
136 1
    private function chmod()
137
    {
138 1
        if (strlen($this->mode) === 4) {
139 1
            $mode = octdec($this->mode);
140
        } else {
141
            // we need to prepend the 0 before converting
142 1
            $mode = octdec("0" . $this->mode);
143
        }
144

145
        // counters for non-verbose output
146 1
        $total_files = 0;
147 1
        $total_dirs = 0;
148

149
        // one file
150 1
        if ($this->file !== null) {
151 1
            $total_files = 1;
152 1
            $this->chmodFile($this->file, $mode);
153
        }
154

155 1
        $this->filesets = array_merge($this->filesets, $this->dirsets);
156

157
        // filesets
158 1
        foreach ($this->filesets as $fs) {
159 1
            $ds = $fs->getDirectoryScanner($this->project);
160 1
            $fromDir = $fs->getDir($this->project);
161

162 1
            $srcFiles = $ds->getIncludedFiles();
163 1
            $srcDirs = $ds->getIncludedDirectories();
164

165 1
            $filecount = count($srcFiles);
166 1
            $total_files += $filecount;
167 1
            for ($j = 0; $j < $filecount; $j++) {
168 1
                $this->chmodFile(new PhingFile($fromDir, $srcFiles[$j]), $mode);
169
            }
170

171 1
            $dircount = count($srcDirs);
172 1
            $total_dirs += $dircount;
173 1
            for ($j = 0; $j < $dircount; $j++) {
174 1
                $this->chmodFile(new PhingFile($fromDir, $srcDirs[$j]), $mode);
175
            }
176
        }
177

178 1
        if (!$this->verbose) {
179 0
            $this->log('Total files changed to ' . vsprintf('%o', [$mode]) . ': ' . $total_files);
180 0
            $this->log('Total directories changed to ' . vsprintf('%o', [$mode]) . ': ' . $total_dirs);
181
        }
182
    }
183

184
    /**
185
     * Actually change the mode for the file.
186
     *
187
     * @param  PhingFile $file
188
     * @param  int $mode
189
     * @throws BuildException
190
     * @throws Exception
191
     */
192 1
    private function chmodFile(PhingFile $file, $mode)
193
    {
194 1
        if (!$file->exists()) {
195 0
            throw new BuildException("The file " . $file->__toString() . " does not exist");
196
        }
197

198
        try {
199 1
            $file->setMode($mode);
200 1
            if ($this->verbose) {
201 1
                $this->log("Changed file mode on '" . $file->__toString() . "' to " . vsprintf("%o", [$mode]));
202
            }
203 0
        } catch (Exception $e) {
204 0
            if ($this->failonerror) {
205 0
                throw $e;
206
            }
207

208 0
            $this->log($e->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
209
        }
210
    }
211
}

Read our documentation on viewing source code .

Loading