1. /*
  2. * Copyright 2000,2002,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.io.IOException;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Task;
  22. /**
  23. *
  24. *
  25. *
  26. * @since Ant 1.1
  27. *
  28. * @deprecated The deltree task is deprecated since Ant 1.2. Use
  29. * delete instead.
  30. */
  31. public class Deltree extends Task {
  32. private File dir;
  33. /**
  34. * Set the directory to be deleted
  35. *
  36. * @param dir the root of the tree to be removed.
  37. */
  38. public void setDir(File dir) {
  39. this.dir = dir;
  40. }
  41. /**
  42. * Do the work.
  43. *
  44. * @exception BuildException if the task is not configured correctly or
  45. * the tree cannot be removed.
  46. */
  47. public void execute() throws BuildException {
  48. log("DEPRECATED - The deltree task is deprecated. "
  49. + "Use delete instead.");
  50. if (dir == null) {
  51. throw new BuildException("dir attribute must be set!", getLocation());
  52. }
  53. if (dir.exists()) {
  54. if (!dir.isDirectory()) {
  55. if (!dir.delete()) {
  56. throw new BuildException("Unable to delete directory "
  57. + dir.getAbsolutePath(),
  58. getLocation());
  59. }
  60. return;
  61. }
  62. log("Deleting: " + dir.getAbsolutePath());
  63. try {
  64. removeDir(dir);
  65. } catch (IOException ioe) {
  66. String msg = "Unable to delete " + dir.getAbsolutePath();
  67. throw new BuildException(msg, getLocation());
  68. }
  69. }
  70. }
  71. private void removeDir(File dir) throws IOException {
  72. // check to make sure that the given dir isn't a symlink
  73. // the comparison of absolute path and canonical path
  74. // catches this
  75. // if (dir.getCanonicalPath().equals(dir.getAbsolutePath())) {
  76. // (costin) It will not work if /home/costin is symlink to
  77. // /da0/home/costin ( taz for example )
  78. String[] list = dir.list();
  79. for (int i = 0; i < list.length; i++) {
  80. String s = list[i];
  81. File f = new File(dir, s);
  82. if (f.isDirectory()) {
  83. removeDir(f);
  84. } else {
  85. if (!f.delete()) {
  86. throw new BuildException("Unable to delete file "
  87. + f.getAbsolutePath());
  88. }
  89. }
  90. }
  91. if (!dir.delete()) {
  92. throw new BuildException("Unable to delete directory "
  93. + dir.getAbsolutePath());
  94. }
  95. }
  96. }