View Javadoc
1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
49   */
50  @Named("tar")
51  public class TarArchiver extends AbstractArchiver {
52  
53      /**
54       * Indicates whether the user has been warned about long files already.
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       * Set how to handle long files, those with a path&gt;100 chars.
68       * Optional, default=warn.
69       * <p>
70       * Allowable values are </p>
71       * <ul>
72       * <li> truncate - paths are truncated to the maximum length </li>
73       * <li> fail - paths greater than the maximum cause a build exception </li>
74       * <li> warn - paths greater than the maximum cause a warning and GNU is used </li>
75       * <li> gnu - GNU extensions are used for any paths greater than the maximum. </li>
76       * <li> posix - posix extensions are used for any paths greater than the maximum. </li>
77       * <li> posixwarn - posix extensions are used (with warning) for any paths greater than the maximum. </li>
78       * <li> omit - paths greater than the maximum are omitted from the archive </li>
79       * </ul>
80       *
81       * @param mode the mode to handle long file names.
82       */
83      public void setLongfile(TarLongFileMode mode) {
84          this.longFileMode = mode;
85      }
86  
87      /**
88       * Set compression method.
89       * Allowable values are
90       * <ul>
91       * <li> none - no compression
92       * <li> gzip - Gzip compression
93       * <li> bzip2 - Bzip2 compression
94       * </ul>
95       *
96       * @param mode the compression method.
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                 // Todo: Patch 2.5.1   for this fix. Also make closeable fix on 2.5.1
135                 tOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
136             } else if (longFileMode.isFailMode() || longFileMode.isOmitMode()) {
137                 tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_ERROR);
138             } else {
139                 // warn or GNU
140                 tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
141             }
142 
143             longWarningGiven = false;
144             while (iter.hasNext()) {
145                 ArchiveEntry entry = iter.next();
146                 // Check if we don't add tar file in itself
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      * tar a file
160      *
161      * @param entry the file to tar
162      * @param tOut the output stream
163      * @param vPath the path name of the file to tar
164      *
165      * @throws IOException on error
166      */
167     protected void tarFile(ArchiveEntry entry, TarArchiveOutputStream tOut, String vPath)
168             throws ArchiverException, IOException {
169 
170         // don't add "" to the archive
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                 // we would end up adding "" to the archive
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      * Valid Modes for Compression attribute to Tar Task
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          * The username for the tar entry
311          * This is not the same as the UID.
312          *
313          * @param userName the user name for the tar entry.
314          */
315         public void setUserName(String userName) {
316             this.userName = userName;
317         }
318 
319         /**
320          * @return the user name for the tar entry
321          */
322         public String getUserName() {
323             return userName;
324         }
325 
326         /**
327          * The uid for the tar entry
328          * This is not the same as the User name.
329          *
330          * @param uid the id of the user for the tar entry.
331          */
332         public void setUid(int uid) {
333             this.uid = uid;
334         }
335 
336         /**
337          * @return the uid for the tar entry
338          */
339         public int getUid() {
340             return uid;
341         }
342 
343         /**
344          * The groupname for the tar entry; optional, default=""
345          * This is not the same as the GID.
346          *
347          * @param groupName the group name string.
348          */
349         public void setGroup(String groupName) {
350             this.groupName = groupName;
351         }
352 
353         /**
354          * @return the group name string.
355          */
356         public String getGroup() {
357             return groupName;
358         }
359 
360         /**
361          * The GID for the tar entry; optional, default="0"
362          * This is not the same as the group name.
363          *
364          * @param gid the group id.
365          */
366         public void setGid(int gid) {
367             this.gid = gid;
368         }
369 
370         /**
371          * @return the group identifier.
372          */
373         public int getGid() {
374             return gid;
375         }
376 
377         /**
378          * @return the leading slashes flag.
379          */
380         public boolean getPreserveLeadingSlashes() {
381             return preserveLeadingSlashes;
382         }
383 
384         /**
385          * Flag to indicates whether leading `/'s should
386          * be preserved in the file names.
387          * Optional, default is <code>false</code>.
388          *
389          * @param preserveLeadingSlashes the leading slashes flag.
390          */
391         public void setPreserveLeadingSlashes(boolean preserveLeadingSlashes) {
392             this.preserveLeadingSlashes = preserveLeadingSlashes;
393         }
394     }
395 
396     /**
397      * Valid Modes for Compression attribute to Tar Task
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 }