Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7019711

Minor bug in sun.tests.helpers.IOHelpers.deleteDirectory(String dir)

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 7
    • deploy
    • None

      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.

            stephenh Stephen Hu (Inactive)
            stephenh Stephen Hu (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: