001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.BufferedOutputStream;
022: import java.io.File;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027: import java.util.Enumeration;
028: import java.util.HashMap;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.Vector;
032: import java.util.zip.GZIPOutputStream;
033: import org.apache.tools.ant.BuildException;
034: import org.apache.tools.ant.DirectoryScanner;
035: import org.apache.tools.ant.Project;
036: import org.apache.tools.ant.types.ArchiveFileSet;
037: import org.apache.tools.ant.types.EnumeratedAttribute;
038: import org.apache.tools.ant.types.FileSet;
039: import org.apache.tools.ant.types.Resource;
040: import org.apache.tools.ant.types.ResourceCollection;
041: import org.apache.tools.ant.types.resources.ArchiveResource;
042: import org.apache.tools.ant.types.resources.FileResource;
043: import org.apache.tools.ant.types.selectors.SelectorUtils;
044: import org.apache.tools.ant.types.resources.TarResource;
045: import org.apache.tools.ant.util.FileUtils;
046: import org.apache.tools.ant.util.MergingMapper;
047: import org.apache.tools.ant.util.SourceFileScanner;
048: import org.apache.tools.bzip2.CBZip2OutputStream;
049: import org.apache.tools.tar.TarConstants;
050: import org.apache.tools.tar.TarEntry;
051: import org.apache.tools.tar.TarOutputStream;
052:
053: /**
054: * Creates a tar archive.
055: *
056: * @since Ant 1.1
057: *
058: * @ant.task category="packaging"
059: */
060: public class Tar extends MatchingTask {
061:
062: /**
063: * @deprecated since 1.5.x.
064: * Tar.WARN is deprecated and is replaced with
065: * Tar.TarLongFileMode.WARN
066: */
067: public static final String WARN = "warn";
068: /**
069: * @deprecated since 1.5.x.
070: * Tar.FAIL is deprecated and is replaced with
071: * Tar.TarLongFileMode.FAIL
072: */
073: public static final String FAIL = "fail";
074: /**
075: * @deprecated since 1.5.x.
076: * Tar.TRUNCATE is deprecated and is replaced with
077: * Tar.TarLongFileMode.TRUNCATE
078: */
079: public static final String TRUNCATE = "truncate";
080: /**
081: * @deprecated since 1.5.x.
082: * Tar.GNU is deprecated and is replaced with
083: * Tar.TarLongFileMode.GNU
084: */
085: public static final String GNU = "gnu";
086: /**
087: * @deprecated since 1.5.x.
088: * Tar.OMIT is deprecated and is replaced with
089: * Tar.TarLongFileMode.OMIT
090: */
091: public static final String OMIT = "omit";
092:
093: // CheckStyle:VisibilityModifier OFF - bc
094: File tarFile;
095: File baseDir;
096:
097: private TarLongFileMode longFileMode = new TarLongFileMode();
098:
099: // need to keep the package private version for backwards compatibility
100: Vector filesets = new Vector();
101: // we must keep two lists since other classes may modify the
102: // filesets Vector (it is package private) without us noticing
103: private Vector resourceCollections = new Vector();
104:
105: Vector fileSetFiles = new Vector();
106:
107: // CheckStyle:VisibilityModifier ON
108:
109: /**
110: * Indicates whether the user has been warned about long files already.
111: */
112: private boolean longWarningGiven = false;
113:
114: private TarCompressionMethod compression = new TarCompressionMethod();
115:
116: /**
117: * Add a new fileset with the option to specify permissions
118: * @return the tar fileset to be used as the nested element.
119: */
120: public TarFileSet createTarFileSet() {
121: TarFileSet fs = new TarFileSet();
122: fs.setProject(getProject());
123: filesets.addElement(fs);
124: return fs;
125: }
126:
127: /**
128: * Add a collection of resources to archive.
129: * @param res a resource collection to archive.
130: * @since Ant 1.7
131: */
132: public void add(ResourceCollection res) {
133: resourceCollections.add(res);
134: }
135:
136: /**
137: * Set is the name/location of where to create the tar file.
138: * @param tarFile the location of the tar file.
139: * @deprecated since 1.5.x.
140: * For consistency with other tasks, please use setDestFile().
141: */
142: public void setTarfile(File tarFile) {
143: this .tarFile = tarFile;
144: }
145:
146: /**
147: * Set is the name/location of where to create the tar file.
148: * @since Ant 1.5
149: * @param destFile The output of the tar
150: */
151: public void setDestFile(File destFile) {
152: this .tarFile = destFile;
153: }
154:
155: /**
156: * This is the base directory to look in for things to tar.
157: * @param baseDir the base directory.
158: */
159: public void setBasedir(File baseDir) {
160: this .baseDir = baseDir;
161: }
162:
163: /**
164: * Set how to handle long files, those with a path>100 chars.
165: * Optional, default=warn.
166: * <p>
167: * Allowable values are
168: * <ul>
169: * <li> truncate - paths are truncated to the maximum length
170: * <li> fail - paths greater than the maximum cause a build exception
171: * <li> warn - paths greater than the maximum cause a warning and GNU is used
172: * <li> gnu - GNU extensions are used for any paths greater than the maximum.
173: * <li> omit - paths greater than the maximum are omitted from the archive
174: * </ul>
175: * @param mode the mode string to handle long files.
176: * @deprecated since 1.5.x.
177: * setLongFile(String) is deprecated and is replaced with
178: * setLongFile(Tar.TarLongFileMode) to make Ant's Introspection
179: * mechanism do the work and also to encapsulate operations on
180: * the mode in its own class.
181: */
182: public void setLongfile(String mode) {
183: log("DEPRECATED - The setLongfile(String) method has been deprecated."
184: + " Use setLongfile(Tar.TarLongFileMode) instead.");
185: this .longFileMode = new TarLongFileMode();
186: longFileMode.setValue(mode);
187: }
188:
189: /**
190: * Set how to handle long files, those with a path>100 chars.
191: * Optional, default=warn.
192: * <p>
193: * Allowable values are
194: * <ul>
195: * <li> truncate - paths are truncated to the maximum length
196: * <li> fail - paths greater than the maximum cause a build exception
197: * <li> warn - paths greater than the maximum cause a warning and GNU is used
198: * <li> gnu - GNU extensions are used for any paths greater than the maximum.
199: * <li> omit - paths greater than the maximum are omitted from the archive
200: * </ul>
201: * @param mode the mode to handle long file names.
202: */
203: public void setLongfile(TarLongFileMode mode) {
204: this .longFileMode = mode;
205: }
206:
207: /**
208: * Set compression method.
209: * Allowable values are
210: * <ul>
211: * <li> none - no compression
212: * <li> gzip - Gzip compression
213: * <li> bzip2 - Bzip2 compression
214: * </ul>
215: * @param mode the compression method.
216: */
217: public void setCompression(TarCompressionMethod mode) {
218: this .compression = mode;
219: }
220:
221: /**
222: * do the business
223: * @throws BuildException on error
224: */
225: public void execute() throws BuildException {
226: if (tarFile == null) {
227: throw new BuildException("tarfile attribute must be set!",
228: getLocation());
229: }
230:
231: if (tarFile.exists() && tarFile.isDirectory()) {
232: throw new BuildException("tarfile is a directory!",
233: getLocation());
234: }
235:
236: if (tarFile.exists() && !tarFile.canWrite()) {
237: throw new BuildException(
238: "Can not write to the specified tarfile!",
239: getLocation());
240: }
241:
242: Vector savedFileSets = (Vector) filesets.clone();
243: try {
244: if (baseDir != null) {
245: if (!baseDir.exists()) {
246: throw new BuildException("basedir does not exist!",
247: getLocation());
248: }
249:
250: // add the main fileset to the list of filesets to process.
251: TarFileSet mainFileSet = new TarFileSet(fileset);
252: mainFileSet.setDir(baseDir);
253: filesets.addElement(mainFileSet);
254: }
255:
256: if (filesets.size() == 0 && resourceCollections.size() == 0) {
257: throw new BuildException(
258: "You must supply either a basedir "
259: + "attribute or some nested resource"
260: + " collections.", getLocation());
261: }
262:
263: // check if tar is out of date with respect to each
264: // fileset
265: boolean upToDate = true;
266: for (Enumeration e = filesets.elements(); e
267: .hasMoreElements();) {
268: upToDate &= check((TarFileSet) e.nextElement());
269: }
270: for (Enumeration e = resourceCollections.elements(); e
271: .hasMoreElements();) {
272: upToDate &= check((ResourceCollection) e.nextElement());
273: }
274:
275: if (upToDate) {
276: log("Nothing to do: " + tarFile.getAbsolutePath()
277: + " is up to date.", Project.MSG_INFO);
278: return;
279: }
280:
281: log("Building tar: " + tarFile.getAbsolutePath(),
282: Project.MSG_INFO);
283:
284: TarOutputStream tOut = null;
285: try {
286: tOut = new TarOutputStream(compression
287: .compress(new BufferedOutputStream(
288: new FileOutputStream(tarFile))));
289: tOut.setDebug(true);
290: if (longFileMode.isTruncateMode()) {
291: tOut
292: .setLongFileMode(TarOutputStream.LONGFILE_TRUNCATE);
293: } else if (longFileMode.isFailMode()
294: || longFileMode.isOmitMode()) {
295: tOut
296: .setLongFileMode(TarOutputStream.LONGFILE_ERROR);
297: } else {
298: // warn or GNU
299: tOut.setLongFileMode(TarOutputStream.LONGFILE_GNU);
300: }
301:
302: longWarningGiven = false;
303: for (Enumeration e = filesets.elements(); e
304: .hasMoreElements();) {
305: tar((TarFileSet) e.nextElement(), tOut);
306: }
307: for (Enumeration e = resourceCollections.elements(); e
308: .hasMoreElements();) {
309: tar((ResourceCollection) e.nextElement(), tOut);
310: }
311: } catch (IOException ioe) {
312: String msg = "Problem creating TAR: "
313: + ioe.getMessage();
314: throw new BuildException(msg, ioe, getLocation());
315: } finally {
316: FileUtils.close(tOut);
317: }
318: } finally {
319: filesets = savedFileSets;
320: }
321: }
322:
323: /**
324: * tar a file
325: * @param file the file to tar
326: * @param tOut the output stream
327: * @param vPath the path name of the file to tar
328: * @param tarFileSet the fileset that the file came from.
329: * @throws IOException on error
330: */
331: protected void tarFile(File file, TarOutputStream tOut,
332: String vPath, TarFileSet tarFileSet) throws IOException {
333: if (file.equals(tarFile)) {
334: // If the archive is built for the first time and it is
335: // matched by a resource collection, then it hasn't been
336: // found in check (it hasn't been there) but will be
337: // included now.
338: //
339: // for some strange reason the old code would simply skip
340: // the entry and not fail, do the same now for backwards
341: // compatibility reasons. Without this, the which4j build
342: // fails in Gump
343: return;
344: }
345: tarResource(new FileResource(file), tOut, vPath, tarFileSet);
346: }
347:
348: /**
349: * tar a resource
350: * @param r the resource to tar
351: * @param tOut the output stream
352: * @param vPath the path name of the file to tar
353: * @param tarFileSet the fileset that the file came from, may be null.
354: * @throws IOException on error
355: * @since Ant 1.7
356: */
357: protected void tarResource(Resource r, TarOutputStream tOut,
358: String vPath, TarFileSet tarFileSet) throws IOException {
359:
360: if (!r.isExists()) {
361: return;
362: }
363:
364: if (tarFileSet != null) {
365: String fullpath = tarFileSet.getFullpath(this .getProject());
366: if (fullpath.length() > 0) {
367: vPath = fullpath;
368: } else {
369: // don't add "" to the archive
370: if (vPath.length() <= 0) {
371: return;
372: }
373:
374: String prefix = tarFileSet.getPrefix(this .getProject());
375: // '/' is appended for compatibility with the zip task.
376: if (prefix.length() > 0 && !prefix.endsWith("/")) {
377: prefix = prefix + "/";
378: }
379: vPath = prefix + vPath;
380: }
381:
382: if (vPath.startsWith("/")
383: && !tarFileSet.getPreserveLeadingSlashes()) {
384: int l = vPath.length();
385: if (l <= 1) {
386: // we would end up adding "" to the archive
387: return;
388: }
389: vPath = vPath.substring(1, l);
390: }
391: }
392:
393: if (r.isDirectory() && !vPath.endsWith("/")) {
394: vPath += "/";
395: }
396:
397: if (vPath.length() >= TarConstants.NAMELEN) {
398: if (longFileMode.isOmitMode()) {
399: log("Omitting: " + vPath, Project.MSG_INFO);
400: return;
401: } else if (longFileMode.isWarnMode()) {
402: log("Entry: " + vPath + " longer than "
403: + TarConstants.NAMELEN + " characters.",
404: Project.MSG_WARN);
405: if (!longWarningGiven) {
406: log(
407: "Resulting tar file can only be processed "
408: + "successfully by GNU compatible tar commands",
409: Project.MSG_WARN);
410: longWarningGiven = true;
411: }
412: } else if (longFileMode.isFailMode()) {
413: throw new BuildException("Entry: " + vPath
414: + " longer than " + TarConstants.NAMELEN
415: + "characters.", getLocation());
416: }
417: }
418:
419: TarEntry te = new TarEntry(vPath);
420: te.setModTime(r.getLastModified());
421: // preserve permissions
422: if (r instanceof ArchiveResource) {
423: ArchiveResource ar = (ArchiveResource) r;
424: te.setMode(ar.getMode());
425: if (r instanceof TarResource) {
426: TarResource tr = (TarResource) r;
427: te.setUserName(tr.getUserName());
428: te.setUserId(tr.getUid());
429: te.setGroupName(tr.getGroup());
430: te.setGroupId(tr.getGid());
431: }
432: }
433:
434: if (!r.isDirectory()) {
435: if (r.size() > TarConstants.MAXSIZE) {
436: throw new BuildException("Resource: " + r
437: + " larger than " + TarConstants.MAXSIZE
438: + " bytes.");
439: }
440: te.setSize(r.getSize());
441: // override permissions if set explicitly
442: if (tarFileSet != null && tarFileSet.hasFileModeBeenSet()) {
443: te.setMode(tarFileSet.getMode());
444: }
445: } else if (tarFileSet != null && tarFileSet.hasDirModeBeenSet()) {
446: // override permissions if set explicitly
447: te.setMode(tarFileSet.getDirMode(this .getProject()));
448: }
449:
450: if (tarFileSet != null) {
451: // only override permissions if set explicitly
452: if (tarFileSet.hasUserNameBeenSet()) {
453: te.setUserName(tarFileSet.getUserName());
454: }
455: if (tarFileSet.hasGroupBeenSet()) {
456: te.setGroupName(tarFileSet.getGroup());
457: }
458: if (tarFileSet.hasUserIdBeenSet()) {
459: te.setUserId(tarFileSet.getUid());
460: }
461: if (tarFileSet.hasGroupIdBeenSet()) {
462: te.setGroupId(tarFileSet.getGid());
463: }
464: }
465:
466: InputStream in = null;
467: try {
468: tOut.putNextEntry(te);
469:
470: if (!r.isDirectory()) {
471: in = r.getInputStream();
472:
473: byte[] buffer = new byte[8 * 1024];
474: int count = 0;
475: do {
476: tOut.write(buffer, 0, count);
477: count = in.read(buffer, 0, buffer.length);
478: } while (count != -1);
479: }
480:
481: tOut.closeEntry();
482: } finally {
483: FileUtils.close(in);
484: }
485: }
486:
487: /**
488: * Is the archive up to date in relationship to a list of files.
489: * @param files the files to check
490: * @return true if the archive is up to date.
491: * @deprecated since 1.5.x.
492: * use the two-arg version instead.
493: */
494: protected boolean archiveIsUpToDate(String[] files) {
495: return archiveIsUpToDate(files, baseDir);
496: }
497:
498: /**
499: * Is the archive up to date in relationship to a list of files.
500: * @param files the files to check
501: * @param dir the base directory for the files.
502: * @return true if the archive is up to date.
503: * @since Ant 1.5.2
504: */
505: protected boolean archiveIsUpToDate(String[] files, File dir) {
506: SourceFileScanner sfs = new SourceFileScanner(this );
507: MergingMapper mm = new MergingMapper();
508: mm.setTo(tarFile.getAbsolutePath());
509: return sfs.restrict(files, dir, null, mm).length == 0;
510: }
511:
512: /**
513: * Is the archive up to date in relationship to a list of files.
514: * @param r the files to check
515: * @return true if the archive is up to date.
516: * @since Ant 1.7
517: */
518: protected boolean archiveIsUpToDate(Resource r) {
519: return SelectorUtils.isOutOfDate(new FileResource(tarFile), r,
520: FileUtils.getFileUtils().getFileTimestampGranularity());
521: }
522:
523: /**
524: * Whether this task can deal with non-file resources.
525: *
526: * <p>This implementation returns true only if this task is
527: * <tar>. Any subclass of this class that also wants to
528: * support non-file resources needs to override this method. We
529: * need to do so for backwards compatibility reasons since we
530: * can't expect subclasses to support resources.</p>
531: * @return true for this task.
532: * @since Ant 1.7
533: */
534: protected boolean supportsNonFileResources() {
535: return getClass().equals(Tar.class);
536: }
537:
538: /**
539: * Checks whether the archive is out-of-date with respect to the resources
540: * of the given collection.
541: *
542: * <p>Also checks that either all collections only contain file
543: * resources or this class supports non-file collections.</p>
544: *
545: * <p>And - in case of file-collections - ensures that the archive won't
546: * contain itself.</p>
547: *
548: * @param rc the resource collection to check
549: * @return whether the archive is up-to-date
550: * @since Ant 1.7
551: */
552: protected boolean check(ResourceCollection rc) {
553: boolean upToDate = true;
554: if (isFileFileSet(rc)) {
555: FileSet fs = (FileSet) rc;
556: upToDate = check(fs.getDir(getProject()), getFileNames(fs));
557: } else if (!rc.isFilesystemOnly()
558: && !supportsNonFileResources()) {
559: throw new BuildException(
560: "only filesystem resources are supported");
561: } else if (rc.isFilesystemOnly()) {
562: HashSet basedirs = new HashSet();
563: HashMap basedirToFilesMap = new HashMap();
564: Iterator iter = rc.iterator();
565: while (iter.hasNext()) {
566: FileResource r = (FileResource) iter.next();
567: File base = r.getBaseDir();
568: if (base == null) {
569: base = Copy.NULL_FILE_PLACEHOLDER;
570: }
571: basedirs.add(base);
572: Vector files = (Vector) basedirToFilesMap.get(base);
573: if (files == null) {
574: files = new Vector();
575: basedirToFilesMap.put(base, new Vector());
576: }
577: files.add(r.getName());
578: }
579: iter = basedirs.iterator();
580: while (iter.hasNext()) {
581: File base = (File) iter.next();
582: Vector f = (Vector) basedirToFilesMap.get(base);
583: String[] files = (String[]) f.toArray(new String[f
584: .size()]);
585: upToDate &= check(
586: base == Copy.NULL_FILE_PLACEHOLDER ? null
587: : base, files);
588: }
589: } else { // non-file resources
590: Iterator iter = rc.iterator();
591: while (upToDate && iter.hasNext()) {
592: Resource r = (Resource) iter.next();
593: upToDate &= archiveIsUpToDate(r);
594: }
595: }
596:
597: return upToDate;
598: }
599:
600: /**
601: * Checks whether the archive is out-of-date with respect to the
602: * given files, ensures that the archive won't contain itself.</p>
603: *
604: * @param basedir base directory for file names
605: * @param files array of relative file names
606: * @return whether the archive is up-to-date
607: * @since Ant 1.7
608: */
609: protected boolean check(File basedir, String[] files) {
610: boolean upToDate = true;
611: if (!archiveIsUpToDate(files, basedir)) {
612: upToDate = false;
613: }
614:
615: for (int i = 0; i < files.length; ++i) {
616: if (tarFile.equals(new File(basedir, files[i]))) {
617: throw new BuildException("A tar file cannot include "
618: + "itself", getLocation());
619: }
620: }
621: return upToDate;
622: }
623:
624: /**
625: * Adds the resources contained in this collection to the archive.
626: *
627: * <p>Uses the file based methods for file resources for backwards
628: * compatibility.</p>
629: *
630: * @param rc the collection containing resources to add
631: * @param tOut stream writing to the archive.
632: * @throws IOException on error.
633: * @since Ant 1.7
634: */
635: protected void tar(ResourceCollection rc, TarOutputStream tOut)
636: throws IOException {
637: ArchiveFileSet afs = null;
638: if (rc instanceof ArchiveFileSet) {
639: afs = (ArchiveFileSet) rc;
640: }
641: if (afs != null && afs.size() > 1
642: && afs.getFullpath(this .getProject()).length() > 0) {
643: throw new BuildException("fullpath attribute may only "
644: + "be specified for " + "filesets that specify a "
645: + "single file.");
646: }
647: TarFileSet tfs = asTarFileSet(afs);
648:
649: if (isFileFileSet(rc)) {
650: FileSet fs = (FileSet) rc;
651: String[] files = getFileNames(fs);
652: for (int i = 0; i < files.length; i++) {
653: File f = new File(fs.getDir(getProject()), files[i]);
654: String name = files[i].replace(File.separatorChar, '/');
655: tarFile(f, tOut, name, tfs);
656: }
657: } else if (rc.isFilesystemOnly()) {
658: Iterator iter = rc.iterator();
659: while (iter.hasNext()) {
660: FileResource r = (FileResource) iter.next();
661: File f = r.getFile();
662: if (f == null) {
663: f = new File(r.getBaseDir(), r.getName());
664: }
665: tarFile(f, tOut, f.getName(), tfs);
666: }
667: } else { // non-file resources
668: Iterator iter = rc.iterator();
669: while (iter.hasNext()) {
670: Resource r = (Resource) iter.next();
671: tarResource(r, tOut, r.getName(), tfs);
672: }
673: }
674: }
675:
676: /**
677: * whether the given resource collection is a (subclass of)
678: * FileSet that only contains file system resources.
679: * @param rc the resource collection to check.
680: * @return true if the collection is a fileset.
681: * @since Ant 1.7
682: */
683: protected static final boolean isFileFileSet(ResourceCollection rc) {
684: return rc instanceof FileSet && rc.isFilesystemOnly();
685: }
686:
687: /**
688: * Grabs all included files and directors from the FileSet and
689: * returns them as an array of (relative) file names.
690: * @param fs the fileset to operate on.
691: * @return a list of the filenames.
692: * @since Ant 1.7
693: */
694: protected static final String[] getFileNames(FileSet fs) {
695: DirectoryScanner ds = fs.getDirectoryScanner(fs.getProject());
696: String[] directories = ds.getIncludedDirectories();
697: String[] filesPerSe = ds.getIncludedFiles();
698: String[] files = new String[directories.length
699: + filesPerSe.length];
700: System.arraycopy(directories, 0, files, 0, directories.length);
701: System.arraycopy(filesPerSe, 0, files, directories.length,
702: filesPerSe.length);
703: return files;
704: }
705:
706: /**
707: * Copies fullpath, prefix and permission attributes from the
708: * ArchiveFileSet to a new TarFileSet (or returns it unchanged if
709: * it already is a TarFileSet).
710: *
711: * @param archiveFileSet fileset to copy attributes from, may be null
712: * @return a new TarFileSet.
713: * @since Ant 1.7
714: */
715: protected TarFileSet asTarFileSet(ArchiveFileSet archiveFileSet) {
716: TarFileSet tfs = null;
717: if (archiveFileSet != null
718: && archiveFileSet instanceof TarFileSet) {
719: tfs = (TarFileSet) archiveFileSet;
720: } else {
721: tfs = new TarFileSet();
722: tfs.setProject(getProject());
723: if (archiveFileSet != null) {
724: tfs.setPrefix(archiveFileSet.getPrefix(getProject()));
725: tfs.setFullpath(archiveFileSet
726: .getFullpath(getProject()));
727: if (archiveFileSet.hasFileModeBeenSet()) {
728: tfs.integerSetFileMode(archiveFileSet
729: .getFileMode(getProject()));
730: }
731: if (archiveFileSet.hasDirModeBeenSet()) {
732: tfs.integerSetDirMode(archiveFileSet
733: .getDirMode(getProject()));
734: }
735:
736: if (archiveFileSet instanceof org.apache.tools.ant.types.TarFileSet) {
737: org.apache.tools.ant.types.TarFileSet t = (org.apache.tools.ant.types.TarFileSet) archiveFileSet;
738: if (t.hasUserNameBeenSet()) {
739: tfs.setUserName(t.getUserName());
740: }
741: if (t.hasGroupBeenSet()) {
742: tfs.setGroup(t.getGroup());
743: }
744: if (t.hasUserIdBeenSet()) {
745: tfs.setUid(t.getUid());
746: }
747: if (t.hasGroupIdBeenSet()) {
748: tfs.setGid(t.getGid());
749: }
750: }
751: }
752: }
753: return tfs;
754: }
755:
756: /**
757: * This is a FileSet with the option to specify permissions
758: * and other attributes.
759: */
760: public static class TarFileSet extends
761: org.apache.tools.ant.types.TarFileSet {
762: private String[] files = null;
763:
764: private boolean preserveLeadingSlashes = false;
765:
766: /**
767: * Creates a new <code>TarFileSet</code> instance.
768: * Using a fileset as a constructor argument.
769: *
770: * @param fileset a <code>FileSet</code> value
771: */
772: public TarFileSet(FileSet fileset) {
773: super (fileset);
774: }
775:
776: /**
777: * Creates a new <code>TarFileSet</code> instance.
778: *
779: */
780: public TarFileSet() {
781: super ();
782: }
783:
784: /**
785: * Get a list of files and directories specified in the fileset.
786: * @param p the current project.
787: * @return a list of file and directory names, relative to
788: * the baseDir for the project.
789: */
790: public String[] getFiles(Project p) {
791: if (files == null) {
792: files = getFileNames(this );
793: }
794:
795: return files;
796: }
797:
798: /**
799: * A 3 digit octal string, specify the user, group and
800: * other modes in the standard Unix fashion;
801: * optional, default=0644
802: * @param octalString a 3 digit octal string.
803: */
804: public void setMode(String octalString) {
805: setFileMode(octalString);
806: }
807:
808: /**
809: * @return the current mode.
810: */
811: public int getMode() {
812: return getFileMode(this .getProject());
813: }
814:
815: /**
816: * Flag to indicates whether leading `/'s should
817: * be preserved in the file names.
818: * Optional, default is <code>false</code>.
819: * @param b the leading slashes flag.
820: */
821: public void setPreserveLeadingSlashes(boolean b) {
822: this .preserveLeadingSlashes = b;
823: }
824:
825: /**
826: * @return the leading slashes flag.
827: */
828: public boolean getPreserveLeadingSlashes() {
829: return preserveLeadingSlashes;
830: }
831: }
832:
833: /**
834: * Set of options for long file handling in the task.
835: *
836: */
837: public static class TarLongFileMode extends EnumeratedAttribute {
838:
839: /** permissible values for longfile attribute */
840: public static final String WARN = "warn", FAIL = "fail",
841: TRUNCATE = "truncate", GNU = "gnu", OMIT = "omit";
842:
843: private final String[] validModes = { WARN, FAIL, TRUNCATE,
844: GNU, OMIT };
845:
846: /** Constructor, defaults to "warn" */
847: public TarLongFileMode() {
848: super ();
849: setValue(WARN);
850: }
851:
852: /**
853: * @return the possible values for this enumerated type.
854: */
855: public String[] getValues() {
856: return validModes;
857: }
858:
859: /**
860: * @return true if value is "truncate".
861: */
862: public boolean isTruncateMode() {
863: return TRUNCATE.equalsIgnoreCase(getValue());
864: }
865:
866: /**
867: * @return true if value is "warn".
868: */
869: public boolean isWarnMode() {
870: return WARN.equalsIgnoreCase(getValue());
871: }
872:
873: /**
874: * @return true if value is "gnu".
875: */
876: public boolean isGnuMode() {
877: return GNU.equalsIgnoreCase(getValue());
878: }
879:
880: /**
881: * @return true if value is "fail".
882: */
883: public boolean isFailMode() {
884: return FAIL.equalsIgnoreCase(getValue());
885: }
886:
887: /**
888: * @return true if value is "omit".
889: */
890: public boolean isOmitMode() {
891: return OMIT.equalsIgnoreCase(getValue());
892: }
893: }
894:
895: /**
896: * Valid Modes for Compression attribute to Tar Task
897: *
898: */
899: public static final class TarCompressionMethod extends
900: EnumeratedAttribute {
901:
902: // permissible values for compression attribute
903: /**
904: * No compression
905: */
906: private static final String NONE = "none";
907: /**
908: * GZIP compression
909: */
910: private static final String GZIP = "gzip";
911: /**
912: * BZIP2 compression
913: */
914: private static final String BZIP2 = "bzip2";
915:
916: /**
917: * Default constructor
918: */
919: public TarCompressionMethod() {
920: super ();
921: setValue(NONE);
922: }
923:
924: /**
925: * Get valid enumeration values.
926: * @return valid enumeration values
927: */
928: public String[] getValues() {
929: return new String[] { NONE, GZIP, BZIP2 };
930: }
931:
932: /**
933: * This method wraps the output stream with the
934: * corresponding compression method
935: *
936: * @param ostream output stream
937: * @return output stream with on-the-fly compression
938: * @exception IOException thrown if file is not writable
939: */
940: private OutputStream compress(final OutputStream ostream)
941: throws IOException {
942: final String v = getValue();
943: if (GZIP.equals(v)) {
944: return new GZIPOutputStream(ostream);
945: } else {
946: if (BZIP2.equals(v)) {
947: ostream.write('B');
948: ostream.write('Z');
949: return new CBZip2OutputStream(ostream);
950: }
951: }
952: return ostream;
953: }
954: }
955: }
|