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.

Delete.java 21 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-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.File;
  56. import java.util.Vector;
  57. import org.apache.tools.ant.BuildException;
  58. import org.apache.tools.ant.DirectoryScanner;
  59. import org.apache.tools.ant.Project;
  60. import org.apache.tools.ant.types.FileSet;
  61. import org.apache.tools.ant.types.PatternSet;
  62. import org.apache.tools.ant.types.selectors.AndSelector;
  63. import org.apache.tools.ant.types.selectors.ContainsSelector;
  64. import org.apache.tools.ant.types.selectors.DateSelector;
  65. import org.apache.tools.ant.types.selectors.DependSelector;
  66. import org.apache.tools.ant.types.selectors.DepthSelector;
  67. import org.apache.tools.ant.types.selectors.ExtendSelector;
  68. import org.apache.tools.ant.types.selectors.FileSelector;
  69. import org.apache.tools.ant.types.selectors.FilenameSelector;
  70. import org.apache.tools.ant.types.selectors.MajoritySelector;
  71. import org.apache.tools.ant.types.selectors.NoneSelector;
  72. import org.apache.tools.ant.types.selectors.NotSelector;
  73. import org.apache.tools.ant.types.selectors.OrSelector;
  74. import org.apache.tools.ant.types.selectors.PresentSelector;
  75. import org.apache.tools.ant.types.selectors.SelectSelector;
  76. import org.apache.tools.ant.types.selectors.SelectorContainer;
  77. import org.apache.tools.ant.types.selectors.SizeSelector;
  78. /**
  79. * Deletes a file or directory, or set of files defined by a fileset.
  80. * The original delete task would delete a file, or a set of files
  81. * using the include/exclude syntax. The deltree task would delete a
  82. * directory tree. This task combines the functionality of these two
  83. * originally distinct tasks.
  84. * <p>Currently Delete extends MatchingTask. This is intend <i>only</i>
  85. * to provide backwards compatibility for a release. The future position
  86. * is to use nested filesets exclusively.</p>
  87. *
  88. * @author Stefano Mazzocchi
  89. * <a href="mailto:stefano@apache.org">stefano@apache.org</a>
  90. * @author Tom Dimock <a href="mailto:tad1@cornell.edu">tad1@cornell.edu</a>
  91. * @author Glenn McAllister
  92. * <a href="mailto:glennm@ca.ibm.com">glennm@ca.ibm.com</a>
  93. * @author Jon S. Stevens <a href="mailto:jon@latchkey.com">jon@latchkey.com</a>
  94. *
  95. * @since Ant 1.2
  96. *
  97. * @ant.task category="filesystem"
  98. */
  99. public class Delete extends MatchingTask {
  100. protected File file = null;
  101. protected File dir = null;
  102. protected Vector filesets = new Vector();
  103. protected boolean usedMatchingTask = false;
  104. // by default, remove matching empty dirs
  105. protected boolean includeEmpty = false;
  106. private int verbosity = Project.MSG_VERBOSE;
  107. private boolean quiet = false;
  108. private boolean failonerror = true;
  109. /**
  110. * Set the name of a single file to be removed.
  111. *
  112. * @param file the file to be deleted
  113. */
  114. public void setFile(File file) {
  115. this.file = file;
  116. }
  117. /**
  118. * Set the directory from which files are to be deleted
  119. *
  120. * @param dir the directory path.
  121. */
  122. public void setDir(File dir) {
  123. this.dir = dir;
  124. }
  125. /**
  126. * If true, list all names of deleted files.
  127. *
  128. * @param verbose "true" or "on"
  129. */
  130. public void setVerbose(boolean verbose) {
  131. if (verbose) {
  132. this.verbosity = Project.MSG_INFO;
  133. } else {
  134. this.verbosity = Project.MSG_VERBOSE;
  135. }
  136. }
  137. /**
  138. * If true and the file does not exist, do not display a diagnostic
  139. * message or modify the exit status to reflect an error.
  140. * This means that if a file or directory cannot be deleted,
  141. * then no error is reported. This setting emulates the
  142. * -f option to the Unix &quot;rm&quot; command.
  143. * Default is false meaning things are &quot;noisy&quot;
  144. * @param quiet "true" or "on"
  145. */
  146. public void setQuiet(boolean quiet) {
  147. this.quiet = quiet;
  148. if (quiet) {
  149. this.failonerror = false;
  150. }
  151. }
  152. /**
  153. * If false, note errors but continue.
  154. *
  155. * @param failonerror true or false
  156. */
  157. public void setFailOnError(boolean failonerror) {
  158. this.failonerror = failonerror;
  159. }
  160. /**
  161. * If true, delete empty directories.
  162. */
  163. public void setIncludeEmptyDirs(boolean includeEmpty) {
  164. this.includeEmpty = includeEmpty;
  165. }
  166. /**
  167. * Adds a set of files to be deleted.
  168. */
  169. public void addFileset(FileSet set) {
  170. filesets.addElement(set);
  171. }
  172. /**
  173. * add a name entry on the include list
  174. */
  175. public PatternSet.NameEntry createInclude() {
  176. usedMatchingTask = true;
  177. return super.createInclude();
  178. }
  179. /**
  180. * add a name entry on the include files list
  181. */
  182. public PatternSet.NameEntry createIncludesFile() {
  183. usedMatchingTask = true;
  184. return super.createIncludesFile();
  185. }
  186. /**
  187. * add a name entry on the exclude list
  188. */
  189. public PatternSet.NameEntry createExclude() {
  190. usedMatchingTask = true;
  191. return super.createExclude();
  192. }
  193. /**
  194. * add a name entry on the include files list
  195. */
  196. public PatternSet.NameEntry createExcludesFile() {
  197. usedMatchingTask = true;
  198. return super.createExcludesFile();
  199. }
  200. /**
  201. * add a set of patterns
  202. */
  203. public PatternSet createPatternSet() {
  204. usedMatchingTask = true;
  205. return super.createPatternSet();
  206. }
  207. /**
  208. * Sets the set of include patterns. Patterns may be separated by a comma
  209. * or a space.
  210. *
  211. * @param includes the string containing the include patterns
  212. */
  213. public void setIncludes(String includes) {
  214. usedMatchingTask = true;
  215. super.setIncludes(includes);
  216. }
  217. /**
  218. * Sets the set of exclude patterns. Patterns may be separated by a comma
  219. * or a space.
  220. *
  221. * @param excludes the string containing the exclude patterns
  222. */
  223. public void setExcludes(String excludes) {
  224. usedMatchingTask = true;
  225. super.setExcludes(excludes);
  226. }
  227. /**
  228. * Sets whether default exclusions should be used or not.
  229. *
  230. * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
  231. * should be used, "false"|"off"|"no" when they
  232. * shouldn't be used.
  233. */
  234. public void setDefaultexcludes(boolean useDefaultExcludes) {
  235. usedMatchingTask = true;
  236. super.setDefaultexcludes(useDefaultExcludes);
  237. }
  238. /**
  239. * Sets the name of the file containing the includes patterns.
  240. *
  241. * @param includesfile A string containing the filename to fetch
  242. * the include patterns from.
  243. */
  244. public void setIncludesfile(File includesfile) {
  245. usedMatchingTask = true;
  246. super.setIncludesfile(includesfile);
  247. }
  248. /**
  249. * Sets the name of the file containing the includes patterns.
  250. *
  251. * @param excludesfile A string containing the filename to fetch
  252. * the include patterns from.
  253. */
  254. public void setExcludesfile(File excludesfile) {
  255. usedMatchingTask = true;
  256. super.setExcludesfile(excludesfile);
  257. }
  258. /**
  259. * Sets case sensitivity of the file system
  260. *
  261. * @param isCaseSensitive "true"|"on"|"yes" if file system is case
  262. * sensitive, "false"|"off"|"no" when not.
  263. */
  264. public void setCaseSensitive(boolean isCaseSensitive) {
  265. usedMatchingTask = true;
  266. super.setCaseSensitive(isCaseSensitive);
  267. }
  268. /**
  269. * Sets whether or not symbolic links should be followed.
  270. *
  271. * @param followSymlinks whether or not symbolic links should be followed
  272. */
  273. public void setFollowSymlinks(boolean followSymlinks) {
  274. usedMatchingTask = true;
  275. super.setFollowSymlinks(followSymlinks);
  276. }
  277. /**
  278. * add a "Select" selector entry on the selector list
  279. */
  280. public void addSelector(SelectSelector selector) {
  281. usedMatchingTask = true;
  282. super.addSelector(selector);
  283. }
  284. /**
  285. * add an "And" selector entry on the selector list
  286. */
  287. public void addAnd(AndSelector selector) {
  288. usedMatchingTask = true;
  289. super.addAnd(selector);
  290. }
  291. /**
  292. * add an "Or" selector entry on the selector list
  293. */
  294. public void addOr(OrSelector selector) {
  295. usedMatchingTask = true;
  296. super.addOr(selector);
  297. }
  298. /**
  299. * add a "Not" selector entry on the selector list
  300. */
  301. public void addNot(NotSelector selector) {
  302. usedMatchingTask = true;
  303. super.addNot(selector);
  304. }
  305. /**
  306. * add a "None" selector entry on the selector list
  307. */
  308. public void addNone(NoneSelector selector) {
  309. usedMatchingTask = true;
  310. super.addNone(selector);
  311. }
  312. /**
  313. * add a majority selector entry on the selector list
  314. */
  315. public void addMajority(MajoritySelector selector) {
  316. usedMatchingTask = true;
  317. super.addMajority(selector);
  318. }
  319. /**
  320. * add a selector date entry on the selector list
  321. */
  322. public void addDate(DateSelector selector) {
  323. usedMatchingTask = true;
  324. super.addDate(selector);
  325. }
  326. /**
  327. * add a selector size entry on the selector list
  328. */
  329. public void addSize(SizeSelector selector) {
  330. usedMatchingTask = true;
  331. super.addSize(selector);
  332. }
  333. /**
  334. * add a selector filename entry on the selector list
  335. */
  336. public void addFilename(FilenameSelector selector) {
  337. usedMatchingTask = true;
  338. super.addFilename(selector);
  339. }
  340. /**
  341. * add an extended selector entry on the selector list
  342. */
  343. public void addCustom(ExtendSelector selector) {
  344. usedMatchingTask = true;
  345. super.addCustom(selector);
  346. }
  347. /**
  348. * add a contains selector entry on the selector list
  349. */
  350. public void addContains(ContainsSelector selector) {
  351. usedMatchingTask = true;
  352. super.addContains(selector);
  353. }
  354. /**
  355. * add a present selector entry on the selector list
  356. */
  357. public void addPresent(PresentSelector selector) {
  358. usedMatchingTask = true;
  359. super.addPresent(selector);
  360. }
  361. /**
  362. * add a depth selector entry on the selector list
  363. */
  364. public void addDepth(DepthSelector selector) {
  365. usedMatchingTask = true;
  366. super.addDepth(selector);
  367. }
  368. /**
  369. * add a depends selector entry on the selector list
  370. */
  371. public void addDepend(DependSelector selector) {
  372. usedMatchingTask = true;
  373. super.addDepend(selector);
  374. }
  375. /**
  376. * Delete the file(s).
  377. */
  378. public void execute() throws BuildException {
  379. if (usedMatchingTask) {
  380. log("DEPRECATED - Use of the implicit FileSet is deprecated. "
  381. + "Use a nested fileset element instead.");
  382. }
  383. if (file == null && dir == null && filesets.size() == 0) {
  384. throw new BuildException("At least one of the file or dir "
  385. + "attributes, or a fileset element, "
  386. + "must be set.");
  387. }
  388. if (quiet && failonerror) {
  389. throw new BuildException("quiet and failonerror cannot both be "
  390. + "set to true", getLocation());
  391. }
  392. // delete the single file
  393. if (file != null) {
  394. if (file.exists()) {
  395. if (file.isDirectory()) {
  396. log("Directory " + file.getAbsolutePath()
  397. + " cannot be removed using the file attribute. "
  398. + "Use dir instead.");
  399. } else {
  400. log("Deleting: " + file.getAbsolutePath());
  401. if (!file.delete()) {
  402. String message = "Unable to delete file "
  403. + file.getAbsolutePath();
  404. if (failonerror) {
  405. throw new BuildException(message);
  406. } else {
  407. log(message, quiet ? Project.MSG_VERBOSE
  408. : Project.MSG_WARN);
  409. }
  410. }
  411. }
  412. } else {
  413. log("Could not find file " + file.getAbsolutePath()
  414. + " to delete.",
  415. Project.MSG_VERBOSE);
  416. }
  417. }
  418. // delete the directory
  419. if (dir != null && dir.exists() && dir.isDirectory() &&
  420. !usedMatchingTask) {
  421. /*
  422. If verbosity is MSG_VERBOSE, that mean we are doing
  423. regular logging (backwards as that sounds). In that
  424. case, we want to print one message about deleting the
  425. top of the directory tree. Otherwise, the removeDir
  426. method will handle messages for _all_ directories.
  427. */
  428. if (verbosity == Project.MSG_VERBOSE) {
  429. log("Deleting directory " + dir.getAbsolutePath());
  430. }
  431. removeDir(dir);
  432. }
  433. // delete the files in the filesets
  434. for (int i = 0; i < filesets.size(); i++) {
  435. FileSet fs = (FileSet) filesets.elementAt(i);
  436. try {
  437. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  438. String[] files = ds.getIncludedFiles();
  439. String[] dirs = ds.getIncludedDirectories();
  440. removeFiles(fs.getDir(getProject()), files, dirs);
  441. } catch (BuildException be) {
  442. // directory doesn't exist or is not readable
  443. if (failonerror) {
  444. throw be;
  445. } else {
  446. log(be.getMessage(),
  447. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  448. }
  449. }
  450. }
  451. // delete the files from the default fileset
  452. if (usedMatchingTask && dir != null) {
  453. try {
  454. DirectoryScanner ds = super.getDirectoryScanner(dir);
  455. String[] files = ds.getIncludedFiles();
  456. String[] dirs = ds.getIncludedDirectories();
  457. removeFiles(dir, files, dirs);
  458. } catch (BuildException be) {
  459. // directory doesn't exist or is not readable
  460. if (failonerror) {
  461. throw be;
  462. } else {
  463. log(be.getMessage(),
  464. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  465. }
  466. }
  467. }
  468. }
  469. //************************************************************************
  470. // protected and private methods
  471. //************************************************************************
  472. protected void removeDir(File d) {
  473. String[] list = d.list();
  474. if (list == null) {
  475. list = new String[0];
  476. }
  477. for (int i = 0; i < list.length; i++) {
  478. String s = list[i];
  479. File f = new File(d, s);
  480. if (f.isDirectory()) {
  481. removeDir(f);
  482. } else {
  483. log("Deleting " + f.getAbsolutePath(), verbosity);
  484. if (!f.delete()) {
  485. String message = "Unable to delete file "
  486. + f.getAbsolutePath();
  487. if (failonerror) {
  488. throw new BuildException(message);
  489. } else {
  490. log(message,
  491. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  492. }
  493. }
  494. }
  495. }
  496. log("Deleting directory " + d.getAbsolutePath(), verbosity);
  497. if (!d.delete()) {
  498. String message = "Unable to delete directory "
  499. + dir.getAbsolutePath();
  500. if (failonerror) {
  501. throw new BuildException(message);
  502. } else {
  503. log(message,
  504. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  505. }
  506. }
  507. }
  508. /**
  509. * remove an array of files in a directory, and a list of subdirectories
  510. * which will only be deleted if 'includeEmpty' is true
  511. * @param d directory to work from
  512. * @param files array of files to delete; can be of zero length
  513. * @param dirs array of directories to delete; can of zero length
  514. */
  515. protected void removeFiles(File d, String[] files, String[] dirs) {
  516. if (files.length > 0) {
  517. log("Deleting " + files.length + " files from "
  518. + d.getAbsolutePath());
  519. for (int j = 0; j < files.length; j++) {
  520. File f = new File(d, files[j]);
  521. log("Deleting " + f.getAbsolutePath(), verbosity);
  522. if (!f.delete()) {
  523. String message = "Unable to delete file "
  524. + f.getAbsolutePath();
  525. if (failonerror) {
  526. throw new BuildException(message);
  527. } else {
  528. log(message,
  529. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  530. }
  531. }
  532. }
  533. }
  534. if (dirs.length > 0 && includeEmpty) {
  535. int dirCount = 0;
  536. for (int j = dirs.length - 1; j >= 0; j--) {
  537. File dir = new File(d, dirs[j]);
  538. String[] dirFiles = dir.list();
  539. if (dirFiles == null || dirFiles.length == 0) {
  540. log("Deleting " + dir.getAbsolutePath(), verbosity);
  541. if (!dir.delete()) {
  542. String message = "Unable to delete directory "
  543. + dir.getAbsolutePath();
  544. if (failonerror) {
  545. throw new BuildException(message);
  546. } else {
  547. log(message,
  548. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  549. }
  550. } else {
  551. dirCount++;
  552. }
  553. }
  554. }
  555. if (dirCount > 0) {
  556. log("Deleted " + dirCount + " director" +
  557. (dirCount == 1 ? "y" : "ies") +
  558. " from " + d.getAbsolutePath());
  559. }
  560. }
  561. }
  562. }