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