-
Enhancement
-
Resolution: Not an Issue
-
P4
-
None
-
7
-
None
-
generic
-
generic
The helper function sun.tests.helpers.IOHelpers.deleteDirectory(String dir) is to delete a directory recursively. The code is as below:
=====Begin of the Code=====
static public boolean deleteDirectory(String dir) throws IOException {
File path = new File(dir);
// System.err.println("Deleting "+path+" "+path.isDirectory());
if (path.exists() && path.isDirectory()) {
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
boolean res = false;
if (files[i].isDirectory()) {
res = deleteDirectory(files[i].getCanonicalPath());
} else {
files[i].delete();
}
if (!res) {
System.err.println(" Could not delete " +
files[i].getCanonicalPath());
}
}
}
return (path.delete());
}
=====End of the Code=====
Inside the for loop, line
files[i].delete();
should be changed to
res = files[i].delete();
And we should retrun immediately in the if block
if (!res) {
System.err.println(" Could not delete " +
files[i].getCanonicalPath());
}
Because it does not maks sense for us to continue to try to delete the parent directory when we already failed to delete some child file/directory.
=====Begin of the Code=====
static public boolean deleteDirectory(String dir) throws IOException {
File path = new File(dir);
// System.err.println("Deleting "+path+" "+path.isDirectory());
if (path.exists() && path.isDirectory()) {
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
boolean res = false;
if (files[i].isDirectory()) {
res = deleteDirectory(files[i].getCanonicalPath());
} else {
files[i].delete();
}
if (!res) {
System.err.println(" Could not delete " +
files[i].getCanonicalPath());
}
}
}
return (path.delete());
}
=====End of the Code=====
Inside the for loop, line
files[i].delete();
should be changed to
res = files[i].delete();
And we should retrun immediately in the if block
if (!res) {
System.err.println(" Could not delete " +
files[i].getCanonicalPath());
}
Because it does not maks sense for us to continue to try to delete the parent directory when we already failed to delete some child file/directory.