1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.plexus.archiver.tar;
18
19 import javax.inject.Named;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.zip.GZIPOutputStream;
26
27 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
28 import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
29 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
30 import org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorOutputStream;
31 import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
32 import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream;
33 import org.codehaus.plexus.archiver.AbstractArchiver;
34 import org.codehaus.plexus.archiver.ArchiveEntry;
35 import org.codehaus.plexus.archiver.ArchiverException;
36 import org.codehaus.plexus.archiver.ResourceIterator;
37 import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
38 import org.codehaus.plexus.archiver.util.ResourceUtils;
39 import org.codehaus.plexus.archiver.util.Streams;
40 import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
41 import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
42 import org.codehaus.plexus.components.io.resources.PlexusIoResource;
43 import org.codehaus.plexus.util.StringUtils;
44
45 import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
46
47
48
49
50 @Named("tar")
51 public class TarArchiver extends AbstractArchiver {
52
53
54
55
56 private boolean longWarningGiven = false;
57
58 private TarLongFileMode longFileMode = TarLongFileMode.warn;
59
60 private TarCompressionMethod compression = TarCompressionMethod.none;
61
62 private final TarOptions options = new TarOptions();
63
64 private TarArchiveOutputStream tOut;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public void setLongfile(TarLongFileMode mode) {
84 this.longFileMode = mode;
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98 public void setCompression(TarCompressionMethod mode) {
99 this.compression = mode;
100 }
101
102 @Override
103 protected void execute() throws ArchiverException, IOException {
104 if (!checkForced()) {
105 return;
106 }
107
108 ResourceIterator iter = getResources();
109 if (!iter.hasNext()) {
110 throw new EmptyArchiveException("archive cannot be empty");
111 }
112
113 File tarFile = getDestFile();
114
115 if (tarFile == null) {
116 throw new ArchiverException("You must set the destination tar file.");
117 }
118 if (tarFile.exists() && !tarFile.isFile()) {
119 throw new ArchiverException(tarFile + " isn't a file.");
120 }
121 if (tarFile.exists() && !tarFile.canWrite()) {
122 throw new ArchiverException(tarFile + " is read-only.");
123 }
124
125 getLogger().info("Building tar: " + tarFile.getAbsolutePath());
126
127 try (TarArchiveOutputStream out =
128 new TarArchiveOutputStream(compress(compression, Streams.fileOutputStream(tarFile)), "UTF8")) {
129 tOut = out;
130 if (longFileMode.isTruncateMode()) {
131 tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE);
132 } else if (longFileMode.isPosixMode() || longFileMode.isPosixWarnMode()) {
133 tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
134
135 tOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
136 } else if (longFileMode.isFailMode() || longFileMode.isOmitMode()) {
137 tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_ERROR);
138 } else {
139
140 tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
141 }
142
143 longWarningGiven = false;
144 while (iter.hasNext()) {
145 ArchiveEntry entry = iter.next();
146
147 if (ResourceUtils.isSame(entry.getResource(), tarFile)) {
148 throw new ArchiverException("A tar file cannot include itself.");
149 }
150 String fileName = entry.getName();
151 String name = StringUtils.replace(fileName, File.separatorChar, '/');
152
153 tarFile(entry, tOut, name);
154 }
155 }
156 }
157
158
159
160
161
162
163
164
165
166
167 protected void tarFile(ArchiveEntry entry, TarArchiveOutputStream tOut, String vPath)
168 throws ArchiverException, IOException {
169
170
171 if (vPath.length() <= 0) {
172 return;
173 }
174
175 if (entry.getResource().isDirectory() && !vPath.endsWith("/")) {
176 vPath += "/";
177 }
178
179 if (vPath.startsWith("/") && !options.getPreserveLeadingSlashes()) {
180 int l = vPath.length();
181 if (l <= 1) {
182
183 return;
184 }
185 vPath = vPath.substring(1, l);
186 }
187
188 int pathLength = vPath.length();
189 TarArchiveEntry te;
190 if (!longFileMode.isGnuMode() && pathLength >= org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN) {
191 int maxPosixPathLen = org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN
192 + org.apache.commons.compress.archivers.tar.TarConstants.PREFIXLEN;
193 if (longFileMode.isPosixMode()) {
194 } else if (longFileMode.isPosixWarnMode()) {
195 if (pathLength > maxPosixPathLen) {
196 getLogger().warn("Entry: " + vPath + " longer than " + maxPosixPathLen + " characters.");
197 if (!longWarningGiven) {
198 getLogger()
199 .warn("Resulting tar file can only be processed "
200 + "successfully by GNU compatible tar commands");
201 longWarningGiven = true;
202 }
203 }
204 } else if (longFileMode.isOmitMode()) {
205 getLogger().info("Omitting: " + vPath);
206 return;
207 } else if (longFileMode.isWarnMode()) {
208 getLogger()
209 .warn("Entry: " + vPath + " longer than "
210 + org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN
211 + " characters.");
212 if (!longWarningGiven) {
213 getLogger()
214 .warn("Resulting tar file can only be processed "
215 + "successfully by GNU compatible tar commands");
216 longWarningGiven = true;
217 }
218 } else if (longFileMode.isFailMode()) {
219 throw new ArchiverException("Entry: " + vPath + " longer than "
220 + org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN
221 + " characters.");
222 } else {
223 throw new IllegalStateException("Non gnu mode should never get here?");
224 }
225 }
226
227 if (entry.getType() == ArchiveEntry.SYMLINK) {
228 final SymlinkDestinationSupplier plexusIoSymlinkResource = (SymlinkDestinationSupplier) entry.getResource();
229
230 te = new TarArchiveEntry(vPath, TarArchiveEntry.LF_SYMLINK);
231 te.setLinkName(plexusIoSymlinkResource.getSymlinkDestination());
232 } else {
233 te = new TarArchiveEntry(vPath);
234 }
235
236 if (getLastModifiedTime() == null) {
237 long teLastModified = entry.getResource().getLastModified();
238 te.setModTime(
239 teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE
240 ? System.currentTimeMillis()
241 : teLastModified);
242 } else {
243 te.setModTime(getLastModifiedTime().toMillis());
244 }
245
246 if (entry.getType() == ArchiveEntry.SYMLINK) {
247 te.setSize(0);
248
249 } else if (!entry.getResource().isDirectory()) {
250 final long size = entry.getResource().getSize();
251 te.setSize(size == PlexusIoResource.UNKNOWN_RESOURCE_SIZE ? 0 : size);
252 }
253 te.setMode(entry.getMode());
254
255 PlexusIoResourceAttributes attributes = entry.getResourceAttributes();
256
257 te.setUserName(
258 (attributes != null && attributes.getUserName() != null)
259 ? attributes.getUserName()
260 : options.getUserName());
261 te.setGroupName(
262 (attributes != null && attributes.getGroupName() != null)
263 ? attributes.getGroupName()
264 : options.getGroup());
265
266 final int userId =
267 (attributes != null && attributes.getUserId() != null) ? attributes.getUserId() : options.getUid();
268 if (userId >= 0) {
269 te.setUserId(userId);
270 }
271
272 final int groupId =
273 (attributes != null && attributes.getGroupId() != null) ? attributes.getGroupId() : options.getGid();
274 if (groupId >= 0) {
275 te.setGroupId(groupId);
276 }
277
278 tOut.putArchiveEntry(te);
279
280 try {
281 if (entry.getResource().isFile() && !(entry.getType() == ArchiveEntry.SYMLINK)) {
282 try (InputStream fIn = entry.getInputStream()) {
283 Streams.copyFullyDontCloseOutput(fIn, tOut, "xAR");
284 }
285 }
286
287 } catch (Throwable e) {
288 getLogger().warn("When creating tar entry", e);
289 } finally {
290 tOut.closeArchiveEntry();
291 }
292 }
293
294
295
296
297 public class TarOptions {
298
299 private String userName = "";
300
301 private String groupName = "";
302
303 private int uid;
304
305 private int gid;
306
307 private boolean preserveLeadingSlashes = false;
308
309
310
311
312
313
314
315 public void setUserName(String userName) {
316 this.userName = userName;
317 }
318
319
320
321
322 public String getUserName() {
323 return userName;
324 }
325
326
327
328
329
330
331
332 public void setUid(int uid) {
333 this.uid = uid;
334 }
335
336
337
338
339 public int getUid() {
340 return uid;
341 }
342
343
344
345
346
347
348
349 public void setGroup(String groupName) {
350 this.groupName = groupName;
351 }
352
353
354
355
356 public String getGroup() {
357 return groupName;
358 }
359
360
361
362
363
364
365
366 public void setGid(int gid) {
367 this.gid = gid;
368 }
369
370
371
372
373 public int getGid() {
374 return gid;
375 }
376
377
378
379
380 public boolean getPreserveLeadingSlashes() {
381 return preserveLeadingSlashes;
382 }
383
384
385
386
387
388
389
390
391 public void setPreserveLeadingSlashes(boolean preserveLeadingSlashes) {
392 this.preserveLeadingSlashes = preserveLeadingSlashes;
393 }
394 }
395
396
397
398
399 public enum TarCompressionMethod {
400 none,
401 gzip,
402 bzip2,
403 snappy,
404 xz,
405 zstd
406 }
407
408 private OutputStream compress(TarCompressionMethod tarCompressionMethod, final OutputStream ostream)
409 throws IOException {
410 if (TarCompressionMethod.gzip.equals(tarCompressionMethod)) {
411 return bufferedOutputStream(new GZIPOutputStream(ostream));
412 } else if (TarCompressionMethod.bzip2.equals(tarCompressionMethod)) {
413 return new BZip2CompressorOutputStream(bufferedOutputStream(ostream));
414 } else if (TarCompressionMethod.snappy.equals(tarCompressionMethod)) {
415 return new FramedSnappyCompressorOutputStream(bufferedOutputStream(ostream));
416 } else if (TarCompressionMethod.xz.equals(tarCompressionMethod)) {
417 return new XZCompressorOutputStream(bufferedOutputStream(ostream));
418 } else if (TarCompressionMethod.zstd.equals(tarCompressionMethod)) {
419 return new ZstdCompressorOutputStream(bufferedOutputStream(ostream));
420 }
421
422 return bufferedOutputStream(ostream);
423 }
424
425 @Override
426 public boolean isSupportingForced() {
427 return true;
428 }
429
430 @Override
431 protected void cleanUp() throws IOException {
432 super.cleanUp();
433 if (this.tOut != null) {
434 this.tOut.close();
435 }
436 }
437
438 @Override
439 protected void close() throws IOException {
440 if (this.tOut != null) {
441 this.tOut.close();
442 }
443 }
444
445 @Override
446 protected String getArchiveType() {
447 return "TAR";
448 }
449 }