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.

Deltree.java 3.1 kB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.io.File;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Task;
  22. /**
  23. *
  24. *
  25. * @since Ant 1.1
  26. *
  27. * @deprecated The deltree task is deprecated since Ant 1.2. Use
  28. * delete instead.
  29. */
  30. @Deprecated
  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. removeDir(dir);
  64. }
  65. }
  66. private void removeDir(File dir) {
  67. // check to make sure that the given dir isn't a symlink
  68. // the comparison of absolute path and canonical path
  69. // catches this
  70. // if (dir.getCanonicalPath().equals(dir.getAbsolutePath())) {
  71. // (costin) It will not work if /home/costin is symlink to
  72. // /da0/home/costin (taz for example)
  73. for (String s : dir.list()) {
  74. File f = new File(dir, s);
  75. if (f.isDirectory()) {
  76. removeDir(f);
  77. } else if (!f.delete()) {
  78. throw new BuildException("Unable to delete file "
  79. + f.getAbsolutePath());
  80. }
  81. }
  82. if (!dir.delete()) {
  83. throw new BuildException("Unable to delete directory "
  84. + dir.getAbsolutePath());
  85. }
  86. }
  87. }