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
|
|
|
use Phing\Exception\BuildException; |
21
|
|
|
use Phing\Io\IOException; |
22
|
|
|
use Phing\Io\File; |
23
|
|
|
use Phing\Project; |
24
|
|
|
use Phing\Util\SizeHelper; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Siad Ardroumli <[email protected]> |
28
|
|
|
* @package phing.tasks.system |
29
|
|
|
*/ |
30
|
|
|
class TruncateTask extends Task |
31
|
|
|
{ |
32
|
|
|
private $create = true; |
33
|
|
|
private $mkdirs = false; |
34
|
|
|
|
35
|
|
|
private $length; |
36
|
|
|
private $adjust; |
37
|
|
|
private $file; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set a single target File. |
41
|
|
|
* |
42
|
|
|
* @param File $f the single File |
43
|
|
|
* @throws \Phing\Io\IOException |
44
|
|
|
* @throws \Phing\Exception\NullPointerException |
45
|
|
|
*/ |
46
|
22 |
|
public function setFile(File $f): void |
47
|
|
|
{ |
48
|
22 |
|
$this->file = $f; |
49
|
22 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set the amount by which files' lengths should be adjusted. |
53
|
|
|
* It is permissible to append b / k / m / g / t. |
54
|
|
|
* |
55
|
|
|
* @param string $adjust (positive or negative) adjustment amount. |
56
|
|
|
*/ |
57
|
3 |
|
public function setAdjust(string $adjust) |
58
|
|
|
{ |
59
|
3 |
|
$this->adjust = SizeHelper::fromHumanToBytes($adjust); |
60
|
3 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the length to which files should be set. |
64
|
|
|
* It is permissible to append k / m / g. |
65
|
|
|
* |
66
|
|
|
* @param string $length (positive) adjustment amount. |
67
|
|
|
* |
68
|
|
|
* @throws \Phing\Exception\BuildException |
69
|
|
|
*/ |
70
|
22 |
|
public function setLength(string $length) |
71
|
|
|
{ |
72
|
22 |
|
$this->length = SizeHelper::fromHumanToBytes($length); |
73
|
22 |
|
if ($this->length !== null && $this->length < 0) { |
74
|
1 |
|
throw new BuildException('Cannot truncate to length ' . $this->length); |
75
|
|
|
} |
76
|
21 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set whether to create nonexistent files. |
80
|
|
|
* |
81
|
|
|
* @param boolean $create default <code>true</code>. |
82
|
|
|
*/ |
83
|
1 |
|
public function setCreate($create) |
84
|
|
|
{ |
85
|
1 |
|
$this->create = $create; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set whether, when creating nonexistent files, nonexistent directories |
90
|
|
|
* should also be created. |
91
|
|
|
* |
92
|
|
|
* @param boolean $mkdirs default <code>false</code>. |
93
|
|
|
*/ |
94
|
1 |
|
public function setMkdirs($mkdirs) |
95
|
|
|
{ |
96
|
1 |
|
$this->mkdirs = $mkdirs; |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritDoc}. |
101
|
|
|
* |
102
|
|
|
* @throws \Phing\Exception\BuildException |
103
|
|
|
*/ |
104
|
22 |
|
public function main() |
105
|
|
|
{ |
106
|
22 |
|
if ($this->length !== null && $this->adjust !== null) { |
107
|
1 |
|
throw new BuildException( |
108
|
1 |
|
'length and adjust are mutually exclusive options' |
109
|
|
|
); |
110
|
|
|
} |
111
|
21 |
|
if ($this->length === null && $this->adjust === null) { |
112
|
1 |
|
$this->length = 0; |
113
|
|
|
} |
114
|
21 |
|
if ($this->file === null) { |
115
|
1 |
|
throw new BuildException('No files specified.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
20 |
|
if ($this->shouldProcess($this->file)) { |
119
|
19 |
|
$this->process($this->file); |
120
|
|
|
} |
121
|
20 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param File $f |
125
|
|
|
* @return bool |
126
|
|
|
* @throws \Phing\Exception\BuildException |
127
|
|
|
*/ |
128
|
20 |
|
private function shouldProcess(File $f) |
129
|
|
|
{ |
130
|
20 |
|
if ($f->isFile()) { |
131
|
2 |
|
return true; |
132
|
|
|
} |
133
|
20 |
|
if (!$this->create) { |
134
|
1 |
|
return false; |
135
|
|
|
} |
136
|
19 |
|
$exception = null; |
137
|
|
|
try { |
138
|
19 |
|
if ($f->createNewFile($this->mkdirs)) { |
139
|
19 |
|
return true; |
140
|
|
|
} |
141
|
|
|
} catch (IOException $e) { |
142
|
|
|
$exception = $e; |
143
|
|
|
} |
144
|
|
|
$msg = "Unable to create " . $f; |
145
|
|
|
if ($exception === null) { |
146
|
|
|
$this->log($msg, Project::MSG_WARN); |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
throw new BuildException($msg, $exception); |
150
|
|
|
} |
151
|
|
|
|
152
|
19 |
|
private function process(File $f) |
153
|
|
|
{ |
154
|
19 |
|
$len = $f->length(); |
155
|
19 |
|
$newLength = $this->length ?? $len + $this->adjust; |
156
|
|
|
|
157
|
19 |
|
if ($len === $newLength) { |
158
|
|
|
//nothing to do! |
159
|
1 |
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
18 |
|
$splFile = new SplFileObject($f->getPath(), 'a+'); |
163
|
|
|
|
164
|
18 |
|
if (!$splFile->ftruncate((int) $newLength)) { |
165
|
|
|
throw new BuildException("Exception working with " . (string) $splFile); |
166
|
|
|
} |
167
|
|
|
|
168
|
18 |
|
$splFile->rewind(); |
169
|
18 |
|
clearstatcache(); |
170
|
18 |
|
} |
171
|
|
|
} |
172
|
|
|
|