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 22 kB

PR: 19897 Submitted by: Peter Reilly This patch adds the add(Type) to the introspection rules and updates ConditionBase, FilterChain, Path, SelectorBase and TokenFilter to use the new introspection rule. ========================================= = Changed Files ========================================= src/main/org/apache/tools/ant/ProjectHelper.java add two methods used by introspection - getComponentClass and createComponent src/main/org/apache/tools/ant/IntrospectionHelper.java implement addTypeMethods add(Type) src/main/org/apache/tools/ant/filters/TokenFilter.java get TokenFilter to use add(Type) instead of dynamicconfigurator make all nested classes ProjectComponents src/main/org/apache/tools/ant/taskdefs/Delete.java implement an add(FileSelector) method src/main/org/apache/tools/ant/taskdefs/MatchingTask.java implement an add(FileSelector) method src/main/org/apache/tools/ant/taskdefs/condition/ConditionBase.java add an add(Condition) method to demostrate use of add(Type) method src/main/org/apache/tools/ant/types/AbstractFileSet.java implement add(FileSelector) src/main/org/apache/tools/ant/types/FilterChain.java use add(ChainableReader) instead of DynamicConfigurator src/main/org/apache/tools/ant/types/Path.java add an add(Path) method src/main/org/apache/tools/ant/types/optional/ScriptFilter.java remove set/get project as parent imlements them now src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java implement the add(FileSelector) method src/main/org/apache/tools/ant/types/selectors/SelectorContainer.java add an add(FileSelector) method ========================================= = New Files ========================================= src/etc/testcases/types/addtype.xml testcases for addtype src/testcases/org/apache/tools/ant/types/AddTypeTest.java test cases for add type git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274635 13f79535-47bb-0310-9956-ffa450edef68
22 years ago
PR: 19897 Submitted by: Peter Reilly This patch adds the add(Type) to the introspection rules and updates ConditionBase, FilterChain, Path, SelectorBase and TokenFilter to use the new introspection rule. ========================================= = Changed Files ========================================= src/main/org/apache/tools/ant/ProjectHelper.java add two methods used by introspection - getComponentClass and createComponent src/main/org/apache/tools/ant/IntrospectionHelper.java implement addTypeMethods add(Type) src/main/org/apache/tools/ant/filters/TokenFilter.java get TokenFilter to use add(Type) instead of dynamicconfigurator make all nested classes ProjectComponents src/main/org/apache/tools/ant/taskdefs/Delete.java implement an add(FileSelector) method src/main/org/apache/tools/ant/taskdefs/MatchingTask.java implement an add(FileSelector) method src/main/org/apache/tools/ant/taskdefs/condition/ConditionBase.java add an add(Condition) method to demostrate use of add(Type) method src/main/org/apache/tools/ant/types/AbstractFileSet.java implement add(FileSelector) src/main/org/apache/tools/ant/types/FilterChain.java use add(ChainableReader) instead of DynamicConfigurator src/main/org/apache/tools/ant/types/Path.java add an add(Path) method src/main/org/apache/tools/ant/types/optional/ScriptFilter.java remove set/get project as parent imlements them now src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java implement the add(FileSelector) method src/main/org/apache/tools/ant/types/selectors/SelectorContainer.java add an add(FileSelector) method ========================================= = New Files ========================================= src/etc/testcases/types/addtype.xml testcases for addtype src/testcases/org/apache/tools/ant/types/AddTypeTest.java test cases for add type git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274635 13f79535-47bb-0310-9956-ffa450edef68
22 years ago
PR: 19897 Submitted by: Peter Reilly This patch adds the add(Type) to the introspection rules and updates ConditionBase, FilterChain, Path, SelectorBase and TokenFilter to use the new introspection rule. ========================================= = Changed Files ========================================= src/main/org/apache/tools/ant/ProjectHelper.java add two methods used by introspection - getComponentClass and createComponent src/main/org/apache/tools/ant/IntrospectionHelper.java implement addTypeMethods add(Type) src/main/org/apache/tools/ant/filters/TokenFilter.java get TokenFilter to use add(Type) instead of dynamicconfigurator make all nested classes ProjectComponents src/main/org/apache/tools/ant/taskdefs/Delete.java implement an add(FileSelector) method src/main/org/apache/tools/ant/taskdefs/MatchingTask.java implement an add(FileSelector) method src/main/org/apache/tools/ant/taskdefs/condition/ConditionBase.java add an add(Condition) method to demostrate use of add(Type) method src/main/org/apache/tools/ant/types/AbstractFileSet.java implement add(FileSelector) src/main/org/apache/tools/ant/types/FilterChain.java use add(ChainableReader) instead of DynamicConfigurator src/main/org/apache/tools/ant/types/Path.java add an add(Path) method src/main/org/apache/tools/ant/types/optional/ScriptFilter.java remove set/get project as parent imlements them now src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java implement the add(FileSelector) method src/main/org/apache/tools/ant/types/selectors/SelectorContainer.java add an add(FileSelector) method ========================================= = New Files ========================================= src/etc/testcases/types/addtype.xml testcases for addtype src/testcases/org/apache/tools/ant/types/AddTypeTest.java test cases for add type git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274635 13f79535-47bb-0310-9956-ffa450edef68
22 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import java.util.Vector;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.DirectoryScanner;
  22. import org.apache.tools.ant.Project;
  23. import org.apache.tools.ant.taskdefs.condition.Os;
  24. import org.apache.tools.ant.types.FileSet;
  25. import org.apache.tools.ant.types.PatternSet;
  26. import org.apache.tools.ant.types.selectors.AndSelector;
  27. import org.apache.tools.ant.types.selectors.ContainsRegexpSelector;
  28. import org.apache.tools.ant.types.selectors.ContainsSelector;
  29. import org.apache.tools.ant.types.selectors.DateSelector;
  30. import org.apache.tools.ant.types.selectors.DependSelector;
  31. import org.apache.tools.ant.types.selectors.DepthSelector;
  32. import org.apache.tools.ant.types.selectors.ExtendSelector;
  33. import org.apache.tools.ant.types.selectors.FilenameSelector;
  34. import org.apache.tools.ant.types.selectors.MajoritySelector;
  35. import org.apache.tools.ant.types.selectors.NoneSelector;
  36. import org.apache.tools.ant.types.selectors.NotSelector;
  37. import org.apache.tools.ant.types.selectors.OrSelector;
  38. import org.apache.tools.ant.types.selectors.PresentSelector;
  39. import org.apache.tools.ant.types.selectors.SelectSelector;
  40. import org.apache.tools.ant.types.selectors.SizeSelector;
  41. import org.apache.tools.ant.types.selectors.FileSelector;
  42. import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector;
  43. /**
  44. * Deletes a file or directory, or set of files defined by a fileset.
  45. * The original delete task would delete a file, or a set of files
  46. * using the include/exclude syntax. The deltree task would delete a
  47. * directory tree. This task combines the functionality of these two
  48. * originally distinct tasks.
  49. * <p>Currently Delete extends MatchingTask. This is intend <i>only</i>
  50. * to provide backwards compatibility for a release. The future position
  51. * is to use nested filesets exclusively.</p>
  52. *
  53. * @author Stefano Mazzocchi
  54. * <a href="mailto:stefano@apache.org">stefano@apache.org</a>
  55. * @author Tom Dimock <a href="mailto:tad1@cornell.edu">tad1@cornell.edu</a>
  56. * @author Glenn McAllister
  57. * <a href="mailto:glennm@ca.ibm.com">glennm@ca.ibm.com</a>
  58. * @author Jon S. Stevens <a href="mailto:jon@latchkey.com">jon@latchkey.com</a>
  59. *
  60. * @since Ant 1.2
  61. *
  62. * @ant.task category="filesystem"
  63. */
  64. public class Delete extends MatchingTask {
  65. private static final int DELETE_RETRY_SLEEP_MILLIS = 10;
  66. protected File file = null;
  67. protected File dir = null;
  68. protected Vector filesets = new Vector();
  69. protected boolean usedMatchingTask = false;
  70. // by default, remove matching empty dirs
  71. protected boolean includeEmpty = false;
  72. private int verbosity = Project.MSG_VERBOSE;
  73. private boolean quiet = false;
  74. private boolean failonerror = true;
  75. /**
  76. * Set the name of a single file to be removed.
  77. *
  78. * @param file the file to be deleted
  79. */
  80. public void setFile(File file) {
  81. this.file = file;
  82. }
  83. /**
  84. * Set the directory from which files are to be deleted
  85. *
  86. * @param dir the directory path.
  87. */
  88. public void setDir(File dir) {
  89. this.dir = dir;
  90. }
  91. /**
  92. * If true, list all names of deleted files.
  93. *
  94. * @param verbose "true" or "on"
  95. */
  96. public void setVerbose(boolean verbose) {
  97. if (verbose) {
  98. this.verbosity = Project.MSG_INFO;
  99. } else {
  100. this.verbosity = Project.MSG_VERBOSE;
  101. }
  102. }
  103. /**
  104. * If true and the file does not exist, do not display a diagnostic
  105. * message or modify the exit status to reflect an error.
  106. * This means that if a file or directory cannot be deleted,
  107. * then no error is reported. This setting emulates the
  108. * -f option to the Unix &quot;rm&quot; command.
  109. * Default is false meaning things are &quot;noisy&quot;
  110. * @param quiet "true" or "on"
  111. */
  112. public void setQuiet(boolean quiet) {
  113. this.quiet = quiet;
  114. if (quiet) {
  115. this.failonerror = false;
  116. }
  117. }
  118. /**
  119. * If false, note errors but continue.
  120. *
  121. * @param failonerror true or false
  122. */
  123. public void setFailOnError(boolean failonerror) {
  124. this.failonerror = failonerror;
  125. }
  126. /**
  127. * If true, delete empty directories.
  128. * @param includeEmpty if true delete empty directories (only
  129. * for filesets). Default is false.
  130. */
  131. public void setIncludeEmptyDirs(boolean includeEmpty) {
  132. this.includeEmpty = includeEmpty;
  133. }
  134. /**
  135. * Adds a set of files to be deleted.
  136. * @param set the set of files to be deleted
  137. */
  138. public void addFileset(FileSet set) {
  139. filesets.addElement(set);
  140. }
  141. /**
  142. * add a name entry on the include list
  143. * @return a NameEntry object to be configured
  144. */
  145. public PatternSet.NameEntry createInclude() {
  146. usedMatchingTask = true;
  147. return super.createInclude();
  148. }
  149. /**
  150. * add a name entry on the include files list
  151. * @return an NameEntry object to be configured
  152. */
  153. public PatternSet.NameEntry createIncludesFile() {
  154. usedMatchingTask = true;
  155. return super.createIncludesFile();
  156. }
  157. /**
  158. * add a name entry on the exclude list
  159. * @return an NameEntry object to be configured
  160. */
  161. public PatternSet.NameEntry createExclude() {
  162. usedMatchingTask = true;
  163. return super.createExclude();
  164. }
  165. /**
  166. * add a name entry on the include files list
  167. * @return an NameEntry object to be configured
  168. */
  169. public PatternSet.NameEntry createExcludesFile() {
  170. usedMatchingTask = true;
  171. return super.createExcludesFile();
  172. }
  173. /**
  174. * add a set of patterns
  175. * @return PatternSet object to be configured
  176. */
  177. public PatternSet createPatternSet() {
  178. usedMatchingTask = true;
  179. return super.createPatternSet();
  180. }
  181. /**
  182. * Sets the set of include patterns. Patterns may be separated by a comma
  183. * or a space.
  184. *
  185. * @param includes the string containing the include patterns
  186. */
  187. public void setIncludes(String includes) {
  188. usedMatchingTask = true;
  189. super.setIncludes(includes);
  190. }
  191. /**
  192. * Sets the set of exclude patterns. Patterns may be separated by a comma
  193. * or a space.
  194. *
  195. * @param excludes the string containing the exclude patterns
  196. */
  197. public void setExcludes(String excludes) {
  198. usedMatchingTask = true;
  199. super.setExcludes(excludes);
  200. }
  201. /**
  202. * Sets whether default exclusions should be used or not.
  203. *
  204. * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
  205. * should be used, "false"|"off"|"no" when they
  206. * shouldn't be used.
  207. */
  208. public void setDefaultexcludes(boolean useDefaultExcludes) {
  209. usedMatchingTask = true;
  210. super.setDefaultexcludes(useDefaultExcludes);
  211. }
  212. /**
  213. * Sets the name of the file containing the includes patterns.
  214. *
  215. * @param includesfile A string containing the filename to fetch
  216. * the include patterns from.
  217. */
  218. public void setIncludesfile(File includesfile) {
  219. usedMatchingTask = true;
  220. super.setIncludesfile(includesfile);
  221. }
  222. /**
  223. * Sets the name of the file containing the includes patterns.
  224. *
  225. * @param excludesfile A string containing the filename to fetch
  226. * the include patterns from.
  227. */
  228. public void setExcludesfile(File excludesfile) {
  229. usedMatchingTask = true;
  230. super.setExcludesfile(excludesfile);
  231. }
  232. /**
  233. * Sets case sensitivity of the file system
  234. *
  235. * @param isCaseSensitive "true"|"on"|"yes" if file system is case
  236. * sensitive, "false"|"off"|"no" when not.
  237. */
  238. public void setCaseSensitive(boolean isCaseSensitive) {
  239. usedMatchingTask = true;
  240. super.setCaseSensitive(isCaseSensitive);
  241. }
  242. /**
  243. * Sets whether or not symbolic links should be followed.
  244. *
  245. * @param followSymlinks whether or not symbolic links should be followed
  246. */
  247. public void setFollowSymlinks(boolean followSymlinks) {
  248. usedMatchingTask = true;
  249. super.setFollowSymlinks(followSymlinks);
  250. }
  251. /**
  252. * add a "Select" selector entry on the selector list
  253. * @param selector the selector to be added
  254. */
  255. public void addSelector(SelectSelector selector) {
  256. usedMatchingTask = true;
  257. super.addSelector(selector);
  258. }
  259. /**
  260. * add an "And" selector entry on the selector list
  261. * @param selector the selector to be added
  262. */
  263. public void addAnd(AndSelector selector) {
  264. usedMatchingTask = true;
  265. super.addAnd(selector);
  266. }
  267. /**
  268. * add an "Or" selector entry on the selector list
  269. * @param selector the selector to be added
  270. */
  271. public void addOr(OrSelector selector) {
  272. usedMatchingTask = true;
  273. super.addOr(selector);
  274. }
  275. /**
  276. * add a "Not" selector entry on the selector list
  277. * @param selector the selector to be added
  278. */
  279. public void addNot(NotSelector selector) {
  280. usedMatchingTask = true;
  281. super.addNot(selector);
  282. }
  283. /**
  284. * add a "None" selector entry on the selector list
  285. * @param selector the selector to be added
  286. */
  287. public void addNone(NoneSelector selector) {
  288. usedMatchingTask = true;
  289. super.addNone(selector);
  290. }
  291. /**
  292. * add a majority selector entry on the selector list
  293. * @param selector the selector to be added
  294. */
  295. public void addMajority(MajoritySelector selector) {
  296. usedMatchingTask = true;
  297. super.addMajority(selector);
  298. }
  299. /**
  300. * add a selector date entry on the selector list
  301. * @param selector the selector to be added
  302. */
  303. public void addDate(DateSelector selector) {
  304. usedMatchingTask = true;
  305. super.addDate(selector);
  306. }
  307. /**
  308. * add a selector size entry on the selector list
  309. * @param selector the selector to be added
  310. */
  311. public void addSize(SizeSelector selector) {
  312. usedMatchingTask = true;
  313. super.addSize(selector);
  314. }
  315. /**
  316. * add a selector filename entry on the selector list
  317. * @param selector the selector to be added
  318. */
  319. public void addFilename(FilenameSelector selector) {
  320. usedMatchingTask = true;
  321. super.addFilename(selector);
  322. }
  323. /**
  324. * add an extended selector entry on the selector list
  325. * @param selector the selector to be added
  326. */
  327. public void addCustom(ExtendSelector selector) {
  328. usedMatchingTask = true;
  329. super.addCustom(selector);
  330. }
  331. /**
  332. * add a contains selector entry on the selector list
  333. * @param selector the selector to be added
  334. */
  335. public void addContains(ContainsSelector selector) {
  336. usedMatchingTask = true;
  337. super.addContains(selector);
  338. }
  339. /**
  340. * add a present selector entry on the selector list
  341. * @param selector the selector to be added
  342. */
  343. public void addPresent(PresentSelector selector) {
  344. usedMatchingTask = true;
  345. super.addPresent(selector);
  346. }
  347. /**
  348. * add a depth selector entry on the selector list
  349. * @param selector the selector to be added
  350. */
  351. public void addDepth(DepthSelector selector) {
  352. usedMatchingTask = true;
  353. super.addDepth(selector);
  354. }
  355. /**
  356. * add a depends selector entry on the selector list
  357. * @param selector the selector to be added
  358. */
  359. public void addDepend(DependSelector selector) {
  360. usedMatchingTask = true;
  361. super.addDepend(selector);
  362. }
  363. /**
  364. * add a regular expression selector entry on the selector list
  365. * @param selector the selector to be added
  366. */
  367. public void addContainsRegexp(ContainsRegexpSelector selector) {
  368. usedMatchingTask = true;
  369. super.addContainsRegexp(selector);
  370. }
  371. /**
  372. * add the modified selector
  373. * @param selector the selector to add
  374. * @since ant 1.6
  375. */
  376. public void addModified(ModifiedSelector selector) {
  377. usedMatchingTask = true;
  378. super.addModified(selector);
  379. }
  380. /**
  381. * add an arbitrary selector
  382. * @param selector the selector to be added
  383. * @since Ant 1.6
  384. */
  385. public void add(FileSelector selector) {
  386. usedMatchingTask = true;
  387. super.add(selector);
  388. }
  389. /**
  390. * Delete the file(s).
  391. * @exception BuildException if an error occurs
  392. */
  393. public void execute() throws BuildException {
  394. if (usedMatchingTask) {
  395. log("DEPRECATED - Use of the implicit FileSet is deprecated. "
  396. + "Use a nested fileset element instead.");
  397. }
  398. if (file == null && dir == null && filesets.size() == 0) {
  399. throw new BuildException("At least one of the file or dir "
  400. + "attributes, or a fileset element, "
  401. + "must be set.");
  402. }
  403. if (quiet && failonerror) {
  404. throw new BuildException("quiet and failonerror cannot both be "
  405. + "set to true", getLocation());
  406. }
  407. // delete the single file
  408. if (file != null) {
  409. if (file.exists()) {
  410. if (file.isDirectory()) {
  411. log("Directory " + file.getAbsolutePath()
  412. + " cannot be removed using the file attribute. "
  413. + "Use dir instead.");
  414. } else {
  415. log("Deleting: " + file.getAbsolutePath());
  416. if (!delete(file)) {
  417. String message = "Unable to delete file "
  418. + file.getAbsolutePath();
  419. if (failonerror) {
  420. throw new BuildException(message);
  421. } else {
  422. log(message, quiet ? Project.MSG_VERBOSE
  423. : Project.MSG_WARN);
  424. }
  425. }
  426. }
  427. } else {
  428. log("Could not find file " + file.getAbsolutePath()
  429. + " to delete.",
  430. Project.MSG_VERBOSE);
  431. }
  432. }
  433. // delete the directory
  434. if (dir != null && dir.exists() && dir.isDirectory()
  435. && !usedMatchingTask) {
  436. /*
  437. If verbosity is MSG_VERBOSE, that mean we are doing
  438. regular logging (backwards as that sounds). In that
  439. case, we want to print one message about deleting the
  440. top of the directory tree. Otherwise, the removeDir
  441. method will handle messages for _all_ directories.
  442. */
  443. if (verbosity == Project.MSG_VERBOSE) {
  444. log("Deleting directory " + dir.getAbsolutePath());
  445. }
  446. removeDir(dir);
  447. }
  448. // delete the files in the filesets
  449. for (int i = 0; i < filesets.size(); i++) {
  450. FileSet fs = (FileSet) filesets.elementAt(i);
  451. try {
  452. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  453. String[] files = ds.getIncludedFiles();
  454. String[] dirs = ds.getIncludedDirectories();
  455. removeFiles(fs.getDir(getProject()), files, dirs);
  456. } catch (BuildException be) {
  457. // directory doesn't exist or is not readable
  458. if (failonerror) {
  459. throw be;
  460. } else {
  461. log(be.getMessage(),
  462. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  463. }
  464. }
  465. }
  466. // delete the files from the default fileset
  467. if (usedMatchingTask && dir != null) {
  468. try {
  469. DirectoryScanner ds = super.getDirectoryScanner(dir);
  470. String[] files = ds.getIncludedFiles();
  471. String[] dirs = ds.getIncludedDirectories();
  472. removeFiles(dir, files, dirs);
  473. } catch (BuildException be) {
  474. // directory doesn't exist or is not readable
  475. if (failonerror) {
  476. throw be;
  477. } else {
  478. log(be.getMessage(),
  479. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  480. }
  481. }
  482. }
  483. }
  484. //************************************************************************
  485. // protected and private methods
  486. //************************************************************************
  487. /**
  488. * Accommodate Windows bug encountered in both Sun and IBM JDKs.
  489. * Others possible. If the delete does not work, call System.gc(),
  490. * wait a little and try again.
  491. */
  492. private boolean delete(File f) {
  493. if (!f.delete()) {
  494. if (Os.isFamily("windows")) {
  495. System.gc();
  496. }
  497. try {
  498. Thread.sleep(DELETE_RETRY_SLEEP_MILLIS);
  499. return f.delete();
  500. } catch (InterruptedException ex) {
  501. return f.delete();
  502. }
  503. }
  504. return true;
  505. }
  506. /**
  507. * Delete a directory
  508. *
  509. * @param d the directory to delete
  510. */
  511. protected void removeDir(File d) {
  512. String[] list = d.list();
  513. if (list == null) {
  514. list = new String[0];
  515. }
  516. for (int i = 0; i < list.length; i++) {
  517. String s = list[i];
  518. File f = new File(d, s);
  519. if (f.isDirectory()) {
  520. removeDir(f);
  521. } else {
  522. log("Deleting " + f.getAbsolutePath(), verbosity);
  523. if (!delete(f)) {
  524. String message = "Unable to delete file "
  525. + f.getAbsolutePath();
  526. if (failonerror) {
  527. throw new BuildException(message);
  528. } else {
  529. log(message,
  530. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  531. }
  532. }
  533. }
  534. }
  535. log("Deleting directory " + d.getAbsolutePath(), verbosity);
  536. if (!delete(d)) {
  537. String message = "Unable to delete directory "
  538. + dir.getAbsolutePath();
  539. if (failonerror) {
  540. throw new BuildException(message);
  541. } else {
  542. log(message,
  543. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  544. }
  545. }
  546. }
  547. /**
  548. * remove an array of files in a directory, and a list of subdirectories
  549. * which will only be deleted if 'includeEmpty' is true
  550. * @param d directory to work from
  551. * @param files array of files to delete; can be of zero length
  552. * @param dirs array of directories to delete; can of zero length
  553. */
  554. protected void removeFiles(File d, String[] files, String[] dirs) {
  555. if (files.length > 0) {
  556. log("Deleting " + files.length + " files from "
  557. + d.getAbsolutePath());
  558. for (int j = 0; j < files.length; j++) {
  559. File f = new File(d, files[j]);
  560. log("Deleting " + f.getAbsolutePath(), verbosity);
  561. if (!delete(f)) {
  562. String message = "Unable to delete file "
  563. + f.getAbsolutePath();
  564. if (failonerror) {
  565. throw new BuildException(message);
  566. } else {
  567. log(message,
  568. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  569. }
  570. }
  571. }
  572. }
  573. if (dirs.length > 0 && includeEmpty) {
  574. int dirCount = 0;
  575. for (int j = dirs.length - 1; j >= 0; j--) {
  576. File dir = new File(d, dirs[j]);
  577. String[] dirFiles = dir.list();
  578. if (dirFiles == null || dirFiles.length == 0) {
  579. log("Deleting " + dir.getAbsolutePath(), verbosity);
  580. if (!delete(dir)) {
  581. String message = "Unable to delete directory "
  582. + dir.getAbsolutePath();
  583. if (failonerror) {
  584. throw new BuildException(message);
  585. } else {
  586. log(message,
  587. quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
  588. }
  589. } else {
  590. dirCount++;
  591. }
  592. }
  593. }
  594. if (dirCount > 0) {
  595. log("Deleted " + dirCount + " director"
  596. + (dirCount == 1 ? "y" : "ies")
  597. + " from " + d.getAbsolutePath());
  598. }
  599. }
  600. }
  601. }