/jEdit/branches/concurrency/de/masters_of_disaster/ant/tasks/ar/Ar.java

# · Java · 468 lines · 273 code · 59 blank · 136 comment · 47 complexity · d81590ceadbe01031a2f26f7c16bd15f MD5 · raw file

  1. package de.masters_of_disaster.ant.tasks.ar;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.Enumeration;
  8. import java.util.Vector;
  9. import org.apache.tools.ant.BuildException;
  10. import org.apache.tools.ant.DirectoryScanner;
  11. import org.apache.tools.ant.Project;
  12. import org.apache.tools.ant.taskdefs.MatchingTask;
  13. import org.apache.tools.ant.types.EnumeratedAttribute;
  14. import org.apache.tools.ant.types.FileSet;
  15. import org.apache.tools.ant.util.FileUtils;
  16. import org.apache.tools.ant.util.MergingMapper;
  17. import org.apache.tools.ant.util.SourceFileScanner;
  18. import org.apache.tools.zip.UnixStat;
  19. /**
  20. * Creates an ar archive.
  21. *
  22. * @ant.task category="packaging"
  23. */
  24. public class Ar extends MatchingTask {
  25. File destFile;
  26. File baseDir;
  27. private ArLongFileMode longFileMode = new ArLongFileMode();
  28. Vector filesets = new Vector();
  29. /**
  30. * Indicates whether the user has been warned about long files already.
  31. */
  32. private boolean longWarningGiven = false;
  33. /**
  34. * Add a new fileset with the option to specify permissions
  35. * @return the ar fileset to be used as the nested element.
  36. */
  37. public ArFileSet createArFileSet() {
  38. ArFileSet fileset = new ArFileSet();
  39. filesets.addElement(fileset);
  40. return fileset;
  41. }
  42. /**
  43. * Set the name/location of where to create the ar file.
  44. * @param destFile The output of the tar
  45. */
  46. public void setDestFile(File destFile) {
  47. this.destFile = destFile;
  48. }
  49. /**
  50. * This is the base directory to look in for things to ar.
  51. * @param baseDir the base directory.
  52. */
  53. public void setBasedir(File baseDir) {
  54. this.baseDir = baseDir;
  55. }
  56. /**
  57. * Set how to handle long files, those with a name>16 chars or containing spaces.
  58. * Optional, default=warn.
  59. * <p>
  60. * Allowable values are
  61. * <ul>
  62. * <li> truncate - names are truncated to the maximum length, spaces are replaced by '_'
  63. * <li> fail - names greater than the maximum cause a build exception
  64. * <li> warn - names greater than the maximum cause a warning and TRUNCATE is used
  65. * <li> bsd - BSD variant is used if any names are greater than the maximum.
  66. * <li> gnu - GNU variant is used if any names are greater than the maximum.
  67. * <li> omit - files with a name greater than the maximum are omitted from the archive
  68. * </ul>
  69. * @param mode the mode to handle long file names.
  70. */
  71. public void setLongfile(ArLongFileMode mode) {
  72. this.longFileMode = mode;
  73. }
  74. /**
  75. * do the business
  76. * @throws BuildException on error
  77. */
  78. public void execute() throws BuildException {
  79. if (destFile == null) {
  80. throw new BuildException("destFile attribute must be set!",
  81. getLocation());
  82. }
  83. if (destFile.exists() && destFile.isDirectory()) {
  84. throw new BuildException("destFile is a directory!",
  85. getLocation());
  86. }
  87. if (destFile.exists() && !destFile.canWrite()) {
  88. throw new BuildException("Can not write to the specified destFile!",
  89. getLocation());
  90. }
  91. Vector savedFileSets = (Vector) filesets.clone();
  92. try {
  93. if (baseDir != null) {
  94. if (!baseDir.exists()) {
  95. throw new BuildException("basedir does not exist!",
  96. getLocation());
  97. }
  98. // add the main fileset to the list of filesets to process.
  99. ArFileSet mainFileSet = new ArFileSet(fileset);
  100. mainFileSet.setDir(baseDir);
  101. filesets.addElement(mainFileSet);
  102. }
  103. if (filesets.size() == 0) {
  104. throw new BuildException("You must supply either a basedir "
  105. + "attribute or some nested filesets.",
  106. getLocation());
  107. }
  108. // check if ar is out of date with respect to each
  109. // fileset
  110. boolean upToDate = true;
  111. for (Enumeration e = filesets.elements(); e.hasMoreElements();) {
  112. ArFileSet fs = (ArFileSet) e.nextElement();
  113. String[] files = fs.getFiles(getProject());
  114. if (!archiveIsUpToDate(files, fs.getDir(getProject()))) {
  115. upToDate = false;
  116. }
  117. for (int i = 0; i < files.length; ++i) {
  118. if (destFile.equals(new File(fs.getDir(getProject()),
  119. files[i]))) {
  120. throw new BuildException("An ar file cannot include "
  121. + "itself", getLocation());
  122. }
  123. }
  124. }
  125. if (upToDate) {
  126. log("Nothing to do: " + destFile.getAbsolutePath()
  127. + " is up to date.", Project.MSG_INFO);
  128. return;
  129. }
  130. log("Building ar: " + destFile.getAbsolutePath(), Project.MSG_INFO);
  131. ArOutputStream aOut = null;
  132. try {
  133. aOut = new ArOutputStream(
  134. new BufferedOutputStream(
  135. new FileOutputStream(destFile)));
  136. if (longFileMode.isTruncateMode()
  137. || longFileMode.isWarnMode()) {
  138. aOut.setLongFileMode(ArOutputStream.LONGFILE_TRUNCATE);
  139. } else if (longFileMode.isFailMode()
  140. || longFileMode.isOmitMode()) {
  141. aOut.setLongFileMode(ArOutputStream.LONGFILE_ERROR);
  142. } else if (longFileMode.isBsdMode()) {
  143. aOut.setLongFileMode(ArOutputStream.LONGFILE_BSD);
  144. } else {
  145. // GNU
  146. aOut.setLongFileMode(ArOutputStream.LONGFILE_GNU);
  147. }
  148. longWarningGiven = false;
  149. for (Enumeration e = filesets.elements();
  150. e.hasMoreElements();) {
  151. ArFileSet fs = (ArFileSet) e.nextElement();
  152. String[] files = fs.getFiles(getProject());
  153. if (files.length > 1 && fs.getFullpath().length() > 0) {
  154. throw new BuildException("fullpath attribute may only "
  155. + "be specified for "
  156. + "filesets that specify a "
  157. + "single file.");
  158. }
  159. for (int i = 0; i < files.length; i++) {
  160. File f = new File(fs.getDir(getProject()), files[i]);
  161. arFile(f, aOut, fs);
  162. }
  163. }
  164. } catch (IOException ioe) {
  165. String msg = "Problem creating AR: " + ioe.getMessage();
  166. throw new BuildException(msg, ioe, getLocation());
  167. } finally {
  168. FileUtils.close(aOut);
  169. }
  170. } finally {
  171. filesets = savedFileSets;
  172. }
  173. }
  174. /**
  175. * ar a file
  176. * @param file the file to ar
  177. * @param aOut the output stream
  178. * @param arFileSet the fileset that the file came from.
  179. * @throws IOException on error
  180. */
  181. protected void arFile(File file, ArOutputStream aOut, ArFileSet arFileSet)
  182. throws IOException {
  183. FileInputStream fIn = null;
  184. if (file.isDirectory()) {
  185. return;
  186. }
  187. String fileName = file.getName();
  188. String fullpath = arFileSet.getFullpath();
  189. if (fullpath.length() > 0) {
  190. fileName = fullpath.substring(fullpath.lastIndexOf('/'));
  191. }
  192. // don't add "" to the archive
  193. if (fileName.length() <= 0) {
  194. return;
  195. }
  196. try {
  197. if ((fileName.length() >= ArConstants.NAMELEN)
  198. || (-1 != fileName.indexOf(' '))) {
  199. if (longFileMode.isOmitMode()) {
  200. log("Omitting: " + fileName, Project.MSG_INFO);
  201. return;
  202. } else if (longFileMode.isWarnMode()) {
  203. if (!longWarningGiven) {
  204. log("Resulting ar file contains truncated or space converted filenames",
  205. Project.MSG_WARN);
  206. longWarningGiven = true;
  207. }
  208. log("Entry: \"" + fileName + "\" longer than "
  209. + ArConstants.NAMELEN + " characters or containing spaces.",
  210. Project.MSG_WARN);
  211. } else if (longFileMode.isFailMode()) {
  212. throw new BuildException("Entry: \"" + fileName
  213. + "\" longer than " + ArConstants.NAMELEN
  214. + "characters or containting spaces.", getLocation());
  215. }
  216. }
  217. ArEntry ae = new ArEntry(fileName);
  218. ae.setFileDate(file.lastModified());
  219. ae.setUserId(arFileSet.getUid());
  220. ae.setGroupId(arFileSet.getGid());
  221. ae.setMode(arFileSet.getMode());
  222. ae.setSize(file.length());
  223. aOut.putNextEntry(ae);
  224. fIn = new FileInputStream(file);
  225. byte[] buffer = new byte[8 * 1024];
  226. int count = 0;
  227. do {
  228. aOut.write(buffer, 0, count);
  229. count = fIn.read(buffer, 0, buffer.length);
  230. } while (count != -1);
  231. aOut.closeEntry();
  232. } finally {
  233. if (fIn != null) {
  234. fIn.close();
  235. }
  236. }
  237. }
  238. /**
  239. * Is the archive up to date in relationship to a list of files.
  240. * @param files the files to check
  241. * @param dir the base directory for the files.
  242. * @return true if the archive is up to date.
  243. */
  244. protected boolean archiveIsUpToDate(String[] files, File dir) {
  245. SourceFileScanner sfs = new SourceFileScanner(this);
  246. MergingMapper mm = new MergingMapper();
  247. mm.setTo(destFile.getAbsolutePath());
  248. return sfs.restrict(files, dir, null, mm).length == 0;
  249. }
  250. /**
  251. * This is a FileSet with the option to specify permissions
  252. * and other attributes.
  253. */
  254. public static class ArFileSet extends FileSet {
  255. private String[] files = null;
  256. private int fileMode = UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM;
  257. private int uid;
  258. private int gid;
  259. private String fullpath = "";
  260. /**
  261. * Creates a new <code>ArFileSet</code> instance.
  262. * Using a fileset as a constructor argument.
  263. *
  264. * @param fileset a <code>FileSet</code> value
  265. */
  266. public ArFileSet(FileSet fileset) {
  267. super(fileset);
  268. }
  269. /**
  270. * Creates a new <code>ArFileSet</code> instance.
  271. *
  272. */
  273. public ArFileSet() {
  274. super();
  275. }
  276. /**
  277. * Get a list of files and directories specified in the fileset.
  278. * @param p the current project.
  279. * @return a list of file and directory names, relative to
  280. * the baseDir for the project.
  281. */
  282. public String[] getFiles(Project p) {
  283. if (files == null) {
  284. DirectoryScanner ds = getDirectoryScanner(p);
  285. files = ds.getIncludedFiles();
  286. }
  287. return files;
  288. }
  289. /**
  290. * A 3 digit octal string, specify the user, group and
  291. * other modes in the standard Unix fashion;
  292. * optional, default=0644
  293. * @param octalString a 3 digit octal string.
  294. */
  295. public void setMode(String octalString) {
  296. this.fileMode =
  297. UnixStat.FILE_FLAG | Integer.parseInt(octalString, 8);
  298. }
  299. /**
  300. * @return the current mode.
  301. */
  302. public int getMode() {
  303. return fileMode;
  304. }
  305. /**
  306. * The UID for the ar entry; optional, default="0"
  307. * @param uid the id of the user for the ar entry.
  308. */
  309. public void setUid(int uid) {
  310. this.uid = uid;
  311. }
  312. /**
  313. * @return the UID for the ar entry
  314. */
  315. public int getUid() {
  316. return uid;
  317. }
  318. /**
  319. * The GID for the ar entry; optional, default="0"
  320. * @param gid the group id.
  321. */
  322. public void setGid(int gid) {
  323. this.gid = gid;
  324. }
  325. /**
  326. * @return the group identifier.
  327. */
  328. public int getGid() {
  329. return gid;
  330. }
  331. /**
  332. * If the fullpath attribute is set, the file in the fileset
  333. * is written with the last part of the path in the archive.
  334. * If the fullpath ends in '/' the file is omitted from the archive.
  335. * It is an error to have more than one file specified in such a fileset.
  336. * @param fullpath the path to use for the file in a fileset.
  337. */
  338. public void setFullpath(String fullpath) {
  339. this.fullpath = fullpath;
  340. }
  341. /**
  342. * @return the path to use for a single file fileset.
  343. */
  344. public String getFullpath() {
  345. return fullpath;
  346. }
  347. }
  348. /**
  349. * Set of options for long file handling in the task.
  350. */
  351. public static class ArLongFileMode extends EnumeratedAttribute {
  352. /** permissible values for longfile attribute */
  353. public static final String
  354. WARN = "warn",
  355. FAIL = "fail",
  356. TRUNCATE = "truncate",
  357. GNU = "gnu",
  358. BSD = "bsd",
  359. OMIT = "omit";
  360. private final String[] validModes = {WARN, FAIL, TRUNCATE, GNU, BSD, OMIT};
  361. /** Constructor, defaults to "warn" */
  362. public ArLongFileMode() {
  363. super();
  364. setValue(WARN);
  365. }
  366. /**
  367. * @return the possible values for this enumerated type.
  368. */
  369. public String[] getValues() {
  370. return validModes;
  371. }
  372. /**
  373. * @return true if value is "truncate".
  374. */
  375. public boolean isTruncateMode() {
  376. return TRUNCATE.equalsIgnoreCase(getValue());
  377. }
  378. /**
  379. * @return true if value is "warn".
  380. */
  381. public boolean isWarnMode() {
  382. return WARN.equalsIgnoreCase(getValue());
  383. }
  384. /**
  385. * @return true if value is "gnu".
  386. */
  387. public boolean isGnuMode() {
  388. return GNU.equalsIgnoreCase(getValue());
  389. }
  390. /**
  391. * @return true if value is "bsd".
  392. */
  393. public boolean isBsdMode() {
  394. return BSD.equalsIgnoreCase(getValue());
  395. }
  396. /**
  397. * @return true if value is "fail".
  398. */
  399. public boolean isFailMode() {
  400. return FAIL.equalsIgnoreCase(getValue());
  401. }
  402. /**
  403. * @return true if value is "omit".
  404. */
  405. public boolean isOmitMode() {
  406. return OMIT.equalsIgnoreCase(getValue());
  407. }
  408. }
  409. }