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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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.taskdefs.MatchingTask;
  73. import org.apache.tools.ant.types.FileSet;
  74. /**
  75. * This task can be used to create checksums for files.
  76. * It can also be used to verify checksums.
  77. *
  78. * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
  79. *
  80. * @ant.task category="control"
  81. */
  82. public class Checksum extends MatchingTask implements Condition {
  83. /**
  84. * File for which checksum is to be calculated.
  85. */
  86. private File file = null;
  87. /**
  88. * MessageDigest algorithm to be used.
  89. */
  90. private String algorithm = "MD5";
  91. /**
  92. * MessageDigest Algorithm provider
  93. */
  94. private String provider = null;
  95. /**
  96. * File Extension that is be to used to create or identify
  97. * destination file
  98. */
  99. private String fileext;
  100. /**
  101. * Holds generated checksum and gets set as a Project Property.
  102. */
  103. private String property;
  104. /**
  105. * Whether or not to create a new file.
  106. * Defaults to <code>false</code>.
  107. */
  108. private boolean forceOverwrite;
  109. /**
  110. * Contains the result of a checksum verification. ("true" or "false")
  111. */
  112. private String verifyProperty;
  113. /**
  114. * Vector to hold source file sets.
  115. */
  116. private Vector filesets = new Vector();
  117. /**
  118. * Stores SourceFile, DestFile pairs and SourceFile, Property String pairs.
  119. */
  120. private Hashtable includeFileMap = new Hashtable();
  121. /**
  122. * Message Digest instance
  123. */
  124. private MessageDigest messageDigest;
  125. /**
  126. * is this task being used as a nested condition element?
  127. */
  128. private boolean isCondition;
  129. /**
  130. * Sets the file for which the checksum is to be calculated.
  131. */
  132. public void setFile(File file) {
  133. this.file = file;
  134. }
  135. /**
  136. * Sets the MessageDigest algorithm to be used
  137. * to calculate the checksum.
  138. */
  139. public void setAlgorithm(String algorithm) {
  140. this.algorithm = algorithm;
  141. }
  142. /**
  143. * Sets the MessageDigest algorithm provider to be used
  144. * to calculate the checksum.
  145. */
  146. public void setProvider(String provider) {
  147. this.provider = provider;
  148. }
  149. /**
  150. * Sets the File Extension that is be to used to
  151. * create or identify destination file
  152. */
  153. public void setFileext(String fileext) {
  154. this.fileext = fileext;
  155. }
  156. /**
  157. * Sets the property to hold the generated checksum
  158. */
  159. public void setProperty(String property) {
  160. this.property = property;
  161. }
  162. /**
  163. * Sets verify property. This project property holds
  164. * the result of a checksum verification - "true" or "false"
  165. */
  166. public void setVerifyproperty(String verifyProperty) {
  167. this.verifyProperty = verifyProperty;
  168. }
  169. /**
  170. * Whether or not to overwrite existing file irrespective of
  171. * whether it is newer than
  172. * the source file. Defaults to false.
  173. */
  174. public void setForceOverwrite(boolean forceOverwrite) {
  175. this.forceOverwrite = forceOverwrite;
  176. }
  177. /**
  178. * Adds a set of files (nested fileset attribute).
  179. */
  180. public void addFileset(FileSet set) {
  181. filesets.addElement(set);
  182. }
  183. /**
  184. * Calculate the checksum(s).
  185. */
  186. public void execute() throws BuildException {
  187. boolean value = validateAndExecute();
  188. if (verifyProperty != null) {
  189. project.setNewProperty(verifyProperty,
  190. new Boolean(value).toString());
  191. }
  192. }
  193. /**
  194. * Calculate the checksum(s)
  195. *
  196. * @return Returns true if the checksum verification test passed,
  197. * false otherwise.
  198. */
  199. public boolean eval() throws BuildException {
  200. isCondition = true;
  201. return validateAndExecute();
  202. }
  203. /**
  204. * Validate attributes and get down to business.
  205. */
  206. private boolean validateAndExecute() throws BuildException {
  207. if (file == null && filesets.size() == 0) {
  208. throw new BuildException(
  209. "Specify at least one source - a file or a fileset.");
  210. }
  211. if (file != null && file.exists() && file.isDirectory()) {
  212. throw new BuildException(
  213. "Checksum cannot be generated for directories");
  214. }
  215. if (property != null && fileext != null) {
  216. throw new BuildException(
  217. "Property and FileExt cannot co-exist.");
  218. }
  219. if (property != null) {
  220. if (forceOverwrite) {
  221. throw new BuildException(
  222. "ForceOverwrite cannot be used when Property is specified");
  223. }
  224. if (file != null) {
  225. if (filesets.size() > 0) {
  226. throw new BuildException(
  227. "Multiple files cannot be used when Property is specified");
  228. }
  229. } else {
  230. if (filesets.size() > 1) {
  231. throw new BuildException(
  232. "Multiple files cannot be used when Property is specified");
  233. }
  234. }
  235. }
  236. if (verifyProperty != null) {
  237. isCondition = true;
  238. }
  239. if (verifyProperty != null && forceOverwrite) {
  240. throw new BuildException(
  241. "VerifyProperty and ForceOverwrite cannot co-exist.");
  242. }
  243. if (isCondition && forceOverwrite) {
  244. throw new BuildException(
  245. "ForceOverwrite cannot be used when conditions are being used.");
  246. }
  247. if (fileext == null) {
  248. fileext = "." + algorithm;
  249. } else if (fileext.trim().length() == 0) {
  250. throw new BuildException(
  251. "File extension when specified must not be an empty string");
  252. }
  253. messageDigest = null;
  254. if (provider != null) {
  255. try {
  256. messageDigest = MessageDigest.getInstance(algorithm, provider);
  257. } catch (NoSuchAlgorithmException noalgo) {
  258. throw new BuildException(noalgo, location);
  259. } catch (NoSuchProviderException noprovider) {
  260. throw new BuildException(noprovider, location);
  261. }
  262. } else {
  263. try {
  264. messageDigest = MessageDigest.getInstance(algorithm);
  265. } catch (NoSuchAlgorithmException noalgo) {
  266. throw new BuildException(noalgo, location);
  267. }
  268. }
  269. if (messageDigest == null) {
  270. throw new BuildException("Unable to create Message Digest",
  271. location);
  272. }
  273. addToIncludeFileMap(file);
  274. int sizeofFileSet = filesets.size();
  275. for (int i = 0; i < sizeofFileSet; i++) {
  276. FileSet fs = (FileSet) filesets.elementAt(i);
  277. DirectoryScanner ds = fs.getDirectoryScanner(project);
  278. String[] srcFiles = ds.getIncludedFiles();
  279. for (int j = 0; j < srcFiles.length; j++) {
  280. File src = new File(fs.getDir(project), srcFiles[j]);
  281. addToIncludeFileMap(src);
  282. }
  283. }
  284. return generateChecksums();
  285. }
  286. /**
  287. * Add key-value pair to the hashtable upon which
  288. * to later operate upon.
  289. */
  290. private void addToIncludeFileMap(File file) throws BuildException {
  291. if (file != null) {
  292. if (file.exists()) {
  293. if (property == null) {
  294. File dest = new File(file.getParent(), file.getName() + fileext);
  295. if (forceOverwrite || isCondition ||
  296. (file.lastModified() > dest.lastModified())) {
  297. includeFileMap.put(file, dest);
  298. } else {
  299. log(file + " omitted as " + dest + " is up to date.",
  300. Project.MSG_VERBOSE);
  301. }
  302. } else {
  303. includeFileMap.put(file, property);
  304. }
  305. } else {
  306. String message = "Could not find file "
  307. + file.getAbsolutePath()
  308. + " to generate checksum for.";
  309. log(message);
  310. throw new BuildException(message, location);
  311. }
  312. }
  313. }
  314. /**
  315. * Generate checksum(s) using the message digest created earlier.
  316. */
  317. private boolean generateChecksums() throws BuildException {
  318. boolean checksumMatches = true;
  319. FileInputStream fis = null;
  320. FileOutputStream fos = null;
  321. try {
  322. for (Enumeration e = includeFileMap.keys(); e.hasMoreElements();) {
  323. messageDigest.reset();
  324. File src = (File) e.nextElement();
  325. if (!isCondition) {
  326. log("Calculating "+algorithm+" checksum for "+src);
  327. }
  328. fis = new FileInputStream(src);
  329. DigestInputStream dis = new DigestInputStream(fis,
  330. messageDigest);
  331. while (dis.read() != -1) {
  332. ;
  333. }
  334. dis.close();
  335. fis.close();
  336. fis = null;
  337. byte[] fileDigest = messageDigest.digest ();
  338. StringBuffer checksumSb = new StringBuffer();
  339. for (int i = 0; i < fileDigest.length; i++) {
  340. String hexStr = Integer.toHexString(0x00ff & fileDigest[i]);
  341. if (hexStr.length() < 2) {
  342. checksumSb.append("0");
  343. }
  344. checksumSb.append(hexStr);
  345. }
  346. String checksum = checksumSb.toString();
  347. //can either be a property name string or a file
  348. Object destination = includeFileMap.get(src);
  349. if (destination instanceof java.lang.String) {
  350. String prop = (String) destination;
  351. if (isCondition) {
  352. checksumMatches = checksum.equals(property);
  353. } else {
  354. project.setProperty(prop, checksum);
  355. }
  356. } else if (destination instanceof java.io.File) {
  357. if (isCondition) {
  358. File existingFile = (File) destination;
  359. if (existingFile.exists() &&
  360. existingFile.length() == checksum.length()) {
  361. fis = new FileInputStream(existingFile);
  362. InputStreamReader isr = new InputStreamReader(fis);
  363. BufferedReader br = new BufferedReader(isr);
  364. String suppliedChecksum = br.readLine();
  365. fis.close();
  366. fis = null;
  367. br.close();
  368. isr.close();
  369. checksumMatches =
  370. checksum.equals(suppliedChecksum);
  371. } else {
  372. checksumMatches = false;
  373. }
  374. } else {
  375. File dest = (File) destination;
  376. fos = new FileOutputStream(dest);
  377. fos.write(checksum.getBytes());
  378. fos.close();
  379. fos = null;
  380. }
  381. }
  382. }
  383. } catch (Exception e) {
  384. throw new BuildException(e, location);
  385. } finally {
  386. if (fis != null) {
  387. try {
  388. fis.close();
  389. } catch (IOException e) {}
  390. }
  391. if (fos != null) {
  392. try {
  393. fos.close();
  394. } catch (IOException e) {}
  395. }
  396. }
  397. return checksumMatches;
  398. }
  399. }