|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
15
|
|
|
* |
|
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
17
|
|
|
* and is licensed under the LGPL. For more information please see |
|
18
|
|
|
* <http://phing.info>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Phing\Task\Ext\Archive; |
|
22
|
|
|
|
|
23
|
|
|
use Archive_Tar; |
|
24
|
|
|
use PEAR; |
|
25
|
|
|
use Phing\Exception\BuildException; |
|
26
|
|
|
use Phing\Io\IOException; |
|
27
|
|
|
use Phing\Io\File; |
|
28
|
|
|
use Phing\Io\SourceFileScanner; |
|
29
|
|
|
use Phing\Mapper\MergeMapper; |
|
30
|
|
|
use Phing\Project; |
|
31
|
|
|
use Phing\Task\System\MatchingTask; |
|
32
|
|
|
use Phing\Type\FileSet; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Creates a tar archive using PEAR Archive_Tar. |
|
36
|
|
|
* |
|
37
|
|
|
* @author Hans Lellelid <[email protected]> (Phing) |
|
38
|
|
|
* @author Stefano Mazzocchi <[email protected]> (Ant) |
|
39
|
|
|
* @author Stefan Bodewig <[email protected]> (Ant) |
|
40
|
|
|
* @author Magesh Umasankar |
|
41
|
|
|
* |
|
42
|
|
|
* @package phing.tasks.ext |
|
43
|
|
|
*/ |
|
44
|
|
|
class TarTask extends MatchingTask |
|
45
|
|
|
{ |
|
46
|
|
|
public const TAR_NAMELEN = 100; |
|
47
|
|
|
|
|
48
|
|
|
public const WARN = "warn"; |
|
49
|
|
|
public const FAIL = "fail"; |
|
50
|
|
|
public const OMIT = "omit"; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var File |
|
54
|
|
|
*/ |
|
55
|
|
|
private $tarFile; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var File |
|
59
|
|
|
*/ |
|
60
|
|
|
private $baseDir; |
|
61
|
|
|
|
|
62
|
|
|
private $includeEmpty = true; // Whether to include empty dirs in the TAR |
|
63
|
|
|
|
|
64
|
|
|
private $longFileMode = "warn"; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var TarFileSet[] |
|
68
|
|
|
*/ |
|
69
|
|
|
private $filesets = []; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Indicates whether the user has been warned about long files already. |
|
73
|
|
|
*/ |
|
74
|
|
|
private $longWarningGiven = false; |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Compression mode. Available options "gzip", "bzip2", "none" (null). |
|
78
|
|
|
*/ |
|
79
|
|
|
private $compression = null; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* File path prefix in the tar archive |
|
83
|
|
|
* |
|
84
|
|
|
* @var string |
|
85
|
|
|
*/ |
|
86
|
|
|
private $prefix = null; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Ensures that PEAR lib exists. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function init() |
|
92
|
|
|
{ |
|
93
|
|
|
if (!class_exists('Archive_Tar')) { |
|
94
|
|
|
throw new BuildException("You must have installed the pear/archive_tar package use TarTask."); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Add a new fileset |
|
100
|
|
|
* |
|
101
|
|
|
* @return FileSet |
|
102
|
|
|
*/ |
|
103
|
|
|
public function createTarFileSet() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->fileset = new TarFileSet(); |
|
106
|
|
|
$this->filesets[] = $this->fileset; |
|
107
|
|
|
|
|
108
|
|
|
return $this->fileset; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Add a new fileset. Alias to createTarFileSet() for backwards compatibility. |
|
113
|
|
|
* |
|
114
|
|
|
* @return FileSet |
|
115
|
|
|
* @see createTarFileSet() |
|
116
|
|
|
*/ |
|
117
|
|
|
public function createFileSet() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->fileset = new TarFileSet(); |
|
120
|
|
|
$this->filesets[] = $this->fileset; |
|
121
|
|
|
|
|
122
|
|
|
return $this->fileset; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set is the name/location of where to create the tar file. |
|
127
|
|
|
* |
|
128
|
|
|
* @param File $destFile The output of the tar |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setDestFile(File $destFile) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->tarFile = $destFile; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* This is the base directory to look in for things to tar. |
|
137
|
|
|
* |
|
138
|
|
|
* @param File $baseDir |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setBasedir(File $baseDir) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->baseDir = $baseDir; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Set the include empty dirs flag. |
|
147
|
|
|
* |
|
148
|
|
|
* @param boolean $bool Flag if empty dirs should be tarred too |
|
149
|
|
|
* |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setIncludeEmptyDirs($bool) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->includeEmpty = (bool) $bool; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Set how to handle long files, those with a path>100 chars. |
|
159
|
|
|
* Optional, default=warn. |
|
160
|
|
|
* <p> |
|
161
|
|
|
* Allowable values are |
|
162
|
|
|
* <ul> |
|
163
|
|
|
* <li> truncate - paths are truncated to the maximum length |
|
164
|
|
|
* <li> fail - paths greater than the maximim cause a build exception |
|
165
|
|
|
* <li> warn - paths greater than the maximum cause a warning and GNU is used |
|
166
|
|
|
* <li> gnu - GNU extensions are used for any paths greater than the maximum. |
|
167
|
|
|
* <li> omit - paths greater than the maximum are omitted from the archive |
|
168
|
|
|
* </ul> |
|
169
|
|
|
* |
|
170
|
|
|
* @param $mode |
|
171
|
|
|
*/ |
|
172
|
|
|
public function setLongfile($mode) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->longFileMode = $mode; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Set compression method. |
|
179
|
|
|
* Allowable values are |
|
180
|
|
|
* <ul> |
|
181
|
|
|
* <li> none - no compression |
|
182
|
|
|
* <li> gzip - Gzip compression |
|
183
|
|
|
* <li> bzip2 - Bzip2 compression |
|
184
|
|
|
* <li> lzma2 - Lzma2 compression |
|
185
|
|
|
* </ul> |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $mode |
|
188
|
|
|
*/ |
|
189
|
|
|
public function setCompression($mode) |
|
190
|
|
|
{ |
|
191
|
|
|
switch ($mode) { |
|
192
|
|
|
case "gzip": |
|
193
|
|
|
$this->compression = "gz"; |
|
194
|
|
|
break; |
|
195
|
|
|
case "bzip2": |
|
196
|
|
|
$this->compression = "bz2"; |
|
197
|
|
|
break; |
|
198
|
|
|
case "lzma2": |
|
199
|
|
|
$this->compression = "lzma2"; |
|
200
|
|
|
break; |
|
201
|
|
|
case "none": |
|
202
|
|
|
$this->compression = null; |
|
203
|
|
|
break; |
|
204
|
|
|
default: |
|
205
|
|
|
$this->log("Ignoring unknown compression mode: " . $mode, Project::MSG_WARN); |
|
206
|
|
|
$this->compression = null; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Sets the file path prefix for file in the tar file. |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $prefix Prefix |
|
214
|
|
|
* |
|
215
|
|
|
* @return void |
|
216
|
|
|
*/ |
|
217
|
|
|
public function setPrefix($prefix) |
|
218
|
|
|
{ |
|
219
|
|
|
$this->prefix = $prefix; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* do the work |
|
224
|
|
|
* |
|
225
|
|
|
* @throws BuildException |
|
226
|
|
|
*/ |
|
227
|
|
|
public function main() |
|
228
|
|
|
{ |
|
229
|
|
|
if ($this->tarFile === null) { |
|
230
|
|
|
throw new BuildException("tarfile attribute must be set!", $this->getLocation()); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
if ($this->tarFile->exists() && $this->tarFile->isDirectory()) { |
|
234
|
|
|
throw new BuildException("tarfile is a directory!", $this->getLocation()); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
if ($this->tarFile->exists() && !$this->tarFile->canWrite()) { |
|
238
|
|
|
throw new BuildException("Can not write to the specified tarfile!", $this->getLocation()); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
// shouldn't need to clone, since the entries in filesets |
|
242
|
|
|
// themselves won't be modified -- only elements will be added |
|
243
|
|
|
$savedFileSets = $this->filesets; |
|
244
|
|
|
|
|
245
|
|
|
try { |
|
246
|
|
|
if ($this->baseDir !== null) { |
|
247
|
|
|
if (!$this->baseDir->exists()) { |
|
248
|
|
|
throw new BuildException( |
|
249
|
|
|
"basedir '" . (string) $this->baseDir . "' does not exist!", |
|
250
|
|
|
$this->getLocation() |
|
251
|
|
|
); |
|
252
|
|
|
} |
|
253
|
|
|
if (empty($this->filesets)) { // if there weren't any explicit filesets specivied, then |
|
254
|
|
|
// create a default, all-inclusive fileset using the specified basedir. |
|
255
|
|
|
$mainFileSet = new TarFileSet($this->fileset); |
|
256
|
|
|
$mainFileSet->setDir($this->baseDir); |
|
257
|
|
|
$mainFileSet->setProject($this->project); |
|
258
|
|
|
$this->filesets[] = $mainFileSet; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
if (empty($this->filesets)) { |
|
263
|
|
|
throw new BuildException( |
|
264
|
|
|
"You must supply either a basedir " |
|
265
|
|
|
. "attribute or some nested filesets.", |
|
266
|
|
|
$this->getLocation() |
|
267
|
|
|
); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
// check if tar is out of date with respect to each fileset |
|
271
|
|
|
if ($this->tarFile->exists() && $this->isArchiveUpToDate()) { |
|
272
|
|
|
$this->log("Nothing to do: " . $this->tarFile->__toString() . " is up to date.", Project::MSG_INFO); |
|
273
|
|
|
return; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$this->log("Building tar: " . $this->tarFile->__toString(), Project::MSG_INFO); |
|
277
|
|
|
|
|
278
|
|
|
$tar = new Archive_Tar($this->tarFile->getAbsolutePath(), $this->compression); |
|
279
|
|
|
$pear = new PEAR(); |
|
280
|
|
|
|
|
281
|
|
|
if ($pear::isError($tar->error_object)) { |
|
282
|
|
|
throw new BuildException($tar->error_object->getMessage()); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
foreach ($this->filesets as $fs) { |
|
286
|
|
|
$files = $fs->getIterator($this->includeEmpty); |
|
287
|
|
|
if (count($files) > 1 && strlen($fs->getFullpath()) > 0) { |
|
288
|
|
|
throw new BuildException( |
|
289
|
|
|
"fullpath attribute may only " |
|
290
|
|
|
. "be specified for " |
|
291
|
|
|
. "filesets that specify a " |
|
292
|
|
|
. "single file." |
|
293
|
|
|
); |
|
294
|
|
|
} |
|
295
|
|
|
$fsBasedir = $fs->getDir($this->project); |
|
296
|
|
|
$filesToTar = []; |
|
297
|
|
|
for ($i = 0, $fcount = count($files); $i < $fcount; $i++) { |
|
298
|
|
|
$f = new File($fsBasedir, $files[$i]); |
|
299
|
|
|
$filesToTar[] = $f->getAbsolutePath(); |
|
300
|
|
|
$this->log("Adding file " . $f->getPath() . " to archive.", Project::MSG_VERBOSE); |
|
301
|
|
|
} |
|
302
|
|
|
$tar->addModify($filesToTar, $this->prefix, $fsBasedir->getAbsolutePath()); |
|
303
|
|
|
|
|
304
|
|
|
if ($pear::isError($tar->error_object)) { |
|
305
|
|
|
throw new BuildException($tar->error_object->getMessage()); |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
} catch (IOException $ioe) { |
|
309
|
|
|
$msg = "Problem creating TAR: " . $ioe->getMessage(); |
|
310
|
|
|
$this->filesets = $savedFileSets; |
|
311
|
|
|
throw new BuildException($msg, $ioe, $this->getLocation()); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
$this->filesets = $savedFileSets; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* @param \ArrayIterator $files array of filenames |
|
319
|
|
|
* @param File $dir |
|
320
|
|
|
* |
|
321
|
|
|
* @return boolean |
|
322
|
|
|
*/ |
|
323
|
|
|
protected function areFilesUpToDate($files, $dir) |
|
324
|
|
|
{ |
|
325
|
|
|
$sfs = new SourceFileScanner($this); |
|
326
|
|
|
$mm = new MergeMapper(); |
|
327
|
|
|
$mm->setTo($this->tarFile->getAbsolutePath()); |
|
328
|
|
|
|
|
329
|
|
|
return count($sfs->restrict($files, $dir, null, $mm)) == 0; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @return bool |
|
334
|
|
|
* @throws BuildException |
|
335
|
|
|
*/ |
|
336
|
|
|
private function isArchiveUpToDate() |
|
337
|
|
|
{ |
|
338
|
|
|
foreach ($this->filesets as $fs) { |
|
339
|
|
|
$files = $fs->getIterator($this->includeEmpty); |
|
340
|
|
|
if (!$this->areFilesUpToDate($files, $fs->getDir($this->project))) { |
|
341
|
|
|
return false; |
|
342
|
|
|
} |
|
343
|
|
|
for ($i = 0, $fcount = count($files); $i < $fcount; $i++) { |
|
344
|
|
|
if ($this->tarFile->equals(new File($fs->getDir($this->project), $files[$i]))) { |
|
345
|
|
|
throw new BuildException("A tar file cannot include itself", $this->getLocation()); |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
return true; |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|