forked from codehaus-plexus/plexus-archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTarArchiver.java
519 lines (459 loc) · 16.2 KB
/
TarArchiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package org.codehaus.plexus.archiver.tar;
/**
*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
import org.codehaus.plexus.archiver.AbstractArchiver;
import org.codehaus.plexus.archiver.ArchiveEntry;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.ResourceIterator;
import org.codehaus.plexus.archiver.util.ResourceUtils;
import org.codehaus.plexus.archiver.util.Streams;
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.iq80.snappy.SnappyOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
/**
* @author <a href="mailto:[email protected]">Emmanuel Venisse</a>
* @version $Revision$ $Date$
*/
public class TarArchiver
extends AbstractArchiver
{
/**
* Indicates whether the user has been warned about long files already.
*/
private boolean longWarningGiven = false;
private TarLongFileMode longFileMode = TarLongFileMode.warn;
private TarCompressionMethod compression = TarCompressionMethod.none;
private TarOptions options = new TarOptions();
private TarArchiveOutputStream tOut;
/**
* Set how to handle long files, those with a path>100 chars.
* Optional, default=warn.
* <p/>
* Allowable values are
* <ul>
* <li> truncate - paths are truncated to the maximum length
* <li> fail - paths greater than the maximum cause a build exception
* <li> warn - paths greater than the maximum cause a warning and GNU is used
* <li> gnu - GNU extensions are used for any paths greater than the maximum.
* <li> posix - posix extensions are used for any paths greater than the maximum.
* <li> posixwarn - posix extensions are used (with warning) for any paths greater than the maximum.
* <li> omit - paths greater than the maximum are omitted from the archive
* </ul>
*
* @param mode the mode to handle long file names.
*/
public void setLongfile( TarLongFileMode mode )
{
this.longFileMode = mode;
}
/**
* Set compression method.
* Allowable values are
* <ul>
* <li> none - no compression
* <li> gzip - Gzip compression
* <li> bzip2 - Bzip2 compression
* </ul>
*
* @param mode the compression method.
*/
public void setCompression( TarCompressionMethod mode )
{
this.compression = mode;
}
protected void execute()
throws ArchiverException, IOException
{
if ( !checkForced() )
{
return;
}
ResourceIterator iter = getResources();
if ( !iter.hasNext() )
{
throw new ArchiverException( "You must set at least one file." );
}
File tarFile = getDestFile();
if ( tarFile == null )
{
throw new ArchiverException( "You must set the destination tar file." );
}
if ( tarFile.exists() && !tarFile.isFile() )
{
throw new ArchiverException( tarFile + " isn't a file." );
}
if ( tarFile.exists() && !tarFile.canWrite() )
{
throw new ArchiverException( tarFile + " is read-only." );
}
getLogger().info( "Building tar: " + tarFile.getAbsolutePath() );
final OutputStream os = new FileOutputStream( tarFile );
tOut = new TarArchiveOutputStream( compress( compression, os ), "UTF8" );
if ( longFileMode.isTruncateMode() )
{
tOut.setLongFileMode( TarArchiveOutputStream.LONGFILE_TRUNCATE );
}
else if ( longFileMode.isPosixMode() || longFileMode.isPosixWarnMode() )
{
tOut.setLongFileMode( TarArchiveOutputStream.LONGFILE_POSIX );
// Todo: Patch 2.5.1 for this fix. Also make closeable fix on 2.5.1
tOut.setBigNumberMode( TarArchiveOutputStream.BIGNUMBER_POSIX );
}
else if ( longFileMode.isFailMode() || longFileMode.isOmitMode() )
{
tOut.setLongFileMode( TarArchiveOutputStream.LONGFILE_ERROR );
}
else
{
// warn or GNU
tOut.setLongFileMode( TarArchiveOutputStream.LONGFILE_GNU );
}
longWarningGiven = false;
try
{
while ( iter.hasNext() )
{
ArchiveEntry entry = iter.next();
// Check if we don't add tar file in itself
if ( ResourceUtils.isSame( entry.getResource(), tarFile ) )
{
throw new ArchiverException( "A tar file cannot include itself." );
}
String fileName = entry.getName();
String name = StringUtils.replace( fileName, File.separatorChar, '/' );
tarFile( entry, tOut, name );
}
}
finally
{
IOUtil.close( tOut );
}
}
/**
* tar a file
*
* @param entry the file to tar
* @param tOut the output stream
* @param vPath the path name of the file to tar
* @throws IOException on error
*/
protected void tarFile( ArchiveEntry entry, TarArchiveOutputStream tOut, String vPath )
throws ArchiverException, IOException
{
// don't add "" to the archive
if ( vPath.length() <= 0 )
{
return;
}
if ( entry.getResource().isDirectory() && !vPath.endsWith( "/" ) )
{
vPath += "/";
}
if ( vPath.startsWith( "/" ) && !options.getPreserveLeadingSlashes() )
{
int l = vPath.length();
if ( l <= 1 )
{
// we would end up adding "" to the archive
return;
}
vPath = vPath.substring( 1, l );
}
int pathLength = vPath.length();
InputStream fIn = null;
try
{
TarArchiveEntry te;
if ( !longFileMode.isGnuMode()
&& pathLength >= org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN )
{
int maxPosixPathLen = org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN
+ org.apache.commons.compress.archivers.tar.TarConstants.PREFIXLEN;
if ( longFileMode.isPosixMode() )
{
}
else if ( longFileMode.isPosixWarnMode() )
{
if ( pathLength > maxPosixPathLen )
{
getLogger().warn( "Entry: " + vPath + " longer than " + maxPosixPathLen + " characters." );
if ( !longWarningGiven )
{
getLogger().warn( "Resulting tar file can only be processed "
+ "successfully by GNU compatible tar commands" );
longWarningGiven = true;
}
}
}
else if ( longFileMode.isOmitMode() )
{
getLogger().info( "Omitting: " + vPath );
return;
}
else if ( longFileMode.isWarnMode() )
{
getLogger().warn( "Entry: " + vPath + " longer than "
+ org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN
+ " characters." );
if ( !longWarningGiven )
{
getLogger().warn( "Resulting tar file can only be processed "
+ "successfully by GNU compatible tar commands" );
longWarningGiven = true;
}
}
else if ( longFileMode.isFailMode() )
{
throw new ArchiverException( "Entry: " + vPath + " longer than "
+ org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN
+ " characters." );
}
else
{
throw new IllegalStateException( "Non gnu mode should never get here?" );
}
}
if ( entry.getType() == ArchiveEntry.SYMLINK )
{
final SymlinkDestinationSupplier plexusIoSymlinkResource =
(SymlinkDestinationSupplier) entry.getResource();
te = new TarArchiveEntry( vPath, TarArchiveEntry.LF_SYMLINK );
te.setLinkName( plexusIoSymlinkResource.getSymlinkDestination() );
}
else
{
te = new TarArchiveEntry( vPath );
}
long teLastModified = entry.getResource().getLastModified();
te.setModTime( teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE
? System.currentTimeMillis()
: teLastModified );
if ( entry.getType() == ArchiveEntry.SYMLINK )
{
te.setSize( 0 );
}
else if ( !entry.getResource().isDirectory() )
{
final long size = entry.getResource().getSize();
te.setSize( size == PlexusIoResource.UNKNOWN_RESOURCE_SIZE ? 0 : size );
}
te.setMode( entry.getMode() );
PlexusIoResourceAttributes attributes = entry.getResourceAttributes();
te.setUserName( ( attributes != null && attributes.getUserName() != null )
? attributes.getUserName()
: options.getUserName() );
te.setGroupName( ( attributes != null && attributes.getGroupName() != null )
? attributes.getGroupName()
: options.getGroup() );
final int userId =
( attributes != null && attributes.getUserId() != null ) ? attributes.getUserId() : options.getUid();
if ( userId >= 0 )
{
te.setUserId( userId );
}
final int groupId =
( attributes != null && attributes.getGroupId() != null ) ? attributes.getGroupId() : options.getGid();
if ( groupId >= 0 )
{
te.setGroupId( groupId );
}
tOut.putArchiveEntry( te );
try
{
if ( entry.getResource().isFile() && !( entry.getType() == ArchiveEntry.SYMLINK ) )
{
fIn = entry.getInputStream();
Streams.copyFullyDontCloseOutput( fIn, tOut, "xAR" );
}
}
catch ( Throwable e )
{
getLogger().warn( "When creating tar entry", e );
}
finally
{
tOut.closeArchiveEntry();
}
}
finally
{
IOUtil.close( fIn );
}
}
/**
* Valid Modes for Compression attribute to Tar Task
*/
public class TarOptions
{
private String userName = "";
private String groupName = "";
private int uid;
private int gid;
private boolean preserveLeadingSlashes = false;
/**
* The username for the tar entry
* This is not the same as the UID.
*
* @param userName the user name for the tar entry.
*/
public void setUserName( String userName )
{
this.userName = userName;
}
/**
* @return the user name for the tar entry
*/
public String getUserName()
{
return userName;
}
/**
* The uid for the tar entry
* This is not the same as the User name.
*
* @param uid the id of the user for the tar entry.
*/
public void setUid( int uid )
{
this.uid = uid;
}
/**
* @return the uid for the tar entry
*/
public int getUid()
{
return uid;
}
/**
* The groupname for the tar entry; optional, default=""
* This is not the same as the GID.
*
* @param groupName the group name string.
*/
public void setGroup( String groupName )
{
this.groupName = groupName;
}
/**
* @return the group name string.
*/
public String getGroup()
{
return groupName;
}
/**
* The GID for the tar entry; optional, default="0"
* This is not the same as the group name.
*
* @param gid the group id.
*/
public void setGid( int gid )
{
this.gid = gid;
}
/**
* @return the group identifier.
*/
public int getGid()
{
return gid;
}
/**
* @return the leading slashes flag.
*/
public boolean getPreserveLeadingSlashes()
{
return preserveLeadingSlashes;
}
/**
* Flag to indicates whether leading `/'s should
* be preserved in the file names.
* Optional, default is <code>false</code>.
*
* @param preserveLeadingSlashes the leading slashes flag.
*/
public void setPreserveLeadingSlashes( boolean preserveLeadingSlashes )
{
this.preserveLeadingSlashes = preserveLeadingSlashes;
}
}
/**
* Valid Modes for Compression attribute to Tar Task
*/
public static enum TarCompressionMethod
{
none,
gzip,
bzip2,
snappy,
xz
}
private OutputStream compress( TarCompressionMethod tarCompressionMethod, final OutputStream ostream )
throws IOException
{
if ( TarCompressionMethod.gzip.equals( tarCompressionMethod ) )
{
return Streams.bufferedOutputStream( new GZIPOutputStream( ostream ) );
}
else if ( TarCompressionMethod.bzip2.equals( tarCompressionMethod ) )
{
return new BZip2CompressorOutputStream( bufferedOutputStream( ostream ) );
}
else if ( TarCompressionMethod.snappy.equals( tarCompressionMethod ) )
{
return new SnappyOutputStream( bufferedOutputStream( ostream ) );
}
else if (TarCompressionMethod.xz.equals( tarCompressionMethod ) )
{
return new XZCompressorOutputStream( bufferedOutputStream( ostream ) );
}
return ostream;
}
public boolean isSupportingForced()
{
return true;
}
protected void cleanUp()
throws IOException
{
super.cleanUp();
IOUtil.close( tOut );
}
protected void close()
throws IOException
{
IOUtil.close( tOut );
}
protected String getArchiveType()
{
return "TAR";
}
}