You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Checksum.java 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.BufferedReader;
  56. import java.io.File;
  57. import java.io.FileInputStream;
  58. import java.io.FileOutputStream;
  59. import java.io.InputStreamReader;
  60. import java.io.IOException;
  61. import java.security.DigestInputStream;
  62. import java.security.MessageDigest;
  63. import java.security.NoSuchAlgorithmException;
  64. import java.security.NoSuchProviderException;
  65. import java.util.Enumeration;
  66. import java.util.Hashtable;
  67. import java.util.Vector;
  68. import org.apache.tools.ant.BuildException;
  69. import org.apache.tools.ant.DirectoryScanner;
  70. import org.apache.tools.ant.Project;
  71. import org.apache.tools.ant.taskdefs.condition.Condition;
  72. import org.apache.tools.ant.types.FileSet;
  73. /**
  74. * This task can be used to create checksums for files.
  75. * It can also be used to verify checksums.
  76. *
  77. * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
  78. *
  79. * @since Ant 1.5
  80. *
  81. * @ant.task category="control"
  82. */
  83. public class Checksum extends MatchingTask implements Condition {
  84. /**
  85. * File for which checksum is to be calculated.
  86. */
  87. private File file = null;
  88. /**
  89. * MessageDigest algorithm to be used.
  90. */
  91. private String algorithm = "MD5";
  92. /**
  93. * MessageDigest Algorithm provider
  94. */
  95. private String provider = null;
  96. /**
  97. * File Extension that is be to used to create or identify
  98. * destination file
  99. */
  100. private String fileext;
  101. /**
  102. * Holds generated checksum and gets set as a Project Property.
  103. */
  104. private String property;
  105. /**
  106. * Whether or not to create a new file.
  107. * Defaults to <code>false</code>.
  108. */
  109. private boolean forceOverwrite;
  110. /**
  111. * Contains the result of a checksum verification. ("true" or "false")
  112. */
  113. private String verifyProperty;
  114. /**
  115. * Vector to hold source file sets.
  116. */
  117. private Vector filesets = new Vector();
  118. /**
  119. * Stores SourceFile, DestFile pairs and SourceFile, Property String pairs.
  120. */
  121. private Hashtable includeFileMap = new Hashtable();
  122. /**
  123. * Message Digest instance
  124. */
  125. private MessageDigest messageDigest;
  126. /**
  127. * is this task being used as a nested condition element?
  128. */
  129. private boolean isCondition;
  130. /**
  131. * Sets the file for which the checksum is to be calculated.
  132. */
  133. public void setFile(File file) {
  134. this.file = file;
  135. }
  136. /**
  137. * Sets the MessageDigest algorithm to be used
  138. * to calculate the checksum.
  139. */
  140. public void setAlgorithm(String algorithm) {
  141. this.algorithm = algorithm;
  142. }
  143. /**
  144. * Sets the MessageDigest algorithm provider to be used
  145. * to calculate the checksum.
  146. */
  147. public void setProvider(String provider) {
  148. this.provider = provider;
  149. }
  150. /**
  151. * Sets the File Extension that is be to used to
  152. * create or identify destination file
  153. */
  154. public void setFileext(String fileext) {
  155. this.fileext = fileext;
  156. }
  157. /**
  158. * Sets the property to hold the generated checksum
  159. */
  160. public void setProperty(String property) {
  161. this.property = property;
  162. }
  163. /**
  164. * Sets verify property. This project property holds
  165. * the result of a checksum verification - "true" or "false"
  166. */
  167. public void setVerifyproperty(String verifyProperty) {
  168. this.verifyProperty = verifyProperty;
  169. }
  170. /**
  171. * Whether or not to overwrite existing file irrespective of
  172. * whether it is newer than
  173. * the source file. Defaults to false.
  174. */
  175. public void setForceOverwrite(boolean forceOverwrite) {
  176. this.forceOverwrite = forceOverwrite;
  177. }
  178. /**
  179. * Adds a set of files (nested fileset attribute).
  180. */
  181. public void addFileset(FileSet set) {
  182. filesets.addElement(set);
  183. }
  184. /**
  185. * Calculate the checksum(s).
  186. */
  187. public void execute() throws BuildException {
  188. boolean value = validateAndExecute();
  189. if (verifyProperty != null) {
  190. project.setNewProperty(verifyProperty,
  191. new Boolean(value).toString());
  192. }
  193. }
  194. /**
  195. * Calculate the checksum(s)
  196. *
  197. * @return Returns true if the checksum verification test passed,
  198. * false otherwise.
  199. */
  200. public boolean eval() throws BuildException {
  201. isCondition = true;
  202. return validateAndExecute();
  203. }
  204. /**
  205. * Validate attributes and get down to business.
  206. */
  207. private boolean validateAndExecute() throws BuildException {
  208. String savedFileExt = fileext;
  209. if (file == null && filesets.size() == 0) {
  210. throw new BuildException(
  211. "Specify at least one source - a file or a fileset.");
  212. }
  213. if (file != null && file.exists() && file.isDirectory()) {
  214. throw new BuildException(
  215. "Checksum cannot be generated for directories");
  216. }
  217. if (property != null && fileext != null) {
  218. throw new BuildException(
  219. "Property and FileExt cannot co-exist.");
  220. }
  221. if (property != null) {
  222. if (forceOverwrite) {
  223. throw new BuildException(
  224. "ForceOverwrite cannot be used when Property is specified");
  225. }
  226. if (file != null) {
  227. if (filesets.size() > 0) {
  228. throw new BuildException(
  229. "Multiple files cannot be used when Property is specified");
  230. }
  231. } else {
  232. if (filesets.size() > 1) {
  233. throw new BuildException(
  234. "Multiple files cannot be used when Property is specified");
  235. }
  236. }
  237. }
  238. if (verifyProperty != null) {
  239. isCondition = true;
  240. }
  241. if (verifyProperty != null && forceOverwrite) {
  242. throw new BuildException(
  243. "VerifyProperty and ForceOverwrite cannot co-exist.");
  244. }
  245. if (isCondition && forceOverwrite) {
  246. throw new BuildException(
  247. "ForceOverwrite cannot be used when conditions are being used.");
  248. }
  249. messageDigest = null;
  250. if (provider != null) {
  251. try {
  252. messageDigest = MessageDigest.getInstance(algorithm, provider);
  253. } catch (NoSuchAlgorithmException noalgo) {
  254. throw new BuildException(noalgo, location);
  255. } catch (NoSuchProviderException noprovider) {
  256. throw new BuildException(noprovider, location);
  257. }
  258. } else {
  259. try {
  260. messageDigest = MessageDigest.getInstance(algorithm);
  261. } catch (NoSuchAlgorithmException noalgo) {
  262. throw new BuildException(noalgo, location);
  263. }
  264. }
  265. if (messageDigest == null) {
  266. throw new BuildException("Unable to create Message Digest",
  267. location);
  268. }
  269. if (fileext == null) {
  270. fileext = "." + algorithm;
  271. } else if (fileext.trim().length() == 0) {
  272. throw new BuildException(
  273. "File extension when specified must not be an empty string");
  274. }
  275. try {
  276. addToIncludeFileMap(file);
  277. int sizeofFileSet = filesets.size();
  278. for (int i = 0; i < sizeofFileSet; i++) {
  279. FileSet fs = (FileSet) filesets.elementAt(i);
  280. DirectoryScanner ds = fs.getDirectoryScanner(project);
  281. String[] srcFiles = ds.getIncludedFiles();
  282. for (int j = 0; j < srcFiles.length; j++) {
  283. File src = new File(fs.getDir(project), srcFiles[j]);
  284. addToIncludeFileMap(src);
  285. }
  286. }
  287. return generateChecksums();
  288. } finally {
  289. fileext = savedFileExt;
  290. includeFileMap.clear();
  291. }
  292. }
  293. /**
  294. * Add key-value pair to the hashtable upon which
  295. * to later operate upon.
  296. */
  297. private void addToIncludeFileMap(File file) throws BuildException {
  298. if (file != null) {
  299. if (file.exists()) {
  300. if (property == null) {
  301. File dest = new File(file.getParent(), file.getName() + fileext);
  302. if (forceOverwrite || isCondition ||
  303. (file.lastModified() > dest.lastModified())) {
  304. includeFileMap.put(file, dest);
  305. } else {
  306. log(file + " omitted as " + dest + " is up to date.",
  307. Project.MSG_VERBOSE);
  308. }
  309. } else {
  310. includeFileMap.put(file, property);
  311. }
  312. } else {
  313. String message = "Could not find file "
  314. + file.getAbsolutePath()
  315. + " to generate checksum for.";
  316. log(message);
  317. throw new BuildException(message, location);
  318. }
  319. }
  320. }
  321. /**
  322. * Generate checksum(s) using the message digest created earlier.
  323. */
  324. private boolean generateChecksums() throws BuildException {
  325. boolean checksumMatches = true;
  326. FileInputStream fis = null;
  327. FileOutputStream fos = null;
  328. try {
  329. for (Enumeration e = includeFileMap.keys(); e.hasMoreElements();) {
  330. messageDigest.reset();
  331. File src = (File) e.nextElement();
  332. if (!isCondition) {
  333. log("Calculating "+algorithm+" checksum for "+src);
  334. }
  335. fis = new FileInputStream(src);
  336. DigestInputStream dis = new DigestInputStream(fis,
  337. messageDigest);
  338. while (dis.read() != -1) {
  339. ;
  340. }
  341. dis.close();
  342. fis.close();
  343. fis = null;
  344. byte[] fileDigest = messageDigest.digest ();
  345. StringBuffer checksumSb = new StringBuffer();
  346. for (int i = 0; i < fileDigest.length; i++) {
  347. String hexStr = Integer.toHexString(0x00ff & fileDigest[i]);
  348. if (hexStr.length() < 2) {
  349. checksumSb.append("0");
  350. }
  351. checksumSb.append(hexStr);
  352. }
  353. String checksum = checksumSb.toString();
  354. //can either be a property name string or a file
  355. Object destination = includeFileMap.get(src);
  356. if (destination instanceof java.lang.String) {
  357. String prop = (String) destination;
  358. if (isCondition) {
  359. checksumMatches = checksum.equals(property);
  360. } else {
  361. project.setNewProperty(prop, checksum);
  362. }
  363. } else if (destination instanceof java.io.File) {
  364. if (isCondition) {
  365. File existingFile = (File) destination;
  366. if (existingFile.exists()) {
  367. fis = new FileInputStream(existingFile);
  368. InputStreamReader isr = new InputStreamReader(fis);
  369. BufferedReader br = new BufferedReader(isr);
  370. String suppliedChecksum = br.readLine();
  371. fis.close();
  372. fis = null;
  373. br.close();
  374. isr.close();
  375. checksumMatches =
  376. checksum.equals(suppliedChecksum);
  377. } else {
  378. checksumMatches = false;
  379. }
  380. } else {
  381. File dest = (File) destination;
  382. fos = new FileOutputStream(dest);
  383. fos.write(checksum.getBytes());
  384. fos.close();
  385. fos = null;
  386. }
  387. }
  388. }
  389. } catch (Exception e) {
  390. throw new BuildException(e, location);
  391. } finally {
  392. if (fis != null) {
  393. try {
  394. fis.close();
  395. } catch (IOException e) {}
  396. }
  397. if (fos != null) {
  398. try {
  399. fos.close();
  400. } catch (IOException e) {}
  401. }
  402. }
  403. return checksumMatches;
  404. }
  405. }