001: package de.masters_of_disaster.ant.tasks.ar;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.File;
005: import java.io.FileInputStream;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.util.Enumeration;
009: import java.util.Vector;
010: import org.apache.tools.ant.BuildException;
011: import org.apache.tools.ant.DirectoryScanner;
012: import org.apache.tools.ant.Project;
013: import org.apache.tools.ant.taskdefs.MatchingTask;
014: import org.apache.tools.ant.types.EnumeratedAttribute;
015: import org.apache.tools.ant.types.FileSet;
016: import org.apache.tools.ant.util.FileUtils;
017: import org.apache.tools.ant.util.MergingMapper;
018: import org.apache.tools.ant.util.SourceFileScanner;
019: import org.apache.tools.zip.UnixStat;
020:
021: /**
022: * Creates an ar archive.
023: *
024: * @ant.task category="packaging"
025: */
026: public class Ar extends MatchingTask {
027: File destFile;
028: File baseDir;
029:
030: private ArLongFileMode longFileMode = new ArLongFileMode();
031:
032: Vector filesets = new Vector();
033:
034: /**
035: * Indicates whether the user has been warned about long files already.
036: */
037: private boolean longWarningGiven = false;
038:
039: /**
040: * Add a new fileset with the option to specify permissions
041: * @return the ar fileset to be used as the nested element.
042: */
043: public ArFileSet createArFileSet() {
044: ArFileSet fileset = new ArFileSet();
045: filesets.addElement(fileset);
046: return fileset;
047: }
048:
049: /**
050: * Set the name/location of where to create the ar file.
051: * @param destFile The output of the tar
052: */
053: public void setDestFile(File destFile) {
054: this .destFile = destFile;
055: }
056:
057: /**
058: * This is the base directory to look in for things to ar.
059: * @param baseDir the base directory.
060: */
061: public void setBasedir(File baseDir) {
062: this .baseDir = baseDir;
063: }
064:
065: /**
066: * Set how to handle long files, those with a name>16 chars or containing spaces.
067: * Optional, default=warn.
068: * <p>
069: * Allowable values are
070: * <ul>
071: * <li> truncate - names are truncated to the maximum length, spaces are replaced by '_'
072: * <li> fail - names greater than the maximum cause a build exception
073: * <li> warn - names greater than the maximum cause a warning and TRUNCATE is used
074: * <li> bsd - BSD variant is used if any names are greater than the maximum.
075: * <li> gnu - GNU variant is used if any names are greater than the maximum.
076: * <li> omit - files with a name greater than the maximum are omitted from the archive
077: * </ul>
078: * @param mode the mode to handle long file names.
079: */
080: public void setLongfile(ArLongFileMode mode) {
081: this .longFileMode = mode;
082: }
083:
084: /**
085: * do the business
086: * @throws BuildException on error
087: */
088: public void execute() throws BuildException {
089: if (destFile == null) {
090: throw new BuildException("destFile attribute must be set!",
091: getLocation());
092: }
093:
094: if (destFile.exists() && destFile.isDirectory()) {
095: throw new BuildException("destFile is a directory!",
096: getLocation());
097: }
098:
099: if (destFile.exists() && !destFile.canWrite()) {
100: throw new BuildException(
101: "Can not write to the specified destFile!",
102: getLocation());
103: }
104:
105: Vector savedFileSets = (Vector) filesets.clone();
106: try {
107: if (baseDir != null) {
108: if (!baseDir.exists()) {
109: throw new BuildException("basedir does not exist!",
110: getLocation());
111: }
112:
113: // add the main fileset to the list of filesets to process.
114: ArFileSet mainFileSet = new ArFileSet(fileset);
115: mainFileSet.setDir(baseDir);
116: filesets.addElement(mainFileSet);
117: }
118:
119: if (filesets.size() == 0) {
120: throw new BuildException(
121: "You must supply either a basedir "
122: + "attribute or some nested filesets.",
123: getLocation());
124: }
125:
126: // check if ar is out of date with respect to each
127: // fileset
128: boolean upToDate = true;
129: for (Enumeration e = filesets.elements(); e
130: .hasMoreElements();) {
131: ArFileSet fs = (ArFileSet) e.nextElement();
132: String[] files = fs.getFiles(getProject());
133:
134: if (!archiveIsUpToDate(files, fs.getDir(getProject()))) {
135: upToDate = false;
136: }
137:
138: for (int i = 0; i < files.length; ++i) {
139: if (destFile.equals(new File(fs
140: .getDir(getProject()), files[i]))) {
141: throw new BuildException(
142: "An ar file cannot include " + "itself",
143: getLocation());
144: }
145: }
146: }
147:
148: if (upToDate) {
149: log("Nothing to do: " + destFile.getAbsolutePath()
150: + " is up to date.", Project.MSG_INFO);
151: return;
152: }
153:
154: log("Building ar: " + destFile.getAbsolutePath(),
155: Project.MSG_INFO);
156:
157: ArOutputStream aOut = null;
158: try {
159: aOut = new ArOutputStream(new BufferedOutputStream(
160: new FileOutputStream(destFile)));
161: if (longFileMode.isTruncateMode()
162: || longFileMode.isWarnMode()) {
163: aOut
164: .setLongFileMode(ArOutputStream.LONGFILE_TRUNCATE);
165: } else if (longFileMode.isFailMode()
166: || longFileMode.isOmitMode()) {
167: aOut.setLongFileMode(ArOutputStream.LONGFILE_ERROR);
168: } else if (longFileMode.isBsdMode()) {
169: aOut.setLongFileMode(ArOutputStream.LONGFILE_BSD);
170: } else {
171: // GNU
172: aOut.setLongFileMode(ArOutputStream.LONGFILE_GNU);
173: }
174:
175: longWarningGiven = false;
176: for (Enumeration e = filesets.elements(); e
177: .hasMoreElements();) {
178: ArFileSet fs = (ArFileSet) e.nextElement();
179: String[] files = fs.getFiles(getProject());
180: if (files.length > 1
181: && fs.getFullpath().length() > 0) {
182: throw new BuildException(
183: "fullpath attribute may only "
184: + "be specified for "
185: + "filesets that specify a "
186: + "single file.");
187: }
188: for (int i = 0; i < files.length; i++) {
189: File f = new File(fs.getDir(getProject()),
190: files[i]);
191: arFile(f, aOut, fs);
192: }
193: }
194: } catch (IOException ioe) {
195: String msg = "Problem creating AR: " + ioe.getMessage();
196: throw new BuildException(msg, ioe, getLocation());
197: } finally {
198: FileUtils.close(aOut);
199: }
200: } finally {
201: filesets = savedFileSets;
202: }
203: }
204:
205: /**
206: * ar a file
207: * @param file the file to ar
208: * @param aOut the output stream
209: * @param arFileSet the fileset that the file came from.
210: * @throws IOException on error
211: */
212: protected void arFile(File file, ArOutputStream aOut,
213: ArFileSet arFileSet) throws IOException {
214: FileInputStream fIn = null;
215:
216: if (file.isDirectory()) {
217: return;
218: }
219:
220: String fileName = file.getName();
221:
222: String fullpath = arFileSet.getFullpath();
223: if (fullpath.length() > 0) {
224: fileName = fullpath.substring(fullpath.lastIndexOf('/'));
225: }
226:
227: // don't add "" to the archive
228: if (fileName.length() <= 0) {
229: return;
230: }
231:
232: try {
233: if ((fileName.length() >= ArConstants.NAMELEN)
234: || (-1 != fileName.indexOf(' '))) {
235: if (longFileMode.isOmitMode()) {
236: log("Omitting: " + fileName, Project.MSG_INFO);
237: return;
238: } else if (longFileMode.isWarnMode()) {
239: if (!longWarningGiven) {
240: log(
241: "Resulting ar file contains truncated or space converted filenames",
242: Project.MSG_WARN);
243: longWarningGiven = true;
244: }
245: log("Entry: \"" + fileName + "\" longer than "
246: + ArConstants.NAMELEN
247: + " characters or containing spaces.",
248: Project.MSG_WARN);
249: } else if (longFileMode.isFailMode()) {
250: throw new BuildException("Entry: \"" + fileName
251: + "\" longer than " + ArConstants.NAMELEN
252: + "characters or containting spaces.",
253: getLocation());
254: }
255: }
256:
257: ArEntry ae = new ArEntry(fileName);
258: ae.setFileDate(file.lastModified());
259: ae.setUserId(arFileSet.getUid());
260: ae.setGroupId(arFileSet.getGid());
261: ae.setMode(arFileSet.getMode());
262: ae.setSize(file.length());
263:
264: aOut.putNextEntry(ae);
265:
266: fIn = new FileInputStream(file);
267:
268: byte[] buffer = new byte[8 * 1024];
269: int count = 0;
270: do {
271: aOut.write(buffer, 0, count);
272: count = fIn.read(buffer, 0, buffer.length);
273: } while (count != -1);
274:
275: aOut.closeEntry();
276: } finally {
277: if (fIn != null) {
278: fIn.close();
279: }
280: }
281: }
282:
283: /**
284: * Is the archive up to date in relationship to a list of files.
285: * @param files the files to check
286: * @param dir the base directory for the files.
287: * @return true if the archive is up to date.
288: */
289: protected boolean archiveIsUpToDate(String[] files, File dir) {
290: SourceFileScanner sfs = new SourceFileScanner(this );
291: MergingMapper mm = new MergingMapper();
292: mm.setTo(destFile.getAbsolutePath());
293: return sfs.restrict(files, dir, null, mm).length == 0;
294: }
295:
296: /**
297: * This is a FileSet with the option to specify permissions
298: * and other attributes.
299: */
300: public static class ArFileSet extends FileSet {
301: private String[] files = null;
302:
303: private int fileMode = UnixStat.FILE_FLAG
304: | UnixStat.DEFAULT_FILE_PERM;
305: private int uid;
306: private int gid;
307: private String fullpath = "";
308:
309: /**
310: * Creates a new <code>ArFileSet</code> instance.
311: * Using a fileset as a constructor argument.
312: *
313: * @param fileset a <code>FileSet</code> value
314: */
315: public ArFileSet(FileSet fileset) {
316: super (fileset);
317: }
318:
319: /**
320: * Creates a new <code>ArFileSet</code> instance.
321: *
322: */
323: public ArFileSet() {
324: super ();
325: }
326:
327: /**
328: * Get a list of files and directories specified in the fileset.
329: * @param p the current project.
330: * @return a list of file and directory names, relative to
331: * the baseDir for the project.
332: */
333: public String[] getFiles(Project p) {
334: if (files == null) {
335: DirectoryScanner ds = getDirectoryScanner(p);
336: files = ds.getIncludedFiles();
337: }
338:
339: return files;
340: }
341:
342: /**
343: * A 3 digit octal string, specify the user, group and
344: * other modes in the standard Unix fashion;
345: * optional, default=0644
346: * @param octalString a 3 digit octal string.
347: */
348: public void setMode(String octalString) {
349: this .fileMode = UnixStat.FILE_FLAG
350: | Integer.parseInt(octalString, 8);
351: }
352:
353: /**
354: * @return the current mode.
355: */
356: public int getMode() {
357: return fileMode;
358: }
359:
360: /**
361: * The UID for the ar entry; optional, default="0"
362: * @param uid the id of the user for the ar entry.
363: */
364: public void setUid(int uid) {
365: this .uid = uid;
366: }
367:
368: /**
369: * @return the UID for the ar entry
370: */
371: public int getUid() {
372: return uid;
373: }
374:
375: /**
376: * The GID for the ar entry; optional, default="0"
377: * @param gid the group id.
378: */
379: public void setGid(int gid) {
380: this .gid = gid;
381: }
382:
383: /**
384: * @return the group identifier.
385: */
386: public int getGid() {
387: return gid;
388: }
389:
390: /**
391: * If the fullpath attribute is set, the file in the fileset
392: * is written with the last part of the path in the archive.
393: * If the fullpath ends in '/' the file is omitted from the archive.
394: * It is an error to have more than one file specified in such a fileset.
395: * @param fullpath the path to use for the file in a fileset.
396: */
397: public void setFullpath(String fullpath) {
398: this .fullpath = fullpath;
399: }
400:
401: /**
402: * @return the path to use for a single file fileset.
403: */
404: public String getFullpath() {
405: return fullpath;
406: }
407: }
408:
409: /**
410: * Set of options for long file handling in the task.
411: */
412: public static class ArLongFileMode extends EnumeratedAttribute {
413: /** permissible values for longfile attribute */
414: public static final String WARN = "warn", FAIL = "fail",
415: TRUNCATE = "truncate", GNU = "gnu", BSD = "bsd",
416: OMIT = "omit";
417:
418: private final String[] validModes = { WARN, FAIL, TRUNCATE,
419: GNU, BSD, OMIT };
420:
421: /** Constructor, defaults to "warn" */
422: public ArLongFileMode() {
423: super ();
424: setValue(WARN);
425: }
426:
427: /**
428: * @return the possible values for this enumerated type.
429: */
430: public String[] getValues() {
431: return validModes;
432: }
433:
434: /**
435: * @return true if value is "truncate".
436: */
437: public boolean isTruncateMode() {
438: return TRUNCATE.equalsIgnoreCase(getValue());
439: }
440:
441: /**
442: * @return true if value is "warn".
443: */
444: public boolean isWarnMode() {
445: return WARN.equalsIgnoreCase(getValue());
446: }
447:
448: /**
449: * @return true if value is "gnu".
450: */
451: public boolean isGnuMode() {
452: return GNU.equalsIgnoreCase(getValue());
453: }
454:
455: /**
456: * @return true if value is "bsd".
457: */
458: public boolean isBsdMode() {
459: return BSD.equalsIgnoreCase(getValue());
460: }
461:
462: /**
463: * @return true if value is "fail".
464: */
465: public boolean isFailMode() {
466: return FAIL.equalsIgnoreCase(getValue());
467: }
468:
469: /**
470: * @return true if value is "omit".
471: */
472: public boolean isOmitMode() {
473: return OMIT.equalsIgnoreCase(getValue());
474: }
475: }
476: }
|