Browse Source

Remove directories explicitly matched with includes.

PR: 11732


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274593 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
f24b9340dd
2 changed files with 64 additions and 6 deletions
  1. +0
    -1
      build.xml
  2. +64
    -5
      src/main/org/apache/tools/ant/taskdefs/Move.java

+ 0
- 1
build.xml View File

@@ -276,7 +276,6 @@
<patternset id="teststhatfail">
<exclude name="${optional.package}/BeanShellScriptTest.java"/>
<exclude name="${ant.package}/taskdefs/ImportTest.java"/>
<exclude name="${ant.package}/taskdefs/MoveTest.java"/>
</patternset>

<!--


+ 64
- 5
src/main/org/apache/tools/ant/taskdefs/Move.java View File

@@ -194,18 +194,24 @@ public class Move extends Copy {
}

if (includeEmpty) {
Enumeration e = dirCopyMap.elements();
Enumeration e = dirCopyMap.keys();
int count = 0;
while (e.hasMoreElements()) {
File d = new File((String) e.nextElement());
if (!d.exists()) {
if (!d.mkdirs()) {
String fromDirName = (String) e.nextElement();
String toDirName = (String) dirCopyMap.get(fromDirName);
File fromDir = new File(fromDirName);
File toDir = new File(toDirName);
if (!toDir.exists()) {
if (!toDir.mkdirs()) {
log("Unable to create directory "
+ d.getAbsolutePath(), Project.MSG_ERR);
+ toDirName, Project.MSG_ERR);
} else {
count++;
}
}
if (okToDelete(fromDir)) {
deleteDir(fromDir);
}
}

if (count > 0) {
@@ -215,6 +221,59 @@ public class Move extends Copy {
}
}

/**
* Its only ok to delete a directory tree if there are
* no files in it.
* @return true if a deletion can go ahead
*/
protected boolean okToDelete(File d) {
String[] list = d.list();
if (list == null) {
return false;
} // maybe io error?

for (int i = 0; i < list.length; i++) {
String s = list[i];
File f = new File(d, s);
if (f.isDirectory()) {
if (!okToDelete(f)) {
return false;
}
} else {
return false; // found a file
}
}

return true;
}

/**
* Go and delete the directory tree.
*/
protected void deleteDir(File d) {
String[] list = d.list();
if (list == null) {
return;
} // on an io error list() can return null

for (int i = 0; i < list.length; i++) {
String s = list[i];
File f = new File(d, s);
if (f.isDirectory()) {
deleteDir(f);
} else {
throw new BuildException("UNEXPECTED ERROR - The file "
+ f.getAbsolutePath()
+ " should not exist!");
}
}
log("Deleting directory " + d.getAbsolutePath(), verbosity);
if (!d.delete()) {
throw new BuildException("Unable to delete directory "
+ d.getAbsolutePath());
}
}

/**
* Attempts to rename a file from a source to a destination.
* If overwrite is set to true, this method overwrites existing file


Loading…
Cancel
Save