001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import org.apache.tools.ant.BuildException;
024: import org.apache.tools.ant.Task;
025:
026: /**
027: *
028: *
029: * @since Ant 1.1
030: *
031: * @deprecated The deltree task is deprecated since Ant 1.2. Use
032: * delete instead.
033: */
034:
035: public class Deltree extends Task {
036:
037: private File dir;
038:
039: /**
040: * Set the directory to be deleted
041: *
042: * @param dir the root of the tree to be removed.
043: */
044: public void setDir(File dir) {
045: this .dir = dir;
046: }
047:
048: /**
049: * Do the work.
050: *
051: * @exception BuildException if the task is not configured correctly or
052: * the tree cannot be removed.
053: */
054: public void execute() throws BuildException {
055: log("DEPRECATED - The deltree task is deprecated. "
056: + "Use delete instead.");
057:
058: if (dir == null) {
059: throw new BuildException("dir attribute must be set!",
060: getLocation());
061: }
062:
063: if (dir.exists()) {
064: if (!dir.isDirectory()) {
065: if (!dir.delete()) {
066: throw new BuildException(
067: "Unable to delete directory "
068: + dir.getAbsolutePath(),
069: getLocation());
070: }
071: return;
072: }
073:
074: log("Deleting: " + dir.getAbsolutePath());
075:
076: try {
077: removeDir(dir);
078: } catch (IOException ioe) {
079: String msg = "Unable to delete "
080: + dir.getAbsolutePath();
081: throw new BuildException(msg, getLocation());
082: }
083: }
084: }
085:
086: private void removeDir(File dir) throws IOException {
087:
088: // check to make sure that the given dir isn't a symlink
089: // the comparison of absolute path and canonical path
090: // catches this
091:
092: // if (dir.getCanonicalPath().equals(dir.getAbsolutePath())) {
093: // (costin) It will not work if /home/costin is symlink to
094: // /da0/home/costin ( taz for example )
095: String[] list = dir.list();
096: for (int i = 0; i < list.length; i++) {
097: String s = list[i];
098: File f = new File(dir, s);
099: if (f.isDirectory()) {
100: removeDir(f);
101: } else {
102: if (!f.delete()) {
103: throw new BuildException("Unable to delete file "
104: + f.getAbsolutePath());
105: }
106: }
107: }
108: if (!dir.delete()) {
109: throw new BuildException("Unable to delete directory "
110: + dir.getAbsolutePath());
111: }
112: }
113: }
|